Bladeren bron

Timers can be associated with rooms or ids now

tags/v0.1.0
Fen Dweller 6 jaren geleden
bovenliggende
commit
bae70b9663
2 gewijzigde bestanden met toevoegingen van 32 en 8 verwijderingen
  1. +32
    -2
      game.js
  2. +0
    -6
      satiate.js

+ 32
- 2
game.js Bestand weergeven

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

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

state.timers = [];

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

// 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
delay: how long to wait between invocations
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) {
if (config.loop) {
const timeout = setTimeout(() => {
config.func();
state.timers.global.delete(timeout);
state.timers = state.timers.filter(x => x.timeout != timeout);
startTimer(config, state);
}, 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 Bestand weergeven

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

}
},
timers: {
room: {

},
global: new Set()
}
};

initWorld(story, state);


Laden…
Annuleren
Opslaan