mirror of
https://github.com/tobspr/shapez.io.git
synced 2024-10-27 20:34:29 +00:00
Fix flickering in direction lock
This commit is contained in:
parent
6d59847153
commit
26949d5e3d
@ -9,6 +9,7 @@ import { Entity } from "../../entity";
|
|||||||
import { KEYMAPPINGS } from "../../key_action_mapper";
|
import { KEYMAPPINGS } from "../../key_action_mapper";
|
||||||
import { defaultBuildingVariant, MetaBuilding } from "../../meta_building";
|
import { defaultBuildingVariant, MetaBuilding } from "../../meta_building";
|
||||||
import { BaseHUDPart } from "../base_hud_part";
|
import { BaseHUDPart } from "../base_hud_part";
|
||||||
|
import { lerp } from "../../../core/utils";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contains all logic for the building placer - this doesn't include the rendering
|
* Contains all logic for the building placer - this doesn't include the rendering
|
||||||
@ -76,6 +77,12 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
|
|||||||
*/
|
*/
|
||||||
this.lastDragTile = null;
|
this.lastDragTile = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interpolate the side for direction lock slowly so it doesn't flicker
|
||||||
|
* @type {number}
|
||||||
|
*/
|
||||||
|
this.interpolatedDirectionLockSide = 0;
|
||||||
|
|
||||||
this.initializeBindings();
|
this.initializeBindings();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,15 +146,30 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
|
|||||||
const worldPos = this.root.camera.screenToWorld(mousePosition);
|
const worldPos = this.root.camera.screenToWorld(mousePosition);
|
||||||
const mouseTile = worldPos.toTileSpace();
|
const mouseTile = worldPos.toTileSpace();
|
||||||
|
|
||||||
const fractional = worldPos.sub(mouseTile.toWorldSpaceCenterOfTile());
|
if (this.interpolatedDirectionLockSide <= 0) {
|
||||||
|
|
||||||
if (fractional.x + fractional.y < 0) {
|
|
||||||
return new Vector(this.lastDragTile.x, mouseTile.y);
|
return new Vector(this.lastDragTile.x, mouseTile.y);
|
||||||
} else {
|
} else {
|
||||||
return new Vector(mouseTile.x, this.lastDragTile.y);
|
return new Vector(mouseTile.x, this.lastDragTile.y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Computes on which side the direction lock should be active
|
||||||
|
* @returns {-1|0|1}
|
||||||
|
*/
|
||||||
|
get currentDirectionLockSide() {
|
||||||
|
const mousePosition = this.root.app.mousePosition;
|
||||||
|
if (!mousePosition) {
|
||||||
|
// Not on screen
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
const worldPos = this.root.camera.screenToWorld(mousePosition);
|
||||||
|
const mouseTile = worldPos.toTileSpace();
|
||||||
|
|
||||||
|
const fractional = worldPos.sub(mouseTile.toWorldSpaceCenterOfTile());
|
||||||
|
return fractional.x + fractional.y < 0 ? -1 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Aborts the placement
|
* Aborts the placement
|
||||||
*/
|
*/
|
||||||
@ -177,6 +199,13 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
|
|||||||
if (mousePos) {
|
if (mousePos) {
|
||||||
this.onMouseMove(mousePos);
|
this.onMouseMove(mousePos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prevent flickering by interpolating the side
|
||||||
|
this.interpolatedDirectionLockSide = lerp(
|
||||||
|
this.interpolatedDirectionLockSide,
|
||||||
|
this.currentDirectionLockSide,
|
||||||
|
0.06
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -362,12 +391,14 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
|
|||||||
|
|
||||||
let rotation = (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) {
|
if (lengthToCorner > 0) {
|
||||||
result.push({
|
for (let i = 0; i < lengthToCorner; ++i) {
|
||||||
tile: currentPos.copy(),
|
result.push({
|
||||||
rotation,
|
tile: currentPos.copy(),
|
||||||
});
|
rotation,
|
||||||
currentPos.addInplace(deltaToCorner);
|
});
|
||||||
|
currentPos.addInplace(deltaToCorner);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Place from corner to end
|
// Place from corner to end
|
||||||
@ -376,12 +407,14 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
|
|||||||
const lengthFromCorner = Math_round(pathFromCorner.length());
|
const lengthFromCorner = Math_round(pathFromCorner.length());
|
||||||
rotation = (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) {
|
if (lengthFromCorner > 0) {
|
||||||
result.push({
|
for (let i = 0; i < lengthFromCorner + 1; ++i) {
|
||||||
tile: currentPos.copy(),
|
result.push({
|
||||||
rotation,
|
tile: currentPos.copy(),
|
||||||
});
|
rotation,
|
||||||
currentPos.addInplace(deltaFromCorner);
|
});
|
||||||
|
currentPos.addInplace(deltaFromCorner);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user