commit:7cdca8743f42ecb37ef3b125c6ea80545f355c7a
author:Chip Black
committer:Chip Black
date:Thu Aug 6 01:44:10 2020 -0500
parents:a71b09f0aa5c34e4d9abb12468d3be7859663db0
Add player inventory
diff --git a/src/gamestate.ts b/src/gamestate.ts
line changes: +19/-1
index 8199f47..f7129bb
--- a/src/gamestate.ts
+++ b/src/gamestate.ts
@@ -25,6 +25,7 @@ the init() method.
 
 interface Globals {
     objects: GameObjectProperties[]
+    inventory: object
 }
 
 class GameState {
@@ -32,6 +33,7 @@ class GameState {
     events: Map<ID, GameEvent>;
     objects: Map<ID, GameObject>;
     agents: Map<ID, GameAgent>; 
+    playerInventory: Map<ID, number>;
 
     textView: TextView;
     cellEditor: CellEditor;
@@ -45,6 +47,7 @@ class GameState {
         this.objects = new Map();
         this.agents = new Map();
         this.map = new GameMap();
+        this.playerInventory = new Map();
 
         document.body.classList.toggle('edit-mode', this.editMode);
 
@@ -121,13 +124,24 @@ class GameState {
         }
     }
 
+    addInventory(o: ID, count=1) {
+        if (this.playerInventory.has(o)) {
+            const currentCount = this.playerInventory.get(o);
+            this.playerInventory.set(o, currentCount + count);
+        } else {
+            this.playerInventory.set(o, count);
+        }
+    }
+
     storeGlobals(): Globals {
         const objects: GameObjectProperties[] = [];
         for (let [k, o] of this.objects) {
             objects.push(o.store());
         }
 
-        return { objects };
+        const inventory = Object.fromEntries(this.playerInventory);
+
+        return { objects, inventory };
     }
 
     loadGlobals(globals: Globals) {
@@ -138,6 +152,10 @@ class GameState {
                 this.addObject(obj);
             }
         }
+
+        if (globals.inventory) {
+            this.playerInventory = new Map(Object.entries(globals.inventory));
+        }
     }
 }