1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-06-13 13:04:03 +00:00

made blueprints and selections faster

This commit is contained in:
dgs4349 2020-09-27 21:33:28 -04:00
parent 6a19a94435
commit f160fc14aa
5 changed files with 108 additions and 59 deletions

View File

@ -62,6 +62,37 @@ export class Blueprint {
return new Blueprint(newEntities); return new Blueprint(newEntities);
} }
/**
* Creates a new blueprint from the given entity uids
* @param {Array<Entity>} entities
*/
static fromEntities(entities) {
const newEntities = [];
let averagePosition = new Vector();
// First, create a copy
for (let i = entities.length - 1; i >= 0; --i) {
const entity = entities[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 = newEntities.length - 1; i >= 0; --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 * Returns the cost of this blueprint in shapes
*/ */

View File

@ -95,7 +95,7 @@ export class EntityManager extends BasicSerializableObject {
} }
// Give each entity a unique id // Give each entity a unique id
entity.uid = uid ? uid : this.generateUid(); entity.uid = uid || this.generateUid();
this.entities.add(entity); this.entities.add(entity);
this.entitiesByUid.set(uid, entity); this.entitiesByUid.set(uid, entity);

View File

@ -44,6 +44,7 @@ import { HUDWireInfo } from "./parts/wire_info";
import { HUDLeverToggle } from "./parts/lever_toggle"; import { HUDLeverToggle } from "./parts/lever_toggle";
import { HUDLayerPreview } from "./parts/layer_preview"; import { HUDLayerPreview } from "./parts/layer_preview";
import { HUDMinerHighlight } from "./parts/miner_highlight"; import { HUDMinerHighlight } from "./parts/miner_highlight";
import { Entity } from "../entity";
export class GameHUD { export class GameHUD {
/** /**
@ -99,7 +100,7 @@ export class GameHUD {
shapePinRequested: /** @type {TypedSignal<[ShapeDefinition]>} */ (new Signal()), shapePinRequested: /** @type {TypedSignal<[ShapeDefinition]>} */ (new Signal()),
shapeUnpinRequested: /** @type {TypedSignal<[string]>} */ (new Signal()), shapeUnpinRequested: /** @type {TypedSignal<[string]>} */ (new Signal()),
notification: /** @type {TypedSignal<[string, enumNotificationType]>} */ (new Signal()), notification: /** @type {TypedSignal<[string, enumNotificationType]>} */ (new Signal()),
buildingsSelectedForCopy: /** @type {TypedSignal<[Array<number>]>} */ (new Signal()), buildingsSelectedForCopy: /** @type {TypedSignal<[Array<Entity>]>} */ (new Signal()),
pasteBlueprintRequested: /** @type {TypedSignal<[]>} */ (new Signal()), pasteBlueprintRequested: /** @type {TypedSignal<[]>} */ (new Signal()),
viewShapeDetailsRequested: /** @type {TypedSignal<[ShapeDefinition]>} */ (new Signal()), viewShapeDetailsRequested: /** @type {TypedSignal<[ShapeDefinition]>} */ (new Signal()),
}; };

View File

@ -11,6 +11,7 @@ import { BaseHUDPart } from "../base_hud_part";
import { DynamicDomAttach } from "../dynamic_dom_attach"; import { DynamicDomAttach } from "../dynamic_dom_attach";
import { Blueprint } from "../../blueprint"; import { Blueprint } from "../../blueprint";
import { SOUNDS } from "../../../platform/sound"; import { SOUNDS } from "../../../platform/sound";
import { Entity } from "../../entity";
export class HUDBlueprintPlacer extends BaseHUDPart { export class HUDBlueprintPlacer extends BaseHUDPart {
createElements(parent) { createElements(parent) {
@ -140,13 +141,13 @@ export class HUDBlueprintPlacer extends BaseHUDPart {
/** /**
* Called when an array of bulidings was selected * Called when an array of bulidings was selected
* @param {Array<number>} uids * @param {Array<Entity>} entities
*/ */
createBlueprintFromBuildings(uids) { createBlueprintFromBuildings(entities) {
if (uids.length === 0) { if (entities.length === 0) {
return; return;
} }
this.currentBlueprint.set(Blueprint.fromUids(this.root, uids)); this.currentBlueprint.set(Blueprint.fromEntities(entities));
} }
/** /**

View File

@ -5,7 +5,7 @@ import { DrawParameters } from "../../../core/draw_parameters";
import { Entity } from "../../entity"; import { Entity } from "../../entity";
import { Loader } from "../../../core/loader"; import { Loader } from "../../../core/loader";
import { globalConfig } from "../../../core/config"; import { globalConfig } from "../../../core/config";
import { makeDiv, formatBigNumber, formatBigNumberFull } from "../../../core/utils"; import { makeDiv, formatBigNumber, formatBigNumberFull, dirInterval } from "../../../core/utils";
import { DynamicDomAttach } from "../dynamic_dom_attach"; import { DynamicDomAttach } from "../dynamic_dom_attach";
import { createLogger } from "../../../core/logging"; import { createLogger } from "../../../core/logging";
import { enumMouseButton } from "../../camera"; import { enumMouseButton } from "../../camera";
@ -23,7 +23,12 @@ export class HUDMassSelector extends BaseHUDPart {
initialize() { initialize() {
this.currentSelectionStartWorld = null; this.currentSelectionStartWorld = null;
this.currentSelectionEnd = null; this.currentSelectionEnd = null;
this.selectedUids = new Set();
/** @type {Set<Entity>} */
this.selectedEntities = new Set();
/** @type {number} */
this.selectedUids = 42;
this.root.signals.entityQueuedForDestroy.add(this.onEntityDestroyed, this); this.root.signals.entityQueuedForDestroy.add(this.onEntityDestroyed, this);
this.root.hud.signals.pasteBlueprintRequested.add(this.clearSelection, this); this.root.hud.signals.pasteBlueprintRequested.add(this.clearSelection, this);
@ -43,6 +48,20 @@ export class HUDMassSelector extends BaseHUDPart {
this.root.signals.editModeChanged.add(this.clearSelection, this); this.root.signals.editModeChanged.add(this.clearSelection, this);
} }
clear() {
this.selectedEntities.clear();
}
// getUidArray() {
// if (this.selectedEntities.size <= 0) return [];
// const uids = [];
// const arr = [...this.selectedEntities.values()];
// for (let i = arr.length - 1; i >= 0; --i) {
// uids.push(arr[i].uid);
// }
// return uids;
// }
/** /**
* Handles the destroy callback and makes sure we clean our list * Handles the destroy callback and makes sure we clean our list
* @param {Entity} entity * @param {Entity} entity
@ -51,7 +70,7 @@ export class HUDMassSelector extends BaseHUDPart {
if (this.root.bulkOperationRunning) { if (this.root.bulkOperationRunning) {
return; return;
} }
this.selectedUids.delete(entity.uid); this.selectedEntities.delete(entity);
} }
/** /**
@ -59,8 +78,8 @@ export class HUDMassSelector extends BaseHUDPart {
*/ */
onBack() { onBack() {
// Clear entities on escape // Clear entities on escape
if (this.selectedUids.size > 0) { if (this.selectedEntities.size > 0) {
this.selectedUids = new Set(); this.clear();
return STOP_PROPAGATION; return STOP_PROPAGATION;
} }
} }
@ -69,19 +88,19 @@ export class HUDMassSelector extends BaseHUDPart {
* Clears the entire selection * Clears the entire selection
*/ */
clearSelection() { clearSelection() {
this.selectedUids = new Set(); this.clear();
} }
confirmDelete() { confirmDelete() {
if ( if (
!this.root.app.settings.getAllSettings().disableCutDeleteWarnings && !this.root.app.settings.getAllSettings().disableCutDeleteWarnings &&
this.selectedUids.size > 100 this.selectedEntities.size > 100
) { ) {
const { ok } = this.root.hud.parts.dialogs.showWarning( const { ok } = this.root.hud.parts.dialogs.showWarning(
T.dialogs.massDeleteConfirm.title, T.dialogs.massDeleteConfirm.title,
T.dialogs.massDeleteConfirm.desc.replace( T.dialogs.massDeleteConfirm.desc.replace(
"<count>", "<count>",
"" + formatBigNumberFull(this.selectedUids.size) "" + formatBigNumberFull(this.selectedEntities.size)
), ),
["cancel:good:escape", "ok:bad:enter"] ["cancel:good:escape", "ok:bad:enter"]
); );
@ -92,35 +111,26 @@ export class HUDMassSelector extends BaseHUDPart {
} }
doDelete() { doDelete() {
const entityUids = Array.from(this.selectedUids);
// Build mapping from uid to entity // Build mapping from uid to entity
/** /**
* @type {Map<number, Entity>} * @type {Map<number, Entity>}
*/ */
const mapUidToEntity = this.root.entityMgr.getFrozenUidSearchMap(); //const mapUidToEntity = this.root.entityMgr.getFrozenUidSearchMap();
this.root.logic.performBulkOperation(() => { this.root.logic.performBulkOperation(() => {
for (let i = 0; i < entityUids.length; ++i) { const arr = [...this.selectedEntities.values()];
const uid = entityUids[i]; for (let i = arr.length - 1; i >= 0; --i) {
const entity = mapUidToEntity.get(uid); if (!this.root.logic.tryDeleteBuilding(arr[i])) {
if (!entity) {
logger.error("Entity not found by uid:", uid);
continue;
}
if (!this.root.logic.tryDeleteBuilding(entity)) {
logger.error("Error in mass delete, could not remove building"); logger.error("Error in mass delete, could not remove building");
} }
} }
}); });
// Clear uids later this.clear();
this.selectedUids = new Set();
} }
startCopy() { startCopy() {
if (this.selectedUids.size > 0) { if (this.selectedEntities.size > 0) {
if (!this.root.hubGoals.isRewardUnlocked(enumHubGoalRewards.reward_blueprints)) { if (!this.root.hubGoals.isRewardUnlocked(enumHubGoalRewards.reward_blueprints)) {
this.root.hud.parts.dialogs.showInfo( this.root.hud.parts.dialogs.showInfo(
T.dialogs.blueprintsNotUnlocked.title, T.dialogs.blueprintsNotUnlocked.title,
@ -128,8 +138,10 @@ export class HUDMassSelector extends BaseHUDPart {
); );
return; return;
} }
this.root.hud.signals.buildingsSelectedForCopy.dispatch(Array.from(this.selectedUids)); const uids = [];
this.selectedUids = new Set();
this.root.hud.signals.buildingsSelectedForCopy.dispatch([...this.selectedEntities.values()]);
this.selectedEntities.clear();
this.root.soundProxy.playUiClick(); this.root.soundProxy.playUiClick();
} else { } else {
this.root.soundProxy.playUiError(); this.root.soundProxy.playUiError();
@ -144,13 +156,13 @@ export class HUDMassSelector extends BaseHUDPart {
); );
} else if ( } else if (
!this.root.app.settings.getAllSettings().disableCutDeleteWarnings && !this.root.app.settings.getAllSettings().disableCutDeleteWarnings &&
this.selectedUids.size > 100 this.selectedEntities.size > 100
) { ) {
const { ok } = this.root.hud.parts.dialogs.showWarning( const { ok } = this.root.hud.parts.dialogs.showWarning(
T.dialogs.massCutConfirm.title, T.dialogs.massCutConfirm.title,
T.dialogs.massCutConfirm.desc.replace( T.dialogs.massCutConfirm.desc.replace(
"<count>", "<count>",
"" + formatBigNumberFull(this.selectedUids.size) "" + formatBigNumberFull(this.selectedEntities.size)
), ),
["cancel:good:escape", "ok:bad:enter"] ["cancel:good:escape", "ok:bad:enter"]
); );
@ -161,26 +173,26 @@ export class HUDMassSelector extends BaseHUDPart {
} }
doCut() { doCut() {
if (this.selectedUids.size > 0) { if (this.selectedEntities.size > 0) {
const entityUids = Array.from(this.selectedUids); const cutAction = argArray => {
const arr = argArray || [...this.selectedEntities.values()];
const cutAction = () => {
// copy code relies on entities still existing, so must copy before deleting. // copy code relies on entities still existing, so must copy before deleting.
this.root.hud.signals.buildingsSelectedForCopy.dispatch(entityUids);
for (let i = 0; i < entityUids.length; ++i) { this.root.hud.signals.buildingsSelectedForCopy.dispatch(arr);
const uid = entityUids[i];
const entity = this.root.entityMgr.findByUid(uid); for (let i = arr.length - 1; i >= 0; --i) {
const entity = arr[i];
if (!this.root.logic.tryDeleteBuilding(entity)) { if (!this.root.logic.tryDeleteBuilding(entity)) {
logger.error("Error in mass cut, could not remove building"); logger.error("Error in mass cut, could not remove building");
this.selectedUids.delete(uid); this.selectedEntities.delete(entity);
} }
} }
}; };
const blueprint = Blueprint.fromUids(this.root, entityUids); const arr = [...this.selectedEntities.values()];
const blueprint = Blueprint.fromEntities(arr);
if (blueprint.canAfford(this.root)) { if (blueprint.canAfford(this.root)) {
cutAction(); cutAction(arr);
} else { } else {
const { cancel, ok } = this.root.hud.parts.dialogs.showWarning( const { cancel, ok } = this.root.hud.parts.dialogs.showWarning(
T.dialogs.massCutInsufficientConfirm.title, T.dialogs.massCutInsufficientConfirm.title,
@ -212,7 +224,7 @@ export class HUDMassSelector extends BaseHUDPart {
if (!this.root.keyMapper.getBinding(KEYMAPPINGS.massSelect.massSelectSelectMultiple).pressed) { if (!this.root.keyMapper.getBinding(KEYMAPPINGS.massSelect.massSelectSelectMultiple).pressed) {
// Start new selection // Start new selection
this.selectedUids = new Set(); this.clear();
} }
this.currentSelectionStartWorld = this.root.camera.screenToWorld(pos.copy()); this.currentSelectionStartWorld = this.root.camera.screenToWorld(pos.copy());
@ -245,7 +257,7 @@ export class HUDMassSelector extends BaseHUDPart {
for (let y = realTileStart.y; y <= realTileEnd.y; ++y) { for (let y = realTileStart.y; y <= realTileEnd.y; ++y) {
const contents = this.root.map.getLayerContentXY(x, y, this.root.currentLayer); const contents = this.root.map.getLayerContentXY(x, y, this.root.currentLayer);
if (contents && this.root.logic.canDeleteBuilding(contents)) { if (contents && this.root.logic.canDeleteBuilding(contents)) {
this.selectedUids.add(contents.uid); this.selectedEntities.add(contents);
} }
} }
} }
@ -319,8 +331,11 @@ export class HUDMassSelector extends BaseHUDPart {
} }
parameters.context.fillStyle = THEME.map.selectionOverlay; parameters.context.fillStyle = THEME.map.selectionOverlay;
this.selectedUids.forEach(uid => {
const entity = this.root.entityMgr.findByUid(uid); if (this.selectedEntities.size > 0) {
const arr = [...this.selectedEntities.values()];
for (let i = arr.length - 1; i >= 0; --i) {
const entity = arr[i];
const staticComp = entity.components.StaticMapEntity; const staticComp = entity.components.StaticMapEntity;
const bounds = staticComp.getTileSpaceBounds(); const bounds = staticComp.getTileSpaceBounds();
parameters.context.beginRoundedRect( parameters.context.beginRoundedRect(
@ -331,6 +346,7 @@ export class HUDMassSelector extends BaseHUDPart {
2 2
); );
parameters.context.fill(); parameters.context.fill();
}); }
}
} }
} }