From 2018ca3ae89ccba206831dbcac29cd5c3b7e60ad Mon Sep 17 00:00:00 2001 From: LucidCrux Date: Wed, 24 Jul 2013 18:27:04 -0600 Subject: [PATCH] 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 --- script/engine.js | 31 +++++++++++++++++++++++++---- script/events.js | 13 ++++++------ script/outside.js | 15 +++++++------- script/path.js | 14 ++++++------- script/room.js | 21 ++++++++++---------- script/ship.js | 12 +++++------ script/space.js | 10 +++++----- script/state_manager.js | 44 ++++++++++++++++++++++++++++------------- script/world.js | 14 ++++++------- 9 files changed, 105 insertions(+), 69 deletions(-) diff --git a/script/engine.js b/script/engine.js index bf643c3..db3be3a 100644 --- a/script/engine.js +++ b/script/engine.js @@ -7,9 +7,12 @@ 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': { @@ -112,6 +115,9 @@ var Engine = { swipeElement.on('swiperight', Engine.swipeRight); swipeElement.on('swipeup', Engine.swipeUp); swipeElement.on('swipedown', Engine.swipeDown); + + //subscribe to stateUpdates + $.Dispatch('stateUpdate').subscribe(Engine.handleStateUpdates); $SM.init(); Notifications.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(); diff --git a/script/events.js b/script/events.js index e76af55..f8eb520 100644 --- a/script/events.js +++ b/script/events.js @@ -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); \ No newline at end of file + } +}; \ No newline at end of file diff --git a/script/outside.js b/script/outside.js index bee2027..31764ef 100644 --- a/script/outside.js +++ b/script/outside.js @@ -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); \ No newline at end of file + } +}; \ No newline at end of file diff --git a/script/path.js b/script/path.js index 8153e64..22dde15 100644 --- a/script/path.js +++ b/script/path.js @@ -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); \ No newline at end of file + } +}; \ No newline at end of file diff --git a/script/room.js b/script/room.js index cb89384..fc588bf 100644 --- a/script/room.js +++ b/script/room.js @@ -487,6 +487,9 @@ var Room = { // Create the stores container $('
').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); \ No newline at end of file + } +}; \ No newline at end of file diff --git a/script/ship.js b/script/ship.js index 202dde7..00b1a4e 100644 --- a/script/ship.js +++ b/script/ship.js @@ -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); \ No newline at end of file + } +}; \ No newline at end of file diff --git a/script/space.js b/script/space.js index 9752340..1003506 100644 --- a/script/space.js +++ b/script/space.js @@ -41,6 +41,9 @@ var Space = { var h = $('
').attr('id', 'hullRemaining').appendTo(this.panel); $('
').addClass('row_key').text('hull: ').appendTo(h); $('
').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); \ No newline at end of file + } +}; \ No newline at end of file diff --git a/script/state_manager.js b/script/state_manager.js index 25c4b7d..3871ca9 100644 --- a/script/state_manager.js +++ b/script/state_manager.js @@ -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); \ No newline at end of file +var $SM = StateManager; \ No newline at end of file diff --git a/script/world.js b/script/world.js index 45e174f..0fe9ba0 100644 --- a/script/world.js +++ b/script/world.js @@ -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 = { $('
').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); \ No newline at end of file + } +}; \ No newline at end of file