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.

179 lines
5.4 KiB

  1. 
  2. // Main app class
  3. enyo.kind({
  4. name: "FoodChain.App",
  5. kind: enyo.Control,
  6. classes: "board",
  7. components: [
  8. // Card box
  9. { name: "glass", classes: "glass" },
  10. { name: "cardbox", classes: "cardbox", components: [] },
  11. // Logo
  12. { kind: "Image", src: "images/FoodChain.png", classes: "logo" },
  13. // Game button
  14. { name: "LearnGame", kind: "ShadowButton", img: "one", classes: "game-LearnGame", ontap: "playGame", onenter: "showGameDescription", onleave: "hideGameDescription" },
  15. { name: "BuildGame", kind: "ShadowButton", img: "two", classes: "game-BuildGame", ontap: "playGame", onenter: "showGameDescription", onleave: "hideGameDescription" },
  16. { name: "PlayGame", kind: "ShadowButton", img: "three", classes: "game-PlayGame", ontap: "playGame", onenter: "showGameDescription", onleave: "hideGameDescription" },
  17. { kind: "ShadowButton", img: "information", classes: "information", ontap: "showCredits" },
  18. { name: "networkCheck", kind: "FoodChain.NetworkCheck"},
  19. // Popup for game title and description
  20. { name: "popup", classes: "game-popup", components: [
  21. { name: "title", classes: "game-title" },
  22. { name: "description", classes: "game-description" }
  23. ]},
  24. // End of sound event
  25. {kind: "Signals", onEndOfSound: "endOfSound"}
  26. ],
  27. // Constructor, save home
  28. create: function() {
  29. this.inherited(arguments);
  30. FoodChain.context.home = this;
  31. // Start display card timer
  32. this.initCardStack();
  33. // Create game description
  34. this.$.popup.hide();
  35. this.games = [];
  36. this.setLocale();
  37. // Update context, no game playing
  38. FoodChain.context.game = "";
  39. FoodChain.context.object = this;
  40. // Init soundtrack
  41. this.soundtrack = "audio/popcorn";
  42. },
  43. // Localization, changed update cards and description
  44. setLocale: function() {
  45. // Change card localization if any
  46. enyo.forEach(this.$.cardbox.getControls(), function(card) {
  47. card.setLocale();
  48. });
  49. },
  50. // Init card stack for the animation
  51. initCardStack: function() {
  52. // Pick randomly N cards
  53. this.cardcount = 0;
  54. this.cards = [];
  55. var i = 0;
  56. while (i < 12) {
  57. var index = Math.floor(Math.random()*FoodChain.cards.length);
  58. var found = false;
  59. for (var j = 0 ; !found && j < this.cards.length-1 ; j++) {
  60. if (this.cards[j] == FoodChain.cards[index]) found = true;
  61. }
  62. if (!found) {
  63. this.cards.push(FoodChain.cards[index]);
  64. i++;
  65. }
  66. }
  67. },
  68. // Play soundtrack when rendered and restart at end
  69. rendered: function() {
  70. // Play soundtrack
  71. FoodChain.sound.play(this.soundtrack);
  72. // Check network
  73. this.$.networkCheck.check();
  74. // Create timer for card animation
  75. this.createComponent({ name: "timer", kind: "Timer", baseInterval: 1200, onTriggered: "displayCard" }, {owner: this});
  76. },
  77. endOfSound: function(e, s) {
  78. if (s.sound == this.soundtrack)
  79. FoodChain.sound.play(this.soundtrack);
  80. },
  81. // Display card animation
  82. displayCard: function() {
  83. // All cards displayed
  84. if (this.cardcount == this.cards.length) {
  85. this.$.cardbox.destroyComponents();
  86. this.$.cardbox.render();
  87. this.initCardStack();
  88. return;
  89. }
  90. // Display a new card
  91. var x = Math.floor(Math.random()*window.innerWidth*0.7);
  92. var y = Math.floor(Math.random()*window.innerHeight*0.7);
  93. this.$.cardbox.createComponent({ kind: "FoodChain.Card", cardname: this.cards[this.cardcount], x: x, y: y, z: 0}).render();
  94. this.cardcount = this.cardcount + 1;
  95. },
  96. // Show/hide game description
  97. showGameDescription: function(s) {
  98. // Update description
  99. this.games["LearnGame"] = { title: __$FC("learn"), description: __$FC("learndesc") };
  100. this.games["BuildGame"] = { title: __$FC("build"), description: __$FC("builddesc") };
  101. this.games["PlayGame"] = { title: __$FC("play"), description: __$FC("playdesc") };
  102. // Show description
  103. this.$.title.setContent(this.games[s.name].title+":");
  104. this.$.title.addClass("game-color-"+s.name);
  105. this.$.description.setContent(this.games[s.name].description);
  106. this.$.description.addClass("game-color-"+s.name);
  107. this.$.popup.show();
  108. },
  109. hideGameDescription: function(s) {
  110. this.$.title.removeClass("game-color-"+s.name);
  111. this.$.description.removeClass("game-color-"+s.name);
  112. this.$.popup.hide();
  113. },
  114. // Show credit page
  115. showCredits: function() {
  116. this.$.timer.stop();
  117. this.removeComponent(this.$.timer);
  118. FoodChain.context.object = new FoodChain.Credits().renderInto(document.getElementById("body"));
  119. },
  120. // Launch a game
  121. playGame: function(s) {
  122. // Stop sound
  123. this.$.popup.hide();
  124. FoodChain.sound.pause();
  125. this.$.timer.stop();
  126. this.removeComponent(this.$.timer);
  127. var level = (s.level === undefined) ? 1 : s.level;
  128. // Launch Learn game
  129. if (s.name == "LearnGame") {
  130. FoodChain.context.object = new FoodChain.LearnGame({level: level}).renderInto(document.getElementById("body"));
  131. }
  132. // Launch Build game
  133. else if (s.name == "BuildGame") {
  134. FoodChain.context.object = new FoodChain.BuildGame({level: level}).renderInto(document.getElementById("body"));
  135. }
  136. // Launch Play game
  137. else if (s.name == "PlayGame") {
  138. FoodChain.context.object = new FoodChain.PlayGame({level: level}).renderInto(document.getElementById("body"));
  139. }
  140. },
  141. // Test if database is installed
  142. checkDatabase: function(then) {
  143. this.$.networkCheck.check(then);
  144. },
  145. hasDatabase: function() {
  146. return this.$.networkCheck.getConnected();
  147. },
  148. getDatabase: function() {
  149. return (this.hasDatabase() ? "" : "http://server.sugarizer.org/activities/FoodChain.activity/");
  150. }
  151. });