Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Job
- {
- constructor(quantity, slots, unitProduction, unitProductionRatio, classProduction, unitWheatConsumption, classWheatConsumption)
- {
- this.quantity = quantity;
- this.slots = slots;
- this.unitProduction = unitProduction;
- this.unitProductionRatio = unitProductionRatio;
- this.classProduction = classProduction;
- this.unitWheatConsumption = unitWheatConsumption;
- this.classWheatConsumption = classWheatConsumption;
- }
- toJSON()
- {
- return {
- quantity:this.quantity,
- slots:this.slots,
- unitProduction:this.unitProduction,
- unitProductionRatio:this.unitProductionRatio,
- classProduction:this.classProduction,
- unitWheatConsumption:this.unitWheatConsumption,
- classWheatConsumption:this.classWheatConsumption
- }
- }
- static fromJSON(json)
- {
- return new Job(json.quantity, json.slots, json.unitProduction, json.unitProductionRatio, json.classProduction, json.unitWheatConsumption, json.classWheatConsumption);
- }
- }
- class Resource
- {
- constructor(quantity, addition, capacity, initial, slope)
- {
- this.quantity = quantity;
- this.addition = addition;
- this.capacity = capacity;
- this.initial = initial;
- this.slope = slope;
- }
- toJSON()
- {
- return {
- quantity:this.quantity,
- addition:this.addition,
- capacity:this.capacity,
- initial:this.initial,
- slope:this.slope
- }
- }
- static fromJSON(json)
- {
- return new Resource(json.quantity, json.addition, json.capacity, json.initial, json.slope);
- }
- }
- class Factory
- {
- constructor(quantity, unitProduction, unitProductionRatio, classProduction, unitWorkerBoost, unitPrice1, unitPrice2, unitPriceRatio1, unitPriceRatio2, unitResourceStorage)
- {
- this.quantity = quantity;
- this.unitProduction = unitProduction;
- this.unitProductionRatio = unitProductionRatio;
- this.classProduction = classProduction;
- this.unitWorkerBoost = unitWorkerBoost;
- this.unitPrice1 = unitPrice1;
- this.unitPrice2 = unitPrice2;
- this.unitPriceRatio1 = unitPriceRatio1;
- this.unitPriceRatio2 = unitPriceRatio2;
- this.unitResourceStorage = unitResourceStorage;
- }
- /**
- * This is not done as part of the core constructor because it should not be.
- * @param resource
- */
- setResource(resource)
- {
- this.resource = resource;
- return this;
- }
- setWorker(worker)
- {
- this.worker = worker;
- return this;
- }
- toJSON()
- {
- return {
- quantity:this.quantity,
- unitProduction:this.unitProduction,
- unitProductionRatio:this.unitProductionRatio,
- classProduction:this.classProduction,
- unitWorkerBoost:this.unitWorkerBoost,
- unitPrice1:this.unitPrice1,
- unitPrice2:this.unitPrice2,
- unitPriceRatio1:this.unitPriceRatio1,
- unitPriceRatio2:this.unitPriceRatio2,
- unitResourceStorage:this.unitResourceStorage,
- }
- }
- static fromJSON(json)
- {
- return new Factory(json.quantity, json.unitProduction, json.unitProductionRatio, json.classProduction, json.unitWorkerBoost, json.unitPrice1, json.unitPrice2, json.unitPriceRatio1, json.unitPriceRatio2, json.unitResourceStorage);
- }
- }
- const SAVE_FILE_NAME = 'GameSaveFile';
- class DeerGame
- {
- /**
- * Initialise the Game variables
- */
- hardReset()
- {
- // jobs
- this.farmers = new Job(0, 0, 0.75, 1, 0, 0.22, 0);
- this.loggers = new Job(0, 0, 0.50, 1, 0, 0.32, 0);
- this.miners = new Job(0, 0, 0.25, 1, 0, 0.42, 0);
- this.explorers = new Job(0, 0, 0.16, 1, 0, 0.52, 0);
- this.scientists = new Job(0, 0, 0.09, 1, 0, 0.62, 0);
- this.artists = new Job(0, 0, 0.04, 1, 0, 0.72, 0);
- this.priests = new Job(0, 0, 0.01, 1, 0, 0.82, 0);
- // resources
- this.deer = new Resource(0, 1, 5, 5, 0);
- this.rock = new Resource(0, 0, 7, 7, 0);
- this.wood = new Resource(0, 0, 11, 11, 0);
- this.wheat = new Resource(0, 0, 13, 13, 0);
- this.land = new Resource(0, 0, 17, 17, 0);
- this.science = new Resource(0, 0, 19, 19, 0);
- this.faith = new Resource(0, 0, 23, 23, 0);
- // factories
- this.forest = new Factory(0, 0.25, 1, 0, 1.15, 15, 2, 1.1, 1.3, 25).setResource(this.wood).setWorker(this.loggers);
- this.mine = new Factory(0, 0.15, 1, 0, 1.15, 25, 1, 1.2, 1.1, 10).setResource(this.rock).setWorker(this.miners);
- 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);
- }
- toJSON()
- {
- return {
- jobs:{
- farmers:this.farmers.toJSON(),
- loggers:this.loggers.toJSON(),
- miners:this.miners.toJSON(),
- explorers:this.explorers.toJSON(),
- scientists:this.scientists.toJSON(),
- artists:this.artists.toJSON(),
- priests:this.priests.toJSON(),
- },
- resources:{
- deer:this.deer.toJSON(),
- rock:this.rock.toJSON(),
- wood:this.wood.toJSON(),
- wheat:this.wheat.toJSON(),
- land:this.land.toJSON(),
- science:this.science.toJSON(),
- faith:this.faith.toJSON(),
- },
- factories:{
- forest:this.forest.toJSON(),
- mine:this.mine.toJSON(),
- grassland:this.grassland.toJSON()
- }
- }
- }
- loadJSON(json)
- {
- for(let jobName in json.jobs)
- {
- this[jobName] = Job.fromJSON(json.jobs[jobName]);
- }
- for(let resourceName in json.resources)
- {
- this[resourceName] = Resource.fromJSON(json.resources[resourceName]);
- }
- for(let factoryName in json.factories)
- {
- this[factoryName] = Factory.fromJSON(json.factories[factoryName]);
- }
- this.forest.setResource(this.wood).setWorker(this.loggers);
- this.mine.setResource(this.rock).setWorker(this.miners);
- this.grassland.setResource(this.land).setWorker(this.farmers);
- }
- save()
- {
- let jsonString = JSON.stringify(this.toJSON()),
- encodedString = btoa(jsonString);
- localStorage.setItem(SAVE_FILE_NAME, encodedString);
- }
- load()
- {
- let encodedString = localStorage.getItem(SAVE_FILE_NAME);
- if(encodedString === null)
- {
- return false;
- }
- let jsonString = atob(encodedString),
- json = JSON.parse(jsonString);
- this.loadJSON(json);
- return true;
- }
- constructor()
- {
- if(this.load())
- {
- // we have a save file, and should load it
- }
- else
- {
- // we do not have a save file, so we should hard reset the game
- this.hardReset();
- this.save();
- }
- }
- }
- let deerGame = new DeerGame();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement