diff --git a/src/Ecs/Components.ts b/src/Ecs/Components.ts index f0a5a25..c0c0a14 100644 --- a/src/Ecs/Components.ts +++ b/src/Ecs/Components.ts @@ -1,6 +1,6 @@ -import { Data as CoreData, Store, SparseStore } from "./Data"; import { Layer, SpriteSheet } from "../Applet/Render"; +import { copy, Data as CoreData, SparseStore, Store } from "./Data"; export class Box { constructor( @@ -72,4 +72,14 @@ export class Data extends CoreData { renderSprite: SparseStore = {}; collisionSourceClass: SparseStore = {}; collisionTargetClass: SparseStore = {}; + + constructor(source?: Partial) { + super(source); + if(source?.location) this.location = source.location.slice(); + if(source?.bounds) this.bounds = source.bounds.slice(); + if(source?.renderBounds) this.renderBounds = copy(source.renderBounds); + if(source?.renderSprite) this.renderSprite = copy(source.renderSprite); + if(source?.collisionSourceClass) this.collisionSourceClass = copy(source.collisionSourceClass); + if(source?.collisionTargetClass) this.collisionTargetClass = copy(source.collisionTargetClass); + } } diff --git a/src/Ecs/Data.ts b/src/Ecs/Data.ts index d076f46..8c2e156 100644 --- a/src/Ecs/Data.ts +++ b/src/Ecs/Data.ts @@ -19,6 +19,14 @@ export type Store = (T & HasGeneration)[]; export type SparseStore = Record; export class Data { entity: Store = []; + + constructor(source?: Partial) { + if(source?.entity) this.entity = source.entity.slice(); + } +} + +export function copy(source: SparseStore): SparseStore { + return JSON.parse(JSON.stringify(source)); } // Ergonomic Lookup typings diff --git a/src/Game/GameComponents.ts b/src/Game/GameComponents.ts index 2811c5b..724ecce 100644 --- a/src/Game/GameComponents.ts +++ b/src/Game/GameComponents.ts @@ -59,6 +59,15 @@ export class Data extends EcsData { hp: SparseStore = {}; lifetime: SparseStore = {}; message: SparseStore = {}; + + constructor(source?: Partial) { + super(source); + if(source?.boss) this.boss = copy(source.boss); + if(source?.bullet) this.bullet = copy(source.bullet); + if(source?.hp) this.hp = copy(source.hp); + if(source?.lifetime) this.lifetime = copy(source.lifetime); + if(source?.message) this.message = copy(source.message); + } } export enum Teams {