a munch adventure
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

88 lines
2.1 KiB

  1. stories = [];
  2. function initGame(story, state) {
  3. state.info = {};
  4. state.info.time = 60 * 60 * 9;
  5. state.player.stats = {};
  6. state.player.stats.health = 100;
  7. state.timers = [];
  8. state.timers.global = new Set();
  9. }
  10. // TODO: format string this lol
  11. function renderTime(time) {
  12. let hours = Math.floor(time / 3600) % 12;
  13. const ampm = Math.floor(time / 3600) % 24 < 12 ? "AM" : "PM";
  14. let minutes = Math.floor(time / 60) % 60;
  15. let seconds = time % 60;
  16. if (minutes <= 9)
  17. minutes = "0" + minutes;
  18. if (seconds <= 9)
  19. seconds = "0" + seconds;
  20. return hours + ":" + minutes + ":" + seconds + " " + ampm;
  21. }
  22. function updateWorldInfo(state) {
  23. const timeInfo = document.querySelector("#world-info-time");
  24. timeInfo.textContent = "Time: " + renderTime(state.info.time);
  25. }
  26. function updatePlayerInfo(state) {
  27. const health = document.querySelector("#player-info-health");
  28. health.textContent = "Health: " + state.player.stats.health;
  29. }
  30. /*
  31. {
  32. id: an optional name; needed to manually kill a timer
  33. func: the function to invoke
  34. delay: how long to wait between invocations
  35. loop: false = no looping, true = loop forever
  36. room: the room associated with the timer
  37. }
  38. Returns the timeout id - but you still need to cancel it through stopTimer!
  39. */
  40. function startTimer(config, state) {
  41. if (config.loop) {
  42. const timeout = setTimeout(() => {
  43. config.func();
  44. state.timers = state.timers.filter(x => x.timeout != timeout);
  45. startTimer(config, state);
  46. }, config.delay);
  47. state.timers.push({id: config.id, timeout: timeout, room: config.room});
  48. return timeout;
  49. }
  50. }
  51. function stopTimer(id, state) {
  52. const matches = state.timers.filter(timer => timer.id == id);
  53. matches.forEach(timer => clearTimeout(timer.timeout));
  54. state.timers = state.timers.filter(timer => timer.id != id);
  55. }
  56. function stopRoomTimers(room, state) {
  57. const matches = state.timers.filter(timer => timer.room == room);
  58. matches.forEach(timer => clearTimeout(timer.timeout));
  59. state.timers = state.timers.filter(timer => timer.room != room);
  60. }
  61. function stopAllTimers(state) {
  62. state.timers.forEach(x => clearTimeout(x.timeout));
  63. state.timers = [];
  64. }