2020-06-11 09:31:03 +02:00
|
|
|
import { DrawParameters } from "../../../core/draw_parameters";
|
2020-05-27 14:30:59 +02:00
|
|
|
import { STOP_PROPAGATION } from "../../../core/signal";
|
|
|
|
import { TrackedState } from "../../../core/tracked_state";
|
2020-06-21 22:29:23 +02:00
|
|
|
import { makeDiv } from "../../../core/utils";
|
2020-05-27 14:30:59 +02:00
|
|
|
import { Vector } from "../../../core/vector";
|
2020-06-21 22:29:23 +02:00
|
|
|
import { T } from "../../../translations";
|
2020-05-27 14:30:59 +02:00
|
|
|
import { enumMouseButton } from "../../camera";
|
|
|
|
import { KEYMAPPINGS } from "../../key_action_mapper";
|
2020-06-21 22:29:23 +02:00
|
|
|
import { blueprintShape } from "../../upgrades";
|
2020-05-27 14:30:59 +02:00
|
|
|
import { BaseHUDPart } from "../base_hud_part";
|
2020-05-28 18:07:57 +02:00
|
|
|
import { DynamicDomAttach } from "../dynamic_dom_attach";
|
2020-06-26 17:02:52 +02:00
|
|
|
import { Blueprint } from "../../blueprint";
|
2020-06-22 14:32:24 +02:00
|
|
|
import { SOUNDS } from "../../../platform/sound";
|
2020-05-27 14:30:59 +02:00
|
|
|
|
|
|
|
export class HUDBlueprintPlacer extends BaseHUDPart {
|
2020-05-28 18:07:57 +02:00
|
|
|
createElements(parent) {
|
|
|
|
const blueprintCostShape = this.root.shapeDefinitionMgr.getShapeFromShortKey(blueprintShape);
|
|
|
|
const blueprintCostShapeCanvas = blueprintCostShape.generateAsCanvas(80);
|
|
|
|
|
|
|
|
this.costDisplayParent = makeDiv(parent, "ingame_HUD_BlueprintPlacer", [], ``);
|
|
|
|
|
|
|
|
makeDiv(this.costDisplayParent, null, ["label"], T.ingame.blueprintPlacer.cost);
|
|
|
|
const costContainer = makeDiv(this.costDisplayParent, null, ["costContainer"], "");
|
|
|
|
this.costDisplayText = makeDiv(costContainer, null, ["costText"], "");
|
|
|
|
costContainer.appendChild(blueprintCostShapeCanvas);
|
|
|
|
}
|
2020-05-27 14:30:59 +02:00
|
|
|
|
|
|
|
initialize() {
|
2020-06-28 19:34:10 +02:00
|
|
|
this.root.hud.signals.buildingsSelectedForCopy.add(this.createBlueprintFromBuildings, this);
|
2020-05-27 14:30:59 +02:00
|
|
|
|
|
|
|
/** @type {TypedTrackedState<Blueprint?>} */
|
|
|
|
this.currentBlueprint = new TrackedState(this.onBlueprintChanged, this);
|
2020-06-11 23:56:13 +02:00
|
|
|
/** @type {Blueprint?} */
|
|
|
|
this.lastBlueprintUsed = null;
|
2020-05-27 14:30:59 +02:00
|
|
|
|
|
|
|
const keyActionMapper = this.root.keyMapper;
|
|
|
|
keyActionMapper.getBinding(KEYMAPPINGS.general.back).add(this.abortPlacement, this);
|
2020-06-21 22:51:42 +02:00
|
|
|
keyActionMapper.getBinding(KEYMAPPINGS.placement.pipette).add(this.abortPlacement, this);
|
2020-05-27 15:03:36 +02:00
|
|
|
keyActionMapper.getBinding(KEYMAPPINGS.placement.rotateWhilePlacing).add(this.rotateBlueprint, this);
|
2020-06-11 23:56:13 +02:00
|
|
|
keyActionMapper.getBinding(KEYMAPPINGS.massSelect.pasteLastBlueprint).add(this.pasteBlueprint, this);
|
2020-05-27 14:30:59 +02:00
|
|
|
|
|
|
|
this.root.camera.downPreHandler.add(this.onMouseDown, this);
|
|
|
|
this.root.camera.movePreHandler.add(this.onMouseMove, this);
|
|
|
|
|
|
|
|
this.root.hud.signals.selectedPlacementBuildingChanged.add(this.abortPlacement, this);
|
2020-06-28 19:34:10 +02:00
|
|
|
this.root.signals.editModeChanged.add(this.onEditModeChanged, this);
|
2020-05-28 18:07:57 +02:00
|
|
|
|
|
|
|
this.domAttach = new DynamicDomAttach(this.root, this.costDisplayParent);
|
|
|
|
this.trackedCanAfford = new TrackedState(this.onCanAffordChanged, this);
|
2020-05-27 14:30:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
abortPlacement() {
|
|
|
|
if (this.currentBlueprint.get()) {
|
|
|
|
this.currentBlueprint.set(null);
|
|
|
|
|
|
|
|
return STOP_PROPAGATION;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 19:34:10 +02:00
|
|
|
/**
|
|
|
|
* Called when the layer was changed
|
2020-08-15 18:39:08 +02:00
|
|
|
* @param {Layer} layer
|
2020-06-28 19:34:10 +02:00
|
|
|
*/
|
|
|
|
onEditModeChanged(layer) {
|
|
|
|
// Check if the layer of the blueprint differs and thus we have to deselect it
|
|
|
|
const blueprint = this.currentBlueprint.get();
|
|
|
|
if (blueprint) {
|
|
|
|
if (blueprint.layer !== layer) {
|
|
|
|
this.currentBlueprint.set(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the blueprint is now affordable or not
|
|
|
|
* @param {boolean} canAfford
|
|
|
|
*/
|
2020-05-28 18:07:57 +02:00
|
|
|
onCanAffordChanged(canAfford) {
|
|
|
|
this.costDisplayParent.classList.toggle("canAfford", canAfford);
|
|
|
|
}
|
|
|
|
|
|
|
|
update() {
|
2020-06-21 21:44:53 +02:00
|
|
|
const currentBlueprint = this.currentBlueprint.get();
|
|
|
|
this.domAttach.update(currentBlueprint && currentBlueprint.getCost() > 0);
|
|
|
|
this.trackedCanAfford.set(currentBlueprint && currentBlueprint.canAfford(this.root));
|
2020-05-28 18:07:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-28 19:34:10 +02:00
|
|
|
* Called when the blueprint was changed
|
2020-05-28 18:07:57 +02:00
|
|
|
* @param {Blueprint} blueprint
|
|
|
|
*/
|
|
|
|
onBlueprintChanged(blueprint) {
|
|
|
|
if (blueprint) {
|
2020-06-11 23:56:13 +02:00
|
|
|
this.lastBlueprintUsed = blueprint;
|
2020-05-28 18:07:57 +02:00
|
|
|
this.costDisplayText.innerText = "" + blueprint.getCost();
|
|
|
|
}
|
|
|
|
}
|
2020-05-27 14:30:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* mouse down pre handler
|
|
|
|
* @param {Vector} pos
|
|
|
|
* @param {enumMouseButton} button
|
|
|
|
*/
|
|
|
|
onMouseDown(pos, button) {
|
|
|
|
if (button === enumMouseButton.right) {
|
2020-05-28 19:40:48 +02:00
|
|
|
if (this.currentBlueprint.get()) {
|
|
|
|
this.abortPlacement();
|
|
|
|
return STOP_PROPAGATION;
|
|
|
|
}
|
2020-05-27 14:30:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const blueprint = this.currentBlueprint.get();
|
|
|
|
if (!blueprint) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-28 18:07:57 +02:00
|
|
|
if (!blueprint.canAfford(this.root)) {
|
|
|
|
this.root.soundProxy.playUiError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-27 14:30:59 +02:00
|
|
|
const worldPos = this.root.camera.screenToWorld(pos);
|
|
|
|
const tile = worldPos.toTileSpace();
|
|
|
|
if (blueprint.tryPlace(this.root, tile)) {
|
2020-05-28 18:07:57 +02:00
|
|
|
const cost = blueprint.getCost();
|
|
|
|
this.root.hubGoals.takeShapeByKey(blueprintShape, cost);
|
2020-06-22 14:32:24 +02:00
|
|
|
this.root.soundProxy.playUi(SOUNDS.placeBuilding);
|
2020-05-27 14:30:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 19:34:10 +02:00
|
|
|
/**
|
|
|
|
* Mose move handler
|
|
|
|
*/
|
2020-05-27 14:30:59 +02:00
|
|
|
onMouseMove() {
|
|
|
|
// Prevent movement while blueprint is selected
|
|
|
|
if (this.currentBlueprint.get()) {
|
|
|
|
return STOP_PROPAGATION;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-28 19:34:10 +02:00
|
|
|
* Called when an array of bulidings was selected
|
2020-05-27 14:30:59 +02:00
|
|
|
* @param {Array<number>} uids
|
|
|
|
*/
|
2020-06-28 19:34:10 +02:00
|
|
|
createBlueprintFromBuildings(uids) {
|
2020-05-27 14:30:59 +02:00
|
|
|
if (uids.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.currentBlueprint.set(Blueprint.fromUids(this.root, uids));
|
|
|
|
}
|
|
|
|
|
2020-06-28 19:34:10 +02:00
|
|
|
/**
|
|
|
|
* Attempts to rotate the current blueprint
|
|
|
|
*/
|
2020-05-27 15:03:36 +02:00
|
|
|
rotateBlueprint() {
|
|
|
|
if (this.currentBlueprint.get()) {
|
2020-06-17 13:12:39 +02:00
|
|
|
if (this.root.keyMapper.getBinding(KEYMAPPINGS.placement.rotateInverseModifier).pressed) {
|
2020-05-27 15:03:36 +02:00
|
|
|
this.currentBlueprint.get().rotateCcw();
|
|
|
|
} else {
|
|
|
|
this.currentBlueprint.get().rotateCw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 19:34:10 +02:00
|
|
|
/**
|
|
|
|
* Attempts to paste the last blueprint
|
|
|
|
*/
|
2020-06-11 23:56:13 +02:00
|
|
|
pasteBlueprint() {
|
|
|
|
if (this.lastBlueprintUsed !== null) {
|
2020-06-28 19:34:10 +02:00
|
|
|
if (this.lastBlueprintUsed.layer !== this.root.currentLayer) {
|
|
|
|
// Not compatible
|
|
|
|
this.root.soundProxy.playUiError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-11 23:56:13 +02:00
|
|
|
this.root.hud.signals.pasteBlueprintRequested.dispatch();
|
|
|
|
this.currentBlueprint.set(this.lastBlueprintUsed);
|
|
|
|
} else {
|
|
|
|
this.root.soundProxy.playUiError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 14:30:59 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {DrawParameters} parameters
|
|
|
|
*/
|
|
|
|
draw(parameters) {
|
|
|
|
const blueprint = this.currentBlueprint.get();
|
|
|
|
if (!blueprint) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const mousePosition = this.root.app.mousePosition;
|
|
|
|
if (!mousePosition) {
|
|
|
|
// Not on screen
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const worldPos = this.root.camera.screenToWorld(mousePosition);
|
|
|
|
const tile = worldPos.toTileSpace();
|
|
|
|
blueprint.draw(parameters, tile);
|
|
|
|
}
|
|
|
|
}
|