Add originalRotation to static comp in order to fix bugs with the automatic placement

pull/33/head
Tobias Springer 4 years ago
parent 72476486b7
commit bb1758642b

@ -1,19 +1,17 @@
#ingame_HUD_building_placer { #ingame_HUD_building_placer {
position: fixed; position: fixed;
@include S(bottom, 60px); @include S(top, 5q0px);
left: 50%; @include S(right, 10px);
transform: translateX(-50%);
display: flex; display: flex;
flex-direction: column; flex-direction: column;
@include S(padding, 6px); @include S(padding, 6px);
justify-content: center; justify-content: center;
align-items: center; align-items: flex-start;
background-color: $ingameHudBg;
@include S(border-radius, 4px); @include S(border-radius, 4px);
background: #333; background: rgba(#333, 0.5);
@include S(width, 300px); @include S(width, 200px);
.buildingLabel { .buildingLabel {
@include PlainText; @include PlainText;
@ -24,8 +22,8 @@
.instructions, .instructions,
.description { .description {
text-align: center; color: #fff;
color: mix($accentColorDark, $accentColorBright, 50%); opacity: 0.8;
@include SuperSmallText; @include SuperSmallText;
} }

@ -58,12 +58,17 @@
} }
} }
// &:first-child .tooltip { &.selected {
// display: flex; background: rgb(140, 229, 140) !important;
// } transform: scale(1.05);
.keybinding {
color: #111;
}
}
pointer-events: all; pointer-events: all;
transition: background-color 0.1s ease-in-out; transition: all 0.1s ease-in-out;
transition-property: background-color, transform;
&.unlocked:hover { &.unlocked:hover {
background: rgba($accentColorDark, 0.1); background: rgba($accentColorDark, 0.1);
cursor: pointer; cursor: pointer;

@ -32,7 +32,7 @@ $accentColorDark: #7d808a;
$colorGreenBright: #66bb6a; $colorGreenBright: #66bb6a;
$colorRedBright: #ef5072; $colorRedBright: #ef5072;
$themeColor: #393747; $themeColor: #393747;
$ingameHudBg: rgba($accentColorBright, 0.9); $ingameHudBg: rgba($accentColorBright, 0.1);
$ingameHudBorder: #{D(1.5px)} solid $accentColorDark; $ingameHudBorder: #{D(1.5px)} solid $accentColorDark;
$text3dColor: #f4ffff; $text3dColor: #f4ffff;

@ -176,6 +176,7 @@ export class InputDistributor {
*/ */
handleBlur() { handleBlur() {
this.shiftIsDown = false; this.shiftIsDown = false;
this.altIsDown = false;
this.forwardToReceiver("pageBlur", {}); this.forwardToReceiver("pageBlur", {});
this.forwardToReceiver("shiftUp", {}); this.forwardToReceiver("shiftUp", {});
} }
@ -187,6 +188,9 @@ export class InputDistributor {
if (event.keyCode === 16) { if (event.keyCode === 16) {
this.shiftIsDown = true; this.shiftIsDown = true;
} }
if (event.keyCode === 18) {
this.altIsDown = true;
}
if ( if (
// TAB // TAB
@ -225,6 +229,10 @@ export class InputDistributor {
this.shiftIsDown = false; this.shiftIsDown = false;
this.forwardToReceiver("shiftUp", {}); this.forwardToReceiver("shiftUp", {});
} }
if (event.keyCode === 18) {
this.altIsDown = false;
this.forwardToReceiver("altUp", {});
}
this.forwardToReceiver("keyup", { this.forwardToReceiver("keyup", {
keyCode: event.keyCode, keyCode: event.keyCode,

@ -10,6 +10,7 @@ export class InputReceiver {
this.keyup = new Signal(); this.keyup = new Signal();
this.pageBlur = new Signal(); this.pageBlur = new Signal();
this.shiftUp = new Signal(); this.shiftUp = new Signal();
this.altUp = new Signal();
// Dispatched on destroy // Dispatched on destroy
this.destroyed = new Signal(); this.destroyed = new Signal();

@ -130,8 +130,8 @@ export class MetaBeltBaseBuilding extends MetaBuilding {
const { ejectors, acceptors } = root.logic.getEjectorsAndAcceptorsAtTile(tile); const { ejectors, acceptors } = root.logic.getEjectorsAndAcceptorsAtTile(tile);
let hasBottomEjector = false; let hasBottomEjector = false;
let hasLeftEjector = false;
let hasRightEjector = false; let hasRightEjector = false;
let hasLeftEjector = false;
let hasTopAcceptor = false; let hasTopAcceptor = false;
let hasLeftAcceptor = false; let hasLeftAcceptor = false;
@ -144,9 +144,9 @@ export class MetaBeltBaseBuilding extends MetaBuilding {
if (ejector.toDirection === topDirection) { if (ejector.toDirection === topDirection) {
hasBottomEjector = true; hasBottomEjector = true;
} else if (ejector.toDirection === leftDirection) { } else if (ejector.toDirection === leftDirection) {
hasLeftEjector = true;
} else if (ejector.toDirection === rightDirection) {
hasRightEjector = true; hasRightEjector = true;
} else if (ejector.toDirection === rightDirection) {
hasLeftEjector = true;
} }
} }
@ -167,7 +167,9 @@ export class MetaBeltBaseBuilding extends MetaBuilding {
if (!hasBottomEjector) { if (!hasBottomEjector) {
// When something ejects to us from the left and nothing from the right, // When something ejects to us from the left and nothing from the right,
// do a curve from the left to the top // do a curve from the left to the top
if (hasLeftEjector && !hasRightEjector) {
if (hasRightEjector && !hasLeftEjector) {
console.log("e - connect right");
return { return {
rotation: (rotation + 270) % 360, rotation: (rotation + 270) % 360,
rotationVariant: 2, rotationVariant: 2,
@ -176,7 +178,8 @@ export class MetaBeltBaseBuilding extends MetaBuilding {
// When something ejects to us from the right and nothing from the left, // When something ejects to us from the right and nothing from the left,
// do a curve from the right to the top // do a curve from the right to the top
if (hasRightEjector && !hasLeftEjector) { if (hasLeftEjector && !hasRightEjector) {
console.log("e - connect left");
return { return {
rotation: (rotation + 90) % 360, rotation: (rotation + 90) % 360,
rotationVariant: 1, rotationVariant: 1,
@ -190,6 +193,7 @@ export class MetaBeltBaseBuilding extends MetaBuilding {
// When there is an acceptor to the right but no acceptor to the left, // When there is an acceptor to the right but no acceptor to the left,
// do a turn to the right // do a turn to the right
if (hasRightAcceptor && !hasLeftAcceptor) { if (hasRightAcceptor && !hasLeftAcceptor) {
console.log("a - connect right");
return { return {
rotation, rotation,
rotationVariant: 2, rotationVariant: 2,
@ -199,6 +203,7 @@ export class MetaBeltBaseBuilding extends MetaBuilding {
// When there is an acceptor to the left but no acceptor to the right, // When there is an acceptor to the left but no acceptor to the right,
// do a turn to the left // do a turn to the left
if (hasLeftAcceptor && !hasRightAcceptor) { if (hasLeftAcceptor && !hasRightAcceptor) {
console.log("a - connect left");
return { return {
rotation, rotation,
rotationVariant: 1, rotationVariant: 1,

@ -109,12 +109,12 @@ export class MetaUndergroundBeltBuilding extends MetaBuilding {
const undergroundComp = contents.components.UndergroundBelt; const undergroundComp = contents.components.UndergroundBelt;
if (undergroundComp) { if (undergroundComp) {
const staticComp = contents.components.StaticMapEntity; const staticComp = contents.components.StaticMapEntity;
if (staticComp.rotationDegrees === targetRotation) { if (staticComp.rotation === targetRotation) {
if (undergroundComp.mode !== enumUndergroundBeltMode.sender) { if (undergroundComp.mode !== enumUndergroundBeltMode.sender) {
// If we encounter an underground receiver on our way which is also faced in our direction, we don't accept that // If we encounter an underground receiver on our way which is also faced in our direction, we don't accept that
break; break;
} }
// console.log("GOT IT! rotation is", rotation, "and target is", staticComp.rotationDegrees); // console.log("GOT IT! rotation is", rotation, "and target is", staticComp.rotation);
return { return {
rotation: targetRotation, rotation: targetRotation,

@ -13,12 +13,7 @@ export class StaticMapEntityComponent extends Component {
} }
static getSchema() { static getSchema() {
return { return {};
origin: types.tileVector,
tileSize: types.tileVector,
rotationDegrees: types.uint,
spriteKey: types.string,
};
} }
/** /**
@ -26,27 +21,30 @@ export class StaticMapEntityComponent extends Component {
* @param {object} param0 * @param {object} param0
* @param {Vector=} param0.origin Origin (Top Left corner) of the entity * @param {Vector=} param0.origin Origin (Top Left corner) of the entity
* @param {Vector=} param0.tileSize Size of the entity in tiles * @param {Vector=} param0.tileSize Size of the entity in tiles
* @param {number=} param0.rotationDegrees Rotation in degrees. Must be multiple of 90 * @param {number=} param0.rotation Rotation in degrees. Must be multiple of 90
* @param {number=} param0.originalRotation Original Rotation in degrees. Must be multiple of 90
* @param {string=} param0.spriteKey Optional sprite * @param {string=} param0.spriteKey Optional sprite
* @param {string=} param0.silhouetteColor Optional silhouette color override * @param {string=} param0.silhouetteColor Optional silhouette color override
*/ */
constructor({ constructor({
origin = new Vector(), origin = new Vector(),
tileSize = new Vector(1, 1), tileSize = new Vector(1, 1),
rotationDegrees = 0, rotation = 0,
originalRotation = 0,
spriteKey = null, spriteKey = null,
silhouetteColor = null, silhouetteColor = null,
}) { }) {
super(); super();
assert( assert(
rotationDegrees % 90 === 0, rotation % 90 === 0,
"Rotation of static map entity must be multiple of 90 (was " + rotationDegrees + ")" "Rotation of static map entity must be multiple of 90 (was " + rotation + ")"
); );
this.origin = origin; this.origin = origin;
this.tileSize = tileSize; this.tileSize = tileSize;
this.spriteKey = spriteKey; this.spriteKey = spriteKey;
this.rotationDegrees = rotationDegrees; this.rotation = rotation;
this.originalRotation = originalRotation;
this.silhouetteColor = silhouetteColor; this.silhouetteColor = silhouetteColor;
} }
@ -55,7 +53,7 @@ export class StaticMapEntityComponent extends Component {
* @returns {Rectangle} * @returns {Rectangle}
*/ */
getTileSpaceBounds() { getTileSpaceBounds() {
switch (this.rotationDegrees) { switch (this.rotation) {
case 0: case 0:
return new Rectangle(this.origin.x, this.origin.y, this.tileSize.x, this.tileSize.y); return new Rectangle(this.origin.x, this.origin.y, this.tileSize.x, this.tileSize.y);
case 90: case 90:
@ -90,7 +88,7 @@ export class StaticMapEntityComponent extends Component {
* @returns {Vector} * @returns {Vector}
*/ */
applyRotationToVector(vector) { applyRotationToVector(vector) {
return vector.rotateFastMultipleOf90(this.rotationDegrees); return vector.rotateFastMultipleOf90(this.rotation);
} }
/** /**
@ -99,7 +97,7 @@ export class StaticMapEntityComponent extends Component {
* @returns {Vector} * @returns {Vector}
*/ */
unapplyRotationToVector(vector) { unapplyRotationToVector(vector) {
return vector.rotateFastMultipleOf90(360 - this.rotationDegrees); return vector.rotateFastMultipleOf90(360 - this.rotation);
} }
/** /**
@ -108,7 +106,7 @@ export class StaticMapEntityComponent extends Component {
* @returns {enumDirection} * @returns {enumDirection}
*/ */
localDirectionToWorld(direction) { localDirectionToWorld(direction) {
return Vector.transformDirectionFromMultipleOf90(direction, this.rotationDegrees); return Vector.transformDirectionFromMultipleOf90(direction, this.rotation);
} }
/** /**
@ -117,7 +115,7 @@ export class StaticMapEntityComponent extends Component {
* @returns {enumDirection} * @returns {enumDirection}
*/ */
worldDirectionToLocal(direction) { worldDirectionToLocal(direction) {
return Vector.transformDirectionFromMultipleOf90(direction, 360 - this.rotationDegrees); return Vector.transformDirectionFromMultipleOf90(direction, 360 - this.rotation);
} }
/** /**
@ -151,7 +149,7 @@ export class StaticMapEntityComponent extends Component {
const worldX = this.origin.x * globalConfig.tileSize; const worldX = this.origin.x * globalConfig.tileSize;
const worldY = this.origin.y * globalConfig.tileSize; const worldY = this.origin.y * globalConfig.tileSize;
if (this.rotationDegrees === 0) { if (this.rotation === 0) {
// Early out, is faster // Early out, is faster
sprite.drawCached( sprite.drawCached(
parameters, parameters,
@ -166,7 +164,7 @@ export class StaticMapEntityComponent extends Component {
const rotationCenterY = worldY + globalConfig.halfTileSize; const rotationCenterY = worldY + globalConfig.halfTileSize;
parameters.context.translate(rotationCenterX, rotationCenterY); parameters.context.translate(rotationCenterX, rotationCenterY);
parameters.context.rotate(Math_radians(this.rotationDegrees)); parameters.context.rotate(Math_radians(this.rotation));
sprite.drawCached( sprite.drawCached(
parameters, parameters,
@ -177,7 +175,7 @@ export class StaticMapEntityComponent extends Component {
false false
); );
parameters.context.rotate(-Math_radians(this.rotationDegrees)); parameters.context.rotate(-Math_radians(this.rotation));
parameters.context.translate(-rotationCenterX, -rotationCenterY); parameters.context.translate(-rotationCenterX, -rotationCenterY);
} }
} }

@ -132,15 +132,17 @@ export class GameCore {
/** /**
* Initializes a new game, this means creating a new map and centering on the * Initializes a new game, this means creating a new map and centering on the
* plaerbase * playerbase
* */ * */
initNewGame() { initNewGame() {
logger.log("Initializing new game"); logger.log("Initializing new game");
this.root.gameIsFresh = true; this.root.gameIsFresh = true;
gMetaBuildingRegistry gMetaBuildingRegistry.findByClass(MetaHubBuilding).createAndPlaceEntity({
.findByClass(MetaHubBuilding) root: this.root,
.createAndPlaceEntity(this.root, new Vector(-2, -2), 0); origin: new Vector(-2, -2),
rotation: 0,
});
} }
/** /**

@ -99,6 +99,11 @@ export class HUDBuildingPlacer extends BaseHUDPart {
const angleDeg = Math_degrees(delta.angle()); const angleDeg = Math_degrees(delta.angle());
this.currentBaseRotation = (Math.round(angleDeg / 90) * 90 + 360) % 360; this.currentBaseRotation = (Math.round(angleDeg / 90) * 90 + 360) % 360;
// Holding alt inverts the placement
if (this.root.app.inputMgr.altIsDown) {
this.currentBaseRotation = (180 + this.currentBaseRotation) % 360;
}
// - Using bresenhams algorithmus // - Using bresenhams algorithmus
let x0 = oldPos.x; let x0 = oldPos.x;
@ -172,7 +177,7 @@ export class HUDBuildingPlacer extends BaseHUDPart {
this.fakeEntity.addComponent( this.fakeEntity.addComponent(
new StaticMapEntityComponent({ new StaticMapEntityComponent({
origin: new Vector(0, 0), origin: new Vector(0, 0),
rotationDegrees: 0, rotation: 0,
tileSize: metaBuilding.getDimensions().copy(), tileSize: metaBuilding.getDimensions().copy(),
}) })
); );
@ -190,7 +195,7 @@ export class HUDBuildingPlacer extends BaseHUDPart {
if (selectedBuilding) { if (selectedBuilding) {
this.currentBaseRotation = (this.currentBaseRotation + 90) % 360; this.currentBaseRotation = (this.currentBaseRotation + 90) % 360;
const staticComp = this.fakeEntity.components.StaticMapEntity; const staticComp = this.fakeEntity.components.StaticMapEntity;
staticComp.rotationDegrees = this.currentBaseRotation; staticComp.rotation = this.currentBaseRotation;
} }
} }
@ -350,7 +355,7 @@ export class HUDBuildingPlacer extends BaseHUDPart {
// Synchronize rotation and origin // Synchronize rotation and origin
const staticComp = this.fakeEntity.components.StaticMapEntity; const staticComp = this.fakeEntity.components.StaticMapEntity;
staticComp.origin = tile; staticComp.origin = tile;
staticComp.rotationDegrees = rotation; staticComp.rotation = rotation;
metaBuilding.updateRotationVariant(this.fakeEntity, rotationVariant); metaBuilding.updateRotationVariant(this.fakeEntity, rotationVariant);
// Check if we could place the buildnig // Check if we could place the buildnig

@ -33,8 +33,8 @@ export class HUDBuildingsToolbar extends BaseHUDPart {
constructor(root) { constructor(root) {
super(root); super(root);
/** @type {Object.<string, { metaBuilding: MetaBuilding, status: boolean, element: HTMLElement}>} */ /** @type {Object.<string, { metaBuilding: MetaBuilding, unlocked: boolean, selected: boolean, element: HTMLElement}>} */
this.buildingUnlockStates = {}; this.buildingHandles = {};
this.sigBuildingSelected = new Signal(); this.sigBuildingSelected = new Signal();
@ -92,29 +92,50 @@ export class HUDBuildingsToolbar extends BaseHUDPart {
this.trackClicks(itemContainer, () => this.selectBuildingForPlacement(metaBuilding), {}); this.trackClicks(itemContainer, () => this.selectBuildingForPlacement(metaBuilding), {});
this.buildingUnlockStates[metaBuilding.id] = { this.buildingHandles[metaBuilding.id] = {
metaBuilding, metaBuilding,
element: itemContainer, element: itemContainer,
status: false, unlocked: false,
selected: false,
}; };
} }
this.root.hud.signals.selectedPlacementBuildingChanged.add(
this.onSelectedPlacementBuildingChanged,
this
);
} }
update() { update() {
this.trackedIsVisisible.set(!this.root.camera.getIsMapOverlayActive()); this.trackedIsVisisible.set(!this.root.camera.getIsMapOverlayActive());
for (const buildingId in this.buildingUnlockStates) { for (const buildingId in this.buildingHandles) {
const handle = this.buildingUnlockStates[buildingId]; const handle = this.buildingHandles[buildingId];
const newStatus = handle.metaBuilding.getIsUnlocked(this.root); const newStatus = handle.metaBuilding.getIsUnlocked(this.root);
if (handle.status !== newStatus) { if (handle.unlocked !== newStatus) {
handle.status = newStatus; handle.unlocked = newStatus;
handle.element.classList.toggle("unlocked", newStatus); handle.element.classList.toggle("unlocked", newStatus);
} }
} }
} }
/** /**
* * @param {MetaBuilding} metaBuilding
*/
onSelectedPlacementBuildingChanged(metaBuilding) {
for (const buildingId in this.buildingHandles) {
const handle = this.buildingHandles[buildingId];
const newStatus = handle.metaBuilding === metaBuilding;
if (handle.selected !== newStatus) {
handle.selected = newStatus;
handle.element.classList.toggle("selected", newStatus);
}
}
this.element.classList.toggle("buildingSelected", !!metaBuilding);
}
/**
* @param {MetaBuilding} metaBuilding * @param {MetaBuilding} metaBuilding
*/ */
selectBuildingForPlacement(metaBuilding) { selectBuildingForPlacement(metaBuilding) {
@ -124,5 +145,6 @@ export class HUDBuildingsToolbar extends BaseHUDPart {
} }
this.sigBuildingSelected.dispatch(metaBuilding); this.sigBuildingSelected.dispatch(metaBuilding);
this.onSelectedPlacementBuildingChanged(metaBuilding);
} }
} }

@ -6,6 +6,11 @@ import { TrackedState } from "../../../core/tracked_state";
export class HUDKeybindingOverlay extends BaseHUDPart { export class HUDKeybindingOverlay extends BaseHUDPart {
initialize() { initialize() {
this.shiftDownTracker = new TrackedState(this.onShiftStateChanged, this); this.shiftDownTracker = new TrackedState(this.onShiftStateChanged, this);
this.root.hud.signals.selectedPlacementBuildingChanged.add(
this.onSelectedBuildingForPlacementChanged,
this
);
} }
onShiftStateChanged(shiftDown) { onShiftStateChanged(shiftDown) {
@ -56,9 +61,14 @@ export class HUDKeybindingOverlay extends BaseHUDPart {
</div> </div>
<div class="binding placementOnly shift"> <div class="binding placementOnly shift">
<code class="keybinding">SHIFT</code> <code class="keybinding"> SHIFT</code>
<label>Place Multiple</label> <label>Place Multiple</label>
</div> </div>
<div class="binding placementOnly shift">
<code class="keybinding">ALT</code>
<label>Reverse orientation</label>
</div>
` `
); );
} }

@ -0,0 +1,3 @@
import { BaseHUDPart } from "../base_hud_part";
export class HUDMassSelector extends BaseHUDPart {}

@ -58,7 +58,7 @@ export class GameLogic {
const checker = new StaticMapEntityComponent({ const checker = new StaticMapEntityComponent({
origin, origin,
tileSize: building.getDimensions(), tileSize: building.getDimensions(),
rotationDegrees: rotation, rotation,
}); });
const rect = checker.getTileSpaceBounds(); const rect = checker.getTileSpaceBounds();
@ -106,7 +106,7 @@ export class GameLogic {
const beltComp = original.components.Belt; const beltComp = original.components.Belt;
if (beltComp) { if (beltComp) {
// Its a belt, check if it differs in either rotation or rotation variant // Its a belt, check if it differs in either rotation or rotation variant
if (staticComp.rotationDegrees !== rotation) { if (staticComp.rotation !== rotation) {
return true; return true;
} }
if (beltComp.direction !== arrayBeltVariantToRotation[rotationVariant]) { if (beltComp.direction !== arrayBeltVariantToRotation[rotationVariant]) {
@ -150,7 +150,7 @@ export class GameLogic {
const checker = new StaticMapEntityComponent({ const checker = new StaticMapEntityComponent({
origin, origin,
tileSize: building.getDimensions(), tileSize: building.getDimensions(),
rotationDegrees: rotation, rotation,
}); });
const rect = checker.getTileSpaceBounds(); const rect = checker.getTileSpaceBounds();
@ -167,7 +167,12 @@ export class GameLogic {
} }
} }
building.createAndPlaceEntity(this.root, origin, rotation, rotationVariant); building.createAndPlaceEntity({
root: this.root,
origin,
rotation,
rotationVariant,
});
return true; return true;
} }
return false; return false;

@ -98,18 +98,20 @@ export class MetaBuilding {
/** /**
* Creates the entity at the given location * Creates the entity at the given location
* @param {GameRoot} root * @param {object} param0
* @param {Vector} origin Origin tile * @param {GameRoot} param0.root
* @param {number=} rotation Rotation * @param {Vector} param0.origin Origin tile
* @param {number=} rotationVariant Rotation variant * @param {number=} param0.rotation Rotation
* @param {number=} param0.rotationVariant Rotation variant
*/ */
createAndPlaceEntity(root, origin, rotation = 0, rotationVariant = 0) { createAndPlaceEntity({ root, origin, rotation = 0, rotationVariant = 0 }) {
const entity = new Entity(root); const entity = new Entity(root);
entity.addComponent( entity.addComponent(
new StaticMapEntityComponent({ new StaticMapEntityComponent({
spriteKey: "sprites/buildings/" + this.id + ".png", spriteKey: "sprites/buildings/" + this.id + ".png",
origin: new Vector(origin.x, origin.y), origin: new Vector(origin.x, origin.y),
rotationDegrees: rotation, rotation,
originalRotation: rotation,
tileSize: this.getDimensions().copy(), tileSize: this.getDimensions().copy(),
silhouetteColor: this.getSilhouetteColor(), silhouetteColor: this.getSilhouetteColor(),
}) })

@ -87,9 +87,9 @@ export class BeltSystem extends GameSystemWithFilter {
} = metaBelt.computeOptimalDirectionAndRotationVariantAtTile( } = metaBelt.computeOptimalDirectionAndRotationVariantAtTile(
this.root, this.root,
new Vector(x, y), new Vector(x, y),
targetStaticComp.rotationDegrees targetStaticComp.originalRotation
); );
targetStaticComp.rotationDegrees = rotation; targetStaticComp.rotation = rotation;
metaBelt.updateRotationVariant(targetEntity, rotationVariant); metaBelt.updateRotationVariant(targetEntity, rotationVariant);
} }
} }

Loading…
Cancel
Save