From 851649229793cccd1307dcd5ffc43b2d1639cf4e Mon Sep 17 00:00:00 2001 From: Kevin Dungs Date: Sun, 7 Sep 2014 20:11:15 +0200 Subject: [PATCH] Integrate achievements into GameObject system. Now have to adjust the UI accordingly. --- js/achievements.js | 145 --------------------------------------------- js/game.js | 11 +++- js/gameobjects.js | 52 +++++++++++++++- 3 files changed, 58 insertions(+), 150 deletions(-) delete mode 100644 js/achievements.js diff --git a/js/achievements.js b/js/achievements.js deleted file mode 100644 index 7d81a25..0000000 --- a/js/achievements.js +++ /dev/null @@ -1,145 +0,0 @@ -var achievements = -{ - list: [], - listSpecial: [], - listSummary: [], - - startTime: new Date().getTime(), - lastSave: new Date().getTime(), - - count: - { - clicks: 0, - data: 0, - money: 0, - reputation: 0, - workers: 0, - dataSpent: 0, - moneyWorkers: 0, - moneyUpgrades: 0 - }, - - workers: {}, - research: {}, - - setList: function(list) - { - for (var i = 0; i < list.length; i++) { - if (list[i].type == list[i].target) { - achievements.listSpecial.push(list[i]); - for (var item in achievements[list[i].type]) { - var a = $.extend(true, {}, list[i]); - a.target = item; - if (list[i].type == 'workers' && list[i].threshold == 1) { - a.description = a.description.replace('${name}', item.substring(0, item.length - 1)); - } else { - a.description = a.description.replace('${name}', item); - } - achievements.list.push(a); - } - } else { - achievements.list.push(list[i]); - } - } - - achievements.list.map(function(item) { - item.completed = false; - item.alerted = false; - item.is_visible = function() { - return this.completed; - }; - }); - }, - - restore: function() - { - achievements = $.extend({}, achievements, ObjectStorage.load('achievements')); - achievements.startTime = new Date().getTime() - (achievements.lastSave - achievements.startTime); - }, - - addWorkers: function(list) - { - for (var i = 0; i < list.length; i++) { - achievements.workers[list[i].name] = 0; - } - }, - - addResearch: function(list) - { - for (var i = 0; i < list.length; i++) { - achievements.research[list[i].name] = 0; - } - }, - - update: function(type, subtype, val) - { - achievements[type][subtype] += val; - - for (var i = 0; i < achievements.list.length; i++) { - if (achievements.list[i].type == type && - achievements.list[i].target == subtype && - achievements[type][subtype] >= achievements.list[i].threshold - ) - { - achievements.list[i].completed = true; - achievements.displayAchievement(i); - } - } - }, - - displayAchievement: function(i) - { - var alert = ''; - - alert = $(alert); - - if (achievements.list[i].completed && !achievements.list[i].alerted) { - $('#achievements-container').prepend(alert); - - var remove = function(a) - { - return function() - { - a.slideUp(300, function() { a.remove(); }); - }; - }; - - window.setTimeout(remove(alert), 2000); - achievements.list[i].alerted = true; - - var a = $.extend(true, {}, achievements.list[i]); - a.time = achievements.timeFormatter(new Date().getTime() - achievements.startTime); - achievements.listSummary.push(a); - } - }, - - timeFormatter: function(msec) - { - var totals = Math.ceil(msec / 1000); - var days = Math.floor(totals / (24 * 60 * 60)); - var hours = Math.floor((totals % (24 * 60 * 60)) / (60 * 60)); - var totalmin = (totals % (24 * 60 * 60)) % (60 * 60); - var mins = Math.floor(totalmin / 60); - var secs = totalmin % 60; - - var str = []; - if (days > 0) { - str.push(days + ' day' + (days % 100 == 1 ? '' : 's')); - } - if (hours > 0) { - str.push(hours + ' h'); - } - if (mins > 0) { - str.push(mins + ' min'); - } - if (secs > 0) { - str.push(secs + ' s'); - } - - return str.join(', '); - } -}; - diff --git a/js/game.js b/js/game.js index 764c8fb..9625a38 100644 --- a/js/game.js +++ b/js/game.js @@ -6,6 +6,7 @@ var Game = (function() { this.research = null; this.workers = null; this.upgrades = null; + this.achivements = null; this.allObjects = {lab : this.lab}; this.loaded = false; }; @@ -17,8 +18,9 @@ var Game = (function() { var _this = this; $.when($.get('json/research.json', function(jR) { _this.research = jR; }), $.get('json/workers.json', function(jW) { _this.workers = jW; }), - $.get('json/upgrades.json', function(jU) { _this.upgrades = jU; })) - .then(function() { + $.get('json/upgrades.json', function(jU) { _this.upgrades = jU; }), + $.get('json/achievements.json', + function(jA) { _this.achivements = jA; })).then(function() { // Turn JSON files into actual game objects and fill map of all objects var makeGameObject = function(type, object) { // It's okay to define this function here since load is only called @@ -33,6 +35,11 @@ var Game = (function() { function(w) { return makeGameObject(GameObjects.Worker, w); }); _this.upgrades = _this.upgrades.map( function(u) { return makeGameObject(GameObjects.Upgrade, u); }); + _this.achivements = _this.achivements.map(function(a) { + var _a = makeGameObject(GameObjects.Achievement, a); + _a.setRefAllGameObjects(_this.allObjects); + return _a; + }); // Load states from local store for (var i = 0; i < _this.allObjects.length; i++) { var o = _this.allObjects[i]; diff --git a/js/gameobjects.js b/js/gameobjects.js index 89f9e20..e1d8208 100644 --- a/js/gameobjects.js +++ b/js/gameobjects.js @@ -28,7 +28,12 @@ var GameObjects = (function() { factor : 5, data : 0, money : 0, - reputation : 0 + reputation : 0, + clicks : 0, + moneyCollected : 0, + moneySpent : 0, + dataCollected : 0, + dataSpent : 0 } }]); }; @@ -40,14 +45,19 @@ var GameObjects = (function() { Lab.prototype.getGrant = function() { var addition = this.state.reputation * this.state.factor; this.state.money += addition; + this.state.moneyCollected += addition; return addition; }; - Lab.prototype.acquireData = function(amount) { this.state.data += amount; }; + Lab.prototype.acquireData = function(amount) { + this.state.data += amount; + this.state.dataCollected += amount; + }; Lab.prototype.research = function(cost, reputation) { if (this.state.data >= cost) { this.state.data -= cost; + this.state.dataSpent += cost; this.state.reputation += reputation; return true; } @@ -57,6 +67,7 @@ var GameObjects = (function() { Lab.prototype.buy = function(cost) { if (this.state.money >= cost) { this.state.money -= cost; + this.state.moneySpent += cost; return true; } return false; @@ -208,6 +219,41 @@ var GameObjects = (function() { } }; + + /** @class Achievement + */ + var Achievement = function(obj) { + GameObject.apply(this, + [$.extend({}, obj, {state : {dateAchieved : null}})]); + this._allObjects = {}; + }; + + Achievement.prototype = Object.create(GameObject.prototype); + + Achievement.prototype.setRefAllGameObjects = function (allGameObjects) { + this._allObjects = allGameObjects; + }; + + Achievement.prototype.isAchieved = function() { + if (this.state.dateAchieved) { + return true; + } + if (this._allObjects.hasOwnProperty(this.targetKey) && + this._allObjects[this.targetKey].state.hasOwnProperty( + this.targetProperty)) { + this.state.dateAchieved = new Date(); + return true; + } + return false; + }; + + // Expose classes in module. - return {Lab : Lab, Research : Research, Worker : Worker, Upgrade : Upgrade}; + return { + Lab : Lab, + Research : Research, + Worker : Worker, + Upgrade : Upgrade, + Achievement : Achievement + }; }());