Refactor entity cloning (#671)

This commit is contained in:
Exund 2020-09-19 21:41:48 +02:00 committed by GitHub
parent 1f3991301d
commit c4f2379010
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 39 additions and 127 deletions

View File

@ -44,7 +44,7 @@ export class Blueprint {
const entity = root.entityMgr.findByUid(uids[i]); const entity = root.entityMgr.findByUid(uids[i]);
assert(entity, "Entity for blueprint not found:" + uids[i]); assert(entity, "Entity for blueprint not found:" + uids[i]);
const clone = entity.duplicateWithoutContents(); const clone = entity.clone();
newEntities.push(clone); newEntities.push(clone);
const pos = entity.components.StaticMapEntity.getTileSpaceBounds().getCenter(); const pos = entity.components.StaticMapEntity.getTileSpaceBounds().getCenter();
@ -160,7 +160,7 @@ export class Blueprint {
continue; continue;
} }
const clone = entity.duplicateWithoutContents(); const clone = entity.clone();
clone.components.StaticMapEntity.origin.addInplace(tile); clone.components.StaticMapEntity.origin.addInplace(tile);
root.logic.freeEntityAreaBeforeBuild(clone); root.logic.freeEntityAreaBeforeBuild(clone);
root.map.placeStaticEntity(clone); root.map.placeStaticEntity(clone);

View File

@ -18,12 +18,10 @@ export class Component extends BasicSerializableObject {
} }
/** /**
* Should duplicate the component but without its contents * Copy the current state to another component
* @returns {object} * @param {Component} otherComponent
*/ */
duplicateWithoutContents() { copyAdditionalStateTo(otherComponent) {}
abstract;
}
/* dev:start */ /* dev:start */

View File

