globals_functions.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. const navigation = {
  2. list: [],
  3. prevState: undefined,
  4. audioIcon: undefined,
  5. showAnswerIcon: undefined,
  6. labelLeft: undefined,
  7. labelRight: undefined,
  8. add: {
  9. left: (icons, prevState) => {
  10. navigation.prevState = prevState;
  11. const iconSize = 75;
  12. let x = 10;
  13. navigation.labelLeft = game.add.text(x, 110, '', textStyles.p_);
  14. navigation.labelLeft.align = 'left';
  15. icons.forEach((icon) => {
  16. if (icon === 'back') {
  17. if (!prevState) {
  18. console.error(
  19. "Game error: You tried to add a 'back' icon without the 'state' parameter."
  20. );
  21. return;
  22. } else {
  23. navigation.prevState = prevState;
  24. }
  25. }
  26. if (icon === 'show_answer' && !self.utils.showAnswer) {
  27. console.error(
  28. "Game error: You tried to add a 'show_answer' icon without a 'showAnswer()' function o the game state."
  29. );
  30. return;
  31. }
  32. const asset = game.add.image(x, 10, icon, 1.5);
  33. navigation.showAnswerIcon = asset;
  34. navigation.list.push(asset);
  35. x += iconSize;
  36. });
  37. },
  38. right: (icons) => {
  39. const iconSize = 75;
  40. let x = context.canvas.width - iconSize - 10;
  41. navigation.labelRight = game.add.text(x + 60, 110, '', textStyles.p_);
  42. navigation.labelRight.align = 'right';
  43. icons.forEach((icon) => {
  44. let asset;
  45. if (icon === 'audio') {
  46. asset = game.add.sprite(x, 10, icon, 1, 1.5);
  47. asset.curFrame = audioStatus ? 0 : 1;
  48. navigation.audioIcon = asset;
  49. }
  50. if (icon === 'lang') asset = game.add.image(x, 10, 'lang', 1.5);
  51. navigation.list.push(asset);
  52. x -= iconSize;
  53. });
  54. },
  55. },
  56. changeState: (state) => {
  57. if (audioStatus) game.audio.popSound.play();
  58. game.event.clear(self);
  59. game.state.start(state);
  60. },
  61. disableIcon: (icon) => {
  62. icon.alpha = 0;
  63. icon.isDisabled = true;
  64. },
  65. onInputDown: (x, y) => {
  66. navigation.list.forEach((icon) => {
  67. if (game.math.isOverIcon(x, y, icon) && !icon.isDisabled) {
  68. const iconName = icon.name;
  69. switch (iconName) {
  70. case 'menu':
  71. navigation.changeState('menu');
  72. break;
  73. case 'lang':
  74. navigation.changeState('lang');
  75. break;
  76. case 'back':
  77. const state = navigation.prevState;
  78. navigation.changeState(state);
  79. break;
  80. case 'show_answer':
  81. if (navigation.showAnswerIcon.alpha === 1) {
  82. if (audioStatus) game.audio.popSound.play();
  83. self.utils.showAnswer();
  84. navigation.disableIcon(navigation.showAnswerIcon);
  85. }
  86. break;
  87. case 'audio':
  88. if (audioStatus) {
  89. audioStatus = false;
  90. navigation.audioIcon.curFrame = 1;
  91. } else {
  92. audioStatus = true;
  93. navigation.audioIcon.curFrame = 0;
  94. game.audio.popSound.play();
  95. }
  96. game.render.all();
  97. break;
  98. default:
  99. console.error('Game error: error in navigation icon');
  100. }
  101. }
  102. });
  103. },
  104. onInputOver: (x, y) => {
  105. let isOverIcon = false;
  106. navigation.list.forEach((icon) => {
  107. if (game.math.isOverIcon(x, y, icon) && !icon.isDisabled) {
  108. isOverIcon = true;
  109. const iconName = icon.name;
  110. switch (iconName) {
  111. case 'menu':
  112. navigation.labelLeft.name = game.lang.nav_menu;
  113. break;
  114. case 'lang':
  115. navigation.labelRight.name = game.lang.nav_lang;
  116. break;
  117. case 'back':
  118. navigation.labelLeft.name = game.lang.nav_back;
  119. break;
  120. case 'show_answer':
  121. navigation.labelLeft.name = game.lang.nav_help;
  122. break;
  123. case 'audio':
  124. navigation.labelRight.name = game.lang.audio;
  125. break;
  126. }
  127. }
  128. });
  129. if (!isOverIcon) {
  130. if (navigation.labelLeft) navigation.labelLeft.name = '';
  131. if (navigation.labelRight) navigation.labelRight.name = '';
  132. } else {
  133. document.body.style.cursor = 'pointer';
  134. }
  135. },
  136. };
  137. /**
  138. * Sends game information to database
  139. *
  140. * @param {string} extraData player information for the current game
  141. */
  142. const sendToDatabase = function (extraData) {
  143. // FOR MOODLE
  144. if (moodle) {
  145. if (self.result) moodleVar.hits[curMapPosition - 1]++;
  146. else moodleVar.errors[curMapPosition - 1]++;
  147. moodleVar.time[curMapPosition - 1] += game.timer.elapsed;
  148. const url = iLMparameters.iLM_PARAM_ServerToGetAnswerURL;
  149. const grade = '' + getEvaluation();
  150. const report = getAnswer();
  151. const data =
  152. 'return_get_answer=1' +
  153. '&iLM_PARAM_ActivityEvaluation=' +
  154. encodeURIComponent(grade) +
  155. '&iLM_PARAM_ArchiveContent=' +
  156. encodeURIComponent(report);
  157. const init = {
  158. method: 'POST',
  159. body: data,
  160. headers: {
  161. 'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
  162. },
  163. };
  164. fetch(url, init)
  165. .then((response) => {
  166. if (response.ok) {
  167. if (isDebugMode) console.log('Processing...');
  168. } else {
  169. console.error('Game error: Network response was not ok.');
  170. }
  171. })
  172. .catch((error) => {
  173. console.error(
  174. 'Game error: problem with fetch operation - ' + error.message + '.'
  175. );
  176. });
  177. } else {
  178. // Create some variables we need to send to our PHP file
  179. // Attention: this names must be compactible to data table (MySQL server)
  180. // @see php/save.php
  181. const data =
  182. 'line_ip=143.107.45.11' + // INSERT database server IP
  183. '&line_name=' +
  184. playerName +
  185. '&line_lang=' +
  186. langString +
  187. extraData;
  188. const url = 'php/save.php';
  189. const init = {
  190. method: 'POST',
  191. body: data,
  192. headers: { 'Content-type': 'application/x-www-form-urlencoded' },
  193. };
  194. fetch(url, init)
  195. .then((response) => {
  196. if (response.ok) {
  197. if (isDebugMode) console.log('Processing...');
  198. response.text().then((text) => {
  199. if (isDebugMode) {
  200. console.log(text);
  201. }
  202. });
  203. } else {
  204. console.error('Game error: Network response was not ok.');
  205. }
  206. })
  207. .catch((error) => {
  208. console.error(
  209. 'Game error: problem with fetch operation - ' + error.message + '.'
  210. );
  211. });
  212. }
  213. };
  214. const renderBackground = (type) => {
  215. if (type === 'plain') {
  216. // Add plain blue background
  217. game.add.geom.rect(
  218. 0,
  219. 0,
  220. context.canvas.width,
  221. context.canvas.height,
  222. colors.blueBg
  223. );
  224. return;
  225. }
  226. if (type === 'scale') {
  227. // Add background image
  228. game.add.image(0, 0, 'bg_snow', 1.8);
  229. const floor = {
  230. width: 128,
  231. last: context.canvas.width / 128,
  232. tiles: [3, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 4],
  233. };
  234. for (let i = 0; i < floor.tiles.length; i++) {
  235. game.add
  236. .sprite(i * floor.width, context.canvas.height, 'floor_snow', 0, 2)
  237. .anchor(0, 1);
  238. game.add
  239. .sprite(
  240. i * floor.width,
  241. context.canvas.height - 65,
  242. 'floor_snow',
  243. floor.tiles[i],
  244. 2
  245. )
  246. .anchor(0, 1);
  247. }
  248. game.add
  249. .image(-2, context.canvas.height - 410, 'wood_shelf', 2)
  250. .anchor(0, 1);
  251. game.add
  252. .image(-2, context.canvas.height - 650, 'wood_shelf', 2)
  253. .anchor(0, 1);
  254. game.add
  255. .sprite(4 * floor.width, context.canvas.height - 65, 'floor_snow', 7, 2)
  256. .anchor(0, 1);
  257. game.add
  258. .sprite(8 * floor.width, context.canvas.height - 65, 'floor_snow', 8, 2)
  259. .anchor(0, 1);
  260. game.add
  261. .sprite(13 * floor.width, context.canvas.height - 65, 'floor_snow', 7, 2)
  262. .anchor(0, 1);
  263. return;
  264. }
  265. // Add background image
  266. game.add.image(0, 0, 'bg_default', 2.2);
  267. // Add clouds
  268. game.add.image(300, context.canvas.height / 2 - 50, 'cloud', 1.5);
  269. game.add.image(700, context.canvas.height / 2 + 50 - 50, 'cloud', 1.5);
  270. game.add.image(1280, context.canvas.height / 2 - 50 - 50, 'cloud', 1.5);
  271. // Add floor
  272. const floorSize = 150;
  273. if (type === 'farmRoad') {
  274. game.add.image(0, context.canvas.height - floorSize, 'floor_grass', 1.5);
  275. for (let i = 1; i < context.canvas.width / floorSize - 1; i++) {
  276. game.add.image(
  277. i * floorSize,
  278. context.canvas.height - floorSize,
  279. 'floor_road'
  280. );
  281. }
  282. game.add.image(
  283. context.canvas.width - floorSize,
  284. context.canvas.height - floorSize,
  285. 'floor_grass',
  286. 1.5
  287. );
  288. return;
  289. }
  290. for (let i = 0; i < context.canvas.width / floorSize; i++) {
  291. game.add.image(
  292. i * floorSize,
  293. context.canvas.height - floorSize,
  294. 'floor_grass',
  295. 1.5
  296. );
  297. }
  298. if (type === 'end') {
  299. game.add.geom.rect(
  300. 0,
  301. context.canvas.height - floorSize * 2 + 15,
  302. 150 * (context.canvas.width / floorSize),
  303. 150,
  304. '#48d813'
  305. );
  306. }
  307. };
  308. const getFrameInfo = function () {
  309. let x0 = (y0 = 300);
  310. // width/height - offset on both sides
  311. let width = context.canvas.width - 2 * x0;
  312. let height = context.canvas.height - 2 * y0;
  313. let rect = function () {
  314. game.add.geom.rect(x0, y0, width, height, 'transparent', 1, colors.red, 2);
  315. };
  316. let point = function (offsetW, offsetH) {
  317. for (let i = 0, y1 = y; i < 4; i++) {
  318. x1 = x0;
  319. for (let j = 0; j < 7; j++) {
  320. let sqr = game.add.geom.rect(x1, y1, 20, 20, colors.red);
  321. sqr.anchor(0.5, 0.5);
  322. x1 += offsetW;
  323. }
  324. y1 += offsetH;
  325. }
  326. };
  327. return { x: x0, y: y0, width, height, rect, point };
  328. };
  329. const moveList = function (list, x, y) {
  330. list.forEach((item) => {
  331. item.x += x;
  332. item.y += y;
  333. });
  334. };