mirror of
https://github.com/wassname/cardsforscience.git
synced 2026-06-30 02:54:14 +08:00
Integrate achievements into GameObject system.
Now have to adjust the UI accordingly.
This commit is contained in:
@@ -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 = '<div class="alert alert-success alert-dismissible" role="alert">';
|
||||
alert += '<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>';
|
||||
alert += '<span class="glyphicon ' + achievements.list[i].icon + ' alert-glyph"></span> <span class="alert-text">' + achievements.list[i].description + '</span>';
|
||||
alert += '</div>';
|
||||
|
||||
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(', ');
|
||||
}
|
||||
};
|
||||
|
||||
+9
-2
@@ -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];
|
||||
|
||||
+49
-3
@@ -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
|
||||
};
|
||||
}());
|
||||
|
||||
Reference in New Issue
Block a user