globals_functions.js 10.0 KB

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