globals_control.js 19 KB

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