Check for delayed events

This triggers onload for all those events whose (possible) reward is registered with a time delay.
This commit is contained in:
Andrea Rendine
2015-05-09 02:17:42 +02:00
committed by Blake Grotewold
parent cb717b41d0
commit da0d39b579
4 changed files with 126 additions and 34 deletions
+10
View File
@@ -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){ setTimeout: function(callback, timeout, skipDouble){
if( Engine.options.doubleTime && !skipDouble ){ if( Engine.options.doubleTime && !skipDouble ){
+55 -5
View File
@@ -31,10 +31,14 @@ var Events = {
//subscribe to stateUpdates //subscribe to stateUpdates
$.Dispatch('stateUpdate').subscribe(Events.handleStateUpdates); $.Dispatch('stateUpdate').subscribe(Events.handleStateUpdates);
//check for stored delayed events
Events.initDelay();
}, },
options: {}, // Nothing for now options: {}, // Nothing for now
delayState: 'wait',
activeScene: null, activeScene: null,
loadScene: function(name) { loadScene: function(name) {
@@ -42,6 +46,13 @@ var Events = {
Events.activeScene = name; Events.activeScene = name;
var scene = Events.activeEvent().scenes[name]; var scene = Events.activeEvent().scenes[name];
// Scene reward
if(scene.reward) {
$SM.addM('stores', scene.reward);
}
// onLoad // onLoad
if(scene.onLoad) { if(scene.onLoad) {
scene.onLoad(); scene.onLoad();
@@ -52,11 +63,6 @@ var Events = {
Notifications.notify(null, scene.notification); Notifications.notify(null, scene.notification);
} }
// Scene reward
if(scene.reward) {
$SM.addM('stores', scene.reward);
}
$('#description', Events.eventPanel()).empty(); $('#description', Events.eventPanel()).empty();
$('#buttons', Events.eventPanel()).empty(); $('#buttons', Events.eventPanel()).empty();
if(scene.combat) { if(scene.combat) {
@@ -884,5 +890,49 @@ var Events = {
if((e.category == 'stores' || e.category == 'income') && Events.activeEvent() != null){ if((e.category == 'stores' || e.category == 'income') && Events.activeEvent() != null){
Events.updateButtons(); 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);
} }
}; };
+44 -28
View File
@@ -273,15 +273,15 @@ Events.Room = [
notification: _('a mysterious wanderer arrives'), notification: _('a mysterious wanderer arrives'),
blink: true, blink: true,
buttons: { buttons: {
'100wood': { 'wood100': {
text: _('give 100'), text: _('give 100'),
cost: {wood: 100}, cost: {wood: 100},
nextScene: { 1: '100wood'} nextScene: { 1: 'wood100'}
}, },
'500wood': { 'wood500': {
text: _('give 500'), text: _('give 500'),
cost: {wood: 500}, cost: {wood: 500},
nextScene: { 1: '500wood' } nextScene: { 1: 'wood500' }
}, },
'deny': { 'deny': {
text: _('turn him away'), text: _('turn him away'),
@@ -289,16 +289,20 @@ Events.Room = [
} }
} }
}, },
'100wood': { 'wood100': {
text: [ text: [
_('the wanderer leaves, cart loaded with wood') _('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() { onLoad: function() {
if(Math.random() < 0.5) { if(Math.random() < 0.5) {
Engine.setTimeout(function() { this.action(60);
$SM.add('stores.wood', 300);
Notifications.notify(Room, _('the mysterious wanderer returns, cart piled high with wood.'));
}, 60 * 1000);
} }
}, },
buttons: { buttons: {
@@ -308,16 +312,20 @@ Events.Room = [
} }
} }
}, },
'500wood': { 'wood500': {
text: [ text: [
_('the wanderer leaves, cart loaded with wood') _('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() { onLoad: function() {
if(Math.random() < 0.3) { if(Math.random() < 0.3) {
Engine.setTimeout(function() { this.action(60);
$SM.add('stores.wood', 1500);
Notifications.notify(Room, _('the mysterious wanderer returns, cart piled high with wood.'));
}, 60 * 1000);
} }
}, },
buttons: { buttons: {
@@ -344,15 +352,15 @@ Events.Room = [
notification: _('a mysterious wanderer arrives'), notification: _('a mysterious wanderer arrives'),
blink: true, blink: true,
buttons: { buttons: {
'100fur': { 'fur100': {
text: _('give 100'), text: _('give 100'),
cost: {fur: 100}, cost: {fur: 100},
nextScene: { 1: '100fur'} nextScene: { 1: 'fur100'}
}, },
'500fur': { 'fur500': {
text: _('give 500'), text: _('give 500'),
cost: {fur: 500}, cost: {fur: 500},
nextScene: { 1: '500fur' } nextScene: { 1: 'fur500' }
}, },
'deny': { 'deny': {
text: _('turn her away'), text: _('turn her away'),
@@ -360,16 +368,20 @@ Events.Room = [
} }
} }
}, },
'100fur': { 'fur100': {
text: [ text: [
_('the wanderer leaves, cart loaded with furs') _('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() { onLoad: function() {
if(Math.random() < 0.5) { if(Math.random() < 0.5) {
Engine.setTimeout(function() { this.action(60);
$SM.add('stores.fur', 300);
Notifications.notify(Room, _('the mysterious wanderer returns, cart piled high with furs.'));
}, 60 * 1000);
} }
}, },
buttons: { buttons: {
@@ -379,16 +391,20 @@ Events.Room = [
} }
} }
}, },
'500fur': { 'fur500': {
text: [ text: [
_('the wanderer leaves, cart loaded with furs') _('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() { onLoad: function() {
if(Math.random() < 0.3) { if(Math.random() < 0.3) {
Engine.setTimeout(function() { this.action(60);
$SM.add('stores.fur', 1500);
Notifications.notify(Room, _('the mysterious wanderer returns, cart piled high with furs.'));
}, 60 * 1000);
} }
}, },
buttons: { buttons: {
+17 -1
View File
@@ -37,7 +37,8 @@ var StateManager = {
'playStats', //anything play related: play time, loads, etc 'playStats', //anything play related: play time, loads, etc
'previous', // prestige, score, trophies (in future), achievements (again, not yet), 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 '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) { 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 //creates full reference from input
//hopefully this won't ever need to be more complicated //hopefully this won't ever need to be more complicated
buildPath: function(input){ buildPath: function(input){