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.

157 lines
4.3 KiB

  1. 
  2. // Light sprite class
  3. enyo.kind({
  4. name: "Sprite",
  5. published: { x: 0, y: 0, width: 0, height: 0, heading: 0, images: [], index: -1, sound: "" },
  6. // Create component
  7. create: function() {
  8. this.inherited(arguments);
  9. this.halfWidth = this.width/2;
  10. this.halfHeight = this.height/2;
  11. this.animating = false;
  12. },
  13. // Draw the sprite in the canvas context
  14. draw: function(ctx) {
  15. // Go to pos and heading and draw current image
  16. ctx.save();
  17. ctx.translate(this.x, this.y);
  18. var rotate = (90-this.heading) * (Math.PI / 180);
  19. ctx.rotate(rotate);
  20. ctx.translate(-this.halfWidth,-this.halfHeight);
  21. var image = document.getElementById(this.images[this.index]);
  22. ctx.drawImage(image, 0, 0);
  23. ctx.restore();
  24. },
  25. // Compute min rectangle around sprite
  26. computeRect: function() {
  27. var rotate = (90-this.heading) * (Math.PI / 180);
  28. var wc = this.halfWidth*Math.cos(rotate);
  29. var hc = this.halfHeight*Math.cos(rotate);
  30. var ws = this.halfWidth*Math.sin(rotate);
  31. var hs = this.halfHeight*Math.sin(rotate);
  32. var sx0 = this.x-wc+hs;
  33. var sy0 = this.y-hc+ws;
  34. var sx1 = this.x+wc+hs;
  35. var sy1 = this.y-hc-ws;
  36. var sx2 = this.x+wc-hs;
  37. var sy2 = this.y+hc-ws;
  38. var sx3 = this.x-wc-hs;
  39. var sy3 = this.y+hc+ws;
  40. var x0 = Math.min(sx0, Math.min(sx1, Math.min(sx2, sx3)));
  41. var y0 = Math.min(sy0, Math.min(sy1, Math.min(sy2, sy3)));
  42. var x1 = Math.max(sx1, Math.max(sx1, Math.max(sx2, sx3)));
  43. var y1 = Math.max(sy1, Math.max(sy1, Math.max(sy2, sy3)));
  44. return { x: x0, y: y0, dx: x1-x0, dy: y1-y0 };
  45. },
  46. // Draw border around sprite
  47. drawBorder: function(ctx) {
  48. ctx.save();
  49. ctx.strokeStyle = "blue";
  50. var rect = this.computeRect();
  51. ctx.strokeRect(rect.x, rect.y, rect.dx, rect.dy);
  52. ctx.restore();
  53. },
  54. // Undraw the sprite: i.e. clear canvas at the sprite pos
  55. unDraw: function(ctx) {
  56. var rect = this.computeRect();
  57. ctx.clearRect(rect.x, rect.y, rect.dx, rect.dy);
  58. },
  59. // Use first image for sprite
  60. firstImage: function() {
  61. this.index = 0;
  62. },
  63. // Use next image for sprite
  64. nextImage: function(max) {
  65. this.index = this.index + 1;
  66. if (max === undefined)
  67. max = this.images.length;
  68. if (this.index >= max)
  69. this.index = 0;
  70. },
  71. // Use a specific image
  72. useImage: function(index) {
  73. this.index = index;
  74. },
  75. // Play sound of the sprite
  76. playSound: function() {
  77. if (this.sound != "")
  78. FoodChain.sound.play(this.sound);
  79. },
  80. // Test if the sprite intersect another sprite
  81. intersect: function(sprite) {
  82. var r1 = this.computeRect();
  83. var r2 = sprite.computeRect();
  84. return !(r2.x > (r1.x+r1.dx) || (r2.x+r2.dx) < r1.x ||
  85. r2.y > (r1.y+r1.dy) || (r2.y+r2.dy) < r1.y);
  86. },
  87. // Compute distance between two sprites
  88. distance: function(sprite) {
  89. var dx = sprite.x-this.x;
  90. var dy = sprite.y-this.y;
  91. var dist = Math.sqrt(dx*dx+dy*dy);
  92. return dist;
  93. },
  94. // Animate the sprite using a list of image and a movement
  95. animate: function(ctx, images, dx, dy, action) {
  96. if (this.animating)
  97. return;
  98. this.animating = true;
  99. this.animation = {ctx: ctx, index: 0, images: images, dx: dx, dy: dy, action: action };
  100. this.animation.job = window.setInterval(enyo.bind(this, "animateTimer"), 50);
  101. },
  102. // Timer use of animation
  103. animateTimer: function() {
  104. // End of animation ?
  105. if (this.animation.index == this.animation.images.length) {
  106. this.animating = false;
  107. window.clearInterval(this.animation.job);
  108. return;
  109. }
  110. // Draw current frame
  111. this.unDraw(this.animation.ctx);
  112. if (this.animation.dx != undefined)
  113. this.x += this.animation.dx;
  114. if (this.animation.dy != undefined)
  115. this.y += this.animation.dy;
  116. this.useImage(this.animation.images[this.animation.index]);
  117. this.draw(this.animation.ctx);
  118. // HACK: On Android, force redraw of canvas
  119. if (enyo.platform.android && document.location.protocol.substr(0,4) != "http") {
  120. document.getElementById('acanvas').style.display='none';
  121. document.getElementById('acanvas').offsetHeight;
  122. document.getElementById('acanvas').style.display='block';
  123. }
  124. // Action to do at each move
  125. if (this.animation.action != undefined) {
  126. if (!this.animation.action(this)) {
  127. this.animating = false;
  128. this.animation.index = this.animation.images.length;
  129. window.clearInterval(this.animation.job);
  130. return;
  131. }
  132. }
  133. // Next frame
  134. this.animation.index = this.animation.index + 1;
  135. },
  136. });