1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2026-03-02 03:39:21 +00:00

Refactoring of the key action mapper, allow deselecting buildings, make sure stars always spawn in the start region (closes #7) (closes #9)

This commit is contained in:
tobspr
2020-05-21 10:40:21 +02:00
parent 1577ebe48c
commit 8760026893
28 changed files with 93 additions and 50 deletions

View File

@@ -141,10 +141,7 @@ export class BaseHUDPart {
* @param {KeyActionMapper} sourceMapper
*/
forwardGameSpeedKeybindings(sourceMapper) {
sourceMapper.forward(this.root.gameState.keyActionMapper, [
"gamespeed_pause",
"gamespeed_fastforward",
]);
sourceMapper.forward(this.root.keyMapper, ["gamespeed_pause", "gamespeed_fastforward"]);
}
/**
@@ -153,7 +150,7 @@ export class BaseHUDPart {
* @param {KeyActionMapper} sourceMapper
*/
forwardMapMovementKeybindings(sourceMapper) {
sourceMapper.forward(this.root.gameState.keyActionMapper, [
sourceMapper.forward(this.root.keyMapper, [
"mapMoveUp",
"mapMoveRight",
"mapMoveDown",

View File

@@ -97,7 +97,7 @@ export class GameHUD {
}
this.internalInitSignalConnections();
this.root.gameState.keyActionMapper.getBinding(KEYMAPPINGS.ingame.toggleHud).add(this.toggleUi, this);
this.root.keyMapper.getBinding(KEYMAPPINGS.ingame.toggleHud).add(this.toggleUi, this);
}
/**

View File

@@ -30,7 +30,7 @@ export class HUDBuildingPlacer extends BaseHUDPart {
/** @type {Entity} */
this.fakeEntity = null;
const keyActionMapper = this.root.gameState.keyActionMapper;
const keyActionMapper = this.root.keyMapper;
keyActionMapper
.getBinding(KEYMAPPINGS.placement.abortBuildingPlacement)
.add(this.abortPlacement, this);
@@ -284,9 +284,7 @@ export class HUDBuildingPlacer extends BaseHUDPart {
this.buildingInfoElements.label.innerHTML = T.buildings[metaBuilding.id].name;
this.buildingInfoElements.descText.innerHTML = T.buildings[metaBuilding.id].description;
const binding = this.root.gameState.keyActionMapper.getBinding(
KEYMAPPINGS.buildings[metaBuilding.getId()]
);
const binding = this.root.keyMapper.getBinding(KEYMAPPINGS.buildings[metaBuilding.getId()]);
this.buildingInfoElements.hotkey.innerHTML = T.ingame.buildingPlacement.hotkeyLabel.replace(
"<key>",
"<code class='keybinding'>" + binding.getKeyCodeString() + "</code>"
@@ -327,7 +325,7 @@ export class HUDBuildingPlacer extends BaseHUDPart {
T.ingame.buildingPlacement.cycleBuildingVariants.replace(
"<key>",
"<code class='keybinding'>" +
this.root.gameState.keyActionMapper
this.root.keyMapper
.getBinding(KEYMAPPINGS.placement.cycleBuildingVariants)
.getKeyCodeString() +
"</code>"

View File

@@ -60,7 +60,7 @@ export class HUDBuildingsToolbar extends BaseHUDPart {
}
initialize() {
const actionMapper = this.root.gameState.keyActionMapper;
const actionMapper = this.root.keyMapper;
const items = makeDiv(this.element, null, ["buildings"]);
@@ -143,6 +143,15 @@ export class HUDBuildingsToolbar extends BaseHUDPart {
return;
}
// Allow clicking an item again to deselect it
for (const buildingId in this.buildingHandles) {
const handle = this.buildingHandles[buildingId];
if (handle.selected && handle.metaBuilding === metaBuilding) {
metaBuilding = null;
break;
}
}
this.root.soundProxy.playUiClick();
this.sigBuildingSelected.dispatch(metaBuilding);
this.onSelectedPlacementBuildingChanged(metaBuilding);

View File

@@ -1,6 +1,7 @@
import { BaseHUDPart } from "../base_hud_part";
import { makeDiv, round3Digits, round2Digits } from "../../../core/utils";
import { Math_round } from "../../../core/builtins";
import { DynamicDomAttach } from "../dynamic_dom_attach";
export class HUDDebugInfo extends BaseHUDPart {
createElements(parent) {
@@ -13,6 +14,11 @@ export class HUDDebugInfo extends BaseHUDPart {
initialize() {
this.lastTick = 0;
this.visible = false;
this.domAttach = new DynamicDomAttach(this.root, this.element);
// this.root.keyMapper
}
update() {

View File

@@ -47,7 +47,7 @@ export class HUDGameMenu extends BaseHUDPart {
this.trackClicks(button, handler);
if (keybinding) {
const binding = this.root.gameState.keyActionMapper.getBinding(keybinding);
const binding = this.root.keyMapper.getBinding(keybinding);
binding.add(handler);
binding.appendLabelToElement(button);
}

View File

@@ -20,7 +20,7 @@ export class HUDKeybindingOverlay extends BaseHUDPart {
}
createElements(parent) {
const mapper = this.root.gameState.keyActionMapper;
const mapper = this.root.keyMapper;
const getKeycode = id => {
return getStringForKeyCode(mapper.getBinding(id).keyCode);

View File

@@ -16,12 +16,10 @@ const logger = createLogger("hud/mass_selector");
export class HUDMassSelector extends BaseHUDPart {
createElements(parent) {
const removalKeybinding = this.root.gameState.keyActionMapper
const removalKeybinding = this.root.keyMapper
.getBinding(KEYMAPPINGS.massSelect.confirmMassDelete)
.getKeyCodeString();
const abortKeybinding = this.root.gameState.keyActionMapper
.getBinding(KEYMAPPINGS.general.back)
.getKeyCodeString();
const abortKeybinding = this.root.keyMapper.getBinding(KEYMAPPINGS.general.back).getKeyCodeString();
this.element = makeDiv(
parent,
@@ -46,8 +44,8 @@ export class HUDMassSelector extends BaseHUDPart {
this.root.camera.movePreHandler.add(this.onMouseMove, this);
this.root.camera.upPostHandler.add(this.onMouseUp, this);
this.root.gameState.keyActionMapper.getBinding(KEYMAPPINGS.general.back).add(this.onBack, this);
this.root.gameState.keyActionMapper
this.root.keyMapper.getBinding(KEYMAPPINGS.general.back).add(this.onBack, this);
this.root.keyMapper
.getBinding(KEYMAPPINGS.massSelect.confirmMassDelete)
.add(this.confirmDelete, this);

View File

@@ -7,6 +7,7 @@ import { T } from "../../../translations";
import { StaticMapEntityComponent } from "../../components/static_map_entity";
import { ItemProcessorComponent } from "../../components/item_processor";
import { BeltComponent } from "../../components/belt";
import { IS_DEMO } from "../../../core/config";
export class HUDSettingsMenu extends BaseHUDPart {
createElements(parent) {
@@ -56,7 +57,16 @@ export class HUDSettingsMenu extends BaseHUDPart {
}
returnToMenu() {
this.root.gameState.goBackToMenu();
if (IS_DEMO) {
const { cancel, deleteGame } = this.root.hud.parts.dialogs.showWarning(
T.dialogs.leaveNotPossibleInDemo.title,
T.dialogs.leaveNotPossibleInDemo.desc,
["cancel:good", "deleteGame:bad"]
);
deleteGame.add(() => this.root.gameState.goBackToMenu());
} else {
this.root.gameState.goBackToMenu();
}
}
goToSettings() {
@@ -72,7 +82,7 @@ export class HUDSettingsMenu extends BaseHUDPart {
}
initialize() {
this.root.gameState.keyActionMapper.getBinding(KEYMAPPINGS.general.back).add(this.show, this);
this.root.keyMapper.getBinding(KEYMAPPINGS.general.back).add(this.show, this);
this.domAttach = new DynamicDomAttach(this.root, this.background, {
attachClass: "visible",