Most things working. Apart from achivements somehow. Also achievements pop up has to be reintroduced. Probalby write an AchievementsManager or something.

This commit is contained in:
Kevin Dungs
2014-11-08 15:55:02 +01:00
parent 709cea5180
commit cd9d3673e9
6 changed files with 121 additions and 113 deletions
+43 -45
View File
@@ -2,24 +2,19 @@
(function() {
var game = new Game.Game();
game.load();
var lab, research, workers, upgrades;
$.when(game.load).then(function() {
lab = game.lab;
research = game.research;
workers = game.workers;
upgrades = game.upgrades;
});
var lab = game.lab;
var research = game.research;
var workers = game.workers;
var upgrades = game.upgrades;
var achievements = game.achievements;
UI.validateVersion(lab.version);
achievements.addResearch(research);
achievements.addWorkers(workers);
var app = angular.module('particleClicker', []);
app.filter('niceNumber', ['$filter', function($filter) {
return Helpers.formatNumberPostfix;
return Helpers.formatNumberPostfix;
}]);
app.filter('currency', ['$filter', function($filter) {
@@ -36,11 +31,9 @@
app.controller('DetectorController', function() {
this.click = function() {
lab.acquire(lab.detector.rate);
lab.acquireData(lab.state.detector);
detector.addEvent();
achievements.update('count', 'clicks', 1);
achievements.update('count', 'data', lab.detector.rate);
UI.showUpdateValue("#update-data", lab.detector.rate);
UI.showUpdateValue("#update-data", lab.state.detector);
return false;
};
});
@@ -55,35 +48,34 @@
};
$interval(function() { // one tick
var grant = lab.getGrant();
achievements.update('count', 'money', grant);
UI.showUpdateValue("#update-funding", grant);
var sum = 0;
for (var i = 0; i < workers.length; i++) {
sum += workers[i].hired * workers[i].rate;
sum += workers[i].state.hired * workers[i].state.rate;
}
if (sum > 0) {
lab.acquireData(sum);
UI.showUpdateValue("#update-data", sum);
detector.addEventExternal(workers.map(function(w) {
return w.state.hired;
}).reduce(function(a, b){return a + b}, 0));
}
lab.acquire(sum);
achievements.update('count', 'data', sum);
UI.showUpdateValue("#update-data", sum);
detector.addEventExternal();
}, 1000);
}]);
app.controller('ResearchController', ['$compile', function($compile) {
this.research = research;
this.isVisible = function(item) {
return item.isVisible(lab);
};
this.isAvailable = function(item) {
return item.isAvailable(lab);
};
this.doResearch = function(item) {
var cost = item.research();
var cost = item.research(lab);
if (cost > 0) {
achievements.update('count', 'reputation', item.reputation);
achievements.update('count', 'dataSpent', cost);
achievements.update('research', item.name, 1);
UI.showUpdateValue("#update-data", -cost);
UI.showUpdateValue("#update-reputation", item.reputation);
analytics.sendEvent(
analytics.events.categoryResearch,
analytics.events.actionResearch,
item.name,
item.level
);
}
};
this.showInfo = function(r) {
@@ -94,12 +86,15 @@
app.controller('HRController', function() {
this.workers = workers;
this.isVisible = function(worker) {
return worker.isVisible(lab);
};
this.isAvailable = function(worker) {
return worker.isAvailable(lab);
};
this.hire = function(worker) {
var cost = worker.hire();
var cost = worker.hire(lab);
if (cost > 0) {
achievements.update('count', 'moneyWorkers', cost);
achievements.update('workers', worker.name, 1);
achievements.update('count', 'workers', 1);
UI.showUpdateValue("#update-funding", -cost);
}
};
@@ -107,29 +102,32 @@
app.controller('UpgradesController', function() {
this.upgrades = upgrades;
this.isVisible = function(upgrade) {
return upgrade.isVisible(lab);
};
this.isAvailable = function(upgrade) {
return upgrade.isAvailable(lab);
};
this.upgrade = function(upgrade) {
if (upgrade.buy()) {
achievements.update('count', 'moneyUpgrades', upgrade.cost);
if (upgrade.buy(lab)) {
UI.showUpdateValue("#update-funding", upgrade.cost);
}
}
});
achievements.setList(Helpers.loadFile('json/achievements.json'));
achievements.restore();
app.controller('AchievementsController', function() {
this.achievements = achievements.listSummary;
this.achievementsAll = achievements.list;
app.controller('AchievementsController', function($scope) {
$scope.achievements = achievements;
$scope.progress = function() {
return achievements.filter(function(a) { a.isAchieved(); }).length;
};
});
app.controller('SaveController',
['$scope', '$interval', function($scope, $interval) {
$scope.lastSaved = new Date();
$scope.saveNow = function() {
GameObjects.saveAll();
game.save();
$scope.lastSaved = new Date();
achievements.lastSave = $scope.lastSaved.getTime();
};
$scope.restart = function() {
if (window.confirm(
+2 -2
View File
@@ -273,13 +273,13 @@ var detector =
}
},
addEventExternal: function()
addEventExternal: function(numWorkers)
{
if (!detector.visible) {
return;
}
var num = Math.min(20 * achievements.count.workers / 10, 20);
var num = Math.min(20 * numWorkers / 10, 20);
for (var i = 0; i < num; i++) {
var index = Math.round(Math.random() * (detector.tracks.length - 1));
+37 -33
View File
@@ -6,7 +6,7 @@ var Game = (function() {
this.research = null;
this.workers = null;
this.upgrades = null;
this.achivements = null;
this.achievements = null;
this.allObjects = {lab : this.lab};
this.loaded = false;
};
@@ -15,44 +15,48 @@ var Game = (function() {
if (this.loaded) {
return;
}
// I know synchronous requests are bad as they will block the browser.
// However, I don't see any other reasonable way to do this in order to
// make it work with Angular. If you know a way, let me know, and I'll
// give you a beer. - Kevin
this.research = Helpers.loadFile('json/research.json');
this.workers = Helpers.loadFile('json/workers.json');
this.upgrades = Helpers.loadFile('json/upgrades.json');
this.achievements = Helpers.loadFile('json/achievements.json');
// Turn JSON files into actual game objects and fill map of all objects
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; }),
$.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
// once anyway...
var o = new type(object);
_this.allObjects[o.key] = o;
return o;
};
_this.research = _this.research.map(
function(r) { return makeGameObject(GameObjects.Research, r); });
_this.workers = _this.workers.map(
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];
o.loadState(ObjectStorage.load(o.key));
}
_this.loaded = true;
var makeGameObject = function(type, object) {
// It's okay to define this function here since load is only called
// once anyway...
var o = new type(object);
_this.allObjects[o.key] = o;
return o;
};
this.research = this.research.map(
function(r) { return makeGameObject(GameObjects.Research, r); });
this.workers = this.workers.map(
function(w) { return makeGameObject(GameObjects.Worker, w); });
this.upgrades = this.upgrades.map(
function(u) { return makeGameObject(GameObjects.Upgrade, u); });
this.achievements = this.achievements.map(function(a) {
var _a = makeGameObject(GameObjects.Achievement, a);
_a.setRefAllGameObjects(_this.allObjects);
return _a;
});
// Load states from local store
for (var key in this.allObjects) {
var o = this.allObjects[key];
o.loadState(ObjectStorage.load(key));
}
this.loaded = true;
};
Game.prototype.save = function() {
// Save every object's state to local storage
for (var i = 0; i < this.allObjects.length; i++) {
ObjectStorage.save(this.allObjects[i].state);
for (var key in this.allObjects) {
ObjectStorage.save(key, this.allObjects[key].state);
}
};
+11 -8
View File
@@ -76,8 +76,9 @@ var GameObjects = (function() {
/** @class Research
*/
var Research = function(obj) {
GameObject.apply(
this, [$.extend({}, obj, {state : {level : 0, interesting : false}})]);
GameObject.apply(this, [obj]);
this.state.level = 0;
this.state.interesting = false;
};
Research.prototype = Object.create(GameObject.prototype);
@@ -126,7 +127,8 @@ var GameObjects = (function() {
* Implement an auto-clicker in the game.
*/
var Worker = function(obj) {
GameObject.apply(this, [$.extend({}, obj, {state : {hired : 0}})]);
GameObject.apply(this, [obj]);
this.state.hired = 0;
};
Worker.prototype = Object.create(GameObject.prototype);
@@ -149,7 +151,7 @@ var GameObjects = (function() {
};
Worker.prototype.hire = function(lab) {
if (lab && lab.buy(this.cost)) {
if (lab && lab.buy(this.state.cost)) {
this.state.hired++;
var cost = this.state.cost;
this.state.cost = Math.round(cost * this.cost_increase);
@@ -164,8 +166,9 @@ var GameObjects = (function() {
/** @class Upgrade
*/
var Upgrade = function(obj) {
GameObject.apply(
this, [$.extend({}, obj, {state : {visible : false, used : false}})]);
GameObject.apply(this, [obj]);
this.state.visible = false;
this.state.used = false;
};
Upgrade.prototype = Object.create(GameObject.prototype);
@@ -223,8 +226,8 @@ var GameObjects = (function() {
/** @class Achievement
*/
var Achievement = function(obj) {
GameObject.apply(this,
[$.extend({}, obj, {state : {dateAchieved : null}})]);
GameObject.apply(this, [obj]);
this.state.dateAchieved = null;
this._allObjects = {};
};
+5 -1
View File
@@ -8,6 +8,7 @@ var Helpers = (function() {
var loadFile = function(filename) {
var res;
$.ajax({
async: false,
url : filename,
success : function(data) {
res = data;
@@ -19,6 +20,9 @@ var Helpers = (function() {
/** Format a number with proper postfix.
*/
var formatNumberPostfix = function(number) {
if (typeof number !== "number") {
return -123;
}
var abs = Math.abs(number);
if (abs >= Math.pow(10, 12)) {
number = (number / Math.pow(10, 12)).toFixed(1) + "T";
@@ -37,7 +41,7 @@ var Helpers = (function() {
return {
loadFile: loadFile,
formatNumberPostfix: formatNumberPostfix,
version: '0.2',
version: '0.4',
analytics: ''
};
})();