speed up and bug fix

This commit is contained in:
LucidCrux
2013-07-25 02:00:41 -06:00
parent 766d3879c7
commit f4520bce91
3 changed files with 16 additions and 43 deletions
+1 -1
View File
@@ -172,7 +172,7 @@ var Engine = {
} }
} catch(e) { } catch(e) {
State = {}; State = {};
$SM.set('verson', Engine.VERSION); $SM.set('version', Engine.VERSION);
Engine.event('progress', 'new game'); Engine.event('progress', 'new game');
} }
}, },
+1 -3
View File
@@ -163,9 +163,7 @@ var Outside = {
}, },
numBuilding: function(bName) { numBuilding: function(bName) {
return $SM.get('features.location.outside') && return $SM.get('game.outside.buildings[\''+bName+'\']', true);
$SM.get('game.outside.buildings') &&
$SM.get('game.outside.buildings[\''+bName+'\']', true);
}, },
addBuilding: function(bName, num) { addBuilding: function(bName, num) {
+14 -39
View File
@@ -34,7 +34,7 @@ var StateManager = {
'income', 'income',
'timers', 'timers',
'game', //mostly location related: fire temp, workers, population, world map, etc 'game', //mostly location related: fire temp, workers, population, world map, etc
'playStats', //anything play related: play time, loads, etc 'playStats' //anything play related: play time, loads, etc
]; ];
for(var which in cats) { for(var which in cats) {
@@ -45,37 +45,18 @@ var StateManager = {
$.Dispatch('stateUpdate').subscribe($SM.handleStateUpdates); $.Dispatch('stateUpdate').subscribe($SM.handleStateUpdates);
}, },
//create the parent of a given state, recursive as needed //create all parents and then set state
createParent: function(stateName) { createState: function(stateName, value) {
var err = 0; var words = stateName.split(/[.\[\]']+/);
var obj = State;
//parse path to find last child var w = null;
var lastDot = stateName.lastIndexOf('.'); //if ends with a dot, there is a coding bug, not like ending in a bracket, so don't account for it for(var i=0, len=words.length-1;i<len;i++){
if(lastDot == stateName.length) { w = words[i];
Engine.log('ERROR: '+stateName+' is invalid. Cannot end in a dot.'); if(obj[w] === undefined ) obj[w] = {};
return; obj = obj[w];
}
var lastOB = stateName.lastIndexOf('[');
//make sure last bracket isn't just end of the line
var lastCB = stateName.substr(0, stateName.length -1).lastIndexOf(']');
//account for state[foo][bar] double bracket and state[foo].bar
if(lastCB == lastOB - 1) lastOB = -1;
if(lastCB == lastDot -1) lastDot = -1;
//find last child or return if no more children
var cutoff = Math.max(lastDot, lastOB, lastCB);
if(cutoff <= 0) return;
var parentPath = $SM.buildPath(stateName.substr(0,cutoff));
//try creating the parent
try {
eval('('+parentPath+') = {}');
} catch (e) {
//need to go up another level and make parent of parent
$SM.createParent(stateName.substr(0,cutoff));
//then it will definitely work if not, something is fubar
eval('('+parentPath+') = {}');
} }
obj[words[i]] = value;
return obj;
}, },
//set single state //set single state
@@ -90,9 +71,7 @@ var StateManager = {
eval('('+fullPath+') = value'); eval('('+fullPath+') = value');
} catch (e) { } catch (e) {
//parent doesn't exist, so make parent //parent doesn't exist, so make parent
$SM.createParent(stateName); $SM.createState(stateName, value);
//now it will definitely work. if not, something is broken
eval('('+fullPath+') = value');
} }
//stores values can not be negative //stores values can not be negative
@@ -136,7 +115,7 @@ var StateManager = {
if(old != old){ if(old != old){
Engine.log('WARNING: '+stateName+' was corrupted (NaN). Resetting to 0.'); Engine.log('WARNING: '+stateName+' was corrupted (NaN). Resetting to 0.');
old = 0; old = 0;
$SM.set(stateName, old + value, noEvent);//setState handles event and save $SM.set(stateName, old + value, noEvent);
} else if(typeof old != 'number' || typeof value != 'number'){ } else if(typeof old != 'number' || typeof value != 'number'){
Engine.log('WARNING: Can not do math with state:'+stateName+' or value:'+value+' because at least one is not a number.'); Engine.log('WARNING: Can not do math with state:'+stateName+' or value:'+value+' because at least one is not a number.');
err = 1 err = 1
@@ -207,10 +186,6 @@ var StateManager = {
var category = $SM.getCategory(stateName); var category = $SM.getCategory(stateName);
if(stateName == undefined) stateName = category = 'all'; //best if this doesn't happen as it will trigger more stuff if(stateName == undefined) stateName = category = 'all'; //best if this doesn't happen as it will trigger more stuff
$.Dispatch('stateUpdate').publish({'category': category, 'stateName':stateName}) $.Dispatch('stateUpdate').publish({'category': category, 'stateName':stateName})
//$.event.trigger({
// type: "stateUpdate",
// 'stateName': stateName
//});
if(save) Engine.saveGame(); if(save) Engine.saveGame();
}, },