a munch adventure
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

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