CBall.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. function CBall(oParentContainerBall){
  2. var _iCntFrames = 0;
  3. var _iMaxFrames =40;
  4. var _iWidth;
  5. var _iHeight;
  6. var _bUpdate;
  7. var _bHitted;
  8. var _oBall;
  9. var _oHitValue;
  10. var _pStartPoint = {x: 0, y: 0};
  11. var _pEndPoint = {x: 0, y: 0};
  12. var _aTrajectoryPoint;
  13. this._init = function(oParentContainerBall){
  14. var oSprite = s_oSpriteLibrary.getSprite("ball");
  15. _iWidth = oSprite.width;
  16. _iHeight = oSprite.height;
  17. _oBall = createBitmap(oSprite);
  18. this.reset(oSprite);
  19. oParentContainerBall.addChild(_oBall);
  20. };
  21. this.reset = function(){
  22. _oBall.x = BALL_X;
  23. _oBall.y = BALL_Y;
  24. _oBall.regX = _iWidth/2;
  25. _oBall.regY = _iHeight/2;
  26. _oBall.scaleX = 0.4;
  27. _oBall.scaleY = 0.4;
  28. _oBall.rotation = 0;
  29. _oBall.visible = false;
  30. _pStartPoint.x = _oBall.x;
  31. _pStartPoint.y = _oBall.y;
  32. _pEndPoint.x = END_POINT_X;
  33. _pEndPoint.y = END_POINT_Y;
  34. STEP_SPEED_BALL_HITTED = 1.3;
  35. _bHitted = false;
  36. };
  37. this.viewBall = function(){
  38. _oBall.visible = true;
  39. this._calculateMid(_pStartPoint, _pEndPoint);
  40. _bUpdate = true;
  41. };
  42. this.hideBall = function(){
  43. _oBall.visible = false;
  44. };
  45. this._calculateMid = function(pStartPoint, pEndPoint){
  46. var t0;
  47. var iRandT0 = Math.floor(Math.random() * 50) + 1;
  48. if(_bHitted === true){
  49. if (pEndPoint.x < CANVAS_WIDTH/2) {
  50. if (pEndPoint.y > (CANVAS_HEIGHT / 2)) {
  51. t0 = new createjs.Point(Math.floor(Math.random() * (CANVAS_WIDTH/2)) + 100, ((CANVAS_HEIGHT/2)-200)-iRandT0);
  52. }else {
  53. t0 = new createjs.Point(Math.floor(Math.random() * (CANVAS_WIDTH/2)) + 100, ((CANVAS_HEIGHT/2)-200)+iRandT0);
  54. }
  55. }else if (pEndPoint.x > CANVAS_WIDTH/2) {
  56. if (pEndPoint.y > (CANVAS_HEIGHT / 2)) {
  57. t0 = new createjs.Point(Math.floor(Math.random() * (CANVAS_WIDTH/2)) + 300, ((CANVAS_HEIGHT/2)-200)-iRandT0);
  58. }else {
  59. t0 = new createjs.Point(Math.floor(Math.random() * (CANVAS_WIDTH/2)) + 300, ((CANVAS_HEIGHT/2)-200)+iRandT0);
  60. }
  61. }else{
  62. if (pEndPoint.x > (CANVAS_WIDTH / 2)) {
  63. t0 = new createjs.Point((CANVAS_WIDTH/2)-50, Math.floor(Math.random() * (CANVAS_HEIGHT / 2) - 100) + 100);
  64. }else {
  65. t0 = new createjs.Point((CANVAS_WIDTH/2)+50, Math.floor(Math.random() * (CANVAS_HEIGHT / 2) - 100) + 100);
  66. }
  67. }
  68. }else{
  69. var iVal = Math.random();
  70. if (iVal < 0.5) {
  71. t0 = new createjs.Point((CANVAS_WIDTH/2)-50, Math.floor(Math.random() * (CANVAS_HEIGHT / 2) - 100) + 100);
  72. }else {
  73. t0 = new createjs.Point((CANVAS_WIDTH/2)+50, Math.floor(Math.random() * (CANVAS_HEIGHT / 2) - 100) + 100);
  74. }
  75. }
  76. _aTrajectoryPoint = {start:pStartPoint, end:pEndPoint, traj:t0};
  77. };
  78. this._updateBall = function(){
  79. _iCntFrames += STEP_SPEED_BALL_HITTED;
  80. if (_iCntFrames > _iMaxFrames ) {
  81. _iCntFrames = 0;
  82. _oBall.visible=false; //this is to resolve an issue when ball respawn
  83. _bUpdate = false;
  84. if(_bHitted === true){
  85. s_oGame._viewStadium(_oHitValue, _pEndPoint);
  86. }else{
  87. s_oGame._ballMissed();
  88. }
  89. }
  90. var fLerp;
  91. fLerp=easeLinear( _iCntFrames, 0, 1, _iMaxFrames);
  92. var pPos = getTrajectoryPoint(fLerp, _aTrajectoryPoint);
  93. _oBall.x = pPos.x;
  94. _oBall.y = pPos.y;
  95. if(_bHitted === true){
  96. if(_oBall.scaleX >= 0){
  97. _oBall.scaleX -= 0.03;
  98. _oBall.scaleY -= 0.03;
  99. }else{
  100. STEP_SPEED_BALL_HITTED -=0.2;
  101. }
  102. }else{
  103. if(_oBall.scaleX < 1){
  104. _oBall.scaleX += 0.03;
  105. _oBall.scaleY += 0.03;
  106. }else{
  107. STEP_SPEED_BALL_HITTED +=0.2;
  108. }
  109. }
  110. };
  111. this.getValue = function(){
  112. return _oBall;
  113. };
  114. this.hittedControl = function(){
  115. STEP_SPEED_BALL_HITTED = 2;
  116. if(_oBall.y >= PERFECT_HIT_Y-OFFSET_FOR_PERFECT_HIT && _oBall.y <= PERFECT_HIT_Y+OFFSET_FOR_PERFECT_HIT){
  117. playSound("baseball_hit_ball",1,false);
  118. if(_aTrajectoryPoint.traj.x < CANVAS_WIDTH/2){ //IF IS COMING FROM LEFT
  119. _iCntFrames = 0;
  120. _pStartPoint.x = PERFECT_HIT_X;
  121. _pStartPoint.y = PERFECT_HIT_Y;
  122. _pEndPoint.x = END_POINT_X_PERFECT_LEFT;
  123. _pEndPoint.y = END_POINT_Y_PERFECT;
  124. _oHitValue = _oBall.y - PERFECT_HIT_Y;
  125. if(_oHitValue < 0){
  126. _oHitValue *= -1;
  127. }
  128. _bHitted = true;
  129. _aTrajectoryPoint = {start:_pStartPoint, end:_pEndPoint, traj:_pEndPoint};
  130. }else{
  131. _iCntFrames = 0;
  132. _pStartPoint.x = PERFECT_HIT_X;
  133. _pStartPoint.y = PERFECT_HIT_Y;
  134. _pEndPoint.x = END_POINT_X_PERFECT_RIGHT;
  135. _pEndPoint.y = END_POINT_Y_PERFECT;
  136. _oHitValue = _oBall.y - PERFECT_HIT_Y;
  137. if(_oHitValue < 0){
  138. _oHitValue *= -1;
  139. }
  140. _bHitted = true;
  141. _aTrajectoryPoint = {start:_pStartPoint, end:_pEndPoint, traj:_pEndPoint};
  142. }
  143. }else if(_oBall.y >= ALMOST_MINUS && _oBall.y <= PERFECT_HIT_Y){
  144. playSound("baseball_hit_ball",1,false);
  145. if(_aTrajectoryPoint.traj.x < CANVAS_WIDTH/2){ //IF IS COMING FROM LEFT
  146. _iCntFrames = 0;
  147. _pStartPoint.x = PERFECT_HIT_X;
  148. _pStartPoint.y = PERFECT_HIT_Y;
  149. _pEndPoint.x = END_POINT_X_ALMOST_MINUS_RIGHT;
  150. _pEndPoint.y = END_POINT_Y_ALMOST_MINUS;
  151. _oHitValue = _oBall.y - PERFECT_HIT_Y;
  152. if(_oHitValue < 0){
  153. _oHitValue *= -1;
  154. }
  155. _bHitted = true;
  156. _aTrajectoryPoint = {start:_pStartPoint, end:_pEndPoint, traj:_pEndPoint};
  157. }else{
  158. _iCntFrames = 0;
  159. _pStartPoint.x = PERFECT_HIT_X;
  160. _pStartPoint.y = PERFECT_HIT_Y;
  161. _pEndPoint.x = END_POINT_X_ALMOST_MINUS_LEFT;
  162. _pEndPoint.y = END_POINT_Y_ALMOST_MINUS;
  163. _oHitValue = _oBall.y - PERFECT_HIT_Y;
  164. if(_oHitValue < 0){
  165. _oHitValue *= -1;
  166. }
  167. _bHitted = true;
  168. _aTrajectoryPoint = {start:_pStartPoint, end:_pEndPoint, traj:_pEndPoint};
  169. }
  170. }else if(_oBall.y <= ALMOST_PLUS && _oBall.y >= PERFECT_HIT_Y){
  171. playSound("baseball_hit_ball",1,false);
  172. if(_aTrajectoryPoint.traj.x < CANVAS_WIDTH/2){ //IF IS COMING FROM LEFT
  173. _iCntFrames = 0;
  174. _pStartPoint.x = PERFECT_HIT_X;
  175. _pStartPoint.y = PERFECT_HIT_Y;
  176. _pEndPoint.x = END_POINT_X_ALMOST_PLUS_RIGHT;
  177. _pEndPoint.y = END_POINT_Y_ALMOST_PLUS;
  178. _oHitValue = _oBall.y - PERFECT_HIT_Y;
  179. if(_oHitValue < 0){
  180. _oHitValue *= -1;
  181. }
  182. _bHitted = true;
  183. _aTrajectoryPoint = {start:_pStartPoint, end:_pEndPoint, traj:_pEndPoint};
  184. }else{
  185. _iCntFrames = 0;
  186. _pStartPoint.x = PERFECT_HIT_X;
  187. _pStartPoint.y = PERFECT_HIT_Y;
  188. _pEndPoint.x = END_POINT_X_ALMOST_PLUS_LEFT;
  189. _pEndPoint.y = END_POINT_Y_ALMOST_PLUS;
  190. _oHitValue = _oBall.y - PERFECT_HIT_Y;
  191. if(_oHitValue < 0){
  192. _oHitValue *= -1;
  193. }
  194. _bHitted = true;
  195. _aTrajectoryPoint = {start:_pStartPoint, end:_pEndPoint, traj:_pEndPoint};
  196. }
  197. }
  198. _bUpdate = true;
  199. };
  200. this.unload = function(){
  201. };
  202. this.update = function(){
  203. if(_bUpdate){
  204. this._updateBall();
  205. }
  206. };
  207. this._init(oParentContainerBall);
  208. }