mirror of
https://github.com/tobspr/shapez.io.git
synced 2026-03-02 03:39:21 +00:00
Improve belt direction lock
This commit is contained in:
@@ -3,7 +3,7 @@ import { globalConfig } from "../../../core/config";
|
||||
import { DrawParameters } from "../../../core/draw_parameters";
|
||||
import { drawRotatedSprite } from "../../../core/draw_utils";
|
||||
import { Loader } from "../../../core/loader";
|
||||
import { makeDiv, removeAllChildren } from "../../../core/utils";
|
||||
import { makeDiv, removeAllChildren, pulseAnimation, clamp } from "../../../core/utils";
|
||||
import {
|
||||
enumDirectionToAngle,
|
||||
enumDirectionToVector,
|
||||
@@ -50,6 +50,8 @@ export class HUDBuildingPlacer extends HUDBuildingPlacerLogic {
|
||||
this.variantsAttach = new DynamicDomAttach(this.root, this.variantsElement, {});
|
||||
|
||||
this.currentInterpolatedCornerTile = new Vector();
|
||||
|
||||
this.lockIndicatorSprite = Loader.getSprite("sprites/misc/lock_direction_indicator.png");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -319,6 +321,25 @@ export class HUDBuildingPlacer extends HUDBuildingPlacerLogic {
|
||||
|
||||
parameters.context.beginCircle(endLine.x, endLine.y, 4);
|
||||
parameters.context.fill();
|
||||
|
||||
// Draw arrows
|
||||
const path = this.computeDirectionLockPath();
|
||||
for (let i = 0; i < path.length - 1; i += 1) {
|
||||
const { rotation, tile } = path[i];
|
||||
const worldPos = tile.toWorldSpaceCenterOfTile();
|
||||
drawRotatedSprite({
|
||||
parameters,
|
||||
sprite: this.lockIndicatorSprite,
|
||||
x: worldPos.x,
|
||||
y: worldPos.y,
|
||||
angle: Math_radians(rotation),
|
||||
size: 12,
|
||||
offsetY:
|
||||
-globalConfig.halfTileSize -
|
||||
clamp((this.root.time.now() * 1.5) % 1.0, 0, 1) * 1 * globalConfig.tileSize +
|
||||
globalConfig.halfTileSize,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -91,6 +91,7 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
|
||||
.getBinding(KEYMAPPINGS.placement.abortBuildingPlacement)
|
||||
.add(this.abortPlacement, this);
|
||||
keyActionMapper.getBinding(KEYMAPPINGS.general.back).add(this.abortPlacement, this);
|
||||
this.root.gameState.inputReciever.keyup.add(this.checkForDirectionLockSwitch, this);
|
||||
|
||||
// BINDINGS TO GAME EVENTS
|
||||
this.root.hud.signals.buildingsSelectedForCopy.add(this.abortPlacement, this);
|
||||
@@ -211,6 +212,16 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the direction lock key got released and if such, resets the placement
|
||||
* @param {any} args
|
||||
*/
|
||||
checkForDirectionLockSwitch({ keyCode }) {
|
||||
if (keyCode === this.root.keyMapper.getBinding(KEYMAPPINGS.placement.lockBeltDirection).keyCode) {
|
||||
this.abortDragging();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Canvas click handler
|
||||
* @param {Vector} mousePos
|
||||
@@ -316,12 +327,28 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
|
||||
* releasing the mouse
|
||||
*/
|
||||
executeDirectionLockedPlacement() {
|
||||
const path = this.computeDirectionLockPath();
|
||||
for (let i = 0; i < path.length; ++i) {
|
||||
const { rotation, tile } = path[i];
|
||||
|
||||
this.currentBaseRotation = rotation;
|
||||
this.tryPlaceCurrentBuildingAt(tile);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the path which the current direction lock will use
|
||||
* @returns {Array<{ tile: Vector, rotation: number }>}
|
||||
*/
|
||||
computeDirectionLockPath() {
|
||||
const mousePosition = this.root.app.mousePosition;
|
||||
if (!mousePosition) {
|
||||
// Not on screen
|
||||
return;
|
||||
return [];
|
||||
}
|
||||
|
||||
let result = [];
|
||||
|
||||
// Figure which points the line visits
|
||||
const worldPos = this.root.camera.screenToWorld(mousePosition);
|
||||
const mouseTile = worldPos.toTileSpace();
|
||||
@@ -333,10 +360,13 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
|
||||
const lengthToCorner = Math_round(pathToCorner.length());
|
||||
let currentPos = startTile.copy();
|
||||
|
||||
this.currentBaseRotation = (Math.round(Math_degrees(deltaToCorner.angle()) / 90) * 90 + 360) % 360;
|
||||
let rotation = (Math.round(Math_degrees(deltaToCorner.angle()) / 90) * 90 + 360) % 360;
|
||||
|
||||
for (let i = 0; i < lengthToCorner; ++i) {
|
||||
this.tryPlaceCurrentBuildingAt(currentPos);
|
||||
result.push({
|
||||
tile: currentPos.copy(),
|
||||
rotation,
|
||||
});
|
||||
currentPos.addInplace(deltaToCorner);
|
||||
}
|
||||
|
||||
@@ -344,12 +374,16 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
|
||||
const pathFromCorner = mouseTile.sub(this.currentDirectionLockCorner);
|
||||
const deltaFromCorner = pathFromCorner.normalize().round();
|
||||
const lengthFromCorner = Math_round(pathFromCorner.length());
|
||||
this.currentBaseRotation = (Math.round(Math_degrees(deltaFromCorner.angle()) / 90) * 90 + 360) % 360;
|
||||
rotation = (Math.round(Math_degrees(deltaFromCorner.angle()) / 90) * 90 + 360) % 360;
|
||||
|
||||
for (let i = 0; i < lengthFromCorner + 1; ++i) {
|
||||
this.tryPlaceCurrentBuildingAt(currentPos);
|
||||
result.push({
|
||||
tile: currentPos.copy(),
|
||||
rotation,
|
||||
});
|
||||
currentPos.addInplace(deltaFromCorner);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user