|
- stories = [];
-
- function initGame(story, state) {
- state.info = {};
- state.info.time = 60 * 60 * 9;
-
- state.player.stats = {};
- state.player.stats.health = 100;
-
- state.timers = [];
- }
-
- // TODO: format string this lol
- function renderTime(time) {
- let hours = Math.floor(time / 3600) % 12;
- const ampm = Math.floor(time / 3600) % 24 < 12 ? "AM" : "PM";
- let minutes = Math.floor(time / 60) % 60;
- let seconds = time % 60;
-
- if (minutes <= 9)
- minutes = "0" + minutes;
-
- if (seconds <= 9)
- seconds = "0" + seconds;
-
- return hours + ":" + minutes + ":" + seconds + " " + ampm;
- }
-
- function updateWorldInfo(state) {
- const timeInfo = document.querySelector("#world-info-time");
-
- timeInfo.textContent = "Time: " + renderTime(state.info.time);
- }
-
- function updatePlayerInfo(state) {
- const health = document.querySelector("#player-info-health");
-
- health.textContent = "Health: " + state.player.stats.health;
- }
-
- /*
- {
- 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(() => {
- const result = config.func(state);
- state.timers = state.timers.filter(x => x.timeout != timeout);
- refresh();
-
- if (result)
- startTimer(config, state);
- }, config.delay);
-
- 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 = [];
- }
|