mirror of
https://github.com/tobspr/shapez.io.git
synced 2024-10-27 20:34:29 +00:00
Refactor entity cloning (#671)
This commit is contained in:
parent
1f3991301d
commit
c4f2379010
@ -44,7 +44,7 @@ export class Blueprint {
|
||||
const entity = root.entityMgr.findByUid(uids[i]);
|
||||
assert(entity, "Entity for blueprint not found:" + uids[i]);
|
||||
|
||||
const clone = entity.duplicateWithoutContents();
|
||||
const clone = entity.clone();
|
||||
newEntities.push(clone);
|
||||
|
||||
const pos = entity.components.StaticMapEntity.getTileSpaceBounds().getCenter();
|
||||
@ -160,7 +160,7 @@ export class Blueprint {
|
||||
continue;
|
||||
}
|
||||
|
||||
const clone = entity.duplicateWithoutContents();
|
||||
const clone = entity.clone();
|
||||
clone.components.StaticMapEntity.origin.addInplace(tile);
|
||||
root.logic.freeEntityAreaBeforeBuild(clone);
|
||||
root.map.placeStaticEntity(clone);
|
||||
|
@ -18,12 +18,10 @@ export class Component extends BasicSerializableObject {
|
||||
}
|
||||
|
||||
/**
|
||||
* Should duplicate the component but without its contents
|
||||
* @returns {object}
|
||||
* Copy the current state to another component
|
||||
* @param {Component} otherComponent
|
||||
*/
|
||||
duplicateWithoutContents() {
|
||||
abstract;
|
||||
}
|
||||
copyAdditionalStateTo(otherComponent) {}
|
||||
|
||||
/* dev:start */
|
||||
|
||||
|
@ -40,10 +40,6 @@ export class BeltComponent extends Component {
|
||||
return "Belt";
|
||||
}
|
||||
|
||||
duplicateWithoutContents() {
|
||||
return new BeltComponent({ direction: this.direction });
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {object} param0
|
||||
|
@ -8,10 +8,6 @@ export class BeltReaderComponent extends Component {
|
||||
return "BeltReader";
|
||||
}
|
||||
|
||||
duplicateWithoutContents() {
|
||||
return new BeltReaderComponent();
|
||||
}
|
||||
|
||||
static getSchema() {
|
||||
return {
|
||||
lastItem: types.nullable(typeItemSingleton),
|
||||
|
@ -30,21 +30,6 @@ export class BeltUnderlaysComponent extends Component {
|
||||
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 {Array<BeltUnderlayTile>=} param0.underlays Where to render belt underlays
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -4,8 +4,4 @@ export class DisplayComponent extends Component {
|
||||
static getId() {
|
||||
return "Display";
|
||||
}
|
||||
|
||||
duplicateWithoutContents() {
|
||||
return new DisplayComponent();
|
||||
}
|
||||
}
|
||||
|
@ -28,22 +28,6 @@ export class ItemAcceptorComponent extends Component {
|
||||
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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -22,10 +22,6 @@ export class LogicGateComponent extends Component {
|
||||
return "LogicGate";
|
||||
}
|
||||
|
||||
duplicateWithoutContents() {
|
||||
return new LogicGateComponent({ type: this.type });
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {object} param0
|
||||
|
@ -19,12 +19,6 @@ export class MinerComponent extends Component {
|
||||
};
|
||||
}
|
||||
|
||||
duplicateWithoutContents() {
|
||||
return new MinerComponent({
|
||||
chainable: this.chainable,
|
||||
});
|
||||
}
|
||||
|
||||
constructor({ chainable = false }) {
|
||||
super();
|
||||
this.lastMiningTime = 0;
|
||||
|
@ -63,7 +63,11 @@ export class StaticMapEntityComponent extends Component {
|
||||
return getBuildingDataFromCode(this.code).metaInstance;
|
||||
}
|
||||
|
||||
duplicateWithoutContents() {
|
||||
/**
|
||||
* Copy the current state to another component
|
||||
* @param {Component} otherComponent
|
||||
*/
|
||||
copyAdditionalStateTo(otherComponent) {
|
||||
return new StaticMapEntityComponent({
|
||||
origin: this.origin.copy(),
|
||||
rotation: this.rotation,
|
||||
|
@ -17,10 +17,6 @@ export class StorageComponent extends Component {
|
||||
};
|
||||
}
|
||||
|
||||
duplicateWithoutContents() {
|
||||
return new StorageComponent({ maximumStorage: this.maximumStorage });
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {object} param0
|
||||
* @param {number=} param0.maximumStorage How much this storage can hold
|
||||
|
@ -29,13 +29,6 @@ export class UndergroundBeltComponent extends Component {
|
||||
};
|
||||
}
|
||||
|
||||
duplicateWithoutContents() {
|
||||
return new UndergroundBeltComponent({
|
||||
mode: this.mode,
|
||||
tier: this.tier,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {object} param0
|
||||
|
@ -13,10 +13,6 @@ export class WireComponent extends Component {
|
||||
return "Wire";
|
||||
}
|
||||
|
||||
duplicateWithoutContents() {
|
||||
return new WireComponent({ type: this.type });
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {object} param0
|
||||
* @param {enumWireType=} param0.type
|
||||
|
@ -5,10 +5,6 @@ export class WireTunnelComponent extends Component {
|
||||
return "WireTunnel";
|
||||
}
|
||||
|
||||
duplicateWithoutContents() {
|
||||
return new WireTunnelComponent({ multipleDirections: this.multipleDirections });
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {object} param0
|
||||
* @param {boolean=} param0.multipleDirections
|
||||
|
@ -49,20 +49,6 @@ export class WiredPinsComponent extends Component {
|
||||
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
|
||||
* @param {Array<WirePinSlotDefinition>} slots
|
||||
|
@ -11,6 +11,7 @@ import { EntityComponentStorage } from "./entity_components";
|
||||
import { Loader } from "../core/loader";
|
||||
import { drawRotatedSprite } from "../core/draw_utils";
|
||||
import { gComponentRegistry } from "../core/global_registries";
|
||||
import { getBuildingDataFromCode } from "./building_codes";
|
||||
|
||||
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() {
|
||||
const clone = new Entity(this.root);
|
||||
clone() {
|
||||
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) {
|
||||
clone.components[key] = this.components[key].duplicateWithoutContents();
|
||||
/** @type {Component} */ (this.components[key]).copyAdditionalStateTo(clone.components[key]);
|
||||
}
|
||||
clone.layer = this.layer;
|
||||
|
||||
return clone;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user