mirror of
https://github.com/doublespeakgames/adarkroom.git
synced 2026-05-28 00:01:54 +08:00
Hyper mode and LightsOff is now stored in the State
This commit is contained in:
committed by
Blake Grotewold
parent
f85b8fdf7e
commit
8320156f42
+66
-65
@@ -1,52 +1,53 @@
|
||||
/*
|
||||
* Module for handling States
|
||||
*
|
||||
*
|
||||
* All states should be get and set through the StateManager ($SM).
|
||||
*
|
||||
*
|
||||
* The manager is intended to handle all needed checks and error catching.
|
||||
* This includes creating the parents of layered/deep states so undefined states
|
||||
* do not need to be tested for and created beforehand.
|
||||
*
|
||||
*
|
||||
* When a state is changed, an update event is sent out containing the name of the state
|
||||
* changed or in the case of multiple changes (.setM, .addM) the parent class changed.
|
||||
* Event: type: 'stateUpdate', stateName: <path of state or parent state>
|
||||
*
|
||||
*
|
||||
* Original file created by: Michael Galusha
|
||||
*/
|
||||
|
||||
var StateManager = {
|
||||
|
||||
|
||||
MAX_STORE: 99999999999999,
|
||||
|
||||
|
||||
options: {},
|
||||
|
||||
|
||||
init: function(options) {
|
||||
this.options = $.extend(
|
||||
this.options,
|
||||
options
|
||||
);
|
||||
|
||||
|
||||
//create categories
|
||||
var cats = [
|
||||
'features', //big features like buildings, location availability, unlocks, etc
|
||||
'stores', //little stuff, items, weapons, etc
|
||||
'character', //this is for player's character stats such as perks
|
||||
'features', //big features like buildings, location availability, unlocks, etc
|
||||
'stores', //little stuff, items, weapons, etc
|
||||
'character', //this is for player's character stats such as perks
|
||||
'income',
|
||||
'timers',
|
||||
'game', //mostly location related: fire temp, workers, population, world map, etc
|
||||
'playStats', //anything play related: play time, loads, etc
|
||||
'previous', // prestige, score, trophies (in future), achievements (again, not yet), etc
|
||||
'outfit' // used to temporarily store the items to be taken on the path
|
||||
'game', //mostly location related: fire temp, workers, population, world map, etc
|
||||
'playStats', //anything play related: play time, loads, etc
|
||||
'previous', // prestige, score, trophies (in future), achievements (again, not yet), etc
|
||||
'outfit', // used to temporarily store the items to be taken on the path
|
||||
'config'
|
||||
];
|
||||
|
||||
|
||||
for(var which in cats) {
|
||||
if(!$SM.get(cats[which])) $SM.set(cats[which], {});
|
||||
if(!$SM.get(cats[which])) $SM.set(cats[which], {});
|
||||
}
|
||||
|
||||
|
||||
//subscribe to stateUpdates
|
||||
$.Dispatch('stateUpdate').subscribe($SM.handleStateUpdates);
|
||||
},
|
||||
|
||||
|
||||
//create all parents and then set state
|
||||
createState: function(stateName, value) {
|
||||
var words = stateName.split(/[.\[\]'"]+/);
|
||||
@@ -67,51 +68,51 @@ var StateManager = {
|
||||
obj[words[i]] = value;
|
||||
return obj;
|
||||
},
|
||||
|
||||
|
||||
//set single state
|
||||
//if noEvent is true, the update event won't trigger, useful for setting multiple states first
|
||||
set: function(stateName, value, noEvent) {
|
||||
var fullPath = $SM.buildPath(stateName);
|
||||
|
||||
|
||||
//make sure the value isn't over the engine maximum
|
||||
if(typeof value == 'number' && value > $SM.MAX_STORE) value = $SM.MAX_STORE;
|
||||
|
||||
|
||||
try{
|
||||
eval('('+fullPath+') = value');
|
||||
} catch (e) {
|
||||
//parent doesn't exist, so make parent
|
||||
$SM.createState(stateName, value);
|
||||
}
|
||||
|
||||
|
||||
//stores values can not be negative
|
||||
if(stateName.indexOf('stores') === 0 && $SM.get(stateName, true) < 0) {
|
||||
eval('('+fullPath+') = 0');
|
||||
Engine.log('WARNING: state:' + stateName + ' can not be a negative value. Set to 0 instead.');
|
||||
}
|
||||
|
||||
|
||||
if(!noEvent) {
|
||||
Engine.saveGame();
|
||||
$SM.fireUpdate(stateName);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
//sets a list of states
|
||||
setM: function(parentName, list, noEvent) {
|
||||
$SM.buildPath(parentName);
|
||||
|
||||
|
||||
//make sure the state exists to avoid errors,
|
||||
if($SM.get(parentName) === undefined) $SM.set(parentName, {}, true);
|
||||
|
||||
|
||||
for(var k in list){
|
||||
$SM.set(parentName+'["'+k+'"]', list[k], true);
|
||||
}
|
||||
|
||||
|
||||
if(!noEvent) {
|
||||
Engine.saveGame();
|
||||
$SM.fireUpdate(parentName);
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
//shortcut for altering number values, return 1 if state wasn't a number
|
||||
add: function(stateName, value, noEvent) {
|
||||
var err = 0;
|
||||
@@ -119,7 +120,7 @@ var StateManager = {
|
||||
//could also add in a true = 1 thing, to have something go from existing (true)
|
||||
//to be a count, but that might be unwanted behavior (add with loose eval probably will happen anyways)
|
||||
var old = $SM.get(stateName, true);
|
||||
|
||||
|
||||
//check for NaN (old != old) and non number values
|
||||
if(old != old){
|
||||
Engine.log('WARNING: '+stateName+' was corrupted (NaN). Resetting to 0.');
|
||||
@@ -131,52 +132,52 @@ var StateManager = {
|
||||
} else {
|
||||
$SM.set(stateName, old + value, noEvent); //setState handles event and save
|
||||
}
|
||||
|
||||
|
||||
return err;
|
||||
},
|
||||
|
||||
|
||||
//alters multiple number values, return number of fails
|
||||
addM: function(parentName, list, noEvent) {
|
||||
var err = 0;
|
||||
|
||||
|
||||
//make sure the parent exists to avoid errors
|
||||
if($SM.get(parentName) === undefined) $SM.set(parentName, {}, true);
|
||||
|
||||
|
||||
for(var k in list){
|
||||
if($SM.add(parentName+'["'+k+'"]', list[k], true)) err++;
|
||||
}
|
||||
|
||||
|
||||
if(!noEvent) {
|
||||
Engine.saveGame();
|
||||
$SM.fireUpdate(parentName);
|
||||
}
|
||||
return err;
|
||||
},
|
||||
|
||||
|
||||
//return state, undefined or 0
|
||||
get: function(stateName, requestZero) {
|
||||
var whichState = null;
|
||||
var fullPath = $SM.buildPath(stateName);
|
||||
|
||||
|
||||
//catch errors if parent of state doesn't exist
|
||||
try{
|
||||
eval('whichState = ('+fullPath+')');
|
||||
} catch (e) {
|
||||
whichState = undefined;
|
||||
}
|
||||
|
||||
|
||||
//prevents repeated if undefined, null, false or {}, then x = 0 situations
|
||||
if((!whichState || whichState == {}) && requestZero) return 0;
|
||||
else return whichState;
|
||||
},
|
||||
|
||||
|
||||
//mainly for local copy use, add(M) can fail so we can't shortcut them
|
||||
//since set does not fail, we know state exists and can simply return the object
|
||||
setget: function(stateName, value, noEvent){
|
||||
$SM.set(stateName, value, noEvent);
|
||||
return eval('('+$SM.buildPath(stateName)+')');
|
||||
},
|
||||
|
||||
|
||||
remove: function(stateName, noEvent) {
|
||||
var whichState = $SM.buildPath(stateName);
|
||||
try{
|
||||
@@ -190,21 +191,21 @@ var StateManager = {
|
||||
$SM.fireUpdate(stateName);
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
//creates full reference from input
|
||||
//hopefully this won't ever need to be more complicated
|
||||
buildPath: function(input){
|
||||
var dot = (input.charAt(0) == '[')? '' : '.'; //if it starts with [foo] no dot to join
|
||||
return 'State' + dot + input;
|
||||
},
|
||||
|
||||
|
||||
fireUpdate: function(stateName, save){
|
||||
var category = $SM.getCategory(stateName);
|
||||
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});
|
||||
if(save) Engine.saveGame();
|
||||
},
|
||||
|
||||
|
||||
getCategory: function(stateName){
|
||||
var firstOB = stateName.indexOf('[');
|
||||
var firstDot = stateName.indexOf('.');
|
||||
@@ -220,7 +221,7 @@ var StateManager = {
|
||||
return stateName.substr(0,cutoff);
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
//Use this function to make old save games compatible with new version
|
||||
updateOldState: function(){
|
||||
var version = $SM.get('version');
|
||||
@@ -299,7 +300,7 @@ var StateManager = {
|
||||
$SM.set('version', 1.3);
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
/******************************************************************
|
||||
* Start of specific state functions
|
||||
******************************************************************/
|
||||
@@ -308,11 +309,11 @@ var StateManager = {
|
||||
$SM.set('character.perks["'+name+'"]', true);
|
||||
Notifications.notify(null, Engine.Perks[name].notify);
|
||||
},
|
||||
|
||||
|
||||
hasPerk: function(name) {
|
||||
return $SM.get('character.perks["'+name+'"]');
|
||||
},
|
||||
|
||||
|
||||
//INCOME
|
||||
setIncome: function(source, options) {
|
||||
var existing = $SM.get('income["'+source+'"]');
|
||||
@@ -321,7 +322,7 @@ var StateManager = {
|
||||
}
|
||||
$SM.set('income["'+source+'"]', options);
|
||||
},
|
||||
|
||||
|
||||
getIncome: function(source) {
|
||||
var existing = $SM.get('income["'+source+'"]');
|
||||
if(typeof existing != 'undefined') {
|
||||
@@ -329,7 +330,7 @@ var StateManager = {
|
||||
}
|
||||
return {};
|
||||
},
|
||||
|
||||
|
||||
collectIncome: function() {
|
||||
var changed = false;
|
||||
if(typeof $SM.get('income') != 'undefined' && Engine.activeModule != Space) {
|
||||
@@ -340,21 +341,21 @@ var StateManager = {
|
||||
income.timeLeft = 0;
|
||||
}
|
||||
income.timeLeft--;
|
||||
|
||||
|
||||
if(income.timeLeft <= 0) {
|
||||
Engine.log('collection income from ' + source);
|
||||
if(source == 'thieves') $SM.addStolen(income.stores);
|
||||
if(source == 'thieves') $SM.addStolen(income.stores);
|
||||
|
||||
var cost = income.stores;
|
||||
var ok = true;
|
||||
if (source != 'thieves') {
|
||||
for (var k in cost) {
|
||||
var have = $SM.get('stores["' + k + '"]', true);
|
||||
if (have + cost[k] < 0) {
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (var k in cost) {
|
||||
var have = $SM.get('stores["' + k + '"]', true);
|
||||
if (have + cost[k] < 0) {
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(ok){
|
||||
@@ -372,7 +373,7 @@ var StateManager = {
|
||||
}
|
||||
Engine._incomeTimeout = setTimeout($SM.collectIncome, 1000);
|
||||
},
|
||||
|
||||
|
||||
//Thieves
|
||||
addStolen: function(stores) {
|
||||
for(var k in stores) {
|
||||
@@ -386,7 +387,7 @@ var StateManager = {
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
startThieves: function() {
|
||||
$SM.set('game.thieves', 1);
|
||||
$SM.setIncome('thieves', {
|
||||
@@ -398,7 +399,7 @@ var StateManager = {
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
|
||||
//Misc
|
||||
num: function(name, craftable) {
|
||||
switch(craftable.type) {
|
||||
@@ -411,10 +412,10 @@ var StateManager = {
|
||||
return $SM.get('game.buildings["'+name+'"]', true);
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
handleStateUpdates: function(e){
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
//alias
|
||||
|
||||
Reference in New Issue
Block a user