Improve belt lock

This commit is contained in:
tobspr 2020-06-16 19:45:16 +02:00
parent 3afe2eb329
commit 4fc1ff6b0d
1 changed files with 7 additions and 29 deletions

View File

@ -11,6 +11,7 @@ import {
enumDirectionToVector, enumDirectionToVector,
enumInvertedDirections, enumInvertedDirections,
Vector, Vector,
enumAngleToDirection,
} from "../../../core/vector"; } from "../../../core/vector";
import { enumMouseButton } from "../../camera"; import { enumMouseButton } from "../../camera";
import { StaticMapEntityComponent } from "../../components/static_map_entity"; import { StaticMapEntityComponent } from "../../components/static_map_entity";
@ -39,7 +40,6 @@ export class HUDBuildingPlacer extends BaseHUDPart {
keyActionMapper.getBinding(KEYMAPPINGS.placement.rotateWhilePlacing).add(this.tryRotate, this); keyActionMapper.getBinding(KEYMAPPINGS.placement.rotateWhilePlacing).add(this.tryRotate, this);
keyActionMapper.getBinding(KEYMAPPINGS.placement.cycleBuildingVariants).add(this.cycleVariants, this); keyActionMapper.getBinding(KEYMAPPINGS.placement.cycleBuildingVariants).add(this.cycleVariants, this);
keyActionMapper.getBinding(KEYMAPPINGS.placement.lockBeltDirection).add(this.resetBeltLock, this);
this.root.hud.signals.buildingsSelectedForCopy.add(this.abortPlacement, this); this.root.hud.signals.buildingsSelectedForCopy.add(this.abortPlacement, this);
this.root.hud.signals.pasteBlueprintRequested.add(this.abortPlacement, this); this.root.hud.signals.pasteBlueprintRequested.add(this.abortPlacement, this);
@ -78,12 +78,6 @@ export class HUDBuildingPlacer extends BaseHUDPart {
*/ */
this.initialDragTile = null; this.initialDragTile = null;
/**
* The first initial placement direction we performed
* @type {Vector}
*/
this.initialPlacementVector = null;
this.root.signals.storyGoalCompleted.add(this.rerenderVariants, this); this.root.signals.storyGoalCompleted.add(this.rerenderVariants, this);
this.root.signals.upgradePurchased.add(this.rerenderVariants, this); this.root.signals.upgradePurchased.add(this.rerenderVariants, this);
} }
@ -114,13 +108,6 @@ export class HUDBuildingPlacer extends BaseHUDPart {
} }
} }
/**
* Resets the belt lock, called after pressing shift
*/
resetBeltLock() {
this.initialPlacementVector = null;
}
/** /**
* mouse down pre handler * mouse down pre handler
* @param {Vector} pos * @param {Vector} pos
@ -177,14 +164,11 @@ export class HUDBuildingPlacer extends BaseHUDPart {
if ( if (
this.root.keyMapper.getBinding(KEYMAPPINGS.placement.lockBeltDirection).isCurrentlyPressed() this.root.keyMapper.getBinding(KEYMAPPINGS.placement.lockBeltDirection).isCurrentlyPressed()
) { ) {
if (this.initialPlacementVector) { const vector = enumDirectionToVector[enumAngleToDirection[this.currentBaseRotation]];
const lockX = Math_abs(Math_sign(this.initialPlacementVector.x)); const delta = newPos.sub(oldPos);
const lockY = Math_abs(Math_sign(this.initialPlacementVector.y)); delta.x *= Math_abs(vector.x);
const delta = newPos.sub(oldPos); delta.y *= Math_abs(vector.y);
delta.x *= lockX; newPos = oldPos.add(delta);
delta.y *= lockY;
newPos = oldPos.add(delta);
}
} }
if (!oldPos.equals(newPos)) { if (!oldPos.equals(newPos)) {
@ -229,12 +213,6 @@ export class HUDBuildingPlacer extends BaseHUDPart {
this.root.logic.tryDeleteBuilding(contents); this.root.logic.tryDeleteBuilding(contents);
} }
} else { } else {
if (!this.initialPlacementVector) {
const origin = newPos.sub(oldPos);
console.log("ORIGIN:", origin);
this.initialPlacementVector = origin;
}
this.tryPlaceCurrentBuildingAt(new Vector(x0, y0)); this.tryPlaceCurrentBuildingAt(new Vector(x0, y0));
} }
if (x0 === x1 && y0 === y1) break; if (x0 === x1 && y0 === y1) break;
@ -677,7 +655,7 @@ export class HUDBuildingPlacer extends BaseHUDPart {
// Draw direction lock // Draw direction lock
if (this.root.keyMapper.getBinding(KEYMAPPINGS.placement.lockBeltDirection).isCurrentlyPressed()) { if (this.root.keyMapper.getBinding(KEYMAPPINGS.placement.lockBeltDirection).isCurrentlyPressed()) {
if (this.lastDragTile && this.initialPlacementVector) { if (this.lastDragTile) {
parameters.context.fillStyle = THEME.map.selectionBackground; parameters.context.fillStyle = THEME.map.selectionBackground;
parameters.context.strokeStyle = THEME.map.selectionOverlay; parameters.context.strokeStyle = THEME.map.selectionOverlay;
parameters.context.lineWidth = 3; parameters.context.lineWidth = 3;