not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

332 lines
10 KiB

  1. // Level config
  2. FoodChain.learnLevels = [
  3. { size: 2, count: 5, time: 10 }, // Level 1
  4. { size: 2, count: 10, time: 15 }, // Level 2
  5. { size: 2, count: 15, time: 20 }, // Level 3
  6. { size: 3, count: 5, time: 10 }, // Level 4
  7. { size: 3, count: 10, time: 15 }, // Level 5
  8. { size: 3, count: 15, time: 20 } // Level 6
  9. ];
  10. // Learn Game class
  11. enyo.kind({
  12. name: "FoodChain.LearnGame",
  13. kind: enyo.Control,
  14. published: {level: 1},
  15. classes: "board",
  16. components: [
  17. { name: "cards", components: [
  18. // Level - Score - Time bar
  19. { components: [
  20. { classes: "level-zone", components: [
  21. { name: "textlevel", classes: "title level-value" },
  22. { name: "level", content: "0", classes: "title level-value" }
  23. ]},
  24. { classes: "score-zone", components: [
  25. { name: "textscore", classes: "title score-text" },
  26. { name: "score", content: "0000", classes: "title score-value" },
  27. { name: "timercount", content: "0:0,0", classes: "title timer-value" }
  28. ]}
  29. ]},
  30. // Board zone
  31. { name: "startbox", classes: "start-box", components: [] },
  32. { components: [
  33. { name: "herbbox", classes: "end-box", ontap: "chooseAsEnd", ondrop: "drop", ondragover: "dragover", components: [
  34. { name: "herbname", classes: "box-name herb-color" }
  35. ]},
  36. { name: "carnbox", classes: "end-box", ontap: "chooseAsEnd", ondrop: "drop", ondragover: "dragover", components: [
  37. { name: "carnname", classes: "box-name" }
  38. ]},
  39. { name: "omnibox", classes: "end-box omni-box-two", ontap: "chooseAsEnd", ondrop: "drop", ondragover: "dragover", components: [
  40. { name: "omniname", classes: "box-name" }
  41. ]}
  42. ]},
  43. // Buttons bar
  44. { name: "play", kind: "ShadowButton", img: "play", classes: "play", ontap: "play" },
  45. { name: "pause", kind: "ShadowButton", img: "pause", classes: "play", ontap: "pause" },
  46. { name: "forward", kind: "ShadowButton", img: "forward", classes: "restart", ontap: "next" },
  47. { name: "home", kind: "ShadowButton", img: "home", classes: "home", ontap: "home" },
  48. // End of sound event
  49. {kind: "Signals", onEndOfSound: "endSound"}
  50. ]}
  51. ],
  52. // Constructor
  53. create: function() {
  54. this.inherited(arguments);
  55. this.cardlist = null;
  56. this.nextaction = 0;
  57. this.createComponent({ name: "timer", kind: "Timer", paused: true, onTriggered: "updateTimer" }, {owner: this});
  58. this.setLocale();
  59. this.$.score.setContent(String("0000"+FoodChain.context.score).slice(-4));
  60. FoodChain.context.game = this.kindName;
  61. this.levelChanged();
  62. },
  63. // Localization changed, update cards and string resource
  64. setLocale: function() {
  65. // Update string resources
  66. this.$.textlevel.setContent(__$FC("level"));
  67. this.$.textscore.setContent(__$FC("score"));
  68. this.$.herbname.setContent(__$FC(FoodChain.feedStrategy[0].name));
  69. this.$.carnname.setContent(__$FC(FoodChain.feedStrategy[1].name));
  70. this.$.omniname.setContent(__$FC(FoodChain.feedStrategy[2].name));
  71. // Update cards
  72. enyo.forEach(this.$.startbox.getControls(), function(card) {
  73. card.setLocale();
  74. });
  75. },
  76. // Level changed, init board then start game
  77. levelChanged: function() {
  78. // Box handling
  79. FoodChain.context.level = this.level;
  80. if (FoodChain.learnLevels[this.level-1].size == 2) {
  81. FoodChain.addRemoveClass(this.$.herbbox, "herb-box-two", "herb-box-three");
  82. FoodChain.addRemoveClass(this.$.carnbox, "carn-box-two", "carn-box-three");
  83. FoodChain.addRemoveClass(this.$.omnibox, "omni-box-two", "omni-box-three");
  84. this.$.omnibox.hide();
  85. } else {
  86. FoodChain.addRemoveClass(this.$.herbbox, "herb-box-three", "herb-box-two");
  87. FoodChain.addRemoveClass(this.$.carnbox, "carn-box-three", "carn-box-two");
  88. FoodChain.addRemoveClass(this.$.omnibox, "omni-box-three", "omni-box-two");
  89. this.$.omnibox.show();
  90. }
  91. this.dragobject = null;
  92. this.selectedobject = null;
  93. // Compute the card list to sort
  94. if (this.cardlist == null) {
  95. this.cardlist = FoodChain.randomFeedList(FoodChain.learnLevels[this.level-1].size, FoodChain.learnLevels[this.level-1].count);
  96. }
  97. // Display the first card
  98. this.currentcard = 0;
  99. var card = this.$.startbox.createComponent({ kind: "FoodChain.Card", cardname: this.cardlist[this.currentcard].cardname, x: 10, y: 10, ontap: "taped", ondragstart: "dragstart", ondragfinish: "dragfinish"}, {owner: this});
  100. FoodChain.sound.play(card.sound);
  101. // Saving context
  102. FoodChain.saveContext();
  103. // Button handling
  104. this.$.play.hide();
  105. this.$.pause.show();
  106. this.$.forward.hide();
  107. this.$.home.hide();
  108. // Timer and level init
  109. this.$.level.setContent(" "+this.level);
  110. this.timecount = {mins:0, secs:0, tenth:0};
  111. this.$.timercount.removeClass("timer-overtime");
  112. this.displayTimer();
  113. this.render();
  114. },
  115. // Sound ended, start game
  116. endSound: function(e, s) {
  117. // What next ?
  118. if (this.nextaction == 1) {
  119. // Right play, next card or next level
  120. this.nextaction = 0;
  121. this.dragobject.getContainer().removeClass("box-win");
  122. this.dragobject.destroy();
  123. this.dragobject = null;
  124. // Next level
  125. if (this.currentcard+1 == this.cardlist.length) {
  126. this.computeLevelScore();
  127. this.$.play.hide();
  128. this.$.pause.hide();
  129. this.$.home.show();
  130. this.render();
  131. if (this.level != FoodChain.learnLevels.length)
  132. this.$.forward.show();
  133. // Display next card
  134. } else {
  135. this.currentcard = this.currentcard + 1;
  136. var card = this.$.startbox.createComponent({ kind: "FoodChain.Card", cardname: this.cardlist[this.currentcard].cardname, x: 5, y: 5, ontap: "taped", ondragstart: "dragstart", ondragfinish: "dragfinish"}, {owner: this});
  137. FoodChain.sound.play(card.sound);
  138. this.render();
  139. }
  140. return;
  141. }
  142. else if (this.nextaction == 2) {
  143. // Bad play, put card at start
  144. this.nextaction = 0;
  145. this.dragobject.getContainer().removeClass("box-lost");
  146. this.dragobject.setContainer(this.$.startbox);
  147. this.dragobject = null;
  148. this.render();
  149. }
  150. // Start timer
  151. this.$.timer.resume();
  152. },
  153. // Display timer value
  154. displayTimer: function() {
  155. this.$.timercount.setContent(this.timecount.mins+":"+String("00"+this.timecount.secs).slice(-2)+","+this.timecount.tenth);
  156. },
  157. // Update timer
  158. updateTimer: function(s, e) {
  159. this.timecount.tenth = this.timecount.tenth + 1;
  160. if (this.timecount.tenth == 10) {
  161. this.timecount.tenth = 0;
  162. this.timecount.secs = this.timecount.secs + 1;
  163. var currentcount = this.timecount.mins * 60 + this.timecount.secs;
  164. if (currentcount >= FoodChain.learnLevels[this.level-1].time) {
  165. this.$.timercount.addClass("timer-overtime");
  166. }
  167. if (this.timecount.secs == 60) {
  168. this.timecount.secs = 0;
  169. this.timecount.mins = this.timecount.mins + 1;
  170. }
  171. }
  172. this.displayTimer();
  173. },
  174. // Play sound when card taped, set card as selected (avoid need of drag&drop)
  175. taped: function(s, e) {
  176. if (this.$.timer.paused)
  177. return true;
  178. FoodChain.sound.play(s.sound);
  179. FoodChain.log(s.cardname+" taped");
  180. this.selectedobject = s;
  181. s.addClass("card-dragged");
  182. },
  183. // Card drag start, change style to dragged
  184. dragstart: function(s, e) {
  185. if (this.$.timer.paused)
  186. return true;
  187. s.addClass("card-dragged");
  188. this.$.startbox.addClass("box-dragging");
  189. this.$.herbbox.addClass("box-dragging");
  190. this.$.carnbox.addClass("box-dragging");
  191. this.$.omnibox.addClass("box-dragging");
  192. FoodChain.sound.play(s.sound);
  193. this.dragobject = s;
  194. this.selectedobject = null;
  195. },
  196. // Card drag end, change style to not dragged
  197. dragfinish: function(s, e) {
  198. s.removeClass("card-dragged");
  199. this.$.startbox.removeClass("box-dragging");
  200. this.$.herbbox.removeClass("box-dragging");
  201. this.$.carnbox.removeClass("box-dragging");
  202. this.$.omnibox.removeClass("box-dragging");
  203. },
  204. // Drag over the box, allow dragging
  205. dragover: function(s, e) {
  206. if (this.dragobject == null)
  207. return true;
  208. e.preventDefault();
  209. return false;
  210. },
  211. // Choose the final box for the card, same as drop but without drag
  212. chooseAsEnd: function(s, e) {
  213. if (this.selectedobject == null)
  214. return true;
  215. this.selectedobject.removeClass("card-dragged");
  216. this.dragobject = this.selectedobject;
  217. this.drop(s, e);
  218. this.selectedobject = null;
  219. },
  220. // Dropped in the box, check if correct
  221. drop: function(s, e) {
  222. if (this.dragobject == null || this.$.timer.paused)
  223. return true;
  224. e.preventDefault();
  225. // Draw in the new box
  226. this.dragobject.setContainer(s);
  227. this.dragobject.moveTo(5,0);
  228. this.render();
  229. // Test if in the right box
  230. this.$.timer.pause();
  231. var win = (s.name.substring(0,4) == FoodChain.feedStrategy[this.cardlist[this.currentcard].strategy].name.substring(0,4));
  232. if (win) {
  233. this.nextaction = 1;
  234. this.addScore(1);
  235. FoodChain.sound.play("audio/applause");
  236. s.addClass("box-win");
  237. }
  238. else {
  239. this.nextaction = 2;
  240. FoodChain.sound.play("audio/disappointed");
  241. s.addClass("box-lost");
  242. }
  243. },
  244. // Compute score
  245. addScore: function(score) {
  246. FoodChain.context.score += score;
  247. this.$.score.setContent(String("0000"+FoodChain.context.score).slice(-4));
  248. },
  249. // Compute score for this level
  250. computeLevelScore: function() {
  251. var score = 0;
  252. var currentcount = this.timecount.mins * 60 + this.timecount.secs;
  253. if (currentcount < FoodChain.learnLevels[this.level-1].time) {
  254. score += (FoodChain.learnLevels[this.level-1].time - currentcount);
  255. }
  256. this.addScore(score);
  257. },
  258. // Resume game
  259. play: function() {
  260. // Show cards
  261. enyo.forEach(this.$.startbox.getControls(), function(card) {
  262. card.show();
  263. });
  264. // Show pause button, hide play button
  265. this.$.timer.resume();
  266. this.$.play.hide();
  267. this.$.pause.show();
  268. this.$.home.hide();
  269. },
  270. // Pause game
  271. pause: function() {
  272. // Hide cards
  273. enyo.forEach(this.$.startbox.getControls(), function(card) {
  274. card.hide();
  275. });
  276. // Show play button, hide pause button
  277. this.$.timer.pause();
  278. this.$.pause.hide();
  279. this.$.play.show();
  280. this.$.home.show();
  281. },
  282. // Go to the next level
  283. next: function() {
  284. this.level = this.level + 1;
  285. this.cardlist = null;
  286. this.levelChanged();
  287. },
  288. // Go to the home page of the app
  289. home: function() {
  290. this.$.timer.stop();
  291. FoodChain.goHome();
  292. }
  293. });