@ -40,10 +40,6 @@ export class BeltComponent extends Component {
return "Belt"; return "Belt";
} }
duplicateWithoutContents() {
return new BeltComponent({ direction: this.direction });
}
/** /**
* *
* @param {object} param0 * @param {object} param0

View File

@ -8,10 +8,6 @@ export class BeltReaderComponent extends Component {
return "BeltReader"; return "BeltReader";
} }
duplicateWithoutContents() {
return new BeltReaderComponent();
}
static getSchema() { static getSchema() {
return { return {
lastItem: types.nullable(typeItemSingleton), lastItem: types.nullable(typeItemSingleton),

View File

@ -30,21 +30,6 @@ export class BeltUnderlaysComponent extends Component {
return "BeltUnderlays"; return "BeltUnderlays";
} }
duplicateWithoutContents() {
const beltUnderlaysCopy = [];
for (let i = 0; i < this.underlays.length; ++i) {
const underlay = this.underlays[i];
beltUnderlaysCopy.push({
pos: underlay.pos.copy(),
direction: underlay.direction,
});
}
return new BeltUnderlaysComponent({
underlays: beltUnderlaysCopy,
});
}
/** /**
* @param {object} param0 * @param {object} param0
* @param {Array<BeltUnderlayTile>=} param0.underlays Where to render belt underlays * @param {Array<BeltUnderlayTile>=} param0.underlays Where to render belt underlays

View File

@ -15,8 +15,12 @@ export class ConstantSignalComponent extends Component {
}; };
} }
duplicateWithoutContents() { /**
return new ConstantSignalComponent({ signal: this.signal }); * Copy the current state to another component
* @param {ConstantSignalComponent} otherComponent
*/
copyAdditionalStateTo(otherComponent) {
otherComponent.signal = this.signal;
} }
/** /**

View File

@ -4,8 +4,4 @@ export class DisplayComponent extends Component {
static getId() { static getId() {
return "Display"; return "Display";
} }
duplicateWithoutContents() {
return new DisplayComponent();
}
} }

View File

@ -28,22 +28,6 @@ export class ItemAcceptorComponent extends Component {
return "ItemAcceptor"; return "ItemAcceptor";
} }
duplicateWithoutContents() {
const slotsCopy = [];
for (let i = 0; i < this.slots.length; ++i) {
const slot = this.slots[i];
slotsCopy.push({
pos: slot.pos.copy(),
directions: slot.directions.slice(),
filter: slot.filter,
});
}
return new ItemAcceptorComponent({
slots: slotsCopy,
});
}
/** /**
* *
* @param {object} param0 * @param {object} param0

View File

@ -35,22 +35,6 @@ export class ItemEjectorComponent extends Component {
}; };
} }
duplicateWithoutContents() {
const slotsCopy = [];
for (let i = 0; i < this.slots.length; ++i) {
const slot = this.slots[i];
slotsCopy.push({
pos: slot.pos.copy(),
direction: slot.direction,
});
}
return new ItemEjectorComponent({
slots: slotsCopy,
renderFloatingItems: this.renderFloatingItems,
});
}
/** /**
* *
* @param {object} param0 * @param {object} param0

View File

@ -48,14 +48,6 @@ export class ItemProcessorComponent extends Component {
}; };
} }
duplicateWithoutContents() {
return new ItemProcessorComponent({
processorType: this.type,
processingRequirement: this.processingRequirement,
inputsPerCharge: this.inputsPerCharge,
});
}
/** /**
* *
* @param {object} param0 * @param {object} param0

View File

@ -12,8 +12,12 @@ export class LeverComponent extends Component {
}; };
} }
duplicateWithoutContents() { /**
return new LeverComponent({ toggled: this.toggled }); * Copy the current state to another component
* @param {LeverComponent} otherComponent
*/
copyAdditionalStateTo(otherComponent) {
otherComponent.toggled = this.toggled;
} }
/** /**

View File

@ -22,10 +22,6 @@ export class LogicGateComponent extends Component {
return "LogicGate"; return "LogicGate";
} }
duplicateWithoutContents() {
return new LogicGateComponent({ type: this.type });
}
/** /**
* *
* @param {object} param0 * @param {object} param0

View File

@ -19,12 +19,6 @@ export class MinerComponent extends Component {
}; };
} }
duplicateWithoutContents() {
return new MinerComponent({
chainable: this.chainable,
});
}
constructor({ chainable = false }) { constructor({ chainable = false }) {
super(); super();
this.lastMiningTime = 0; this.lastMiningTime = 0;

View File

@ -63,7 +63,11 @@ export class StaticMapEntityComponent extends Component {
return getBuildingDataFromCode(this.code).metaInstance; return getBuildingDataFromCode(this.code).metaInstance;
} }
duplicateWithoutContents() { /**
* Copy the current state to another component
* @param {Component} otherComponent
*/
copyAdditionalStateTo(otherComponent) {
return new StaticMapEntityComponent({ return new StaticMapEntityComponent({
origin: this.origin.copy(), origin: this.origin.copy(),
rotation: this.rotation, rotation: this.rotation,

View File

@ -17,10 +17,6 @@ export class StorageComponent extends Component {
}; };
} }
duplicateWithoutContents() {
return new StorageComponent({ maximumStorage: this.maximumStorage });
}
/** /**
* @param {object} param0 * @param {object} param0
* @param {number=} param0.maximumStorage How much this storage can hold * @param {number=} param0.maximumStorage How much this storage can hold

View File

@ -29,13 +29,6 @@ export class UndergroundBeltComponent extends Component {
}; };
} }
duplicateWithoutContents() {
return new UndergroundBeltComponent({
mode: this.mode,
tier: this.tier,
});
}
/** /**
* *
* @param {object} param0 * @param {object} param0

View File

@ -13,10 +13,6 @@ export class WireComponent extends Component {
return "Wire"; return "Wire";
} }
duplicateWithoutContents() {
return new WireComponent({ type: this.type });
}
/** /**
* @param {object} param0 * @param {object} param0
* @param {enumWireType=} param0.type * @param {enumWireType=} param0.type

View File

@ -5,10 +5,6 @@ export class WireTunnelComponent extends Component {
return "WireTunnel"; return "WireTunnel";
} }
duplicateWithoutContents() {
return new WireTunnelComponent({ multipleDirections: this.multipleDirections });
}
/** /**
* @param {object} param0 * @param {object} param0
* @param {boolean=} param0.multipleDirections * @param {boolean=} param0.multipleDirections

View File

@ -49,20 +49,6 @@ export class WiredPinsComponent extends Component {
this.setSlots(slots); this.setSlots(slots);
} }
duplicateWithoutContents() {
const slots = [];
for (let i = 0; i < this.slots.length; ++i) {
const slot = this.slots[i];
slots.push({
pos: slot.pos.copy(),
type: slot.type,
direction: slot.direction,
});
}
return new WiredPinsComponent({ slots });
}
/** /**
* Sets the slots of this building * Sets the slots of this building
* @param {Array<WirePinSlotDefinition>} slots * @param {Array<WirePinSlotDefinition>} slots

View File

@ -11,6 +11,7 @@ import { EntityComponentStorage } from "./entity_components";
import { Loader } from "../core/loader"; import { Loader } from "../core/loader";
import { drawRotatedSprite } from "../core/draw_utils"; import { drawRotatedSprite } from "../core/draw_utils";
import { gComponentRegistry } from "../core/global_registries"; import { gComponentRegistry } from "../core/global_registries";
import { getBuildingDataFromCode } from "./building_codes";
export class Entity extends BasicSerializableObject { export class Entity extends BasicSerializableObject {
/** /**
@ -82,14 +83,25 @@ export class Entity extends BasicSerializableObject {
} }
/** /**
* Returns a clone of this entity without contents * Returns a clone of this entity
*/ */
duplicateWithoutContents() { clone() {
const clone = new Entity(this.root); const staticComp = this.components.StaticMapEntity;
const buildingData = getBuildingDataFromCode(staticComp.code);
const clone = buildingData.metaInstance.createEntity({
root: this.root,
origin: staticComp.origin,
originalRotation: staticComp.originalRotation,
rotation: staticComp.rotation,
rotationVariant: buildingData.rotationVariant,
variant: buildingData.variant,
});
for (const key in this.components) { for (const key in this.components) {
clone.components[key] = this.components[key].duplicateWithoutContents(); /** @type {Component} */ (this.components[key]).copyAdditionalStateTo(clone.components[key]);
} }
clone.layer = this.layer;
return clone; return clone;
} }