Add global object save/load
import { ID } from './id';
import { GameEvent, GameEventType } from './event';
-import { GameObject } from './object';
+import { GameObject, GameObjectProperties } from './object';
import { GameAgent } from './agent';
/* GameState
*/
+interface ObjectBag {
+ [id: string]: GameObjectProperties
+}
+
+interface Globals {
+ objects: ObjectBag
+}
+
class GameState {
map: GameMap;
events: Map<ID, GameEvent>;
if (localStorage['map']) {
this.map.load(JSON.parse(localStorage.map));
}
+ if (localStorage['globals']) {
+ this.loadGlobals(JSON.parse(localStorage.globals));
+ }
this.enterCell();
}
this.objects.delete(o);
}
}
+
+ storeGlobals(): Globals {
+ const objects: ObjectBag = {}
+ for (let [k, o] of this.objects) {
+ console.log('storeGlobals', k, o);
+ objects[k] = o.store();
+ }
+
+ return { objects };
+ }
+
+ loadGlobals(globals: Globals) {
+ if (globals.objects) {
+ for (let k of Object.keys(globals.objects)) {
+ const obj = new GameObject();
+ obj.load(globals.objects[k]);
+ this.addObject(obj);
+ }
+ }
+ }
}
// Create a default GameState object and export it
case 'KeyS':
localStorage['map'] = JSON.stringify(gs.map.store());
console.log(localStorage['map']);
+ localStorage['globals'] = JSON.stringify(gs.storeGlobals());
+ console.log(localStorage['objects']);
break;
case 'KeyL':
gs.map.load(JSON.parse(localStorage['map']));
type: GameObjectType
name: string
description: string
+ properties: PropertyBag
}
interface PropertyBag {
name: string
description: string
properties: PropertyBag
- saveProperties = ['id', 'type', 'properties']
+ saveProperties = ['id', 'type', 'name', 'description', 'properties']
constructor(t: GameObjectType = GameObjectType.Unknown) {
super();
this.id = generateID();
this.type = t;
- this.objectProperties = {};
+ this.properties = {};
}
set(k: string, v: number | string) {
get(k: string): number | string {
return this.properties[k];
}
+
+ store(): GameObjectProperties {
+ return <GameObjectProperties>super.store();
+ }
+
+ load(o: GameObjectProperties) {
+ super.load(o);
+ }
}