scaleOne.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /******************************
  2. * This file holds game states.
  3. ******************************/
  4. /** [GAME STATE]
  5. *
  6. * ...
  7. *
  8. * @namespace
  9. */
  10. const scaleOne = {
  11. control: undefined,
  12. scale: undefined,
  13. icicles: undefined,
  14. icicleFractions: undefined,
  15. gameIcicle: undefined,
  16. /**
  17. * Main code
  18. */
  19. create: function () {
  20. this.control = {
  21. scale: {
  22. x0: (context.canvas.width / 3) * 2,
  23. y0: context.canvas.height - 128,
  24. move: {
  25. center: 0,
  26. left: -15,
  27. right: 15,
  28. },
  29. coordinates: undefined,
  30. },
  31. icicles: {
  32. x0: 100,
  33. y0: 197,
  34. coordinates: undefined,
  35. fractions: [
  36. // { nom: 1, denom: 2 },
  37. // { nom: 1, denom: 3 },
  38. // { nom: 1, denom: 4 },
  39. { nom: 1, denom: 2 },
  40. { nom: 1, denom: 3 },
  41. { nom: 1, denom: 4 },
  42. ],
  43. },
  44. };
  45. this.control.scale.coordinates = {
  46. base: {
  47. x: this.control.scale.x0,
  48. y: this.control.scale.y0,
  49. },
  50. top: {
  51. x: this.control.scale.x0,
  52. y: this.control.scale.y0 - 170,
  53. },
  54. plate_left: {
  55. x: this.control.scale.x0 - 258,
  56. y: this.control.scale.y0 - 200,
  57. },
  58. plate_right: {
  59. x: this.control.scale.x0 + 258,
  60. y: this.control.scale.y0 - 200,
  61. },
  62. };
  63. this.control.icicles.coordinates = [
  64. // { x: this.control.icicles.x0, y: this.control.icicles.y0 },
  65. // { x: this.control.icicles.x0 + 160, y: this.control.icicles.y0 },
  66. // {
  67. // x: this.control.icicles.x0 + 160 * 2,
  68. // y: this.control.icicles.y0,
  69. // },
  70. {
  71. x: this.control.icicles.x0,
  72. y: this.control.icicles.y0 + 160 * 1.5,
  73. },
  74. {
  75. x: this.control.icicles.x0 + 160,
  76. y: this.control.icicles.y0 + 160 * 1.5,
  77. },
  78. {
  79. x: this.control.icicles.x0 + 160 * 2,
  80. y: this.control.icicles.y0 + 160 * 1.5,
  81. },
  82. ];
  83. this.scale = {
  84. base: null,
  85. top: null,
  86. plate_left: null,
  87. plate_right: null,
  88. };
  89. this.icicles = [];
  90. this.icicleFractions = [];
  91. renderBackground('scale');
  92. // FOR MOODLE
  93. if (moodle) {
  94. navigation.add.right(['audio']);
  95. } else {
  96. navigation.add.left(['back', 'menu', 'show_answer'], 'customMenu');
  97. navigation.add.right(['audio']);
  98. }
  99. this.utils.render.scale();
  100. this.utils.render.icicles();
  101. this.gameIcicle = game.add.sprite(
  102. this.scale.plate_left.x,
  103. this.scale.plate_left.y - 50,
  104. 'floor_snow',
  105. 2,
  106. 2
  107. );
  108. this.gameIcicle.anchor(0.5, 1);
  109. this.gameIcicleFraction = game.add.text(
  110. this.gameIcicle.x,
  111. this.gameIcicle.y - 50,
  112. '1/2',
  113. textStyles.h2_
  114. );
  115. this.utils.animate.moveScale('left');
  116. // this.counter = 0;
  117. game.timer.start(); // Set a timer for the current level (used in postScore())
  118. game.event.add('click', this.onInputDown);
  119. game.event.add('mousemove', this.onInputOver);
  120. },
  121. /**
  122. * Game loop
  123. */
  124. update: function () {
  125. // self.counter++;
  126. game.render.all();
  127. },
  128. utils: {
  129. render: {
  130. scale: () => {
  131. // base of the scale
  132. self.scale.base = game.add.image(
  133. self.control.scale.coordinates.base.x,
  134. self.control.scale.coordinates.base.y,
  135. 'scale_base',
  136. 2
  137. );
  138. // top of the scale
  139. self.scale.top = game.add.image(
  140. self.control.scale.coordinates.top.x,
  141. self.control.scale.coordinates.top.y,
  142. 'scale_arm',
  143. 2
  144. );
  145. self.scale.top.rotate = self.control.scale.rotate;
  146. // left plate
  147. self.scale.plate_left = game.add.image(
  148. self.control.scale.coordinates.plate_left.x,
  149. self.control.scale.coordinates.plate_left.y,
  150. 'scale_plate',
  151. 2
  152. );
  153. // right plate
  154. self.scale.plate_right = game.add.image(
  155. self.control.scale.coordinates.plate_right.x,
  156. self.control.scale.coordinates.plate_right.y,
  157. 'scale_plate',
  158. 2
  159. );
  160. for (let item in self.scale) {
  161. if (self.scale[item] !== null) self.scale[item].anchor(0.5, 1);
  162. }
  163. },
  164. icicles: () => {
  165. for (let i = 0; i < self.control.icicles.coordinates.length; i++) {
  166. const x = self.control.icicles.coordinates[i].x;
  167. const y = self.control.icicles.coordinates[i].y;
  168. const icicle = game.add.sprite(x, y, 'floor_snow', 2, 2);
  169. self.icicles.push(icicle);
  170. const fractionTop = game.add.text(
  171. x + 65,
  172. y + 120,
  173. '1',
  174. textStyles.h2_
  175. );
  176. const fractionLine = game.add.text(
  177. x + 65,
  178. y + 120 + 3,
  179. '__',
  180. textStyles.h2_
  181. );
  182. const fractionBottom = game.add.text(
  183. x + 65,
  184. y + 120 * 1.4,
  185. '2',
  186. textStyles.h2_
  187. );
  188. self.icicleFractions.push({
  189. fractionTop,
  190. fractionLine,
  191. fractionBottom,
  192. });
  193. }
  194. },
  195. },
  196. animate: {
  197. moveScale: (type) => {
  198. const leftY = self.control.scale.coordinates.plate_left.y;
  199. const rightY = self.control.scale.coordinates.plate_right.y;
  200. let leftOffset = (rightOffset = 0);
  201. let icicleOffset = 50;
  202. switch (type) {
  203. case 'left':
  204. self.scale.top.rotate = self.control.scale.move.left;
  205. leftOffset = 80;
  206. rightOffset = -40;
  207. break;
  208. case 'right':
  209. self.scale.top.rotate = self.control.scale.move.right;
  210. leftOffset = -40;
  211. rightOffset = 80;
  212. break;
  213. case 'center':
  214. self.scale.top.rotate = self.control.scale.move.center;
  215. leftOffset = 0;
  216. rightOffset = 0;
  217. break;
  218. default:
  219. console.error('Game error: could not render scale animation.');
  220. }
  221. self.scale.plate_left.y = leftY + leftOffset;
  222. self.scale.plate_right.y = rightY + rightOffset;
  223. self.gameIcicle.y = leftY + leftOffset - icicleOffset;
  224. self.gameIcicleFraction.y = leftY + leftOffset - icicleOffset * 2;
  225. },
  226. },
  227. /**
  228. * Display correct answer
  229. */
  230. showAnswer: function () {},
  231. },
  232. events: {
  233. /**
  234. * Function called by self.onInputOver() when cursor is over a valid rectangle
  235. *
  236. * @param {object} cur rectangle the cursor is over
  237. */
  238. overIcicleHandler: function (cur) {
  239. // for (let i in self.icicles) {
  240. // if (i === cur.index) {
  241. // self.icicles[i].scale = 1.2;
  242. // }
  243. // }
  244. },
  245. /**
  246. * Called by mouse click event
  247. *
  248. * @param {object} mouseEvent contains the mouse click coordinates
  249. */
  250. onInputDown: function (mouseEvent) {
  251. // const x = game.math.getMouse(mouseEvent).x;
  252. // const y = game.math.getMouse(mouseEvent).y;
  253. // navigationIcons.onInputDown(x, y);
  254. // game.render.all();
  255. },
  256. /**
  257. * Called by mouse move event
  258. *
  259. * @param {object} mouseEvent contains the mouse move coordinates
  260. */
  261. onInputOver: function (mouseEvent) {
  262. // const x = game.math.getMouse(mouseEvent).x;
  263. // const y = game.math.getMouse(mouseEvent).y;
  264. // self.icicles.forEach((icicle) => {
  265. // if (game.math.isOverIcon(x, y, icicle)) {
  266. // self.overIcicleHandler(icicle);
  267. // }
  268. // });
  269. // navigationIcons.onInputOver(x, y);
  270. // game.render.all();
  271. },
  272. },
  273. fetch: {
  274. /**
  275. * Saves players data after level ends - to be sent to database <br>
  276. *
  277. * Attention: the 'line_' prefix data table must be compatible to data table fields (MySQL server)
  278. *
  279. * @see /php/save.php
  280. */
  281. postScore: function () {},
  282. },
  283. };