commit:be4f9cbc334b631a5ec720744a56338fa3601a22
author:Chip Black
committer:Chip Black
date:Sun Nov 11 01:04:50 2018 -0600
parents:14d4702ea0f7bab56146324d7920080dac84b953
Add global object save/load
diff --git a/src/gamestate.ts b/src/gamestate.ts
line changes: +32/-1
index ca9a573..f6550bc
--- a/src/gamestate.ts
+++ b/src/gamestate.ts
@@ -7,7 +7,7 @@ import Actions from './actions';
 
 import { ID } from './id';
 import { GameEvent, GameEventType } from './event';
-import { GameObject } from './object';
+import { GameObject, GameObjectProperties } from './object';
 import { GameAgent } from './agent';
 
 /* GameState
@@ -23,6 +23,14 @@ the init() method.
 */
 
 
+interface ObjectBag {
+    [id: string]: GameObjectProperties
+}
+
+interface Globals {
+    objects: ObjectBag
+}
+
 class GameState {
     map: GameMap;
     events: Map<ID, GameEvent>;
@@ -54,6 +62,9 @@ class GameState {
         if (localStorage['map']) {
             this.map.load(JSON.parse(localStorage.map));
         }
+        if (localStorage['globals']) {
+            this.loadGlobals(JSON.parse(localStorage.globals));
+        }
 
         this.enterCell();
     }
@@ -113,6 +124,26 @@ class GameState {
             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

diff --git a/src/main.ts b/src/main.ts
line changes: +2/-0
index 0811d9e..b74f28e
--- a/src/main.ts
+++ b/src/main.ts
@@ -30,6 +30,8 @@ function keydown(e: KeyboardEvent) {
         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']));

diff --git a/src/object.ts b/src/object.ts
line changes: +11/-2
index 974d426..1761dfe
--- a/src/object.ts
+++ b/src/object.ts
@@ -11,6 +11,7 @@ export interface GameObjectProperties {
     type: GameObjectType
     name: string
     description: string
+    properties: PropertyBag
 }
 
 interface PropertyBag {
@@ -23,13 +24,13 @@ export class GameObject extends Storable {
     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) {
@@ -39,4 +40,12 @@ export class GameObject extends Storable {
     get(k: string): number | string {
         return this.properties[k];
     }
+
+    store(): GameObjectProperties {
+        return <GameObjectProperties>super.store();
+    }
+
+    load(o: GameObjectProperties) {
+        super.load(o);
+    }
 }