globals_functions.js 9.7 KB

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