globals_functions.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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.control.isCorrect) 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. console.log(grade);
  160. console.log(report);
  161. console.log(moodleVar);
  162. const init = {
  163. method: 'POST',
  164. body: data,
  165. headers: {
  166. 'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
  167. },
  168. };
  169. fetch(url, init)
  170. .then((response) => {
  171. if (response.ok) {
  172. if (isDebugMode) console.log('Processing...');
  173. } else {
  174. console.error('Game error: Network response was not ok.');
  175. }
  176. })
  177. .catch((error) => {
  178. console.error(
  179. 'Game error: problem with fetch operation - ' + error.message + '.'
  180. );
  181. });
  182. } else {
  183. // Create some variables we need to send to our PHP file
  184. // Attention: this names must be compactible to data table (MySQL server)
  185. // @see php/save.php
  186. const data =
  187. 'line_ip=143.107.45.11' + // INSERT database server IP
  188. '&line_name=' +
  189. playerName +
  190. '&line_lang=' +
  191. langString +
  192. extraData;
  193. console.log('----------------');
  194. console.log(data);
  195. const url = 'php/save.php';
  196. const init = {
  197. method: 'POST',
  198. body: data,
  199. headers: { 'Content-type': 'application/x-www-form-urlencoded' },
  200. };
  201. fetch(url, init)
  202. .then((response) => {
  203. if (response.ok) {
  204. if (isDebugMode) console.log('Processing...');
  205. response.text().then((text) => {
  206. if (isDebugMode) {
  207. console.log(text);
  208. }
  209. });
  210. } else {
  211. console.error('Game error: Network response was not ok.');
  212. }
  213. })
  214. .catch((error) => {
  215. console.error(
  216. 'Game error: problem with fetch operation - ' + error.message + '.'
  217. );
  218. });
  219. }
  220. };
  221. const renderBackground = (type) => {
  222. if (type === 'plain') {
  223. // Add plain blue background
  224. game.add.geom.rect(
  225. 0,
  226. 0,
  227. context.canvas.width,
  228. context.canvas.height,
  229. colors.blueBg
  230. );
  231. return;
  232. }
  233. if (type === 'scale') {
  234. // Add background image
  235. game.add.image(0, 0, 'bg_snow', 1.8);
  236. const floor = {
  237. width: 128,
  238. last: context.canvas.width / 128,
  239. tiles: [3, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 4],
  240. };
  241. for (let i = 0; i < floor.tiles.length; i++) {
  242. game.add
  243. .sprite(i * floor.width, context.canvas.height, 'floor_snow', 0, 2)
  244. .anchor(0, 1);
  245. game.add
  246. .sprite(
  247. i * floor.width,
  248. context.canvas.height - 65,
  249. 'floor_snow',
  250. floor.tiles[i],
  251. 2
  252. )
  253. .anchor(0, 1);
  254. }
  255. game.add
  256. .image(-2, context.canvas.height - 410, 'wood_shelf', 2)
  257. .anchor(0, 1);
  258. game.add
  259. .image(-2, context.canvas.height - 650, 'wood_shelf', 2)
  260. .anchor(0, 1);
  261. game.add
  262. .sprite(4 * floor.width, context.canvas.height - 65, 'floor_snow', 7, 2)
  263. .anchor(0, 1);
  264. game.add
  265. .sprite(8 * floor.width, context.canvas.height - 65, 'floor_snow', 8, 2)
  266. .anchor(0, 1);
  267. game.add
  268. .sprite(13 * floor.width, context.canvas.height - 65, 'floor_snow', 7, 2)
  269. .anchor(0, 1);
  270. return;
  271. }
  272. // Add background image
  273. game.add.image(0, 0, 'bg_default', 2.2);
  274. // Add clouds
  275. game.add.image(300, context.canvas.height / 2 - 50, 'cloud', 1.5);
  276. game.add.image(700, context.canvas.height / 2 + 50 - 50, 'cloud', 1.5);
  277. game.add.image(1280, context.canvas.height / 2 - 50 - 50, 'cloud', 1.5);
  278. // Add floor
  279. const floorSize = 150;
  280. if (type === 'farmRoad') {
  281. game.add.image(0, context.canvas.height - floorSize, 'floor_grass', 1.5);
  282. for (let i = 1; i < context.canvas.width / floorSize - 1; i++) {
  283. game.add.image(
  284. i * floorSize,
  285. context.canvas.height - floorSize,
  286. 'floor_road'
  287. );
  288. }
  289. game.add.image(
  290. context.canvas.width - floorSize,
  291. context.canvas.height - floorSize,
  292. 'floor_grass',
  293. 1.5
  294. );
  295. return;
  296. }
  297. for (let i = 0; i < context.canvas.width / floorSize; i++) {
  298. game.add.image(
  299. i * floorSize,
  300. context.canvas.height - floorSize,
  301. 'floor_grass',
  302. 1.5
  303. );
  304. }
  305. if (type === 'end') {
  306. game.add.geom.rect(
  307. 0,
  308. context.canvas.height - floorSize * 2 + 15,
  309. 150 * (context.canvas.width / floorSize),
  310. 150,
  311. '#48d813'
  312. );
  313. }
  314. };
  315. const getFrameInfo = function () {
  316. let x0 = (y0 = 300);
  317. // width/height - offset on both sides
  318. let width = context.canvas.width - 2 * x0;
  319. let height = context.canvas.height - 2 * y0;
  320. let rect = function () {
  321. game.add.geom.rect(x0, y0, width, height, 'transparent', 1, colors.red, 2);
  322. };
  323. let point = function (offsetW, offsetH) {
  324. for (let i = 0, y1 = y; i < 4; i++) {
  325. x1 = x0;
  326. for (let j = 0; j < 7; j++) {
  327. let sqr = game.add.geom.rect(x1, y1, 20, 20, colors.red);
  328. sqr.anchor(0.5, 0.5);
  329. x1 += offsetW;
  330. }
  331. y1 += offsetH;
  332. }
  333. };
  334. return { x: x0, y: y0, width, height, rect, point };
  335. };
  336. const moveList = function (list, x, y) {
  337. list.forEach((item) => {
  338. item.x += x;
  339. item.y += y;
  340. });
  341. };