dreamcode.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright 2018, 2024 UltrasonicMadness
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. const INTERVAL_LENGTH = 60000; // 60000 ms = 1 minute
  17. // Keeping track of how far through this Easter egg the user is.
  18. let dreamStates =
  19. {
  20. STARTED: 0,
  21. GRAY: 1,
  22. SEPIA: 2,
  23. SILENT: 3,
  24. SILENT_BUTTON1: 4,
  25. MUSIC: 5,
  26. MUSIC_BUTTON1: 6,
  27. MESSAGE: 7,
  28. MESSAGE2: 8,
  29. FINISHED: 9
  30. };
  31. let keysPressed; // Keeps track of which keys have been pressed.
  32. let interval; // Used to keep track of .setInterval so that it can be cleared.
  33. let time = 0; // How many minutes the program has been running
  34. let currentState = dreamStates.STARTED;
  35. const dreamcodeGray = new Event("dreamcodeGray");
  36. const dreamcodeSepia = new Event("dreamcodeSepia");
  37. const dreamcodeSilent = new Event("dreamcodeSilent");
  38. const dreamcodeMusic = new Event("dreamcodeMusic");
  39. const dreamcodeMessage = new Event("dreamcodeMessage");
  40. const dreamcodeMessage2 = new Event("dreamcodeMessage2");
  41. const dreamcodeFinished = new Event("dreamcodeFinished");
  42. function initKeys() // Resets logged key presses to false
  43. {
  44. keysPressed =
  45. {
  46. ArrowUp: false,
  47. ArrowDown: false,
  48. ArrowLeft: false,
  49. ArrowRight: false,
  50. a: false,
  51. b: false,
  52. Enter: false,
  53. Shift: false,
  54. };
  55. }
  56. // This function is run at an interval; it increments the time variable and
  57. // triggers events depending on how long the program has been running
  58. function incrementMinutes()
  59. {
  60. time++;
  61. // Unless noted, all these do is change the state of the program.
  62. if (time >= 91 && currentState == dreamStates.SEPIA)
  63. {
  64. initKeys();
  65. currentState = dreamStates.SILENT;
  66. dispatchEvent(dreamcodeSilent);
  67. time = 0; // Reset the counter
  68. window.clearTimeout(interval); // Don't run this function anymore
  69. }
  70. else if (time >= 36 && currentState == dreamStates.GRAY)
  71. {
  72. initKeys();
  73. currentState = dreamStates.SEPIA;
  74. dispatchEvent(dreamcodeSepia);
  75. }
  76. else if (time >= 18 && currentState == dreamStates.STARTED)
  77. {
  78. initKeys();
  79. currentState = dreamStates.GRAY;
  80. dispatchEvent(dreamcodeGray);
  81. }
  82. }
  83. function incrementMinutesEnd()
  84. {
  85. time++;
  86. // After 18 runs of this function, trigger dreamcodeFinished and
  87. // stop running the program.
  88. if (time >= 18)
  89. {
  90. currentState = dreamStates.FINISHED;
  91. dispatchEvent(dreamcodeFinished);
  92. time = 0;
  93. window.clearTimeout(interval);
  94. }
  95. }
  96. addEventListener("DOMContentLoaded", function()
  97. {
  98. initKeys();
  99. addEventListener("keydown", function(event)
  100. {
  101. let key = event.key;
  102. if (key === "A" || key === "B" ) {
  103. key = key.toLowerCase();
  104. }
  105. if (key in keysPressed === false) {
  106. return;
  107. }
  108. keysPressed[key] = true;
  109. switch(currentState)
  110. {
  111. case dreamStates.SILENT:
  112. // After pressing A, B, return, shift and left
  113. if (keysPressed["a"] &&
  114. keysPressed["b"] &&
  115. keysPressed["Enter"] &&
  116. keysPressed["Shift"] &&
  117. keysPressed["ArrowLeft"])
  118. {
  119. currentState = dreamStates.SILENT_BUTTON1;
  120. initKeys();
  121. }
  122. break;
  123. case dreamStates.SILENT_BUTTON1:
  124. // After pressing A, B and right
  125. if (keysPressed["a"] &&
  126. keysPressed["b"] &&
  127. keysPressed["ArrowRight"])
  128. {
  129. currentState = dreamStates.MUSIC;
  130. initKeys();
  131. dispatchEvent(dreamcodeMusic);
  132. }
  133. break;
  134. case dreamStates.MUSIC:
  135. // After pressing B, shift and right
  136. if (keysPressed["b"] &&
  137. keysPressed["Shift"] &&
  138. keysPressed["ArrowRight"])
  139. {
  140. currentState = dreamStates.MUSIC_BUTTON1;
  141. initKeys();
  142. }
  143. break;
  144. case dreamStates.MUSIC_BUTTON1:
  145. // After pressing B, right and down
  146. if (keysPressed["b"] &&
  147. keysPressed["ArrowRight"] &&
  148. keysPressed["ArrowDown"])
  149. {
  150. currentState = dreamStates.MESSAGE;
  151. initKeys();
  152. dispatchEvent(dreamcodeMessage);
  153. }
  154. break;
  155. case dreamStates.MESSAGE:
  156. // After pressing A, B and up
  157. if (keysPressed["a"] &&
  158. keysPressed["b"] &&
  159. keysPressed["ArrowUp"])
  160. {
  161. currentState = dreamStates.MESSAGE2;
  162. initKeys();
  163. dispatchEvent(dreamcodeMessage2);
  164. window.clearTimeout(interval);
  165. interval = window.setInterval(incrementMinutesEnd, INTERVAL_LENGTH);
  166. }
  167. break;
  168. }
  169. });
  170. // Keep track of how long the program has been running
  171. interval = window.setInterval(incrementMinutes, INTERVAL_LENGTH);
  172. });