globals_control.js 18 KB

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