|
- stories = [];
-
- function initGame(story, state) {
- state.info = {};
- state.info.time = 60 * 60 * 9;
-
- state.player.stats = {};
-
- state.timers = [];
- }
-
- function initGamePostSetup(state) {
- const holder = document.querySelector("#player-info");
-
- holder.innerHTML = "";
-
- Object.entries(state.player.stats).forEach(([key, val]) => {
-
- if (val.type == "meter") {
- const field = document.createElement("div");
- field.id = "player-info-" + key;
- field.setAttribute("max", val.max);
- field.setAttribute("value", val.value);
- field.classList.add("stat-bar-holder");
-
- const label = document.createElement("div");
- label.classList.add("stat-bar-label");
- label.textContent = val.name;
-
- const bar = document.createElement("div");
- bar.classList.add("stat-bar");
- bar.style["background-color"] = val.color;
-
- field.appendChild(label);
- field.appendChild(bar);
-
- holder.appendChild(field);
- }
-
- });
- }
-
- function changeStat(stat, amount, state) {
- let value = state.player.stats[stat].value;
- value += amount;
- value = Math.max(value, state.player.stats[stat].min);
- value = Math.min(value, state.player.stats[stat].max);
- state.player.stats[stat].value = value;
- }
-
- // 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) {
- Object.entries(state.player.stats).forEach(([key, val]) => {
-
- if (val.type == "meter") {
- const field = document.querySelector("#player-info-" + key + " > .stat-bar");
- field.style.width = (val.value / val.max * 100) + "%";
- }
-
- });
- }
-
- /*
- {
- 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, config);
- refresh();
-
- // the timer may have terminated itself!
- // we have to make sure it still exists
-
- if (state.timers.some(x => x.timeout == timeout)){
- state.timers = state.timers.filter(x => x.timeout != timeout);
- if (typeof(result) === "number") {
- config.delay = result;
- }
-
- // you shouldn't use a delay of 0 anyway
- if (result) {
- startTimer(config, state);
- }
-
- }
-
- }, config.delay);
-
- state.timers.push({id: config.id, timeout: timeout, room: config.room, classes: config.classes || []});
-
- 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 stopClassTimers(timerClass, state, inverse) {
- const matches = state.timers.filter(timer => timer.classes.includes(timerClass));
- const others = state.timers.filter(timer => !timer.classes.includes(timerClass));
-
- if (inverse) {
- others.forEach(timer => clearTimeout(timer.timeout));
- state.timers = matches;
- } else {
- matches.forEach(timer => clearTimeout(timer.timeout));
- state.timers = others;
- }
- }
-
- function stopAllTimers(state) {
- state.timers.forEach(x => clearTimeout(x.timeout));
- state.timers = [];
- }
-
- function setBackgroundColor(r, g, b) {
- document.querySelector(".scene").style["background-color"] = "rgb(" + r + "," + g + "," + b + ")";
-
- }
|