Skip to content

Commit

Permalink
Check for delayed events
Browse files Browse the repository at this point in the history
This triggers onload for all those events whose (possible) reward is registered with a time delay.
  • Loading branch information
AndySky21 authored and groteworld committed Jun 27, 2015
1 parent cb717b4 commit da0d39b
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 34 deletions.
10 changes: 10 additions & 0 deletions script/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ){
Expand Down
60 changes: 55 additions & 5 deletions script/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,28 @@ 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) {
Engine.log('loading scene: ' + name);
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();
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
};
72 changes: 44 additions & 28 deletions script/events/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,32 +273,36 @@ 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'),
nextScene: 'end'
}
}
},
'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: {
Expand All @@ -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: {
Expand All @@ -344,32 +352,36 @@ 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'),
nextScene: 'end'
}
}
},
'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: {
Expand All @@ -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: {
Expand Down
18 changes: 17 additions & 1 deletion script/state_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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){
Expand Down

0 comments on commit da0d39b

Please sign in to comment.