CStadiumBall.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. function CStadiumBall(pStart, pEnd, pEndBounce, oParentContainer, iArea, oHitValue){
  2. var _iCntFrames = 0;
  3. var _iMaxFrames = 40;
  4. var _iArea;
  5. var _bUpdate;
  6. var _oBall;
  7. var _oBallShadow;
  8. var _pStartPoint = {x: 0, y: 0};
  9. var _pStartPointShadow = {x: 0, y: 0};
  10. var _pEndPoint = {x: 0, y: 0};
  11. var _pEndBounce = {x: 0, y: 0};
  12. var _aTrajectoryPoint;
  13. var _aTrajectoryPointShadow;
  14. var _bBounced = false;
  15. this._init = function(pStart, pEnd, pEndBounce, oParentContainer, iArea){
  16. _iArea = iArea;
  17. _oBallShadow = createBitmap(s_oSpriteLibrary.getSprite("ball_shadow"));
  18. _oBallShadow.x = pStart.x;
  19. _oBallShadow.y = pStart.y;
  20. _oBallShadow.regX = _oBallShadow.width/2;
  21. _oBallShadow.regY = _oBallShadow.height/2;
  22. _oBallShadow.scaleX = 0.2;
  23. _oBallShadow.scaleY = 0.2;
  24. _oBallShadow.alpha = 0.5;
  25. _oBallShadow.rotation = 0;
  26. oParentContainer.addChild(_oBallShadow);
  27. _oBall = createBitmap(s_oSpriteLibrary.getSprite("ball"));
  28. _oBall.x = pStart.x;
  29. _oBall.y = pStart.y;
  30. _oBall.regX = _oBall.width/2;
  31. _oBall.regY = _oBall.height/2;
  32. _oBall.scaleX = 0.3;
  33. _oBall.scaleY = 0.3;
  34. _oBall.rotation = 0;
  35. oParentContainer.addChild(_oBall);
  36. _pStartPointShadow.x = _oBallShadow.x;
  37. _pStartPointShadow.y = _oBallShadow.y;
  38. _pStartPoint.x = _oBall.x;
  39. _pStartPoint.y = _oBall.y;
  40. _pEndPoint.x = pEnd.x;
  41. _pEndPoint.y = pEnd.y;
  42. _pEndBounce.x = pEndBounce.x;
  43. _pEndBounce.y = pEndBounce.y;
  44. _bUpdate = true;
  45. this._calculateMid(_pStartPoint, _pEndPoint);
  46. };
  47. this.viewBall = function(){
  48. _oBall.visible = true;
  49. _oBallShadow.visible = true;
  50. _bUpdate = true;
  51. };
  52. this.hideBall = function(){
  53. _oBall.visible = false;
  54. _oBallShadow.visible = false;
  55. };
  56. this._calculateMid = function(pStartPoint, pEndPoint){
  57. var t0;
  58. if(_bBounced === false){
  59. var iVal = Math.random() * ((CANVAS_WIDTH / 2) - (CANVAS_WIDTH / 2)) + ((CANVAS_WIDTH / 2));
  60. if (iVal > (CANVAS_WIDTH / 2)) {
  61. t0 = new createjs.Point((CANVAS_WIDTH/2), Math.floor(Math.random() * (CANVAS_HEIGHT / 2) - 100) + 100);
  62. }else {
  63. t0 = new createjs.Point((CANVAS_WIDTH/2), Math.floor(Math.random() * (CANVAS_HEIGHT / 2) - 100) + 100);
  64. }
  65. }else{
  66. if(pStartPoint.x < CANVAS_WIDTH/2){
  67. t0 = new createjs.Point(pEndPoint.x+5, pStartPoint.y-20);
  68. }else{
  69. t0 = new createjs.Point(pEndPoint.x-5, pStartPoint.y-20);
  70. }
  71. }
  72. _aTrajectoryPoint = {start:pStartPoint, end:pEndPoint, traj:t0};
  73. };
  74. this._updateBall = function(){
  75. _iCntFrames += STEP_SPEED_STADIUM;
  76. if (_iCntFrames > _iMaxFrames ) {
  77. _iCntFrames = 0;
  78. playSound("baseball_drop_bounce_grass",1,false);
  79. if(_bBounced === true || s_bBounce === false){
  80. _bUpdate = false;
  81. _iMaxFrames = 40;
  82. _oBall.scaleX = 0.3;
  83. _oBall.scaleY = 0.3;
  84. _oBallShadow.scaleX = 0.3;
  85. _oBallShadow.scaleY = 0.3;
  86. var iScore;
  87. switch (_iArea){
  88. case 0:
  89. iScore = Math.floor(AREA_VALUE[0]+(oHitValue/10));
  90. playSound("baseball_applauses",1,false);
  91. break;
  92. case 1:
  93. iScore = Math.floor(AREA_VALUE[1]+(oHitValue/5));
  94. playSound("crowd_ohhh",1,false);
  95. break;
  96. case 2:
  97. iScore = Math.floor(AREA_VALUE[2]+oHitValue+10);
  98. playSound("baseball_crowd_homerun",1,false);
  99. break;
  100. case 3:
  101. iScore = Math.floor(AREA_VALUE[2]+oHitValue+10);
  102. playSound("baseball_crowd_homerun",1,false);
  103. break;
  104. }
  105. if(iScore < 0){
  106. iScore *= -1;
  107. iScore -= 10;
  108. }
  109. if(_iArea >= 2){
  110. _oBall.visible = false;
  111. _oBallShadow.visible = false;
  112. }
  113. s_oStadium.viewAreaEffect( _iArea, iScore );
  114. }
  115. if(s_bBounce === true && _bBounced === false){
  116. _bBounced = true;
  117. _iMaxFrames = _iMaxFrames/2;
  118. this._calculateMid(_pEndPoint, _pEndBounce);
  119. }
  120. }
  121. else{
  122. var fLerp;
  123. fLerp=easeLinear( _iCntFrames, 0, 1, _iMaxFrames);
  124. if(_bBounced === false){
  125. var pPos = getTrajectoryPoint(fLerp, _aTrajectoryPoint);
  126. _oBall.x = pPos.x;
  127. _oBall.y = pPos.y;
  128. var fLerpShadow;
  129. fLerpShadow=easeInSine( _iCntFrames, 0, 1, _iMaxFrames);
  130. _aTrajectoryPointShadow = {start:_pStartPointShadow, end:_pEndPoint, traj:_pEndPoint};
  131. var pPos = getTrajectoryPoint(fLerpShadow, _aTrajectoryPointShadow);
  132. _oBallShadow.x = pPos.x;
  133. _oBallShadow.y = pPos.y;
  134. if(_oBall.scaleX > 0.25){
  135. _oBall.scaleX -= 0.005;
  136. _oBall.scaleY -= 0.005;
  137. _oBallShadow.scaleX -= 0.008;
  138. _oBallShadow.scaleY -= 0.008;
  139. }
  140. }else{
  141. var pPos = getTrajectoryPoint(fLerp, _aTrajectoryPoint);
  142. _oBall.x = pPos.x;
  143. _oBall.y = pPos.y;
  144. var fLerpShadow;
  145. fLerpShadow=easeInSine( _iCntFrames, 0, 1, _iMaxFrames);
  146. _aTrajectoryPointShadow = {start:_pEndPoint, end:_pEndBounce, traj:_pEndBounce};
  147. var pPos = getTrajectoryPoint(fLerpShadow, _aTrajectoryPointShadow);
  148. _oBallShadow.x = pPos.x;
  149. _oBallShadow.y = pPos.y;
  150. }
  151. }
  152. };
  153. this.getValue = function(){
  154. return _oBall;
  155. };
  156. this.reset = function(){
  157. _oBall.x = BALL_X;
  158. _oBall.y = BALL_Y;
  159. _oBall.regX = _oBall.width/2;
  160. _oBall.regY = _oBall.height/2;
  161. _oBall.scaleX = 0.4;
  162. _oBall.scaleY = 0.4;
  163. _oBall.rotation = 0;
  164. _oBall.visible = false;
  165. _pStartPoint.x = _oBall.x;
  166. _pStartPoint.y = _oBall.y;
  167. _pEndPoint.x = END_POINT_X;
  168. _pEndPoint.y = END_POINT_Y;
  169. STEP_SPEED_STADIUM = 0.5;
  170. };
  171. this.unload = function(){
  172. s_oStage.removeChild(oParentContainer);
  173. };
  174. this.update = function(){
  175. if(_bUpdate){
  176. this._updateBall();
  177. }
  178. };
  179. this._init(pStart, pEnd, pEndBounce, oParentContainer, iArea, oHitValue);
  180. }