copy constructors for states
This commit is contained in:
parent
5ca9397621
commit
390e6744b9
3 changed files with 28 additions and 1 deletions
|
@ -1,6 +1,6 @@
|
||||||
|
|
||||||
import { Data as CoreData, Store, SparseStore } from "./Data";
|
|
||||||
import { Layer, SpriteSheet } from "../Applet/Render";
|
import { Layer, SpriteSheet } from "../Applet/Render";
|
||||||
|
import { copy, Data as CoreData, SparseStore, Store } from "./Data";
|
||||||
|
|
||||||
export class Box {
|
export class Box {
|
||||||
constructor(
|
constructor(
|
||||||
|
@ -72,4 +72,14 @@ export class Data extends CoreData {
|
||||||
renderSprite: SparseStore<RenderSprite> = {};
|
renderSprite: SparseStore<RenderSprite> = {};
|
||||||
collisionSourceClass: SparseStore<CollisionClass> = {};
|
collisionSourceClass: SparseStore<CollisionClass> = {};
|
||||||
collisionTargetClass: SparseStore<CollisionClass> = {};
|
collisionTargetClass: SparseStore<CollisionClass> = {};
|
||||||
|
|
||||||
|
constructor(source?: Partial<Data>) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,14 @@ export type Store<T> = (T & HasGeneration)[];
|
||||||
export type SparseStore<T> = Record<number, T & HasGeneration>;
|
export type SparseStore<T> = Record<number, T & HasGeneration>;
|
||||||
export class Data {
|
export class Data {
|
||||||
entity: Store<EntityState> = [];
|
entity: Store<EntityState> = [];
|
||||||
|
|
||||||
|
constructor(source?: Partial<Data>) {
|
||||||
|
if(source?.entity) this.entity = source.entity.slice();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function copy<T>(source: SparseStore<T>): SparseStore<T> {
|
||||||
|
return JSON.parse(JSON.stringify(source));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ergonomic Lookup typings
|
// Ergonomic Lookup typings
|
||||||
|
|
|
@ -59,6 +59,15 @@ export class Data extends EcsData {
|
||||||
hp: SparseStore<Hp> = {};
|
hp: SparseStore<Hp> = {};
|
||||||
lifetime: SparseStore<Lifetime> = {};
|
lifetime: SparseStore<Lifetime> = {};
|
||||||
message: SparseStore<Message> = {};
|
message: SparseStore<Message> = {};
|
||||||
|
|
||||||
|
constructor(source?: Partial<Data>) {
|
||||||
|
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 {
|
export enum Teams {
|
||||||
|
|
Loading…
Reference in a new issue