Working on this A LOT. However, at this point this version is unfinished and DOESNT WORK!

This commit is contained in:
Kevin Dungs
2014-11-08 15:55:01 +01:00
parent 156b1337e4
commit bfd38af8b8
9 changed files with 503 additions and 380 deletions
+196 -176
View File
@@ -1,193 +1,213 @@
'use strict';
/** Define all objects used in the game.
* They can be either loaded from localStorage or from JSON in case nothing is
* found in the localStorage.
*/
var GameObjects = (function() {
var allObjects = {
push: function(key_property, list) {
for (var i = 0; i < list.length; i++) {
this[list[i][key_property]] = list[i];
}
'use strict';
var GLOBAL_VISIBILITY_THRESHOLD = 0.7;
/** @class GameObject
* Base class for all objects in the game. This works together with the
* saving mechanism.
*/
var GameObject = function(obj) {
this.state = {};
$.extend(this, obj);
if (!this.key) {
throw 'Error: GameObject has to have a key!';
}
};
GameObject.prototype.loadState =
function(state) { $.extend(this.state, state); };
/** Lab
/** @class Lab
*/
var labPrototype = {
version: '0.2',
name: 'Click here to give your lab a catchy name',
detector: {
rate: 1
},
factor: {
rate: 5
},
data: 0,
reputation: 0,
money: 0,
getGrant: function () {
var addition = this.reputation * this.factor.rate;
this.money += addition;
return addition;
},
acquire: function(amount) {
this.data += amount;
},
research: function(cost, reputation) {
if (this.data >= cost) {
this.data -= cost;
this.reputation += reputation;
return true;
}
return false;
},
buy: function(cost) {
if (this.money >= cost) {
this.money -= cost;
return true;
}
return false;
},
sell: function(cost) {
this.money += cost;
}
var Lab = function() {
GameObject.apply(this, [{
key : 'lab',
version : 0.4,
state : {
name : 'Give your lab an awesome name!',
detector : 1,
factor : 5,
data : 0,
money : 0,
reputation : 0
}
}]);
};
var lab = $.extend({}, labPrototype, ObjectStorage.load('lab'));
allObjects['lab'] = lab;
/** Research
*/
var researchPrototype = {
level: 0,
interesting: false,
is_visible: function() {
return this.level > 0 || lab.data >= this.cost * .7;
},
is_available: function() {
return lab.data >= this.cost;
},
research: function() {
if (lab.research(this.cost, this.reputation)) {
this.level++;
if (this.info_levels.length > 0 && this.level === this.info_levels[0]) {
this.interesting = true;
this.info_levels.splice(0, 1);
}
var oldCost = this.cost;
this.cost = Math.round(this.cost * this.cost_increase);
return oldCost;
}
return -1;
},
getInfo: function() {
if (!this._info) {
this._info = Helpers.loadFile(this.info);
}
this.interesting = false;
return this._info;
},
Lab.prototype = Object.create(GameObject.prototype);
Lab.prototype.constructor = Lab;
Lab.prototype.getGrant = function() {
var addition = this.state.reputation * this.state.factor;
this.state.money += addition;
return addition;
};
var research = $.extend([], Helpers.loadFile('json/research.json'),
ObjectStorage.load('research'));
research = research.map(function(item) {
return $.extend({}, researchPrototype, item);
});
allObjects.push('name', research);
Lab.prototype.acquireData = function(amount) { this.state.data += amount; };
/** Workers
*/
var workersPrototype = {
hired: 0,
is_visible: function() {
return this.hired > 0 || lab.money >= this.cost * .7;
},
is_available: function() {
return lab.money >= this.cost;
},
hire: function() {
if (lab.buy(this.cost)) {
this.hired++;
analytics.sendEvent(analytics.events.categoryHR, analytics.events.actionHire, this.name, this.hired);
var cost = this.cost;
this.cost = Math.round(this.cost * this.cost_increase);
return cost;
}
return -1;
}
};
var workers = $.extend([], Helpers.loadFile('json/workers.json'),
ObjectStorage.load('workers'));
workers = workers.map(function(worker) {
return $.extend({}, workersPrototype, worker);
});
allObjects.push('name', workers);
/** Upgrades
*/
var upgradesPrototype = {
_visible: false,
_used: false,
meetsRequirements: function() {
for (var i = 0; i < this.requirements.length; i++) {
var req = this.requirements[i];
if (allObjects[req.key][req.property] < req.threshold) {
return false;
}
}
Lab.prototype.research = function(cost, reputation) {
if (this.state.data >= cost) {
this.state.data -= cost;
this.state.reputation += reputation;
return true;
},
isAvailable: function() {
if (!this._used && lab.money >= this.cost && this.meetsRequirements()) {
return true;
}
}
return false;
};
Lab.prototype.buy = function(cost) {
if (this.state.money >= cost) {
this.state.money -= cost;
return true;
}
return false;
};
/** @class Research
*/
var Research = function(obj) {
GameObject.apply(
this, [$.extend({}, obj, {state : {level : 0, interesting : false}})]);
};
Research.prototype = Object.create(GameObject.prototype);
Research.prototype.constructor = Research;
Research.prototype.isVisible = function(lab) {
if (!lab) {
return false;
},
isVisible: function() {
if (!this._used && (this._visible || lab.money >= this.cost * .7 && this.meetsRequirements())) {
this._visible = true;
return true;
}
}
return this.state.level > 0 ||
lab.state.data >= this.state.cost * GLOBAL_VISIBILITY_THRESHOLD;
};
Research.prototype.isAvailable = function(lab) {
if (!lab) {
return false;
},
buy: function() {
if (!this._used && lab.buy(this.cost)) {
for (var i = 0; i < this.targets.length; i++) {
var t = this.targets[i];
allObjects[t.key][t.property] *= this.factor || 1;
allObjects[t.key][t.property] += this.constant || 0;
}
this._used = true;
this._visible = false;
}
return lab.state.data >= this.state.cost;
};
Research.prototype.research = function(lab) {
if (lab && lab.research(this.state.cost, this.state.reputation)) {
this.state.level++;
if (this.state.info_levels.length > 0 &&
this.state.level === this.state.info_levels[0]) {
this.state.interesting = true;
this.state.info_levels.splice(0, 1);
}
var old_cost = this.state.cost;
this.state.cost = Math.round(this.state.cost * this.cost_increase);
return old_cost;
}
return -1;
};
Research.prototype.getInfo = function() {
if (!this._info) {
this._info = Helpers.loadFile(this.info);
}
this.state.interesting = false;
return this._info;
};
/** @class Worker
* Implement an auto-clicker in the game.
*/
var Worker = function(obj) {
GameObject.apply(this, [$.extend({}, obj, {state : {hired : 0}})]);
};
Worker.prototype = Object.create(GameObject.prototype);
Worker.prototype.constructor = Worker;
Worker.prototype.isVisible = function(lab) {
if (!lab) {
return false;
}
return this.state.hired > 0 ||
lab.state.money >= this.state.cost * GLOBAL_VISIBILITY_THRESHOLD;
};
Worker.prototype.isAvailable = function(lab) {
if (!lab) {
return false;
}
return lab.state.money >= this.state.cost;
};
Worker.prototype.hire = function(lab) {
if (lab && lab.buy(this.cost)) {
this.state.hired++;
var cost = this.state.cost;
this.state.cost = Math.round(cost * this.cost_increase);
return cost;
}
return -1; // not enough money
};
Worker.prototype.getTotal =
function() { return this.state.hired * this.state.rate; }
/** @class Upgrade
*/
var Upgrade = function(obj) {
GameObject.apply(
this, [$.extend({}, obj, {state : {visible : false, used : false}})]);
};
Upgrade.prototype = Object.create(GameObject.prototype);
Upgrade.prototype.constructor = Upgrade;
Upgrade.prototype.meetsRequirements = function(allObjects) {
if (!allObjects) {
return false;
}
for (var i = 0; i < this.requirements.length; i++) {
var req = this.requirements[i];
if (allObjects[req.key].state[req.property] < req.threshold) {
return false;
}
}
};
var upgrades = $.extend([], Helpers.loadFile('json/upgrades.json'),
ObjectStorage.load('upgrades'));
upgrades = upgrades.map(function(upgrade) {
return $.extend({}, upgradesPrototype, upgrade);
});
allObjects.push('name', upgrades);
/** Save all the game objects at once.
*/
var saveAll = function() {
ObjectStorage.save('lab', lab);
ObjectStorage.save('research', research);
ObjectStorage.save('workers', workers);
ObjectStorage.save('upgrades', upgrades);
ObjectStorage.save('achievements', achievements);
return true;
};
return {
lab: lab,
research: research,
workers: workers,
upgrades: upgrades,
saveAll: saveAll,
}
})();
Upgrade.prototype.isAvailable = function(lab) {
if (!lab) {
return false;
}
return !this.state.used && lab.state.money >= this.cost &&
this.meetsRequirements();
};
Upgrade.prototype.isVisible = function(lab) {
if (!lab) {
return false;
}
if (!this.state.used &&
(this.state.visible ||
lab.state.money >= this.cost * GLOBAL_VISIBILITY_THRESHOLD &&
this.meetsRequirements())) {
this._visible = true;
return true;
}
return false;
};
Upgrade.prototype.buy = function(lab, allObjects) {
if (lab && allObjects && !this.state.used && lab.buy(this.cost)) {
for (var i = 0; i < this.targets.length; i++) {
var t = this.targets[i];
allObjects[t.key].state[t.property] *= this.factor || 1;
allObjects[t.key].state[t.property] += this.constant || 0;
}
this.state.used = true; // How about actually REMOVING used upgrades?
this.state.visible = false;
}
};
// Expose classes in module.
return {Lab : Lab, Research : Research, Worker : Worker, Upgrade : Upgrade};
}());