trinSprite.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. function TrinSprite(animation) {
  2. TrinSprite.superclass.constructor.apply(this);
  3. this.animations = [];
  4. this.currentAnimation = null;
  5. this.currentFrame = 0;
  6. this.animationProgress = 0;
  7. this.animationSpeed = 30;
  8. this.alpha = 1;
  9. this.looped = true;
  10. this.cutArea = new TrinRectangle(0, 0, 0, 0);
  11. if (animation !== undefined) {
  12. this.addAnimationFromCache(animation);
  13. }
  14. }
  15. extend(TrinSprite, TrinEntity);
  16. TrinSprite.prototype.update = function() {
  17. TrinSprite.superclass.update.apply(this);
  18. if (this.currentAnimation !== null) {
  19. var prevFrame = this.currentFrame;
  20. this.animationProgress += 1 / (_TrinGame.frameRate / this.animationSpeed);
  21. if (this.animationProgress >= this.currentAnimation.frames.length) {
  22. if (this.looped) {
  23. this.animationProgress -= Math.floor(this.animationProgress);
  24. } else {
  25. this.animationProgress = this.currentAnimation.frames.length - 1;
  26. }
  27. }
  28. this.currentFrame = Math.floor(this.animationProgress);
  29. if (this.currentFrame !== prevFrame) {
  30. this.updateSize();
  31. }
  32. }
  33. };
  34. TrinSprite.prototype.draw = function(context) {
  35. TrinSprite.superclass.draw.apply(this, [context]);
  36. var animation = this.currentAnimation;
  37. if (animation !== null && this.visible && this.exists) {
  38. var frame = animation.frames[Math.floor(this.currentFrame)];
  39. var sx = frame.x + this.cutArea.x;
  40. var sy = frame.y + this.cutArea.y;
  41. var sw = frame.width - this.cutArea.width - this.cutArea.x;
  42. var sh = frame.height - this.cutArea.height - this.cutArea.y;
  43. var dx = this.x + this.cutArea.x - this.orign.x * this.scale.x;
  44. var dy = this.y + this.cutArea.y - this.orign.y * this.scale.y;
  45. var dw = (frame.width - this.cutArea.width - this.cutArea.x) * this.scale.x;
  46. var dh = (frame.height - this.cutArea.height - this.cutArea.y) * this.scale.y;
  47. context.globalAlpha = this.alpha;
  48. if (sw > 0 && sh > 0 && dw > 0 && dh > 0) {
  49. context.drawImage(animation.image, sx, sy, sw, sh, dx, dy, dw, dh);
  50. }
  51. }
  52. //this.drawBounds(context);
  53. };
  54. TrinSprite.prototype.drawBounds = function(context) {
  55. context.beginPath();
  56. context.rect(this.bounds.x, this.bounds.y, this.bounds.width, this.bounds.height);
  57. context.stroke();
  58. };
  59. TrinSprite.prototype.addAnimationFromCache = function(name, switchToThis) {
  60. if (switchToThis === undefined) {
  61. switchToThis = true;
  62. }
  63. var animation = TrinAnimation.prototype.getAnimation(name);
  64. if (animation === null) {
  65. return;
  66. }
  67. this.animations[name] = animation;
  68. if (switchToThis) {
  69. this.switchAnimation(name);
  70. }
  71. };
  72. TrinSprite.prototype.switchAnimation = function(name) {
  73. var animation = this.animations[name];
  74. if (animation === undefined || animation === null) {
  75. console.log("Can't switch to animation. No such added animation with name \"" + name + "\"");
  76. return;
  77. }
  78. this.currentAnimation = animation;
  79. this.updateSize();
  80. };
  81. TrinSprite.prototype.updateSize = function() {
  82. var frame = this.currentAnimation.frames[Math.floor(this.currentFrame)];
  83. this.width = frame.width * this.scale.x;
  84. this.height = frame.height * this.scale.y;
  85. this.updateBounds();
  86. };
  87. TrinSprite.prototype.updateBounds = function() {
  88. this.bounds.set(this.x - this.orign.x * this.scale.x + this.boundsOffset.left,
  89. this.y - this.orign.y * this.scale.y + this.boundsOffset.top,
  90. this.width - this.boundsOffset.right,
  91. this.height - this.boundsOffset.bottom);
  92. };
  93. TrinSprite.prototype.destroy = function() {
  94. TrinSprite.superclass.destroy.apply(this);
  95. this.animations = null;
  96. this.currentAnimation = null;
  97. this.cutArea = null;
  98. };
  99. TrinSprite.prototype.hitTest = function(x, y, d) {
  100. TrinSprite.superclass.hitTest.apply(this);
  101. var animation = this.currentAnimation;
  102. if (animation === null) {
  103. return false;
  104. }
  105. if (d === undefined) {
  106. d = 0;
  107. }
  108. x -= this.x;
  109. y -= this.y;
  110. var frame = animation.frames[this.currentFrame];
  111. var l = x - d;
  112. var r = x + d;
  113. var t = y - d;
  114. var b = y + d;
  115. if (l > this.bounds.right || r < this.bounds.left || t > this.bounds.bottom || b < this.bounds.top) {
  116. return false;
  117. }
  118. var image = TrinAnimation.prototype.IMAGES[animation.name];
  119. var canvas = document.createElement("Canvas");
  120. canvas.width = frame.width;
  121. canvas.height = frame.height;
  122. var context = canvas.getContext("2d");
  123. context.drawImage(image, frame.x, frame.y, frame.width, frame.height, 0, 0, frame.width, frame.height);
  124. var imageData;
  125. var cx = 0;
  126. var cy = 0;
  127. try {
  128. imageData = context.getImageData(frame.x, frame.y, frame.width, frame.height);
  129. } catch (e) {
  130. return true;
  131. }
  132. for (var i = -d; i <= d; i++) {
  133. for (var j = -d; j <= d; j++) {
  134. cx = x + j;
  135. cy = y + i;
  136. if (cx >= 0 && cx < this.width && cy >= 0 && cy < this.height) {
  137. if (imageData.data[(cy * frame.width + cx) * 4 + 3] > 0) {
  138. return true;
  139. }
  140. }
  141. }
  142. }
  143. return false;
  144. };
  145. TrinSprite.prototype.isFinished = function() {
  146. return this.currentFrame === this.currentAnimation.frames.length - 1;
  147. };
  148. TrinSprite.prototype.play = function() {
  149. this.animationProgress = 0;
  150. };