From eacf165e915634d06cf6527c0809583b43450f24 Mon Sep 17 00:00:00 2001 From: Christopher-Robin Date: Fri, 30 Oct 2020 03:06:42 +1100 Subject: [PATCH] Moved from Set to Array for Selected Entities --- src/js/game/blueprint.js | 34 +++++++++ src/js/game/hud/hud.js | 3 +- src/js/game/hud/parts/blueprint_placer.js | 12 +-- src/js/game/hud/parts/keybinding_overlay.js | 2 +- src/js/game/hud/parts/mass_selector.js | 82 +++++++++++---------- 5 files changed, 89 insertions(+), 44 deletions(-) diff --git a/src/js/game/blueprint.js b/src/js/game/blueprint.js index 63989393..3923b08e 100644 --- a/src/js/game/blueprint.js +++ b/src/js/game/blueprint.js @@ -57,6 +57,40 @@ export class Blueprint { return new Blueprint(newEntities); } + /** + * Creates a new blueprint from the given entities + * @param {GameRoot} root + * @param {Array} entities + */ + static fromEntities(root, entities) { + const newEntities = []; + + let averagePosition = new Vector(); + + // First, create a copy + for (let i = 0; i < entities.length; ++i) { + + const entity = entities[i];//root.entityMgr.findByUid(uids[i]); + // assert(entity, "Entity for blueprint not found:" + uids[i]); + + const clone = entity.clone(); + newEntities.push(clone); + + const pos = entity.components.StaticMapEntity.getTileSpaceBounds().getCenter(); + averagePosition.addInplace(pos); + } + + averagePosition.divideScalarInplace(entities.length); + const blueprintOrigin = averagePosition.subScalars(0.5, 0.5).floor(); + + for (let i = 0; i < entities.length; ++i) { + newEntities[i].components.StaticMapEntity.origin.subInplace(blueprintOrigin); + } + + // Now, make sure the origin is 0,0 + return new Blueprint(newEntities); + } + /** * Returns the cost of this blueprint in shapes */ diff --git a/src/js/game/hud/hud.js b/src/js/game/hud/hud.js index 5f1bd226..a8448975 100644 --- a/src/js/game/hud/hud.js +++ b/src/js/game/hud/hud.js @@ -48,6 +48,7 @@ import { HUDBetaOverlay } from "./parts/beta_overlay"; import { HUDStandaloneAdvantages } from "./parts/standalone_advantages"; import { HUDCatMemes } from "./parts/cat_memes"; import { HUDTutorialVideoOffer } from "./parts/tutorial_video_offer"; +import { Entity } from "../entity"; export class GameHUD { /** @@ -67,7 +68,7 @@ export class GameHUD { shapePinRequested: /** @type {TypedSignal<[ShapeDefinition]>} */ (new Signal()), shapeUnpinRequested: /** @type {TypedSignal<[string]>} */ (new Signal()), notification: /** @type {TypedSignal<[string, enumNotificationType]>} */ (new Signal()), - buildingsSelectedForCopy: /** @type {TypedSignal<[Array]>} */ (new Signal()), + buildingsSelectedForCopy: /** @type {TypedSignal<[Array]>} */ (new Signal()), pasteBlueprintRequested: /** @type {TypedSignal<[]>} */ (new Signal()), viewShapeDetailsRequested: /** @type {TypedSignal<[ShapeDefinition]>} */ (new Signal()), unlockNotificationFinished: /** @type {TypedSignal<[]>} */ (new Signal()), diff --git a/src/js/game/hud/parts/blueprint_placer.js b/src/js/game/hud/parts/blueprint_placer.js index e1040c3b..1c405a91 100644 --- a/src/js/game/hud/parts/blueprint_placer.js +++ b/src/js/game/hud/parts/blueprint_placer.js @@ -7,6 +7,7 @@ import { SOUNDS } from "../../../platform/sound"; import { T } from "../../../translations"; import { Blueprint } from "../../blueprint"; import { enumMouseButton } from "../../camera"; +import { Entity } from "../../entity"; import { KEYMAPPINGS } from "../../key_action_mapper"; import { BaseHUDPart } from "../base_hud_part"; import { DynamicDomAttach } from "../dynamic_dom_attach"; @@ -141,13 +142,13 @@ export class HUDBlueprintPlacer extends BaseHUDPart { /** * Called when an array of bulidings was selected - * @param {Array} uids + * @param {Array} entities */ - createBlueprintFromBuildings(uids) { - if (uids.length === 0) { + createBlueprintFromBuildings(entities) { + if (entities.length === 0) { return; } - this.currentBlueprint.set(Blueprint.fromUids(this.root, uids)); + this.currentBlueprint.set(Blueprint.fromEntities(this.root, entities)); } /** @@ -174,7 +175,8 @@ export class HUDBlueprintPlacer extends BaseHUDPart { return; } - this.root.hud.signals.pasteBlueprintRequested.dispatch(); + this.root.hud.signals.pasteBlueprintRequested.dispatch(); + this.currentBlueprint.set(this.lastBlueprintUsed); } else { this.root.soundProxy.playUiError(); diff --git a/src/js/game/hud/parts/keybinding_overlay.js b/src/js/game/hud/parts/keybinding_overlay.js index 65919d3c..2417edcd 100644 --- a/src/js/game/hud/parts/keybinding_overlay.js +++ b/src/js/game/hud/parts/keybinding_overlay.js @@ -101,7 +101,7 @@ export class HUDKeybindingOverlay extends BaseHUDPart { */ get anythingSelectedOnMap() { const selector = this.root.hud.parts.massSelector; - return selector && selector.selectedUids.size > 0; + return selector && selector.selectedEntities.length > 0; } /** diff --git a/src/js/game/hud/parts/mass_selector.js b/src/js/game/hud/parts/mass_selector.js index 8f7d9176..baf992d7 100644 --- a/src/js/game/hud/parts/mass_selector.js +++ b/src/js/game/hud/parts/mass_selector.js @@ -25,8 +25,10 @@ export class HUDMassSelector extends BaseHUDPart { initialize() { this.multiLayerSelect = false; this.currentSelectionStartWorld = null; - this.currentSelectionEnd = null; - this.selectedUids = new Set(); + this.currentSelectionEnd = null; + + /**@type {Array} */ + this.selectedEntities = []; this.root.signals.entityQueuedForDestroy.add(this.onEntityDestroyed, this); this.root.hud.signals.pasteBlueprintRequested.add(this.clearSelection, this); @@ -53,8 +55,10 @@ export class HUDMassSelector extends BaseHUDPart { onEntityDestroyed(entity) { if (this.root.bulkOperationRunning) { return; - } - this.selectedUids.delete(entity.uid); + } + const index = this.selectedEntities.indexOf(entity); + if(index != -1) + this.selectedEntities.splice(index, 1); } /** @@ -62,8 +66,8 @@ export class HUDMassSelector extends BaseHUDPart { */ onBack() { // Clear entities on escape - if (this.selectedUids.size > 0) { - this.selectedUids = new Set(); + if (this.selectedEntities.length > 0) { + this.selectedEntities = []; return STOP_PROPAGATION; } } @@ -72,19 +76,19 @@ export class HUDMassSelector extends BaseHUDPart { * Clears the entire selection */ clearSelection() { - this.selectedUids = new Set(); + this.selectedEntities = []; } confirmDelete() { if ( !this.root.app.settings.getAllSettings().disableCutDeleteWarnings && - this.selectedUids.size > 100 + this.selectedEntities.length > 100 ) { const { ok } = this.root.hud.parts.dialogs.showWarning( T.dialogs.massDeleteConfirm.title, T.dialogs.massDeleteConfirm.desc.replace( "", - "" + formatBigNumberFull(this.selectedUids.size) + "" + formatBigNumberFull(this.selectedEntities.length) ), ["cancel:good:escape", "ok:bad:enter"] ); @@ -95,7 +99,7 @@ export class HUDMassSelector extends BaseHUDPart { } doDelete() { - const entityUids = Array.from(this.selectedUids); + const entities = Array.from(this.selectedEntities); // Build mapping from uid to entity /** @@ -104,11 +108,11 @@ export class HUDMassSelector extends BaseHUDPart { const mapUidToEntity = this.root.entityMgr.getFrozenUidSearchMap(); this.root.logic.performBulkOperation(() => { - for (let i = 0; i < entityUids.length; ++i) { - const uid = entityUids[i]; - const entity = mapUidToEntity.get(uid); + for (let i = 0; i < entities.length; ++i) { + const entity = mapUidToEntity.get(entities[i].uid); + if (!entity) { - logger.error("Entity not found by uid:", uid); + logger.warn("Invalid Entity in Selected Entities"); continue; } @@ -119,20 +123,22 @@ export class HUDMassSelector extends BaseHUDPart { }); // Clear uids later - this.selectedUids = new Set(); + this.selectedEntities = []; } startCopy() { - if (this.selectedUids.size > 0) { + if (this.selectedEntities.length > 0) { if (!this.root.hubGoals.isRewardUnlocked(enumHubGoalRewards.reward_blueprints)) { this.root.hud.parts.dialogs.showInfo( T.dialogs.blueprintsNotUnlocked.title, T.dialogs.blueprintsNotUnlocked.desc ); return; - } - this.root.hud.signals.buildingsSelectedForCopy.dispatch(Array.from(this.selectedUids)); - this.selectedUids = new Set(); + } + + this.root.hud.signals.buildingsSelectedForCopy.dispatch(this.selectedEntities); + + this.selectedEntities = []; this.root.soundProxy.playUiClick(); } else { this.root.soundProxy.playUiError(); @@ -147,13 +153,13 @@ export class HUDMassSelector extends BaseHUDPart { ); } else if ( !this.root.app.settings.getAllSettings().disableCutDeleteWarnings && - this.selectedUids.size > 100 + this.selectedEntities.length > 100 ) { const { ok } = this.root.hud.parts.dialogs.showWarning( T.dialogs.massCutConfirm.title, T.dialogs.massCutConfirm.desc.replace( "", - "" + formatBigNumberFull(this.selectedUids.size) + "" + formatBigNumberFull(this.selectedEntities.length) ), ["cancel:good:escape", "ok:bad:enter"] ); @@ -164,24 +170,22 @@ export class HUDMassSelector extends BaseHUDPart { } doCut() { - if (this.selectedUids.size > 0) { - const entityUids = Array.from(this.selectedUids); - + if (this.selectedEntities.length > 0) { + const entities = Array.from(this.selectedEntities); const cutAction = () => { // copy code relies on entities still existing, so must copy before deleting. - this.root.hud.signals.buildingsSelectedForCopy.dispatch(entityUids); + this.root.hud.signals.buildingsSelectedForCopy.dispatch(entities); - for (let i = 0; i < entityUids.length; ++i) { - const uid = entityUids[i]; - const entity = this.root.entityMgr.findByUid(uid); + for (let i = 0; i < entities.length; ++i) { + const entity = entities[i]; if (!this.root.logic.tryDeleteBuilding(entity)) { logger.error("Error in mass cut, could not remove building"); - this.selectedUids.delete(uid); + this.selectedEntities.splice(i, 1); } } }; - const blueprint = Blueprint.fromUids(this.root, entityUids); + const blueprint = Blueprint.fromEntities(this.root, entities); if (blueprint.canAfford(this.root)) { cutAction(); } else { @@ -219,7 +223,7 @@ export class HUDMassSelector extends BaseHUDPart { if (!this.root.keyMapper.getBinding(KEYMAPPINGS.massSelect.massSelectSelectMultiple).pressed) { // Start new selection - this.selectedUids = new Set(); + this.selectedEntities = []; } this.currentSelectionStartWorld = this.root.camera.screenToWorld(pos.copy()); @@ -260,7 +264,7 @@ export class HUDMassSelector extends BaseHUDPart { for (let i = 0; i < entities.length; ++i) { let entity = entities[i]; if (entity && this.root.logic.canDeleteBuilding(entity)) - this.selectedUids.add(entity.uid); + this.selectedEntities.push(entity); } } } @@ -333,12 +337,16 @@ export class HUDMassSelector extends BaseHUDPart { } } - //EXTREMELY SLOW. There must be a better way. (Possibly use a Array) - this.selectedUids.forEach(uid => { - const entity = this.root.entityMgr.findByUid(uid); + //EXTREMELY SLOW. There must be a better way. (Possibly use a Array) + for(let i = 0; i < this.selectedEntities.length; ++ i){ + const entity = this.selectedEntities[i]; + this.RenderSelectonPreviewTile(parameters, entity); + } + // this.selectedUids.forEach(uid => { + // const entity = this.root.entityMgr.findByUid(uid); - this.RenderSelectonPreviewTile(parameters, entity); - }); + // this.RenderSelectonPreviewTile(parameters, entity); + // }); parameters.context.globalAlpha = 1; }