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]);
|
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);
|
||||||
|
@ -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 */
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
@ -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),
|
||||||
|
@ -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
|
||||||
|
@ -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() {
|
static getId() {
|
||||||
return "Display";
|
return "Display";
|
||||||
}
|
}
|
||||||
|
|
||||||
duplicateWithoutContents() {
|
|
||||||
return new DisplayComponent();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -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";
|
return "LogicGate";
|
||||||
}
|
}
|
||||||
|
|
||||||
duplicateWithoutContents() {
|
|
||||||
return new LogicGateComponent({ type: this.type });
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param {object} param0
|
* @param {object} param0
|
||||||
|
@ -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;
|
||||||
|
@ -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,
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user