mirror of
https://github.com/doublespeakgames/adarkroom.git
synced 2026-05-28 08:11:54 +08:00
create and configure callback handlers for state updates
add $.Dispatch to handle object events add subscribe calls to files update SM fireUpdate to use new Dispatch There are some NaN issues right now, no time to bug hunt right now, it does kind of function though
This commit is contained in:
+27
-4
@@ -7,10 +7,13 @@ var Engine = {
|
||||
* That would be so elegant and awesome.
|
||||
*/
|
||||
SITE_URL: encodeURIComponent("http://adarkroom.doublespeakgames.com"),
|
||||
VERSION: 1.2;
|
||||
VERSION: 1.2,
|
||||
MAX_STORE: 99999999999999,
|
||||
SAVE_DISPLAY: 30 * 1000,
|
||||
|
||||
//object event types
|
||||
topics: {},
|
||||
|
||||
Perks: {
|
||||
'boxer': {
|
||||
desc: 'punches do more damage',
|
||||
@@ -113,6 +116,9 @@ var Engine = {
|
||||
swipeElement.on('swipeup', Engine.swipeUp);
|
||||
swipeElement.on('swipedown', Engine.swipeDown);
|
||||
|
||||
//subscribe to stateUpdates
|
||||
$.Dispatch('stateUpdate').subscribe(Engine.handleStateUpdates);
|
||||
|
||||
$SM.init();
|
||||
Notifications.init();
|
||||
Events.init();
|
||||
@@ -165,6 +171,7 @@ var Engine = {
|
||||
Engine.log("loaded save!");
|
||||
}
|
||||
} catch(e) {
|
||||
State = {};
|
||||
$SM.set('verson', Engine.VERSION);
|
||||
Engine.event('progress', 'new game');
|
||||
}
|
||||
@@ -384,11 +391,27 @@ var Engine = {
|
||||
|
||||
handleStateUpdates: function(e){
|
||||
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
//listener for StateManager update events
|
||||
$(Engine).on('stateUpdate', Engine.handleStateUpdates);
|
||||
//create jQuery Callbacks() to handle object events
|
||||
$.Dispatch = function( id ) {
|
||||
var callbacks,
|
||||
method,
|
||||
topic = id && Engine.topics[ id ];
|
||||
if ( !topic ) {
|
||||
callbacks = jQuery.Callbacks();
|
||||
topic = {
|
||||
publish: callbacks.fire,
|
||||
subscribe: callbacks.add,
|
||||
unsubscribe: callbacks.remove
|
||||
};
|
||||
if ( id ) {
|
||||
Engine.topics[ id ] = topic;
|
||||
}
|
||||
}
|
||||
return topic;
|
||||
};
|
||||
|
||||
$(function() {
|
||||
Engine.init();
|
||||
|
||||
+5
-6
@@ -26,6 +26,9 @@ var Events = {
|
||||
Events.eventStack = [];
|
||||
|
||||
Events.scheduleNextEvent();
|
||||
|
||||
//subscribe to stateUpdates
|
||||
$.Dispatch('stateUpdate').subscribe(Events.handleStateUpdates);
|
||||
},
|
||||
|
||||
options: {}, // Nothing for now
|
||||
@@ -806,12 +809,8 @@ var Events = {
|
||||
},
|
||||
|
||||
handleStateUpdates: function(e){
|
||||
//updates to run on stores changes if an event is active
|
||||
if(e.stateName.indexOf('stores') == 0 && Events.activeEvent() != null){
|
||||
if(e.category == 'stores' && Events.activeEvent() != null){
|
||||
Events.updateButtons();
|
||||
}
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
//listener for StateManager update events
|
||||
$(Events).on('stateUpdate', Events.handleStateUpdates);
|
||||
+6
-7
@@ -135,8 +135,11 @@ var Outside = {
|
||||
.addClass('location')
|
||||
.appendTo('div#locationSlider');
|
||||
|
||||
//subscribe to stateUpdates
|
||||
$.Dispatch('stateUpdate').subscribe(Outside.handleStateUpdates);
|
||||
|
||||
if(typeof $SM.get('features.location.outside') == 'undefined') {
|
||||
$SM.set('features.location.outside', true);
|
||||
$SM.set('features.location.outside');
|
||||
$SM.setM('game.outside', {
|
||||
buildings: {},
|
||||
population: 0,
|
||||
@@ -631,12 +634,8 @@ var Outside = {
|
||||
},
|
||||
|
||||
handleStateUpdates: function(e){
|
||||
//updates to run on stores changes
|
||||
if(e.stateName.indexOf('stores') == 0){
|
||||
if(e.category == 'stores'){
|
||||
Outside.updateVillage();
|
||||
}
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
//listener for StateManager update events
|
||||
$(Outside).on('stateUpdate', Outside.handleStateUpdates);
|
||||
+6
-6
@@ -49,6 +49,9 @@ var Path = {
|
||||
Path.outfit = {};
|
||||
|
||||
Engine.updateSlider();
|
||||
|
||||
//subscribe to stateUpdates
|
||||
$.Dispatch('stateUpdate').subscribe(Path.handleStateUpdates);
|
||||
},
|
||||
|
||||
openPath: function() {
|
||||
@@ -131,7 +134,7 @@ var Path = {
|
||||
armour = "steel";
|
||||
else if($SM.get('stores[\'i armour\']', true) > 0)
|
||||
armour = "iron";
|
||||
else if($SM.get('stores[\'l armour']', true) > 0)
|
||||
else if($SM.get('stores[\'l armour\']', true) > 0)
|
||||
armour = "leather";
|
||||
var aRow = $('#armourRow');
|
||||
if(aRow.length == 0) {
|
||||
@@ -301,11 +304,8 @@ var Path = {
|
||||
},
|
||||
|
||||
handleStateUpdates: function(e){
|
||||
if(e.stateName.indexOf('character.perks') == 0 && Engine.activeModule == Path){
|
||||
if(e.category == 'character' && e.stateName.indexOf('character.perks') == 0 && Engine.activeModule == Path){
|
||||
Path.updatePerks();
|
||||
};
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
//listener for StateManager update events
|
||||
$(Path).on('stateUpdate', Path.handleStateUpdates);
|
||||
+9
-10
@@ -487,6 +487,9 @@ var Room = {
|
||||
// Create the stores container
|
||||
$('<div>').attr('id', 'storesContainer').appendTo('div#roomPanel');
|
||||
|
||||
//subscribe to stateUpdates
|
||||
$.Dispatch('stateUpdate').subscribe(Room.handleStateUpdates);
|
||||
|
||||
Room.updateButton();
|
||||
Room.updateStoresView();
|
||||
Room.updateIncomeView();
|
||||
@@ -606,7 +609,7 @@ var Room = {
|
||||
_fireTimer: null,
|
||||
_tempTimer: null,
|
||||
lightFire: function() {
|
||||
var wood = $SM.get('stores.wood', true);
|
||||
var wood = $SM.get('stores.wood');
|
||||
if(wood < 5) {
|
||||
Notifications.notify(Room, "not enough wood to get the fire going");
|
||||
Button.clearCooldown($('#lightButton.button'));
|
||||
@@ -619,7 +622,7 @@ var Room = {
|
||||
},
|
||||
|
||||
stokeFire: function() {
|
||||
var wood = $SM.get('stores.wood', true);
|
||||
var wood = $SM.get('stores.wood');
|
||||
if(wood === 0) {
|
||||
Notifications.notify(Room, "the wood has run out");
|
||||
Button.clearCooldown($('#stokeButton.button'));
|
||||
@@ -956,7 +959,7 @@ var Room = {
|
||||
return false;
|
||||
}
|
||||
for(var c in cost) {
|
||||
if(!$SM.get('stores[\''+c'\']')) {
|
||||
if(!$SM.get('stores[\''+c+'\']')) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1082,15 +1085,11 @@ var Room = {
|
||||
},
|
||||
|
||||
handleStateUpdates: function(e){
|
||||
//updates to run on stores changes
|
||||
if(e.stateName.indexOf('stores') == 0){
|
||||
if(e.category == 'stores'){
|
||||
Room.updateStoresView();
|
||||
Room.updateBuildButtons();
|
||||
} else if(e.stateName.indexOf('income') == 0){
|
||||
} else if(e.category == 'income'){
|
||||
Room.updateIncomeView();
|
||||
};
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
//listener for StateManager update events
|
||||
$(Room).on('stateUpdate', Room.handleStateUpdates);
|
||||
+5
-5
@@ -16,7 +16,7 @@ var Ship = {
|
||||
);
|
||||
|
||||
if(!$SM.get('features.location.spaceShip')) {
|
||||
$SM.set('features.location.spaceShip', true);
|
||||
$SM.set('features.location.spaceShip');
|
||||
$SM.setM('game.spaceShip', {
|
||||
hull: Ship.BASE_HULL,
|
||||
thrusters: Ship.BASE_THRUSTERS
|
||||
@@ -78,6 +78,9 @@ var Ship = {
|
||||
|
||||
// Init Space
|
||||
Space.init();
|
||||
|
||||
//subscribe to stateUpdates
|
||||
$.Dispatch('stateUpdate').subscribe(Ship.handleStateUpdates);
|
||||
},
|
||||
|
||||
options: {}, // Nothing for now
|
||||
@@ -168,8 +171,5 @@ var Ship = {
|
||||
|
||||
handleStateUpdates: function(e){
|
||||
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
//listener for StateManager update events
|
||||
$(Ship).on('stateUpdate', Ship.handleStateUpdates);
|
||||
+4
-4
@@ -41,6 +41,9 @@ var Space = {
|
||||
var h = $('<div>').attr('id', 'hullRemaining').appendTo(this.panel);
|
||||
$('<div>').addClass('row_key').text('hull: ').appendTo(h);
|
||||
$('<div>').addClass('row_val').appendTo(h);
|
||||
|
||||
//subscribe to stateUpdates
|
||||
$.Dispatch('stateUpdate').subscribe(Space.handleStateUpdates);
|
||||
},
|
||||
|
||||
options: {}, // Nothing for now
|
||||
@@ -449,8 +452,5 @@ var Space = {
|
||||
|
||||
handleStateUpdates: function(e){
|
||||
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
//listener for StateManager update events
|
||||
$(Space).on('stateUpdate', Space.handleStateUpdates);
|
||||
+29
-13
@@ -40,6 +40,9 @@ var StateManager = {
|
||||
for(var which in cats) {
|
||||
if(!$SM.get(cats[which])) $SM.set(cats[which], {});
|
||||
};
|
||||
|
||||
//subscribe to stateUpdates
|
||||
$.Dispatch('stateUpdate').subscribe($SM.handleStateUpdates);
|
||||
},
|
||||
|
||||
//create the parent of a given state, recursive as needed
|
||||
@@ -129,8 +132,8 @@ var StateManager = {
|
||||
//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){
|
||||
//check for NaN (old != old) and non number values
|
||||
if(old != old){
|
||||
Engine.log('WARNING: '+stateName+' was corrupted (NaN). Resetting to 0.');
|
||||
old = 0;
|
||||
$SM.set(stateName, old + value, noEvent);//setState handles event and save
|
||||
@@ -200,17 +203,33 @@ var StateManager = {
|
||||
return 'State' + dot + input;
|
||||
},
|
||||
|
||||
|
||||
|
||||
fireUpdate: function(stateName, save){
|
||||
if(stateName == undefined) stateName = 'all'; //best if this doesn't happen as it will trigger more stuff
|
||||
$.event.trigger({
|
||||
'type': 'stateUpdate',
|
||||
'stateName': stateName,
|
||||
});
|
||||
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})
|
||||
//$.event.trigger({
|
||||
// type: "stateUpdate",
|
||||
// 'stateName': stateName
|
||||
//});
|
||||
if(save) Engine.saveGame();
|
||||
},
|
||||
|
||||
getCategory: function(stateName){
|
||||
var firstOB = stateName.indexOf('[');
|
||||
var firstDot = stateName.indexOf('.');
|
||||
var cutoff = null;
|
||||
if(firstOB == -1 || firstDot == -1){
|
||||
cutoff = firstOB > firstDot ? firstOB : firstDot;
|
||||
} else {
|
||||
cutoff = firstOB < firstDot ? firstOB : firstDot;
|
||||
}
|
||||
if (cutoff == -1){
|
||||
return stateName;
|
||||
} else {
|
||||
return stateName.substr(0,cutoff);
|
||||
}
|
||||
},
|
||||
|
||||
//Use this function to make old save games compatible with new version
|
||||
updateOldState: function(){
|
||||
var version = $SM.get('version');
|
||||
@@ -329,11 +348,8 @@ var StateManager = {
|
||||
|
||||
handleStateUpdates: function(e){
|
||||
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
//alias
|
||||
var $SM = StateManager;
|
||||
|
||||
//listener for StateManager update events
|
||||
$(StateManager).on('stateUpdate', $SM.handleStateUpdates);
|
||||
+6
-6
@@ -129,7 +129,7 @@ var World = {
|
||||
World.LANDMARKS[World.TILE.SWAMP] = {num: 1, minRadius: 15, maxRadius: World.RADIUS * 1.5, scene: 'swamp', label: 'A Murky Swamp'};
|
||||
|
||||
if(typeof $SM.get('features.location.world') == 'undefined') {
|
||||
$SM.set('features.location.world', true);
|
||||
$SM.set('features.location.world');
|
||||
$SM.setM('game.world', {
|
||||
map: World.generateMap(),
|
||||
mask: World.newMask()
|
||||
@@ -149,6 +149,9 @@ var World = {
|
||||
$('<div>').attr('id', 'healthCounter').appendTo(outer);
|
||||
|
||||
Engine.updateOuterSlider();
|
||||
|
||||
//subscribe to stateUpdates
|
||||
$.Dispatch('stateUpdate').subscribe(World.handleStateUpdates);
|
||||
},
|
||||
|
||||
clearDungeon: function() {
|
||||
@@ -870,7 +873,7 @@ var World = {
|
||||
return World.BASE_HEALTH + 35;
|
||||
} else if($SM.get('stores[\'i armour\']', true) > 0) {
|
||||
return World.BASE_HEALTH + 15;
|
||||
} else if($SM.get('stores[\'l armour']', true) > 0) {
|
||||
} else if($SM.get('stores[\'l armour\']', true) > 0) {
|
||||
return World.BASE_HEALTH + 5;
|
||||
}
|
||||
return World.BASE_HEALTH;
|
||||
@@ -940,8 +943,5 @@ var World = {
|
||||
|
||||
handleStateUpdates: function(e){
|
||||
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
//listener for StateManager update events
|
||||
$(World).on('stateUpdate', World.handleStateUpdates);
|
||||
Reference in New Issue
Block a user