globals_control.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /**************************************************************
  2. * LInE - Free Education, Private Data - http://www.usp.br/line
  3. *
  4. * This file holds all global elements to the game.
  5. *
  6. * Generating game levels in menu:
  7. * .....................................................
  8. * ...............square....................circle...... } = gameShape
  9. * .........../...........\....................|........ } = gameName (game)
  10. * ........One.............Two................One....... }
  11. * ......./...\.........../...\............./....\......
  12. * ......a.....b.........a.....b...........a......b..... = gameMode (game mode)
  13. * .(floor)..(stack)..(top)..(bottom)..(floor)..(stack).
  14. * .......\./.............\./................\./........
  15. * ........|...............|..................|.........
  16. * ......./.\..............|................/.|.\.......
  17. * ...plus...minus.......minus........plus.minus.mixed. = gameOperation (game math operation)
  18. * .......\./..............|................\.|./.......
  19. * ........|...............|..................|.........
  20. * ......1,2,3.........1,2,3,4,5..........1,2,3,4,5..... = gameDifficulty (difficulty level)
  21. * .....................................................
  22. *
  23. * About levels in map:
  24. *
  25. * ..................(game.levels)......................
  26. * ......................__|__..........................
  27. * .....................|.|.|.|.........................
  28. * ...................0,1,2,3,4,5....................... = curMapPosition (map positions)
  29. * ...................|.........|.......................
  30. * ................(start)....(end).....................
  31. **************************************************************/
  32. /** FOR MOODLE <br>
  33. *
  34. * iFractions can run on a server or inside moodle through iAssign. <br>
  35. * This variable should be set according to where it is suposed to run: <br>
  36. * - if true, on moodle <br>
  37. * - if false, on a server
  38. */
  39. const moodle = false;
  40. /**
  41. * Index of the current game in gameList array
  42. *
  43. * @type {number}
  44. */
  45. let gameId;
  46. /**
  47. * Selected game name.<br>
  48. * Can be: 'squareOne', 'squareTwo' or 'circleOne'.
  49. *
  50. * @type {string}
  51. */
  52. let gameName;
  53. /**
  54. * Used for text and game information.<br>
  55. * Shape that makes the name of the game - e.g in 'squareOne' it is 'square'.<br>
  56. * Can be: 'circle' or 'square'.
  57. *
  58. * @type {string}
  59. */
  60. let gameShape;
  61. /**
  62. * Holds selected game mode.<br>
  63. * In squareOne/circleOne can be: 'a' (click on the floor) or 'b' (click on the amount to go/stacked figures).<br>
  64. * In squareTwo can be: 'a' (more subdivisions on top) or 'b' (more subdivisions on bottom).
  65. *
  66. * @type {string}
  67. */
  68. let gameMode;
  69. /**
  70. * Holds game math operation.<br>
  71. * In squareOne can be: 'plus' (green tractor goes right) or 'minus' (red tractor goes left).<br>
  72. * In circleOne can be: 'plus' (green tractor goes right), 'minus' (red tractor goes left) or 'mixed' (green tractor goes both sides).<br>
  73. * In squareTwo can be: 'minus' (compares two rectangle subdivisions).
  74. *
  75. * @type {string}
  76. */
  77. let gameOperation;
  78. /**
  79. * Holds game overall difficulty. 1 (easier) -> n (harder).<br>
  80. * In squareOne can be: 1..3.<br>
  81. * In circleOne/squareTwo can be: 1..5.
  82. *
  83. * @type {number}
  84. */
  85. let gameDifficulty;
  86. /**
  87. * Turns displaying the fraction labels on levels ON/OFF
  88. * @type {boolean}
  89. */
  90. let fractionLabel = true;
  91. /**
  92. * When true, the character can move to next position in the map
  93. * @type {boolean}
  94. */
  95. let canGoToNextMapPosition;
  96. /**
  97. * Character position on the map, aka game levels (1..4: valid; 5: end)
  98. * @type {number}
  99. */
  100. let curMapPosition;
  101. /**
  102. * Number of finished levels in the map
  103. * @type {number}
  104. */
  105. let completedLevels;
  106. /**
  107. * Turns game audio ON/OFF
  108. * @type {boolean}
  109. */
  110. let audioStatus = false;
  111. /**
  112. * Player's name
  113. * @type {string}
  114. */
  115. let playerName;
  116. /**
  117. * String that contains the selected language.<br>
  118. * It is the name of the language file.
  119. * @type {string}
  120. */
  121. let langString;
  122. /**
  123. * Holds the current state.<br>
  124. * Is used as if it was a 'this' inside state functions.
  125. * @type {object}
  126. */
  127. let self;
  128. /**
  129. * Information for all the games
  130. * @type {Array}
  131. */
  132. const gameList = [
  133. {
  134. gameName: 'squareOne',
  135. gameMode: ['a', 'b'],
  136. gameOperation: ['plus', 'minus'],
  137. gameDifficulty: 3,
  138. gameShape: 'square',
  139. assets: {
  140. menu: {
  141. gameNameBtn: 'game_0',
  142. infoBox: () => ({
  143. title: `<strong>${game.lang.game}:</strong> ${game.lang.square} I`,
  144. body: `<ul>${game.lang.infoBox_squareOne}</ul>`,
  145. img: `<img class="ifr-infoBox__menu__img" src="${game.image['s1-A'].src}">`,
  146. }),
  147. },
  148. customMenu: {
  149. gameModeBtn: ['mode_0', 'mode_1'],
  150. gameOperationBtn: ['operation_plus', 'operation_minus'],
  151. infoBox: () => ({
  152. gameMode: {
  153. title: game.lang.game_modes,
  154. body: `<p>${game.lang.infoBox_mode}</p>`,
  155. img: `<table>
  156. <tr>
  157. <td><b>${game.lang.mode}: A</b> - ${game.lang.infoBox_mode_s1_A}</td>
  158. <td><b>${game.lang.mode}: B</b> - ${game.lang.infoBox_mode_s1_B}</td>
  159. </tr>
  160. <tr>
  161. <td><img width=100% src="${game.image['s1-A-h'].src}"></td>
  162. <td><img width=100% src="${game.image['s1-B-h'].src}"></td>
  163. </tr>
  164. <table>`,
  165. },
  166. gameDifficulty: {
  167. title: game.lang.difficulties,
  168. body: `<p>${game.lang.infoBox_diff}</p><p>${game.lang.infoBox_diff_obs}</p>`,
  169. img: `<table>
  170. <tr>
  171. <td><b>${game.lang.difficulty}:</b> 1</td>
  172. <td><b>${game.lang.difficulty}:</b> 3</td>
  173. </tr>
  174. <tr>
  175. <td><img width=100% src="${game.image['s1-diff-1'].src}"></td>
  176. <td style="border-left: 4px solid white"><img width=100% src="${game.image['s1-diff-3'].src}"></td>
  177. </tr>
  178. </table>
  179. <br>
  180. ${game.lang.infoBox_diff_aux}
  181. <div><img width=50% src="${game.image['map-s1'].src}"></div>`,
  182. },
  183. gameMisc: {
  184. title: `${game.lang.show} ${self.auxText}`,
  185. body: game.lang.infoBox_misc_label,
  186. img: `<img class="mx-auto" width=80% src="${game.image['s1-label'].src}">`,
  187. },
  188. }),
  189. },
  190. map: {
  191. characterAnimation: (operation) => {
  192. return operation === 'plus'
  193. ? ['green_tractor', [0, 1, 2, 3, 4], 3]
  194. : ['red_tractor', [10, 11, 12, 13, 14], 3];
  195. },
  196. character: (operation) => {
  197. let char;
  198. if (operation == 'plus') {
  199. char = game.add.sprite(
  200. self.scene.roadPoints.x[curMapPosition],
  201. self.scene.roadPoints.y[curMapPosition],
  202. 'tractor',
  203. 0,
  204. 0.75
  205. );
  206. }
  207. if (operation === 'minus') {
  208. char = game.add.sprite(
  209. self.scene.roadPoints.x[curMapPosition],
  210. self.scene.roadPoints.y[curMapPosition],
  211. 'tractor',
  212. 10,
  213. 0.75
  214. );
  215. }
  216. char.rotate = -30; // 25 anticlock
  217. char.anchor(0.25, 0.5);
  218. return char;
  219. },
  220. startBuilding: () => {
  221. return game.add.image(
  222. self.scene.roadPoints.x[0] - 60,
  223. self.scene.roadPoints.y[0] - 155,
  224. 'garage',
  225. 0.6
  226. );
  227. },
  228. endBuilding: () => {
  229. return game.add.image(
  230. self.scene.roadPoints.x[5] - 10,
  231. self.scene.roadPoints.y[5] - 215,
  232. 'farm',
  233. 0.9
  234. );
  235. },
  236. },
  237. end: {
  238. characterAnimation: (animation) =>
  239. animation === 'plus'
  240. ? ['move', [0, 1, 2, 3, 4], 4]
  241. : ['move', [10, 11, 12, 13, 14], 4],
  242. character: (operation) => {
  243. const char = game.add.sprite(
  244. 0,
  245. context.canvas.height - 170,
  246. 'tractor',
  247. 0,
  248. 1.05
  249. );
  250. char.anchor(0.5, 0.5);
  251. if (operation === 'plus') char.curFrame = 10;
  252. return char;
  253. },
  254. building: () =>
  255. game.add.image(
  256. context.canvas.width - 400,
  257. context.canvas.height - 400,
  258. 'farm',
  259. 1.155
  260. ),
  261. },
  262. },
  263. },
  264. {
  265. gameName: 'circleOne',
  266. gameMode: ['a', 'b'],
  267. gameOperation: ['plus', 'minus', 'mixed'],
  268. gameDifficulty: 5,
  269. // info
  270. gameShape: 'circle',
  271. assets: {
  272. menu: {
  273. gameNameBtn: 'game_1',
  274. infoBox: () => ({
  275. title: `<strong>${game.lang.game}:</strong> ${game.lang.circle} I`,
  276. body: `<ul>${game.lang.infoBox_circleOne}</ul>`,
  277. img: `<img class="mx-auto" width=60% src="${game.image['c1-A'].src}">`,
  278. }),
  279. },
  280. customMenu: {
  281. gameModeBtn: ['mode_2', 'mode_3'],
  282. gameOperationBtn: [
  283. 'operation_plus',
  284. 'operation_minus',
  285. 'operation_mixed',
  286. ],
  287. infoBox: () => ({
  288. gameMode: {
  289. title: game.lang.game_modes,
  290. body: `<p>${game.lang.infoBox_mode}</p>`,
  291. img: `<table>
  292. <tr style="border-bottom: 5px solid white">
  293. <td width=70%>
  294. <img width=100% src=${game.image['c1-A-h'].src}>
  295. </td>
  296. <td>&nbsp;<b>${game.lang.mode}: A</b> - ${game.lang.infoBox_mode_c1_A}</td>
  297. </tr>
  298. <tr>
  299. <td><img width=100% src=${game.image['c1-B-h'].src}></td>
  300. <td>&nbsp;<b>${game.lang.mode}: B</b> - ${game.lang.infoBox_mode_c1_B}</td>
  301. </tr>
  302. </table>`,
  303. },
  304. gameDifficulty: {
  305. title: game.lang.difficulties,
  306. body: `<p>${game.lang.infoBox_diff}</p><p>${game.lang.infoBox_diff_obs}</p>`,
  307. img: `<table>
  308. <tr>
  309. <td style="border-right: 4px solid white">
  310. <b>${game.lang.difficulty}:</b> 1
  311. </td>
  312. <td>
  313. <b>${game.lang.difficulty}:</b> 5
  314. </td>
  315. </tr>
  316. <tr>
  317. <td>
  318. <img width=100% src="${game.image['c1-diff-1'].src}">
  319. </td>
  320. <td style="border-left: 4px solid white">
  321. <img width=100% src="${game.image['c1-diff-5'].src}">
  322. </td>
  323. </tr>
  324. </table>
  325. <br>
  326. ${game.lang.infoBox_diff_aux}
  327. <div><img width=50% src="${game.image['map-c1s2'].src}"></div>`,
  328. },
  329. gameMisc: {
  330. title: `${game.lang.show} ${self.auxText}`,
  331. body: game.lang.infoBox_misc_label,
  332. img: `<img class="mx-auto" width=60% src="${game.image['c1-label'].src}">`,
  333. },
  334. }),
  335. },
  336. map: {
  337. characterAnimation: (operation) => {
  338. return ['kid', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3];
  339. },
  340. character: (operation) => {
  341. const char = game.add.sprite(
  342. self.scene.roadPoints.x[curMapPosition],
  343. self.scene.roadPoints.y[curMapPosition],
  344. 'kid_running',
  345. 0,
  346. 0.57
  347. );
  348. char.anchor(0, 0.85);
  349. return char;
  350. },
  351. startBuilding: () => {
  352. return game.add.image(
  353. self.scene.roadPoints.x[0] - 193,
  354. self.scene.roadPoints.y[0] - 205,
  355. 'house',
  356. 1.03
  357. );
  358. },
  359. endBuilding: () => {
  360. return game.add.image(
  361. self.scene.roadPoints.x[5] - 28,
  362. self.scene.roadPoints.y[5] - 215,
  363. 'school',
  364. 0.52
  365. );
  366. },
  367. },
  368. end: {
  369. characterAnimation: ['move', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 3],
  370. character: () => {
  371. const char = game.add.sprite(0, -152, 'kid_running', 0, 1.05);
  372. char.anchor(0.5, 0.5);
  373. return char;
  374. },
  375. building: () => game.add.image(600, 222, 'school', 1.05),
  376. },
  377. },
  378. },
  379. {
  380. gameName: 'squareTwo',
  381. gameMode: ['a', 'b'],
  382. gameOperation: ['minus'],
  383. gameDifficulty: 5,
  384. // info
  385. gameShape: 'square',
  386. assets: {
  387. menu: {
  388. gameNameBtn: 'game_2',
  389. infoBox: () => ({
  390. title: `<strong>${game.lang.game}:</strong> ${game.lang.square} II`,
  391. body: `<ul>${game.lang.infoBox_squareTwo}</ul>`,
  392. img: `<img class="mx-auto" width=60% src="${game.image['s2'].src}">`,
  393. }),
  394. },
  395. customMenu: {
  396. gameModeBtn: ['mode_4', 'mode_5'],
  397. gameOperationBtn: ['operation_equals'],
  398. infoBox: () => ({
  399. gameMode: {
  400. title: game.lang.game_modes,
  401. body: `<p>${game.lang.infoBox_mode}</p>`,
  402. img: `<table>
  403. <tr>
  404. <td><b>${game.lang.mode}: A</b> - ${game.lang.infoBox_mode_s2_A}</td>
  405. <td><b>${game.lang.mode}: B</b> - ${game.lang.infoBox_mode_s2_B}</td>
  406. </tr>
  407. <tr>
  408. <td><img width=98% src="${game.image['s2-A-h'].src}"></td>
  409. <td><img width=98% src="${game.image['s2-B-h'].src}"></td>
  410. </tr>
  411. <table>`,
  412. },
  413. gameDifficulty: {
  414. title: game.lang.difficulties,
  415. body: game.lang.infoBox_diff,
  416. img: `<table>
  417. <tr>
  418. <td><b>${game.lang.difficulty}:</b> 1</td>
  419. <td><b>${game.lang.difficulty}:</b> 5</td>
  420. </tr>
  421. <tr>
  422. <td><img width=100% src="${game.image['s2-diff-1'].src}"></td>
  423. <td style="border-left: 4px solid white"><img width=100% src="${game.image['s2-diff-5'].src}"></td>
  424. </tr>
  425. </table>
  426. <br>
  427. ${game.lang.infoBox_diff_aux}
  428. <div><img width=50% src="${game.image['map-c1s2'].src}"></div>`,
  429. },
  430. gameMisc: {
  431. title: `${game.lang.show} ${self.auxText}`,
  432. body: game.lang.infoBox_misc_rect,
  433. img: `<img class="mx-auto" width=100% src="${game.image['s2-label'].src}">`,
  434. },
  435. }),
  436. },
  437. map: {
  438. characterAnimation: (operation) => {
  439. return ['kid', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3];
  440. },
  441. character: (operation) => {
  442. const char = game.add.sprite(
  443. self.scene.roadPoints.x[curMapPosition],
  444. self.scene.roadPoints.y[curMapPosition],
  445. 'kid_running',
  446. 0,
  447. 0.57
  448. );
  449. char.anchor(0, 0.85);
  450. return char;
  451. },
  452. startBuilding: () => {
  453. return game.add.image(
  454. self.scene.roadPoints.x[0] - 193,
  455. self.scene.roadPoints.y[0] - 205,
  456. 'house',
  457. 1.03
  458. );
  459. },
  460. endBuilding: () => {
  461. return game.add.image(
  462. self.scene.roadPoints.x[5] - 28,
  463. self.scene.roadPoints.y[5] - 215,
  464. 'school',
  465. 0.52
  466. );
  467. },
  468. },
  469. end: {
  470. characterAnimation: ['move', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 3],
  471. character: () => {
  472. const char = game.add.sprite(0, 460, 'kid_running', 6, 1.05);
  473. char.anchor(0.5, 0.5);
  474. return char;
  475. },
  476. building: () => game.add.image(600, 222, 'school', 1.05),
  477. },
  478. },
  479. },
  480. // {
  481. // gameName: 'scaleOne',
  482. // gameMode: ['a'],
  483. // gameOperation: ['plus'],
  484. // gameDifficulty: 1,
  485. // // info
  486. // gameShape: 'noShape',
  487. // assets: {
  488. // menu: {
  489. // gameNameBtn: 'game_3',
  490. // // infoBox
  491. // },
  492. // customMenu: {
  493. // gameModeBtn: ['mode_6'],
  494. // gameOperationBtn: ['operation_equals'],
  495. // // infoBox
  496. // },
  497. // map: {
  498. // characterAnimation: (operation) => {
  499. // return operation === 'plus'
  500. // ? ['green_tractor', [0, 1, 2, 3, 4], 3]
  501. // : ['red_tractor', [10, 11, 12, 13, 14], 3];
  502. // },
  503. // character: (operation) => {
  504. // let char;
  505. // if (operation == 'plus') {
  506. // char = game.add.sprite(
  507. // self.scene.roadPoints.x[curMapPosition],
  508. // self.scene.roadPoints.y[curMapPosition],
  509. // 'tractor',
  510. // 0,
  511. // 0.75
  512. // );
  513. // }
  514. // if (operation === 'minus') {
  515. // char = game.add.sprite(
  516. // self.scene.roadPoints.x[curMapPosition],
  517. // self.scene.roadPoints.y[curMapPosition],
  518. // 'tractor',
  519. // 10,
  520. // 0.75
  521. // );
  522. // }
  523. // char.rotate = -30; // 25 anticlock
  524. // return char;
  525. // },
  526. // startBuilding: () => {
  527. // return game.add
  528. // .image(self.scene.roadPoints.x[0], self.scene.roadPoints.y[0], 'garage', 0.6)
  529. // .anchor(0.5, 1);
  530. // },
  531. // endBuilding: () => {
  532. // return game.add
  533. // .image(self.scene.roadPoints.x[5], self.scene.roadPoints.y[5], 'farm', 0.9)
  534. // .anchor(0.4, 0.7);
  535. // },
  536. // },
  537. // end: {
  538. // // TODO
  539. // },
  540. // },
  541. // },
  542. ];