From da0d39b579e766150f29cb8ea6583b5411476199 Mon Sep 17 00:00:00 2001 From: Andrea Rendine Date: Sat, 9 May 2015 02:17:42 +0200 Subject: [PATCH] Check for delayed events This triggers onload for all those events whose (possible) reward is registered with a time delay. --- script/engine.js | 10 ++++++ script/events.js | 60 +++++++++++++++++++++++++++++++--- script/events/room.js | 72 +++++++++++++++++++++++++---------------- script/state_manager.js | 18 ++++++++++- 4 files changed, 126 insertions(+), 34 deletions(-) diff --git a/script/engine.js b/script/engine.js index 05c9b3a..a644318 100644 --- a/script/engine.js +++ b/script/engine.js @@ -725,6 +725,16 @@ } }, + setInterval: function(callback, interval, skipDouble){ + if( Engine.options.doubleTime && !skipDouble ){ + Engine.log('Double time, cutting interval in half'); + interval /= 2; + } + + return setInterval(callback, interval); + + }, + setTimeout: function(callback, timeout, skipDouble){ if( Engine.options.doubleTime && !skipDouble ){ diff --git a/script/events.js b/script/events.js index 1c08ff7..566bc17 100644 --- a/script/events.js +++ b/script/events.js @@ -31,10 +31,14 @@ var Events = { //subscribe to stateUpdates $.Dispatch('stateUpdate').subscribe(Events.handleStateUpdates); + + //check for stored delayed events + Events.initDelay(); }, options: {}, // Nothing for now + delayState: 'wait', activeScene: null, loadScene: function(name) { @@ -42,6 +46,13 @@ var Events = { Events.activeScene = name; var scene = Events.activeEvent().scenes[name]; + // Scene reward + if(scene.reward) { + $SM.addM('stores', scene.reward); + } + + + // onLoad if(scene.onLoad) { scene.onLoad(); @@ -52,11 +63,6 @@ var Events = { Notifications.notify(null, scene.notification); } - // Scene reward - if(scene.reward) { - $SM.addM('stores', scene.reward); - } - $('#description', Events.eventPanel()).empty(); $('#buttons', Events.eventPanel()).empty(); if(scene.combat) { @@ -884,5 +890,49 @@ var Events = { if((e.category == 'stores' || e.category == 'income') && Events.activeEvent() != null){ Events.updateButtons(); } + }, + + initDelay: function(){ + if($SM.get(Events.delayState)){ + Events.recallDelay(Events.delayState, Events); + } + }, + + recallDelay: function(stateName, target){ + var state = $SM.get(stateName); + for(var i in state){ + if(typeof(state[i]) == 'object'){ + Events.recallDelay(stateName +'["'+ i +'"]', target[i]); + } else { + if(typeof target[i] == 'function'){ + target[i](); + } else { + $SM.remove(stateName) + } + } + } + if($.isEmptyObject(state)){ + $SM.remove(stateName); + } + }, + + saveDelay: function(action, stateName, delay){ + var state = Events.delayState + '.' + stateName; + if(delay){ + $SM.set(state, delay); + } else { + var delay = $SM.get(state, true) + } + var time = Engine.setInterval(function(){ + // update state every half second + $SM.set(state, ($SM.get(state) - 0.5), true); + }, 500); + Engine.setTimeout(function(){ + // outcome realizes. erase countdown + window.clearInterval(time); + $SM.remove(state); + $SM.removeBranch(Events.delayState); + action(); + }, delay * 1000); } }; diff --git a/script/events/room.js b/script/events/room.js index a3d98ce..82635f9 100644 --- a/script/events/room.js +++ b/script/events/room.js @@ -273,15 +273,15 @@ Events.Room = [ notification: _('a mysterious wanderer arrives'), blink: true, buttons: { - '100wood': { + 'wood100': { text: _('give 100'), cost: {wood: 100}, - nextScene: { 1: '100wood'} + nextScene: { 1: 'wood100'} }, - '500wood': { + 'wood500': { text: _('give 500'), cost: {wood: 500}, - nextScene: { 1: '500wood' } + nextScene: { 1: 'wood500' } }, 'deny': { text: _('turn him away'), @@ -289,16 +289,20 @@ Events.Room = [ } } }, - '100wood': { + 'wood100': { text: [ _('the wanderer leaves, cart loaded with wood') ], + action: function(delay) { + var delay = delay || false; + Events.saveDelay(function() { + $SM.add('stores.wood', 300); + Notifications.notify(Room, _('the mysterious wanderer returns, cart piled high with wood.')); + }, 'Room[4].scenes.wood100.action', delay); + }, onLoad: function() { if(Math.random() < 0.5) { - Engine.setTimeout(function() { - $SM.add('stores.wood', 300); - Notifications.notify(Room, _('the mysterious wanderer returns, cart piled high with wood.')); - }, 60 * 1000); + this.action(60); } }, buttons: { @@ -308,16 +312,20 @@ Events.Room = [ } } }, - '500wood': { + 'wood500': { text: [ _('the wanderer leaves, cart loaded with wood') ], + action: function(delay) { + var delay = delay || false; + Events.saveDelay(function() { + $SM.add('stores.wood', 1500); + Notifications.notify(Room, _('the mysterious wanderer returns, cart piled high with wood.')); + }, 'Room[4].scenes.wood500.action', delay); + }, onLoad: function() { if(Math.random() < 0.3) { - Engine.setTimeout(function() { - $SM.add('stores.wood', 1500); - Notifications.notify(Room, _('the mysterious wanderer returns, cart piled high with wood.')); - }, 60 * 1000); + this.action(60); } }, buttons: { @@ -344,15 +352,15 @@ Events.Room = [ notification: _('a mysterious wanderer arrives'), blink: true, buttons: { - '100fur': { + 'fur100': { text: _('give 100'), cost: {fur: 100}, - nextScene: { 1: '100fur'} + nextScene: { 1: 'fur100'} }, - '500fur': { + 'fur500': { text: _('give 500'), cost: {fur: 500}, - nextScene: { 1: '500fur' } + nextScene: { 1: 'fur500' } }, 'deny': { text: _('turn her away'), @@ -360,16 +368,20 @@ Events.Room = [ } } }, - '100fur': { + 'fur100': { text: [ _('the wanderer leaves, cart loaded with furs') ], + action: function(delay) { + var delay = delay || false; + Events.saveDelay(function() { + $SM.add('stores.wood', 300); + Notifications.notify(Room, _('the mysterious wanderer returns, cart piled high with furs.')); + }, 'Room[5].scenes.fur100.action', delay); + }, onLoad: function() { if(Math.random() < 0.5) { - Engine.setTimeout(function() { - $SM.add('stores.fur', 300); - Notifications.notify(Room, _('the mysterious wanderer returns, cart piled high with furs.')); - }, 60 * 1000); + this.action(60); } }, buttons: { @@ -379,16 +391,20 @@ Events.Room = [ } } }, - '500fur': { + 'fur500': { text: [ _('the wanderer leaves, cart loaded with furs') ], + action: function(delay) { + var delay = delay || false; + Events.saveDelay(function() { + $SM.add('stores.wood', 1500); + Notifications.notify(Room, _('the mysterious wanderer returns, cart piled high with furs.')); + }, 'Room[5].scenes.fur500.action', delay); + }, onLoad: function() { if(Math.random() < 0.3) { - Engine.setTimeout(function() { - $SM.add('stores.fur', 1500); - Notifications.notify(Room, _('the mysterious wanderer returns, cart piled high with furs.')); - }, 60 * 1000); + this.action(60); } }, buttons: { diff --git a/script/state_manager.js b/script/state_manager.js index 2c4590f..12282fa 100644 --- a/script/state_manager.js +++ b/script/state_manager.js @@ -37,7 +37,8 @@ var StateManager = { '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' + 'config', + 'wait' // mysterious wanderers are coming back ]; for(var which in cats) { @@ -192,6 +193,21 @@ var StateManager = { } }, + removeBranch(stateName, noEvent) { + for(var i in $SM.get(stateName)){ + if(typeof $SM.get(stateName)[i] == 'object'){ + $SM.removeBranch(stateName +'["'+ i +'"]'); + } + } + if($.isEmptyObject($SM.get(stateName))){ + $SM.remove(stateName); + } + if(!noEvent){ + Engine.saveGame(); + $SM.fireUpdate(stateName); + } + }, + //creates full reference from input //hopefully this won't ever need to be more complicated buildPath: function(input){