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

Introduce cost for blueprints

This commit is contained in:
tobspr
2020-05-28 18:07:57 +02:00
parent 955cba74aa
commit 37685d64bd
8 changed files with 135 additions and 6 deletions

View File

@@ -4,6 +4,9 @@ import { createLogger } from "../../../core/logging";
import { Vector } from "../../../core/vector";
import { Entity } from "../../entity";
import { GameRoot } from "../../root";
import { findNiceIntegerValue } from "../../../core/utils";
import { Math_pow } from "../../../core/builtins";
import { blueprintShape } from "../../upgrades";
const logger = createLogger("blueprint");
@@ -47,6 +50,13 @@ export class Blueprint {
return new Blueprint(newEntities);
}
/**
* Returns the cost of this blueprint in shapes
*/
getCost() {
return findNiceIntegerValue(4 * Math_pow(this.entities.length, 1.1));
}
/**
* Draws the blueprint at the given origin
* @param {DrawParameters} parameters
@@ -147,6 +157,13 @@ export class Blueprint {
return anyPlaceable;
}
/**
* @param {GameRoot} root
*/
canAfford(root) {
return root.hubGoals.getShapesStoredByKey(blueprintShape) >= this.getCost();
}
/**
* Attempts to place the blueprint at the given tile
* @param {GameRoot} root

View File

@@ -6,9 +6,23 @@ import { enumMouseButton } from "../../camera";
import { KEYMAPPINGS } from "../../key_action_mapper";
import { BaseHUDPart } from "../base_hud_part";
import { Blueprint } from "./blueprint";
import { makeDiv } from "../../../core/utils";
import { DynamicDomAttach } from "../dynamic_dom_attach";
import { blueprintShape } from "../../upgrades";
import { T } from "../../../translations";
export class HUDBlueprintPlacer extends BaseHUDPart {
createElements(parent) {}
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);
}
initialize() {
this.root.hud.signals.buildingsSelectedForCopy.add(this.onBuildingsSelected, this);
@@ -27,6 +41,9 @@ export class HUDBlueprintPlacer extends BaseHUDPart {
this.root.camera.movePreHandler.add(this.onMouseMove, this);
this.root.hud.signals.selectedPlacementBuildingChanged.add(this.abortPlacement, this);
this.domAttach = new DynamicDomAttach(this.root, this.costDisplayParent);
this.trackedCanAfford = new TrackedState(this.onCanAffordChanged, this);
}
abortPlacement() {
@@ -37,7 +54,25 @@ export class HUDBlueprintPlacer extends BaseHUDPart {
}
}
onBlueprintChanged(blueprint) {}
onCanAffordChanged(canAfford) {
this.costDisplayParent.classList.toggle("canAfford", canAfford);
}
update() {
this.domAttach.update(this.currentBlueprint.get());
this.trackedCanAfford.set(
this.currentBlueprint.get() && this.currentBlueprint.get().canAfford(this.root)
);
}
/**
* @param {Blueprint} blueprint
*/
onBlueprintChanged(blueprint) {
if (blueprint) {
this.costDisplayText.innerText = "" + blueprint.getCost();
}
}
/**
* mouse down pre handler
@@ -55,9 +90,17 @@ export class HUDBlueprintPlacer extends BaseHUDPart {
return;
}
if (!blueprint.canAfford(this.root)) {
this.root.soundProxy.playUiError();
return;
}
const worldPos = this.root.camera.screenToWorld(pos);
const tile = worldPos.toTileSpace();
if (blueprint.tryPlace(this.root, tile)) {
const cost = blueprint.getCost();
this.root.hubGoals.takeShapeByKey(blueprintShape, cost);
// This actually feels weird
// if (!this.root.keyMapper.getBinding(KEYMAPPINGS.placementModifiers.placeMultiple).isCurrentlyPressed()) {
// this.currentBlueprint.set(null);

View File

@@ -77,7 +77,7 @@ export class HUDPinnedShapes extends BaseHUDPart {
this.internalPinShape(currentKey, currentGoal.required, false);
if (this.root.hubGoals.isRewardUnlocked(enumHubGoalRewards.reward_blueprints)) {
this.internalPinShape(blueprintShape, currentGoal.required, false);
this.internalPinShape(blueprintShape, null, false);
}
for (let i = 0; i < this.pinnedShapes.length; ++i) {
@@ -114,7 +114,10 @@ export class HUDPinnedShapes extends BaseHUDPart {
}
const amountLabel = makeDiv(element, null, ["amountLabel"], "");
const goalLabel = makeDiv(element, null, ["goalLabel"], "/" + formatBigNumber(goal));
if (goal) {
makeDiv(element, null, ["goalLabel"], "/" + formatBigNumber(goal));
}
this.handles.push({
key,