Advertisement
CipRos

GAME Injection

Jun 25th, 2021 (edited)
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Game = (function() {
  2.     'use strict';
  3.  
  4.     var Game = function() {
  5.       this.lab = new GameObjects.Lab();
  6.       this.research = null;
  7.       this.workers = null;
  8.       this.upgrades = null;
  9.       this.achievements = null;
  10.       this.allObjects = {lab : this.lab};
  11.       this.loaded = false;
  12.     };
  13.  
  14.     Game.prototype.load = function() {
  15.       if (this.loaded) {
  16.         return;
  17.       }
  18.  
  19.       // I know synchronous requests are bad as they will block the browser.
  20.       // However, I don't see any other reasonable way to do this in order to
  21.       // make it work with Angular. If you know a way, let me know, and I'll
  22.       // give you a beer. - Kevin
  23.       this.research = Helpers.loadFile('json/research.json');
  24.       this.workers = Helpers.loadFile('json/workers.json');
  25.       this.upgrades = Helpers.loadFile('json/upgrades.json');
  26.       this.achievements = Helpers.loadFile('json/achievements.json');
  27.  
  28.       // Turn JSON files into actual game objects and fill map of all objects
  29.       var _this = this;
  30.       var makeGameObject = function(type, object) {
  31.         // It's okay to define this function here since load is only called
  32.         // once anyway...
  33.         var o = new type(object);
  34.         _this.allObjects[o.key] = o;
  35.         return o;
  36.       };
  37.       this.research = this.research.map(
  38.           function(r) { return makeGameObject(GameObjects.Research, r); });
  39.       this.workers = this.workers.map(
  40.           function(w) { return makeGameObject(GameObjects.Worker, w); });
  41.       this.upgrades = this.upgrades.map(
  42.           function(u) { return makeGameObject(GameObjects.Upgrade, u); });
  43.       this.achievements = this.achievements.map(
  44.           function(a) { return makeGameObject(GameObjects.Achievement, a); });
  45.       // Load states from local store
  46.       for (var key in this.allObjects) {
  47.         var o = this.allObjects[key];
  48.         o.loadState(ObjectStorage.load(key));
  49.       }
  50.       this.loaded = true;
  51.     };
  52.  
  53.     Game.prototype.save = function() {
  54.       // Save every object's state to local storage
  55.       for (var key in this.allObjects) {
  56.         console.log(1)
  57.         ObjectStorage.save(key, this.allObjects[key].state);
  58.       }
  59.     };
  60.  
  61.     return {Game : Game};
  62.   }());
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement