copy constructors for states

This commit is contained in:
Tangent Wantwight 2020-02-15 23:17:13 -05:00
parent 5ca9397621
commit 390e6744b9
3 changed files with 28 additions and 1 deletions

View file

@ -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<RenderSprite> = {};
collisionSourceClass: 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);
}
}

View file

@ -19,6 +19,14 @@ export type Store<T> = (T & HasGeneration)[];
export type SparseStore<T> = Record<number, T & HasGeneration>;
export class Data {
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

View file

@ -59,6 +59,15 @@ export class Data extends EcsData {
hp: SparseStore<Hp> = {};
lifetime: SparseStore<Lifetime> = {};
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 {