Sprite.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // Sprite.js
  2. // For Sprite management and drawing support
  3. // FZ, Copyright (c) 2010 Zlongames
  4. (function()
  5. {
  6. FZ.Sprite = FZ.newClass(
  7. {
  8. __currentIdx : 0,
  9. __currentFrameTime: 0.0,
  10. __matrixDirty: true,
  11. __inverseMatrixDirty: true,
  12. init:function (_images , autoPlay) // _images [ [image1, time1 /*optional*/ ] , [ image2 , time2 ] . [image3 ] , ... ]
  13. {
  14. this.__theImages = [];
  15. this.__children = [];
  16. this.__localMatrix= new FZ.Math.Matrix3();
  17. this.__worldMatrix= new FZ.Math.Matrix3();
  18. this.__inverseMatrix = new FZ.Math.Matrix3();
  19. this.__parentSprite= undefined;
  20. this.__playing = Boolean(autoPlay);
  21. if (!_images || _images.length === 0)
  22. {
  23. this.__currentIdx = -1; // set Idx to -1 to indicate there is no image in this sprite
  24. return;
  25. }
  26. for (var i=0, forLength = _images.length; i< forLength; ++i)
  27. {
  28. if (_images[i] instanceof Array)
  29. this.__theImages.push([ _images[i][0] , _images[i][1]? _images[i][1]:0.033]);
  30. else
  31. this.__theImages.push([ _images[i] , 0.033]);
  32. }
  33. },
  34. clone: function ()
  35. {
  36. var cloneSprite = new FZ.Sprite();
  37. cloneSprite.__theImages = this.__theImages;
  38. cloneSprite.__localMatrix = this.__localMatrix.clone();
  39. cloneSprite.__currentIdx = this.__theImages.length === 0? -1 : 0;
  40. for (var i=0 , len = this.__children.length; i< len; ++i)
  41. {
  42. cloneSprite.addChild(this.__children[i].clone());
  43. }
  44. return cloneSprite;
  45. },
  46. setFrameTime:function(frameTime)
  47. {
  48. if (typeof frameTime === "number")
  49. for (var i=0, len=this.__theImages.length; i<len; ++i)
  50. this.__theImages[i][1]=frameTime;
  51. else
  52. for (var i=0; i<frameTime.length && i<__theImages.length; ++i)
  53. this.__theImages[i][1]=frameTime[i];
  54. },
  55. addChild:function(theChild)
  56. {
  57. this.__children.push(theChild);
  58. theChild.__parentSprite = this;
  59. },
  60. update: function (deltaTime)
  61. {
  62. if (!this.__playing) return;
  63. for (var i=0 , len = this.__children.length; i< len; ++i)
  64. {
  65. this.__children[i].update(deltaTime);
  66. }
  67. if (this.__currentIdx === -1)
  68. return;
  69. this.__currentFrameTime += deltaTime;
  70. while (this.__currentFrameTime > this.__theImages[this.__currentIdx][1])
  71. {
  72. this.__currentFrameTime -= this.__theImages[this.__currentIdx][1];
  73. this.__currentIdx++;
  74. if (this.__currentIdx === this.__theImages.length)
  75. this.__currentIdx = 0;
  76. }
  77. },
  78. setFrame:function(index)
  79. {
  80. this.__currentIdx = FZ.Math.clamp(index , 0, this.__theImages.length-1);
  81. },
  82. stop: function()
  83. {
  84. this.__playing = false;
  85. },
  86. play: function()
  87. {
  88. this.__playing = true;
  89. },
  90. getPosition: function()
  91. {
  92. this.__DirtyMatrixProcess();
  93. var retValue = new FZ.Math.Vector2(0,0);
  94. return retValue.applyTransform(this.__worldMatrix, retValue);
  95. },
  96. localToWorld:function(position)
  97. {
  98. this.__DirtyMatrixProcess();
  99. return position.applyTransform(this.__worldMatrix);
  100. },
  101. worldToLocal:function(position)
  102. {
  103. this.__DirtyInverseMatrixProcess();
  104. return position.applyTransform(this.__inverseMatrix);
  105. },
  106. __markMatrixDirty:function()
  107. {
  108. if (!this.__matrixDirty)
  109. {
  110. this.__matrixDirty = true;
  111. this.__inverseMatrixDirty = true;
  112. for (var i=0 , len = this.__children.length; i< len; ++i)
  113. {
  114. this.__children[i].__markMatrixDirty();
  115. }
  116. }
  117. },
  118. __DirtyInverseMatrixProcess:function()
  119. {
  120. this.__DirtyMatrixProcess();
  121. if (this.__inverseMatrixDirty)
  122. {
  123. this.__worldMatrix.inverseAffine(this.__inverseMatrix);
  124. this.__inverseMatrixDirty = false;
  125. }
  126. },
  127. __DirtyMatrixProcess:function()
  128. {
  129. if (this.__matrixDirty)
  130. {
  131. if ( this.__parentSprite )
  132. {
  133. //this.__localMatrix.mul(this.__parentSprite.__worldMatrix , this.__worldMatrix);
  134. this.__parentSprite.__worldMatrix.mul(this.__localMatrix, this.__worldMatrix);
  135. } else
  136. {
  137. this.__worldMatrix = this.__localMatrix;
  138. }
  139. this.__matrixDirty = false;
  140. }
  141. },
  142. draw:function (_ctx)
  143. {
  144. this.__DirtyMatrixProcess();
  145. for (var i=0 , len = this.__children.length; i< len; ++i)
  146. {
  147. this.__children[i].draw(_ctx);
  148. }
  149. if (this.__currentIdx === -1)
  150. return;
  151. _ctx.setTransform(this.__worldMatrix.m11, this.__worldMatrix.m12, this.__worldMatrix.m21, this.__worldMatrix.m22, this.__worldMatrix.dx, this.__worldMatrix.dy);
  152. _ctx.drawImage(this.__theImages[this.__currentIdx][0], 0,0);
  153. },
  154. translate:function (x,y)
  155. {
  156. this.__localMatrix.translate(x,y);
  157. this.__markMatrixDirty();
  158. },
  159. rotate: function (angle)
  160. {
  161. this.__localMatrix.rotate(angle);
  162. this.__markMatrixDirty();
  163. },
  164. scale1: function (s)
  165. {
  166. this.__localMatrix.scale1(s);
  167. this.__markMatrixDirty();
  168. },
  169. scale2: function (sx, sy)
  170. {
  171. this.__localMatrix.scale2(sx,sy);
  172. this.__markMatrixDirty();
  173. },
  174. makeTranslate:function (x,y)
  175. {
  176. this.__localMatrix.makeTranslate(x,y);
  177. this.__markMatrixDirty();
  178. },
  179. makeRotate: function (angle)
  180. {
  181. this.__localMatrix.makeRotate(angle);
  182. this.__markMatrixDirty();
  183. },
  184. makeScale1: function (s)
  185. {
  186. this.__localMatrix.makeScale1(s);
  187. this.__markMatrixDirty();
  188. },
  189. makeScale2: function (sx, sy)
  190. {
  191. this.__localMatrix.makeScale2(sx,sy);
  192. this.__markMatrixDirty();
  193. },
  194. resetMatrix: function()
  195. {
  196. this.__localMatrix.identity();
  197. this.__markMatrixDirty();
  198. }
  199. }
  200. );
  201. FZ.spriteManager=
  202. {
  203. __sprites:{},
  204. addResource:function(name, zOrder, URL, x,y, cols)
  205. {
  206. FZ.ResourceManager.addResource({"Name":name, "Type":"Image", "URL":URL, callback: function(theImage)
  207. {
  208. if (cols === 1)
  209. FZ.spriteManager.__sprites[name] = new FZ.Sprite([theImage]);
  210. else
  211. {
  212. var theImages =[];
  213. for (var i=0; i<cols; ++i)
  214. {
  215. var tCan = document.createElement("canvas");
  216. tCan.width = theImage.width/cols;
  217. tCan.height = theImage.height;
  218. tCan.getContext("2d").drawImage(theImage, theImage.width /cols * i, 0, theImage.width /cols, theImage.height , 0 , 0 , theImage.width/cols, theImage.height);
  219. theImages.push([tCan]);
  220. }
  221. FZ.spriteManager.__sprites[name] = new FZ.Sprite(theImages);
  222. }
  223. FZ.spriteManager.__sprites[name].makeTranslate(x,y);
  224. } });
  225. },
  226. getSprite:function (name)
  227. {
  228. return this.__sprites[name];
  229. }
  230. };
  231. })();