Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Job
  2. {
  3.     constructor(quantity, slots, unitProduction, unitProductionRatio, classProduction, unitWheatConsumption, classWheatConsumption)
  4.     {
  5.         this.quantity = quantity;
  6.         this.slots = slots;
  7.         this.unitProduction = unitProduction;
  8.         this.unitProductionRatio = unitProductionRatio;
  9.         this.classProduction = classProduction;
  10.         this.unitWheatConsumption = unitWheatConsumption;
  11.         this.classWheatConsumption = classWheatConsumption;
  12.     }
  13.  
  14.     toJSON()
  15.     {
  16.         return {
  17.             quantity:this.quantity,
  18.             slots:this.slots,
  19.             unitProduction:this.unitProduction,
  20.             unitProductionRatio:this.unitProductionRatio,
  21.             classProduction:this.classProduction,
  22.             unitWheatConsumption:this.unitWheatConsumption,
  23.             classWheatConsumption:this.classWheatConsumption
  24.         }
  25.     }
  26.  
  27.     static fromJSON(json)
  28.     {
  29.         return new Job(json.quantity, json.slots, json.unitProduction, json.unitProductionRatio, json.classProduction, json.unitWheatConsumption, json.classWheatConsumption);
  30.     }
  31. }
  32.  
  33. class Resource
  34. {
  35.     constructor(quantity, addition, capacity, initial, slope)
  36.     {
  37.         this.quantity = quantity;
  38.         this.addition = addition;
  39.         this.capacity = capacity;
  40.         this.initial = initial;
  41.         this.slope = slope;
  42.     }
  43.  
  44.     toJSON()
  45.     {
  46.         return {
  47.             quantity:this.quantity,
  48.             addition:this.addition,
  49.             capacity:this.capacity,
  50.             initial:this.initial,
  51.             slope:this.slope
  52.         }
  53.     }
  54.  
  55.     static fromJSON(json)
  56.     {
  57.         return new Resource(json.quantity, json.addition, json.capacity, json.initial, json.slope);
  58.     }
  59. }
  60.  
  61. class Factory
  62. {
  63.     constructor(quantity, unitProduction, unitProductionRatio, classProduction, unitWorkerBoost, unitPrice1, unitPrice2, unitPriceRatio1, unitPriceRatio2, unitResourceStorage)
  64.     {
  65.         this.quantity = quantity;
  66.         this.unitProduction = unitProduction;
  67.         this.unitProductionRatio = unitProductionRatio;
  68.         this.classProduction = classProduction;
  69.         this.unitWorkerBoost = unitWorkerBoost;
  70.         this.unitPrice1 = unitPrice1;
  71.         this.unitPrice2 = unitPrice2;
  72.         this.unitPriceRatio1 = unitPriceRatio1;
  73.         this.unitPriceRatio2 = unitPriceRatio2;
  74.         this.unitResourceStorage = unitResourceStorage;
  75.     }
  76.  
  77.     /**
  78.      * This is not done as part of the core constructor because it should not be.
  79.      * @param resource
  80.      */
  81.  
  82.     setResource(resource)
  83.     {
  84.         this.resource = resource;
  85.         return this;
  86.     }
  87.  
  88.     setWorker(worker)
  89.     {
  90.         this.worker = worker;
  91.         return this;
  92.     }
  93.  
  94.     toJSON()
  95.     {
  96.         return {
  97.             quantity:this.quantity,
  98.             unitProduction:this.unitProduction,
  99.             unitProductionRatio:this.unitProductionRatio,
  100.             classProduction:this.classProduction,
  101.             unitWorkerBoost:this.unitWorkerBoost,
  102.             unitPrice1:this.unitPrice1,
  103.             unitPrice2:this.unitPrice2,
  104.             unitPriceRatio1:this.unitPriceRatio1,
  105.             unitPriceRatio2:this.unitPriceRatio2,
  106.             unitResourceStorage:this.unitResourceStorage,
  107.         }
  108.     }
  109.  
  110.     static fromJSON(json)
  111.     {
  112.         return new Factory(json.quantity, json.unitProduction, json.unitProductionRatio, json.classProduction, json.unitWorkerBoost, json.unitPrice1, json.unitPrice2, json.unitPriceRatio1, json.unitPriceRatio2, json.unitResourceStorage);
  113.     }
  114. }
  115.  
  116. const SAVE_FILE_NAME = 'GameSaveFile';
  117.  
  118. class DeerGame
  119. {
  120.     /**
  121.      * Initialise the Game variables
  122.      */
  123.     hardReset()
  124.     {
  125.         // jobs
  126.         this.farmers =      new Job(0, 0, 0.75, 1, 0, 0.22, 0);
  127.         this.loggers =      new Job(0, 0, 0.50, 1, 0, 0.32, 0);
  128.         this.miners =      new Job(0, 0, 0.25, 1, 0, 0.42, 0);
  129.         this.explorers =    new Job(0, 0, 0.16, 1, 0, 0.52, 0);
  130.         this.scientists =   new Job(0, 0, 0.09, 1, 0, 0.62, 0);
  131.         this.artists =      new Job(0, 0, 0.04, 1, 0, 0.72, 0);
  132.         this.priests =      new Job(0, 0, 0.01, 1, 0, 0.82, 0);
  133.  
  134.         // resources
  135.         this.deer =         new Resource(0, 1, 5, 5, 0);
  136.         this.rock =         new Resource(0, 0, 7, 7, 0);
  137.         this.wood =         new Resource(0, 0, 11, 11, 0);
  138.         this.wheat =        new Resource(0, 0, 13, 13, 0);
  139.         this.land =         new Resource(0, 0, 17, 17, 0);
  140.         this.science =      new Resource(0, 0, 19, 19, 0);
  141.         this.faith =        new Resource(0, 0, 23, 23, 0);
  142.  
  143.         // factories
  144.         this.forest =    new Factory(0, 0.25, 1, 0, 1.15, 15, 2, 1.1, 1.3, 25).setResource(this.wood).setWorker(this.loggers);
  145.         this.mine =      new Factory(0, 0.15, 1, 0, 1.15, 25, 1, 1.2, 1.1, 10).setResource(this.rock).setWorker(this.miners);
  146.         this.grassland = new Factory(0, 0.35, 1, 0, 1.15, 5, 1.5, 1.05, 1.2, 50).setResource(this.wheat).setWorker(this.farmers);
  147.     }
  148.  
  149.     toJSON()
  150.     {
  151.         return {
  152.             jobs:{
  153.                 farmers:this.farmers.toJSON(),
  154.                 loggers:this.loggers.toJSON(),
  155.                 miners:this.miners.toJSON(),
  156.                 explorers:this.explorers.toJSON(),
  157.                 scientists:this.scientists.toJSON(),
  158.                 artists:this.artists.toJSON(),
  159.                 priests:this.priests.toJSON(),
  160.             },
  161.  
  162.             resources:{
  163.                 deer:this.deer.toJSON(),
  164.                 rock:this.rock.toJSON(),
  165.                 wood:this.wood.toJSON(),
  166.                 wheat:this.wheat.toJSON(),
  167.                 land:this.land.toJSON(),
  168.                 science:this.science.toJSON(),
  169.                 faith:this.faith.toJSON(),
  170.             },
  171.  
  172.             factories:{
  173.                 forest:this.forest.toJSON(),
  174.                 mine:this.mine.toJSON(),
  175.                 grassland:this.grassland.toJSON()
  176.             }
  177.         }
  178.     }
  179.  
  180.     loadJSON(json)
  181.     {
  182.         for(let jobName in json.jobs)
  183.         {
  184.             this[jobName] = Job.fromJSON(json.jobs[jobName]);
  185.         }
  186.  
  187.         for(let resourceName in json.resources)
  188.         {
  189.             this[resourceName] = Resource.fromJSON(json.resources[resourceName]);
  190.         }
  191.  
  192.         for(let factoryName in json.factories)
  193.         {
  194.             this[factoryName] = Factory.fromJSON(json.factories[factoryName]);
  195.         }
  196.         this.forest.setResource(this.wood).setWorker(this.loggers);
  197.         this.mine.setResource(this.rock).setWorker(this.miners);
  198.         this.grassland.setResource(this.land).setWorker(this.farmers);
  199.     }
  200.  
  201.     save()
  202.     {
  203.         let jsonString = JSON.stringify(this.toJSON()),
  204.             encodedString = btoa(jsonString);
  205.         localStorage.setItem(SAVE_FILE_NAME, encodedString);
  206.     }
  207.  
  208.     load()
  209.     {
  210.         let encodedString = localStorage.getItem(SAVE_FILE_NAME);
  211.         if(encodedString === null)
  212.         {
  213.             return false;
  214.         }
  215.         let jsonString = atob(encodedString),
  216.             json = JSON.parse(jsonString);
  217.  
  218.         this.loadJSON(json);
  219.         return true;
  220.     }
  221.  
  222.     constructor()
  223.     {
  224.         if(this.load())
  225.         {
  226.             // we have a save file, and should load it
  227.         }
  228.         else
  229.         {
  230.             // we do not have a save file, so we should hard reset the game
  231.             this.hardReset();
  232.             this.save();
  233.         }
  234.     }
  235. }
  236.  
  237. let deerGame = new DeerGame();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement