Commits

Radomir Dopieralski committed 2f7206c

Use cookies to preserve inventory between maps

Comments (0)

Files changed (1)

 
         self.facing = 2;
         self.steps = 0;
-        self.items = [];
+        self.items = module.load_cookie('items').split(',');
         DX = [ 0,  1,  0, -1 ];
         DY = [-1,  0,  1,  0 ];
 
             if (!self.has_item(item)) {
                 self.items.push(item);
             };
+            var items = self.items.join(',');
+            module.save_cookie('items', items);
         };
 
         self.set_pos = function () {
         };
     };
 
+    module.load_cookie = function (name) {
+        name = escape(name);
+        name = name.replace(/[\-\.\+\*]/g, "\\$&");
+        var re = "(?:^|.*;\\s*)" + name + "\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*";
+        return unescape(document.cookie.replace(new RegExp(re), "$1"));
+    };
+
+    module.save_cookie = function (name, value) {
+        name = escape(name);
+        value = escape(value);
+        document.cookie = name + "=" + value;
+    };
+
     module.loop = function () {
         // The main game loop.