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.

217 lines
6.4 KiB

  1. // Utility function
  2. var util = {};
  3. // Heading: 0 <
  4. // 1 ^
  5. // 2 >
  6. // 3 v
  7. util.moves = [{dx: -1, dy: 0}, {dx: 0, dy: -1}, {dx: +1, dy: 0}, {dx: 0, dy: +1}];
  8. // Unit types and powers
  9. util.unitTypes = ["hq", "soldier", "tank", "canon", "helo"];
  10. util.unitPowers = [constant.powerHq, constant.powerSoldier, constant.powerTank, constant.powerCanon, constant.powerHelo];
  11. util.unitStats = [constant.statHq, constant.statSoldier, constant.statTank, constant.statCanon, constant.statHelo];
  12. // Unit type
  13. util.explosionsImages = ["explosion_1", "explosion_2", "explosion_3", "explosion_4", "explosion_5", "explosion_6", "explosion_7"];
  14. // Prepare board using ground model
  15. util.createMap = function(grounds) {
  16. var game = [];
  17. var index = 0;
  18. for (var i = 0 ; i < constant.boardHeight ; i++ ) {
  19. var line = []
  20. for (var j = 0 ; j < constant.boardWidth ; j++ ) {
  21. var ground = constant.tileEmpty;
  22. var current = grounds[index];
  23. if (current == 'O')
  24. ground = constant.tileWater;
  25. else if (current == 'H')
  26. ground = constant.tileTrees;
  27. else if (current == '^')
  28. ground = constant.tileMountain;
  29. line.push(ground);
  30. index++;
  31. }
  32. game.push(line);
  33. }
  34. return game;
  35. }
  36. // Create a set of units
  37. util.createUnit = function(unit) {
  38. var heading = unit.color == "blue" ? 2 : 0;
  39. var power = 0;
  40. for (var i = 0 ; i < util.unitTypes.length ; i++) {
  41. if (util.unitTypes[i] == unit.type) {
  42. power = util.unitPowers[i];
  43. }
  44. }
  45. var imageprefix = unit.type + "_" + unit.color;
  46. while(util.lookForUnit(unit) != null)
  47. unit.x = unit.x+1;
  48. var newUnit = new Sprite({
  49. x: unit.x, y: unit.y,
  50. heading: heading, power: power,
  51. engine: unit.engine,
  52. images: (unit.type == "hq") ? [imageprefix] : [imageprefix+"_0", imageprefix+"_1", imageprefix+"_2", imageprefix+"_3"]
  53. });
  54. return newUnit;
  55. }
  56. util.createUnits = function(units) {
  57. var created = [];
  58. for (var i = 0 ; i < units.length ; i++) {
  59. created.push(util.createUnit(units[i]));
  60. }
  61. return created;
  62. }
  63. // Test if
  64. util.isValidPosition = function(position, sprite) {
  65. // Out of board
  66. if (position.x < 0 || position.x == constant.boardWidth || position.y < 0 || position.y == constant.boardHeight)
  67. return false;
  68. // Authorized ground depend of unit type
  69. var maptype=play.game[position.y][position.x];
  70. var unittype=util.getUnitType(sprite);
  71. if (unittype == 4)
  72. return true; // Helo can go anywhere
  73. if (maptype == constant.tileEmpty)
  74. return true; // Grass is for everyone
  75. if (unittype == 0)
  76. return maptype == constant.tileEmpty; // HQ only on grass
  77. if (maptype == constant.tileTrees)
  78. return unittype == 1; // Trees is only for soldier
  79. else if (maptype == constant.tileWater)
  80. return unittype == 1; // Water is only for soldier
  81. // Already a unit inside
  82. var unitinside = util.lookForUnit(position);
  83. if (unitinside != null)
  84. return unitinside == sprite;
  85. return false;
  86. }
  87. // Compute next position if sprite go ahead in the current heading
  88. util.nextPositionOnHeading = function(sprite) {
  89. return { x: sprite.x + util.moves[sprite.heading].dx, y: sprite.y + util.moves[sprite.heading].dy };
  90. }
  91. // Look for unit at a position
  92. util.lookForUnit = function(position) {
  93. // Check this position for each unit
  94. for (var i = 0 ; i < play.units.length ; i++)
  95. if (play.units[i].x == position.x && play.units[i].y == position.y)
  96. return play.units[i];
  97. return null;
  98. }
  99. // Look for unit with a value
  100. util.lookForValue = function(value) {
  101. // Check this value on each unit
  102. var set = [];
  103. for (var i = 0 ; i < play.units.length ; i++) {
  104. if (play.units[i].value !== undefined && play.units[i].value.result == value)
  105. set.push(play.units[i]);
  106. }
  107. return set;
  108. }
  109. // Look for opponent around the sprite
  110. util.lookForOpponent = function(sprite) {
  111. var oppositeColor = sprite.getCurrentImage().indexOf("red") != -1 ? "blue" : "red";
  112. for (var i = 0 ; i < util.moves.length ; i++ ) {
  113. var position = {x: sprite.x + util.moves[i].dx, y: sprite.y + util.moves[i].dy};
  114. var neighbour = util.lookForUnit(position);
  115. if (neighbour != null && neighbour.getCurrentImage().indexOf(oppositeColor) != -1)
  116. return {heading: i, unit: neighbour};
  117. }
  118. return null;
  119. }
  120. // Get unit type
  121. util.getUnitType = function(unit) {
  122. var image = unit.getCurrentImage();
  123. for (var i = 0 ; i < util.unitTypes.length ; i++)
  124. if (image.indexOf(util.unitTypes[i]) != -1)
  125. return i;
  126. return -1;
  127. }
  128. // Test if a unit could beat another
  129. util.couldBeat = function(unit1, unit2) {
  130. var type1 = util.getUnitType(unit1);
  131. var type2 = util.getUnitType(unit2);
  132. if (type1 == 1 && type2 == util.unitTypes.length - 1) // Soldier could beat Helo
  133. return true;
  134. return (type1 >= type2);
  135. }
  136. // Handle fight between two opponents
  137. util.processFight = function(unit1, unit2, power) {
  138. if (unit1 != null && !util.couldBeat(unit1, unit2))
  139. return;
  140. unit2.power = unit2.power - (power !== undefined ? power : 1);
  141. util.doExplosion(unit2);
  142. }
  143. // Do explosion animation
  144. util.doExplosion = function(position) {
  145. var index = 0;
  146. sound.play("audio/explosion");
  147. var timer = window.setInterval(function() {
  148. if (index == util.explosionsImages.length || app.endOfGame) {
  149. window.clearInterval(timer);
  150. return;
  151. }
  152. var ctx = play.canvas.hasNode().getContext('2d');
  153. var image = document.getElementById(util.explosionsImages[index]);
  154. if (!enyo.platform.android || document.location.protocol.substr(0,4) == "http") {
  155. // HACK: Don't do on Android because it flash the screen
  156. ctx.save();
  157. ctx.translate(position.x*constant.tileSize, position.y*constant.tileSize);
  158. ctx.drawImage(image, 0, 0);
  159. ctx.restore();
  160. }
  161. index++;
  162. }, constant.explosionInterval);
  163. }
  164. // Get URL parameter
  165. util.getUrlParameter = function(name) {
  166. var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
  167. return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
  168. }
  169. // Get a random number
  170. util.random = function(max) {
  171. return Math.floor(Math.random()*max);
  172. }
  173. // Get randomly a unit name
  174. util.randomUnit = function(stats) {
  175. var unittype = util.random(10);
  176. for (var i = util.unitStats.length-1 ; i > 0 ; i--) {
  177. if (unittype >= stats[i]) {
  178. return util.unitTypes[i];
  179. }
  180. }
  181. }
  182. // Computer nearest unit from me
  183. util.nearestUnit = function(me, units) {
  184. var current = -1;
  185. var near = constant.boardWidth*constant.boardHeight;
  186. for (var i = 0 ; i < units.length ; i++) {
  187. var distance = Math.abs(units[i].x - me.x)+Math.abs(units[i].y - me.y);
  188. if (distance < near) {
  189. current = i;
  190. near = distance;
  191. }
  192. }
  193. return current != -1 ? units[current] : null;
  194. }