core.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. var canvas;
  2. var context;
  3. var lastTime;
  4. var updateIntervalId;
  5. var gameTicks;
  6. var camera = { x: 0, y: 0, center: { x: 0, y: 0 }, movementSpeed: 3 };
  7. var isDragging = false;
  8. var dragCompleted = false;
  9. var startDrag = { x: 0, y: 0 };
  10. var dragDelta = { x: 0, y: 0 };
  11. var keys = [];
  12. var sound = new MusicNotes();
  13. var states = [];
  14. states.push(new MenuScene());
  15. states.push(new OverworldScene());
  16. states.push(new PuzzleScene());
  17. states.push({
  18. init: () => {
  19. document.getElementById("game").style.display = "none";
  20. document.getElementById("endgame").style.display = "block";
  21. deleteGame();
  22. },
  23. update: (delta) => {
  24. },
  25. draw: (context) => {
  26. }
  27. });
  28. var activeState = states[0];
  29. var saveData = {
  30. player: { rail: 0, railnode: 0 },
  31. camera: { x: 1280, y: 1280 },
  32. unlocked: [],
  33. mute: false,
  34. };
  35. var puzzleRules = { width: 0, height: 0, successRail: 0, successNode: 0 };
  36. function animate() {
  37. context.save();
  38. context.translate(0.5, 0.5);
  39. draw(context);
  40. context.restore();
  41. requestAnimationFrame(animate);
  42. }
  43. function clearFrame(context) {
  44. context.clearRect(0, 0, canvas.width, canvas.height);
  45. }
  46. function init() {
  47. lastTime = new Date().getUTCMilliseconds();
  48. keys = [];
  49. for (let i = 0; i < 256; i++) {
  50. keys[i] = false;
  51. }
  52. gameTicks = 0;
  53. changeState(0);
  54. }
  55. function changeState(id) {
  56. document.getElementById("debugger-output").innerHTML = "";
  57. try {
  58. states[id].init();
  59. } catch (e) {
  60. console.error("Something went wrong while changing state:", e);
  61. document.getElementById("debugger-output").innerHTML = e.toString();
  62. return;
  63. }
  64. activeState = states[id];
  65. onResize();
  66. }
  67. function update(delta) {
  68. activeState.update(delta);
  69. }
  70. function draw(context) {
  71. functionExists(activeState, "clearFrame") ? activeState.clearFrame(context) : clearFrame(context);
  72. activeState.draw(context);
  73. }
  74. function functionExists(obj, method) {
  75. return typeof obj[method] == "function";
  76. }
  77. function saveGame() {
  78. localStorage.setItem("eyeofmidas404", JSON.stringify(saveData));
  79. }
  80. function loadGame() {
  81. let saveString = localStorage.getItem("eyeofmidas404");
  82. if (!saveString) {
  83. saveGame();
  84. saveString = JSON.stringify(saveData);
  85. }
  86. saveData = JSON.parse(saveString);
  87. }
  88. function deleteGame() {
  89. localStorage.removeItem("eyeofmidas404");
  90. }
  91. function onResize() {
  92. let dpi = window.devicePixelRatio;
  93. canvas.width = canvas.clientWidth;
  94. canvas.height = canvas.clientHeight;
  95. context = canvas.getContext("2d");
  96. context.width = canvas.clientWidth / dpi;
  97. context.height = canvas.clientHeight / dpi;
  98. functionExists(activeState, "onResize") ? activeState.onResize() : null;
  99. }
  100. function onKeyDown(event) {
  101. keys[event.keyCode] = true;
  102. functionExists(activeState, "onKeyDown") ? activeState.onKeyDown(event) : null;
  103. }
  104. function onKeyUp(event) {
  105. functionExists(activeState, "onKeyUp") ? activeState.onKeyUp(event) : null;
  106. keys[event.keyCode] = false;
  107. }
  108. function onMouseMove(event) {
  109. functionExists(activeState, "onMouseMove") ? activeState.onMouseMove(event) : null;
  110. }
  111. function onMouseDown(event) {
  112. functionExists(activeState, "onMouseDown") ? activeState.onMouseDown(event) : null;
  113. }
  114. function onMouseUp(event) {
  115. functionExists(activeState, "onMouseUp") ? activeState.onMouseUp(event) : null;
  116. }
  117. function onRightClick(event) {
  118. functionExists(activeState, "onRightClick") ? activeState.onRightClick(event) : null;
  119. }
  120. function onTouchStart(event) {
  121. functionExists(activeState, "onTouchStart") ? activeState.onTouchStart(event) : null;
  122. }
  123. function onTouchMove(event) {
  124. functionExists(activeState, "onTouchMove") ? activeState.onTouchMove(event) : null;
  125. }
  126. function onTouchEnd(event) {
  127. functionExists(activeState, "onTouchEnd") ? activeState.onTouchEnd(event) : null;
  128. }
  129. document.addEventListener("DOMContentLoaded", event => {
  130. canvas = document.createElement("canvas");
  131. document.getElementById("game").appendChild(canvas);
  132. init();
  133. animate();
  134. updateIntervalId = setInterval(() => {
  135. let currentTime = new Date().getUTCMilliseconds();
  136. update(1 + ((currentTime - lastTime) / 1000));
  137. lastTime = currentTime;
  138. }, 16);
  139. });
  140. window.addEventListener("resize", onResize);
  141. window.addEventListener("keydown", onKeyDown);
  142. window.addEventListener("keyup", onKeyUp);
  143. window.addEventListener("mousedown", onMouseDown);
  144. window.addEventListener("mousemove", onMouseMove);
  145. window.addEventListener("mouseup", onMouseUp);
  146. window.addEventListener("contextmenu", onRightClick);
  147. window.addEventListener("touchstart", onTouchStart);
  148. window.addEventListener("touchmove", onTouchMove);
  149. window.addEventListener("touchend", onTouchEnd);