CBomb.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. function CBomb(iX,iY,oSpriteSheet,oDim,iSpeed,iType,oAttachContainer){
  2. var _bSliced;
  3. var _iRunTime = 0;
  4. var _iRunFactor;
  5. var _iSpeed;
  6. var _iShiftx;
  7. var _iRotFactor;
  8. var _iType;
  9. var _iSliceLenOffset;
  10. var _iRadius;
  11. var _iBrightness;
  12. var _iRotRay;
  13. var _iIdInterval;
  14. var _iIdRayInterval;
  15. var _iCurTrembleIndex;
  16. var _iTimeRay;
  17. var _vStartDir;
  18. var _oFinalPos;
  19. var _aTrembleSequence;
  20. var _oDim;
  21. var _oSprite;
  22. var _oAttachRay;
  23. var _oAttachContainer;
  24. this._init = function(iX,iY,oSpriteSheet,oDim,iSpeed,iType,oAttachContainer){
  25. _oAttachContainer = oAttachContainer;
  26. _bSliced= false;
  27. _oDim = oDim;
  28. _oAttachRay = new createjs.Container();
  29. _oAttachContainer.addChild(_oAttachRay);
  30. _oSprite = createSprite(oSpriteSheet, "idle",oDim.regX,oDim.regY,oDim.width,oDim.height);
  31. _oSprite.x = iX;
  32. _oSprite.y = iY;
  33. _oSprite.regX = _oDim.width/2;
  34. _oSprite.regY = _oDim.height/2;
  35. _oAttachContainer.addChild(_oSprite);
  36. if(_oDim.width < _oDim.height){
  37. _iRadius = Math.floor(_oDim.width/2);
  38. }else{
  39. _iRadius = Math.floor(_oDim.height/2);
  40. }
  41. _iSliceLenOffset = Math.floor((_iRadius * 2) * 0.5);
  42. _iSliceLenOffset = Math.pow(_iSliceLenOffset,2);
  43. _iSpeed = iSpeed;
  44. _iRunFactor = _iSpeed/9;
  45. if(iX > (CANVAS_WIDTH/2) ){
  46. _iShiftx = randomFloatBetween(-0.5,0,2);
  47. }else{
  48. _iShiftx = randomFloatBetween(0,0.5,2);
  49. }
  50. _iRotFactor = randomFloatBetween(-MAX_FRUIT_ROT_SPEED,MAX_FRUIT_ROT_SPEED,2);
  51. _vStartDir = new CVector2(0,-1);
  52. _iType = iType;
  53. _aTrembleSequence = new Array({x:10,y:0},{x:-20,y:0},{x:10,y:-10},{x:0,y:20},{x:10,y:-10},{x:-10,y:0});
  54. playSound("bomb_fuse",1,false);
  55. };
  56. this.unload = function(){
  57. stopSound("bomb_fuse");
  58. _oAttachContainer.removeChild(_oSprite);
  59. };
  60. this.explode = function(){
  61. stopSound("bomb_fuse");
  62. playSound(s_aFruitSpliceSound[_iType],1,false);
  63. _iBrightness = 0;
  64. _iCurTrembleIndex = 0;
  65. _iRotRay = 0;
  66. _iTimeRay = 200;
  67. _bSliced = true;
  68. _oFinalPos = {x:_oSprite.x,y:_oSprite.y};
  69. var oParent = this;
  70. _iIdInterval = setInterval(function(){oParent.changeBrightness();oParent.tremble();},20);
  71. _iIdRayInterval = setInterval(function(){oParent.showRay()},150);
  72. };
  73. this.tremble = function(){
  74. var oDir = _aTrembleSequence[_iCurTrembleIndex];
  75. _oSprite.x = _oSprite.x + oDir.x;
  76. _oSprite.y = _oSprite.y + oDir.y;
  77. _iCurTrembleIndex++;
  78. if(_iCurTrembleIndex === _aTrembleSequence.length){
  79. _iCurTrembleIndex = 0;
  80. }
  81. };
  82. this.showRay = function(){
  83. var oSprite = s_oSpriteLibrary.getSprite('bomb_ray');
  84. var oRay = createBitmap(oSprite);
  85. oRay.alpha = 0;
  86. oRay.regY = oSprite.height/2;
  87. oRay.x = _oFinalPos.x;
  88. oRay.y = _oFinalPos.y;
  89. oRay.scaleX = randomFloatBetween(0.5,1,2);
  90. oRay.rotation = _iRotRay;
  91. _oAttachRay.addChild(oRay);
  92. createjs.Tween.get(oRay).to({alpha:1}, 200);
  93. _iRotRay += randomFloatBetween(40,55,1);
  94. if(_iRotRay > 320){
  95. clearInterval(_iIdInterval);
  96. clearInterval(_iIdRayInterval);
  97. s_oGame.showExplosion();
  98. }
  99. };
  100. this.changeBrightness = function(){
  101. if(_iBrightness <201){
  102. var oMatrix = new createjs.ColorMatrix().adjustBrightness(_iBrightness);
  103. _oSprite.filters = [
  104. new createjs.ColorMatrixFilter(oMatrix)
  105. ];
  106. _oSprite.cache(0,0, _oDim.width, _oDim.height);
  107. _iBrightness +=4;
  108. }
  109. };
  110. this.getCenter = function(){
  111. return new CVector2(_oSprite.x,_oSprite.y);
  112. };
  113. this.getRadius = function(){
  114. return _iRadius;
  115. };
  116. this.getSliceOffset = function(){
  117. return _iSliceLenOffset;
  118. };
  119. this.getY = function(){
  120. return _oSprite.y;
  121. };
  122. this.getSprite = function(){
  123. return _oSprite;
  124. };
  125. this.getRotation = function(){
  126. return _oSprite.rotation;
  127. };
  128. this.getVectorDir = function(){
  129. return rotateVector2D(toRadian(-_oSprite.rotation),_vStartDir);
  130. };
  131. this.isSliced = function(){
  132. return _bSliced;
  133. };
  134. this.getType = function(){
  135. return _iType;
  136. };
  137. this.update = function(){
  138. _iRunTime += _iRunFactor;
  139. if(_bSliced === false){
  140. _oSprite.y = _oSprite.y - _iSpeed + _iRunTime * _iRunTime/1000;
  141. _oSprite.x = _oSprite.x + _iRunTime*0.1*_iShiftx;
  142. _oSprite.rotation += _iRotFactor;
  143. }
  144. };
  145. this._init(iX,iY,oSpriteSheet,oDim,iSpeed,iType,oAttachContainer);
  146. }