Browse Source

Timers can be associated with rooms or ids now

tags/v0.1.0
Fen Dweller 6 years ago
parent
commit
bae70b9663
2 changed files with 32 additions and 8 deletions
  1. +32
    -2
      game.js
  2. +0
    -6
      satiate.js

+ 32
- 2
game.js View File

@@ -6,6 +6,10 @@ function initGame(story, state) {


state.player.stats = {}; state.player.stats = {};
state.player.stats.health = 100; state.player.stats.health = 100;

state.timers = [];

state.timers.global = new Set();
} }


// TODO: format string this lol // TODO: format string this lol
@@ -38,20 +42,46 @@ function updatePlayerInfo(state) {


/* /*
{ {
id: an optional name; needed to manually kill a timer
func: the function to invoke func: the function to invoke
delay: how long to wait between invocations delay: how long to wait between invocations
loop: false = no looping, true = loop forever loop: false = no looping, true = loop forever
room: the room associated with the timer
} }


Returns the timeout id - but you still need to cancel it through stopTimer!

*/ */
function startTimer(config, state) { function startTimer(config, state) {
if (config.loop) { if (config.loop) {
const timeout = setTimeout(() => { const timeout = setTimeout(() => {
config.func(); config.func();
state.timers.global.delete(timeout);
state.timers = state.timers.filter(x => x.timeout != timeout);
startTimer(config, state); startTimer(config, state);
}, config.delay); }, config.delay);


state.timers.global.add(timeout);
state.timers.push({id: config.id, timeout: timeout, room: config.room});

return timeout;
} }
} }

function stopTimer(id, state) {
const matches = state.timers.filter(timer => timer.id == id);
matches.forEach(timer => clearTimeout(timer.timeout));

state.timers = state.timers.filter(timer => timer.id != id);
}

function stopRoomTimers(room, state) {
const matches = state.timers.filter(timer => timer.room == room);
matches.forEach(timer => clearTimeout(timer.timeout));

state.timers = state.timers.filter(timer => timer.room != room);

}

function stopAllTimers(state) {
state.timers.forEach(x => clearTimeout(x.timeout));
state.timers = [];
}

+ 0
- 6
satiate.js View File

@@ -69,12 +69,6 @@ function init(story) {


} }
}, },
timers: {
room: {

},
global: new Set()
}
}; };


initWorld(story, state); initWorld(story, state);


Loading…
Cancel
Save