Element store, and clear all

This commit is contained in:
2016-02-23 17:53:48 +08:00
parent f01955571d
commit e4cde4d8c7
7 changed files with 384 additions and 304 deletions
+4 -1
View File
@@ -316,7 +316,7 @@ h1 br {
min-height: 500px;
}
.ui-draggable-handle {
.ui-draggable {
cursor: move;
}
.element {
@@ -329,6 +329,9 @@ h1 br {
.element.empty {
opacity: 0.2;
}
.white-badge.empty {
opacity: 0.2;
}
#observationsContent {
font-size: 24px;
}
+9 -8
View File
@@ -87,10 +87,10 @@
<div id="elementContent">
<div class="row" ng-cloak>
<div class="col-md-4" ng-repeat="r in rc.elements" ng-show="rc.isVisible(r)">
<span
<div class="col-md-4" ng-repeat="r in rc.elements">
<span ng-show="rc.isVisible(r)"
id="{{r.key}}"
class="{{r.key}} element element-icon {{ r.state.interesting ? 'blink' : '' }} {{'q'+r.state.color+'-12'}} {{r.state.amount>0 ? '': 'empty'}}"
class="{{r.key}} element element-icon {{ r.state.interesting ? 'blink' : '' }} {{'q'+r.state.color+'-12'}} {{r.state.amount>0 ? '': 'empty'}} ui-draggable"
data-element="{{r.key}}"
data-drag="{{r.state.amount>0}}"
jqyoui-draggable="{containment:'offset'}"
@@ -99,7 +99,7 @@
>
{{r.key}}
</span>
<span ng-show="r.state.amount > 0" class="white-badge ng-binding">{{r.state.amount}}</span>
<span ng-show="rc.isVisible(r)" ng-show="r.state.amount > 0" class="{{r.state.amount>0 ? '': 'empty'}} white-badge ng-binding">{{r.state.amount}}</span>
</div>
</div>
</div>
@@ -119,13 +119,13 @@
<div id="detector-element-container" ng-cloak>
<div ng-repeat="r in dc.elements">
<span
class="{{r.key}} element q{{r.state.color}}-12"
class="{{r.key}} element q{{r.state.color}}-12 ui-draggable"
data-element="{{r.key}}"
data-drag="true"
jqyoui-draggable="{containment:'offset'}"
data-jqyoui-options="{{rc.dragOptions}}"
aria-disabled="false"
style="position: fixed; z-index: 9999; left: {{r.state.left}}px; top: {{r.state.top}}px;"
style="position: fixed; z-index: 9999; left: {{r.state.left}}px; top: {{r.state.top}}px;"
data-hashkey={{r.$$hashKey}}
>
{{r.key}}
@@ -139,7 +139,7 @@
id="detector-core"
width="400"
height="400"
class="test-tube prevent-select center-block "
class="test-tube prevent-select center-block"
data-drop="true"
jqyoui-droppable="{onDrop: 'dc.onDrop'}"
data-jqyoui-options="{{dc.dropOptions}}"
@@ -149,10 +149,11 @@
<canvas id="detector-flame" width="300" height="300" class="prevent-select center-block " ng-click="dc.toggleFlameFuel()">
Your bunsen burner. Click it to turn on and off.
</canvas>
<button id="clearall" class="btn btn-default" ng-click="dc.clearAll()"><i class="fa fa-refresh"></i></button>
</div>
</div>
<button id="detector-info" class="btn btn-info" ng-click="lc.showDetectorInfo()"><i class="fa fa-info"></i></button>
<div class="row status" ng-cloak>
<div class="col-xs-4 text-center col-no-padding-xs">
<strong>Data</strong><br>
+20 -24
View File
@@ -24,14 +24,17 @@ var app = (function () {
// factories to provide services. They serve shared game objects
app.factory('elements', function () {
var elements = Helpers.loadFile('json/elements.json');
elements = elements.map(
function (r) {
return new GameObjects.Element(r);
});
return elements
});
// app.factory('elements', function () {
// var elements = Helpers.loadFile('json/elements.json');
// elements = elements.map(
// function (r) {
// return new GameObjects.Element(r);
// });
// // put in extended array with helper methods
// elementStore = new GameObjects.ElementStore();
// elementStore.push.apply(elementStore,elements);
// return elementStore;
// });
app.factory('game', function () {
var game = new Game();
@@ -91,23 +94,12 @@ var app = (function () {
var draggable = angular.element(ui.draggable);
var key = draggable.data('element');
if (!draggable.hasClass('element-icon')) {
var elementStore = vs.elements.filter(function (e) {
return e.key === key;
})[0];
var hashKey=draggable.data('hashkey');
var found=false;
for (var i = 0; i < detector.elements.length; i++) {
if (detector.elements[i].$$hashKey===hashKey){
// delete detector.elements[i];
detector.elements.splice(i,1);
var found=true;
break;
}
}
if (!found) console.warn('Could not find dragged element in detector.elements',draggable);
else elementStore.state.amount+=1;
var elementStore = vs.elements.get(key);
var i = findIndexByHashKey(draggable.data('hashkey'));
detector.elements.splice(i,1);
elementStore.state.amount+=1;
}
}
};
vs.doElement = function (item) {
var cost = item.element(game.lab);
if (cost > 0) {
@@ -149,12 +141,16 @@ var app = (function () {
game.lab.clickDetector();
detector.addEvent();
UI.showUpdateValue("#update-data", game.lab.state.detector);
game.elements.addToStore();
return false;
};
vm.toggleFlameFuel = function () {
console.log('toggleFlameFuel');
detector.flamer.toggleFuel();
};
vm.clearAll = function(){
detector.clearAll(game);
}
}]);
app.controller('LabController', ['$interval', 'game', 'detector', function ($interval, game, detector) {
+175 -170
View File
@@ -5,221 +5,226 @@
This plugin available for use in all personal or commercial projects under both MIT and GPL licenses.
*/
(function ( $, window, document, undefined ) {
(function ($, window, document, undefined) {
var pluginName = "bubblr";
var pluginName = "bubblr";
/** default options **/
/** default options **/
var defaults = {
numberOfBubbles: 10,
backgroundColor: "transparent",
bubbleColor: "White",
bubbleOpacity: 0.7,
bubbleMinSize: 1,
bubbleMaxSize: 2,
bubbleMaxSpeed: 2,
bubbleMinSpeed: 1,
animationSpeed: 10
};
numberOfBubbles: 10,
backgroundColor: "transparent",
bubbleColor: "White",
bubbleOpacity: 0.7,
bubbleMinSize: 1,
bubbleMaxSize: 2,
bubbleMaxSpeed: 2,
bubbleMinSpeed: 1,
animationSpeed: 10
};
/**
* Create the bubblr
* @param {dom element} element - dom canvas element
* @param {object} options - options see Bubblr._defaults for options
*/
function Bubblr( element, options ) {
/**
* Create the bubblr
* @param {dom element} element - dom canvas element
* @param {object} options - options see Bubblr._defaults for options
*/
function Bubblr(element, options) {
this.element = element;
this.options = $.extend( {}, defaults, options) ;
this.options = $.extend({}, defaults, options);
this._defaults = defaults;
this.state = {
bubbles: [],
started: false,
globalTick: 0,
genBubbles: false,
}
this.state = {
bubbles: [],
started: false,
globalTick: 0,
genBubbles: false,
}
this.init();
this.start();
this.start();
}
Bubblr.prototype.init = function () {
Bubblr.prototype.init = function () {
if(this.element.getContext) {
this.ctx = this.element.getContext("2d");
}
else {
return;
}
if (this.element.getContext) {
this.ctx = this.element.getContext("2d");
} else {
return;
}
$canvas = $(this.element);
$canvas = $(this.element);
this.width = $canvas.width();
this.height = $canvas.height();
this.width = $canvas.width();
this.height = $canvas.height();
// this.state.globalTick=0;
// this.state.continuous=false; // new bubbles
// this.state.started = false // if there is an animation loop
$canvas.css("background-color", this.options.backgroundColor)
$canvas.css("background-color", this.options.backgroundColor)
// this.state.bubbles = new Array();
// initial bubbles
for(i = 0; i < this.options.numberOfBubbles; i++) {
this.state.bubbles[i] = this.generateBubble();
}
for(i in this.state.bubbles) {
this.drawBubble(this.state.bubbles[i]);
}
// initial bubbles
for (i = 0; i < this.options.numberOfBubbles; i++) {
this.state.bubbles[i] = this.generateBubble();
}
for (i in this.state.bubbles) {
this.drawBubble(this.state.bubbles[i]);
}
};
/**
* Recenter on resize,
* bind this to $(window).on('resize',bubblr.onResize.bind(bubblr);) if needed
**/
Bubblr.prototype.onResize= function(){
this.clear();
this.width = $(this.element).outerWidth();
this.height = $(this.element).outerWidth();
};
/**
* Recenter on resize,
* bind this to $(window).on('resize',bubblr.onResize.bind(bubblr);) if needed
**/
Bubblr.prototype.onResize = function () {
this.clear();
this.width = $(this.element).outerWidth();
this.height = $(this.element).outerWidth();
};
/** Start animation loop if not started **/
Bubblr.prototype.start = function() {
if (!this.state.started){
this.state.started=true;
this.animationLoop();
}
};
/** Start animation loop if not started **/
Bubblr.prototype.start = function () {
if (!this.state.started) {
this.state.started = true;
this.animationLoop();
}
};
/** Stop animation loop **/
Bubblr.prototype.stop = function() {
this.state.started=false;
this.clear();
};
/** Stop animation loop **/
Bubblr.prototype.stop = function () {
this.state.started = false;
this.clear();
};
/** Animation loop run Bubblr.stop and start control this **/
Bubblr.prototype.animationLoop = function() {
window.requestAnimFrame(this.animationLoop.bind(this));
this.state.globalTick++;
if (this.state.started) this.animate();
};
/** Animation loop run Bubblr.stop and start control this **/
Bubblr.prototype.animationLoop = function () {
window.requestAnimFrame(this.animationLoop.bind(this));
this.state.globalTick++;
if (this.state.started) this.animate();
};
Bubblr.prototype.animate = function (arguments) {
this.clear();
for(i in this.state.bubbles) {
this.update(i, this.state.bubbles[i]);
this.draw(i, this.state.bubbles[i]);
}
}
Bubblr.prototype.animate = function (arguments) {
this.clear();
for (i in this.state.bubbles) {
this.update(i, this.state.bubbles[i]);
this.draw(i, this.state.bubbles[i]);
}
Bubblr.prototype.clear = function () {
this.ctx.globalAlpha = 1;
this.ctx.fillStyle = this.options.backgroundColor;
this.ctx.clearRect(0,0,this.width, this.height);
};
// clear bubbles where y<0 (pop em)
var toRemove = this.state.bubbles.findIndex(function (b) {
return b.y < 0;
});
for (var i = 0; i < toRemove.length; i++) {
var bubble = this.state.bubbles.splice(toRemove[i], 1)[0];
//bubble.pop();
}
};
Bubblr.prototype.clear = function () {
this.ctx.globalAlpha = 1;
this.ctx.fillStyle = this.options.backgroundColor;
this.ctx.clearRect(0, 0, this.width, this.height);
};
Bubblr.prototype.draw = function (index, bubble) {
this.ctx.globalAlpha = this.options.bubbleOpacity;
this.drawBubble(bubble);
};
Bubblr.prototype.draw = function (index, bubble) {
this.ctx.globalAlpha = this.options.bubbleOpacity;
this.drawBubble(bubble);
};
/** Update bubble position **/
Bubblr.prototype.update = function (index, bubble) {
bubble.y = bubble.y - bubble.speed
/** Update bubble position **/
Bubblr.prototype.update = function (index, bubble) {
bubble.y = bubble.y - bubble.speed
if(bubble.y < 0 && this.state.continuous)
// reset to bottom if continuous running is on
this.state.bubbles[index] = this.generateBubble(true);
};
if (bubble.y < 0 && this.state.continuous)
// reset to bottom if continuous running is on
this.state.bubbles[index] = this.generateBubble(true);
else {}
/**
* Generate a single bubble
* @param {object} options - options for the bubble
* @param {Number} options.x - Starting x position. Defaults to a random
* location.
* @param {Number} options.y - starting y level. Default to the bottom or
* null for random position, a number sets a
* specific level. Note the top is 0, while the
* bottom is `-$(element).height()`
*/
Bubblr.prototype.bubble = function (options){
this.state.bubbles.push(this.generateBubble(options));
}
};
/**
* Helper to generate multiple bubble
* @param {[type]} amount - amount of bubbles to create
* @param {[type]} options - see Bubblr.bubble for description
*/
Bubblr.prototype.genBubbles = function (amount,options) {
for (var i = 0; i < amount; i++) {
this.state.bubbles.push(this.generateBubble(options));
}
}
/**
* Generate a single bubble
* @param {object} options - options for the bubble
* @param {Number} options.x - Starting x position. Defaults to a random
* location.
* @param {Number} options.y - starting y level. Default to the bottom or
* null for random position, a number sets a
* specific level. Note the top is 0, while the
* bottom is `-$(element).height()`
*/
Bubblr.prototype.bubble = function (options) {
this.state.bubbles.push(this.generateBubble(options));
};
Bubblr.prototype.drawBubble = function(bubble) {
this.ctx.fillStyle = this.options.bubbleColor;
this.ctx.beginPath();
this.ctx.arc(bubble.x, bubble.y, bubble.radius, 0, Math.PI*2, true);
this.ctx.closePath();
this.ctx.fill();
};
/**
* Helper to generate multiple bubble
* @param {[type]} amount - amount of bubbles to create
* @param {[type]} options - see Bubblr.bubble for description
*/
Bubblr.prototype.genBubbles = function (amount, options) {
for (var i = 0; i < amount; i++) {
this.state.bubbles.push(this.generateBubble(options));
}
}
Bubblr.prototype.generateBubble = function(options) {
options = options || {};
var bubble = new Object();
Bubblr.prototype.drawBubble = function (bubble) {
this.ctx.fillStyle = this.options.bubbleColor;
this.ctx.beginPath();
this.ctx.arc(bubble.x, bubble.y, bubble.radius, 0, Math.PI * 2, true);
this.ctx.closePath();
this.ctx.fill();
};
Bubblr.prototype.generateBubble = function (options) {
options = options || {};
var bubble = new Object();
if(options.x===undefined || options.x===null)
bubble.x = this.randomize(1,this.width);
else
bubble.x = options.x
if (options.x === undefined || options.x === null)
bubble.x = this.randomize(1, this.width);
else
bubble.x = options.x
if(options.y===undefined)
bubble.y = this.height;
else if (options.y===null)
bubble.y = this.randomize(1,this.height);
else
bubble.y = options.y
if (options.y === undefined)
bubble.y = this.height;
else if (options.y === null)
bubble.y = this.randomize(1, this.height);
else
bubble.y = options.y
if (options.radius)
bubble.radius = options.radius
else
bubble.radius = this.randomize(this.options.bubbleMinSize, this.options.bubbleMaxSize);
if (options.radius)
bubble.radius = options.radius
else
bubble.radius = this.randomize(this.options.bubbleMinSize, this.options.bubbleMaxSize);
if (options.speed)
bubble.speed = options.speed;
else
bubble.speed = this.randomize(this.options.bubbleMinSpeed, this.options.bubbleMaxSpeed);
if (options.speed)
bubble.speed = options.speed;
else
bubble.speed = this.randomize(this.options.bubbleMinSpeed, this.options.bubbleMaxSpeed);
return bubble;
};
return bubble;
};
Bubblr.prototype.randomize = function(min,max) {
return Math.floor((Math.random()*max)+min);
};
Bubblr.prototype.randomize = function (min, max) {
return Math.floor((Math.random() * max) + min);
};
// $.flame will create or return the bubblr element
$.fn[pluginName] = function ( options ) {
// $.flame will create or return the bubblr element
$.fn[pluginName] = function (options) {
return this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName,
new Bubblr( this, options ));
new Bubblr(this, options));
}
});
}
})( jQuery, window, document );
})(jQuery, window, document);
// shim for requestAnimFrame
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback, /* DOMElement */ element){
window.setTimeout(callback, 1000 / 60);
};
window.requestAnimFrame = (function () {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function ( /* function */ callback, /* DOMElement */ element) {
window.setTimeout(callback, 1000 / 60);
};
})();
+16 -2
View File
@@ -23,7 +23,7 @@ var Detector = function(){
ctx: null
},
elements: [],
elements: new GameObjects.ElementStore(),
visible: true,
@@ -120,7 +120,7 @@ var Detector = function(){
/** When a worker clicks the detector **/
addEventExternal: function(numWorkers)
{
// this.bubblr.bubble(numWorkers);
this.bubblr.genBubbles(numWorkers);
},
/** Draw current events **/
@@ -129,6 +129,20 @@ var Detector = function(){
this.bubblr.bubble();
},
/** Clear an element back to element Store **/
storeElementByHashKey: function(hashKey,game){
var i = this.elements.findIndexByHashKey(hashKey);
var removedElement = this.elements.splice(i,1)[0];
return game.elements.get(removedElement.key).state.amount+=1;
},
clearAll: function(game){
var hashKeys = this.elements.map(function(e){return e.$$hashKey;});
for (var i = 0; i < hashKeys.length; i++) {
this.storeElementByHashKey(hashKeys[i], game);
}
},
onDrop: function(event, ui, game){
var self=this;
console.debug('onDrop',arguments);
+117 -98
View File
@@ -1,109 +1,128 @@
/**
* Game object load/saves game resources and stores game objects
*/
var Game = (function(Helpers,GameObjects,ObjectStorage) {
'use strict';
var Game = (function (Helpers, GameObjects, ObjectStorage) {
'use strict';
var Game = function() {
this.lab = new GameObjects.Lab();
this.elements = null;
this.workers = null;
this.upgrades = null;
this.achievements = null;
this.allObjects = {lab : this.lab};
this.loaded = false;
this.rules= null;
};
Game.prototype.load = 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.elements = Helpers.loadFile('json/elements.json');
this.workers = Helpers.loadFile('json/workers.json');
this.upgrades = Helpers.loadFile('json/upgrades.json');
this.achievements = Helpers.loadFile('json/achievements.json');
this.keywords = Helpers.loadFile('json/keywords.json');
// Turn JSON files into actual game objects and fill map of all objects
var _this = this;
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;
var Game = function () {
this.lab = new GameObjects.Lab();
this.elements = null;
this.workers = null;
this.upgrades = null;
this.achievements = null;
this.allObjects = {
lab: this.lab
};
this.loaded = false;
this.rules = null;
};
this.elements = this.elements.map(
function(r) { return makeGameObject(GameObjects.Element, 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) { return makeGameObject(GameObjects.Achievement, a); });
// Load states from local store
for (var key in this.allObjects) {
var o = this.allObjects[key];
o.loadState(ObjectStorage.load(key));
}
// this.detector.init(400,$('#detector'));
Game.prototype.load = function () {
if (this.loaded) {
return;
}
this.rules = this.generateRules();
// 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.elements = Helpers.loadFile('json/elements.json');
this.workers = Helpers.loadFile('json/workers.json');
this.upgrades = Helpers.loadFile('json/upgrades.json');
this.achievements = Helpers.loadFile('json/achievements.json');
this.keywords = Helpers.loadFile('json/keywords.json');
this.loaded = true;
};
// Turn JSON files into actual game objects and fill map of all objects
var _this = this;
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.elements = this.elements.map(
function (r) {
return makeGameObject(GameObjects.Element, 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) {
return makeGameObject(GameObjects.Achievement, a);
});
// Load states from local store
for (var key in this.allObjects) {
var o = this.allObjects[key];
o.loadState(ObjectStorage.load(key));
}
/** Generate rules between runes **/
Game.prototype.generateRules= function(){
var elements = this.elements;
// generate rules and store them with a hash of inredients
// the rule will be an object with ingredients, catalysts, conditions, results
// todo make a strcture that's more like a tree with progression etc
// todo add duds, misleading ones, explosions wildcards
// todo make this theory based?
// todo simulation
var rules={};
for (var k = 0; k < this.elements.length*5; k++) {
// make a rules
var rule={ingredients:[],catalysts:[],conditions:[],results:[],inputs:[]}
var numOfIngredients = 2+Math.round(Math.random()*2);
for (var i = 0; i < numOfIngredients; i++) {
var j = Math.round(Math.random()*(elements.length-1));
rule.ingredients.push(elements[j].key);
}
// put elements in extended array with utility methods
this.elementStore = new GameObjects.ElementStore();
this.elementStore.push.apply(this.elementStore,this.elements);
this.elements=this.elementStore;
if (Math.random()<0.1) {
var j = Math.round(Math.random()*(elements.length-1));
rule.catalysts.push(elements[j].key);
}
var numOfresults = Math.round(Math.random()*3);
for (var i = 0; i < numOfresults; i++) {
var j = Math.round(Math.random()*(elements.length-1));
rule.results.push(elements[j].key);
}
rule.inputs=[].concat(rule.ingredients,rule.catalysts)
rule.ingredients.sort()
rule.results.sort()
rule.catalysts.sort()
rule.inputs.sort();
// index byhash of sorted array of ingredients
rules[rule.inputs]=rule
}
return rules;
},
Game.prototype.save = function() {
// Save every object's state to local storage
for (var key in this.allObjects) {
ObjectStorage.save(key, this.allObjects[key].state);
}
};
this.rules = this.generateRules();
return Game;
}(Helpers,GameObjects,ObjectStorage));
this.loaded = true;
};
/** Generate rules between runes **/
Game.prototype.generateRules = function () {
var elements = this.elements;
// generate rules and store them with a hash of inredients
// the rule will be an object with ingredients, catalysts, conditions, results
// todo make a strcture that's more like a tree with progression etc
// todo add duds, misleading ones, explosions wildcards
// todo make this theory based?
// todo simulation
var rules = {};
for (var k = 0; k < this.elements.length * 5; k++) {
// make a rules
var rule = {
ingredients: [],
catalysts: [],
conditions: [],
results: [],
inputs: []
}
var numOfIngredients = 2 + Math.round(Math.random() * 2);
for (var i = 0; i < numOfIngredients; i++) {
var j = Math.round(Math.random() * (elements.length - 1));
rule.ingredients.push(elements[j].key);
}
if (Math.random() < 0.1) {
var j = Math.round(Math.random() * (elements.length - 1));
rule.catalysts.push(elements[j].key);
}
var numOfresults = Math.round(Math.random() * 3);
for (var i = 0; i < numOfresults; i++) {
var j = Math.round(Math.random() * (elements.length - 1));
rule.results.push(elements[j].key);
}
rule.inputs = [].concat(rule.ingredients, rule.catalysts)
rule.ingredients.sort()
rule.results.sort()
rule.catalysts.sort()
rule.inputs.sort();
// index byhash of sorted array of ingredients
rules[rule.inputs] = rule
}
return rules;
},
Game.prototype.save = function () {
// Save every object's state to local storage
for (var key in this.allObjects) {
ObjectStorage.save(key, this.allObjects[key].state);
}
};
return Game;
}(Helpers, GameObjects, ObjectStorage));
+43 -1
View File
@@ -86,6 +86,47 @@ var GameObjects = (function () {
return false;
};
var ElementStore = function (obj) {
Array.apply(this, [obj]);
};
ElementStore.prototype = Object.create(Array.prototype);
ElementStore.prototype.constructor = ElementStore;
/** Add a random element or specify it's key **/
ElementStore.prototype.addToStore = function(element){
if (!element) this.get(element);
if (!element) element = this.select();
return element.state.amount+=1;
};
/** Select random element from store **/
ElementStore.prototype.select = function () {
var i = Math.floor((this.length-1)*Math.random());
return this[i];
};
/** Get element by hashid **/
ElementStore.prototype.get = function (key) {
return this.filter(function (e) {
return e.key === key;
})[0];
}
/** Get element by hashid **/
ElementStore.prototype.getByHashKey = function (hashKey) {
return this.filter(function (e) {
return e.$$hashKey === hashKey;
})[0];
}
/** Get element by hashid **/
ElementStore.prototype.findIndexByHashKey = function (hashKey) {
return this.findIndex(function (e) {
return e.$$hashKey === hashKey;
})[0];
}
/** @class Element
*/
var Element = function (obj) {
@@ -279,6 +320,7 @@ var GameObjects = (function () {
Element: Element,
Worker: Worker,
Upgrade: Upgrade,
Achievement: Achievement
Achievement: Achievement,
ElementStore: ElementStore
};
}());