CBall.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. function CBall(oParentContainerBall) {
  2. var _iCntFrames = 0;
  3. var _iMaxFrames = MAX_FRAMES_THROWN;
  4. var _iWidth;
  5. var _iHeight;
  6. var _iBufferTime = 0;
  7. var _iFrame = 0;
  8. var _fSpeed;
  9. var _fStartSpeed;
  10. var _fSpeedRate = 0.035;
  11. var _bUpdate;
  12. var _bHitted;
  13. var _bMissed = false;
  14. var _bPerfect;
  15. var _bPlus;
  16. var _bMinius;
  17. var _oBall;
  18. var _oHitValue;
  19. var _pStartPoint = {x: 0, y: 0};
  20. var _pEndPoint = {x: 0, y: 0};
  21. var _aTrajectoryPoint;
  22. this._init = function (oParentContainerBall) {
  23. var oSprite = s_oSpriteLibrary.getSprite("ball");
  24. var oData = {
  25. images: [oSprite],
  26. // width, height & registration point of each sprite
  27. frames: {width: (oSprite.width / 7), height: oSprite.height / 3, regX: (oSprite.width / 2) / 7, regY: (oSprite.height / 2) / 3},
  28. animations: {rotate: [0, 20]}
  29. };
  30. var oSpriteSheetHeroBottom = new createjs.SpriteSheet(oData);
  31. _oBall = createSprite(oSpriteSheetHeroBottom, "normal", ((oSprite.width / 2) / 7), ((oSprite.height / 2) / 3), oSprite.width / 7, oSprite.height / 3);
  32. _oBall.stop();
  33. _fSpeed = STEP_SPEED_BALL;
  34. _fStartSpeed = STEP_SPEED_BALL;
  35. oParentContainerBall.addChild(_oBall);
  36. };
  37. this.reset = function (pEndPos) {
  38. _oBall.x = BALL_X;
  39. _oBall.y = BALL_Y;
  40. _oBall.regX = _iWidth / 2;
  41. _oBall.regY = _iHeight / 2;
  42. _oBall.scaleX = 0.4;
  43. _oBall.scaleY = 0.4;
  44. _oBall.rotation = 0;
  45. _oBall.visible = false;
  46. _bHitted = false;
  47. _bMissed = false;
  48. _bPerfect = false;
  49. _bPlus = false;
  50. _bMinius = false;
  51. _iMaxFrames = MAX_FRAMES_THROWN;
  52. this.changeTrajectory(pEndPos);
  53. };
  54. this.changeTrajectory = function (pEndPos) {
  55. _pStartPoint.x = _oBall.x;
  56. _pStartPoint.y = _oBall.y;
  57. _pEndPoint.x = pEndPos.x;
  58. _pEndPoint.y = pEndPos.y;
  59. _fSpeed = _fStartSpeed;
  60. this._calculateMid(_pStartPoint, _pEndPoint);
  61. };
  62. this.addVelocity = function () {
  63. if (_fSpeed < MAX_BALL_VELOCITY) {
  64. _fSpeed = _fSpeed + BALL_VELOCITY_ADDED;
  65. _fStartSpeed = _fSpeed;
  66. } else {
  67. _fSpeed = MAX_BALL_VELOCITY;
  68. }
  69. };
  70. this.viewBall = function () {
  71. _oBall.visible = true;
  72. this._calculateMid(_pStartPoint, _pEndPoint);
  73. _bUpdate = true;
  74. };
  75. this.hideBall = function () {
  76. _oBall.visible = false;
  77. };
  78. this._calculateMid = function (pStartPoint, pEndPoint) {
  79. //TRAIETTORIA CHE DEVE ANDARE SUL PUNTO DA TIRARE
  80. var t0;
  81. var iRandT0 = Math.floor(Math.random() * 50) + 1;
  82. if (_bHitted === true) {
  83. if (_bPerfect) {
  84. t0 = new createjs.Point(Math.floor(Math.random() * 100) + (CANVAS_WIDTH / 2) - 100, (TRAJECTORY_Y_BALL_CAUGHT) - iRandT0);
  85. } else {
  86. if (pEndPoint.x < CANVAS_WIDTH / 2) {
  87. if (pEndPoint.y > (CANVAS_HEIGHT / 2)) {
  88. t0 = new createjs.Point(Math.floor(Math.random() * (CANVAS_WIDTH / 2)) - 100, (TRAJECTORY_Y_BALL_CAUGHT) - iRandT0);
  89. } else {
  90. t0 = new createjs.Point(Math.floor(Math.random() * (CANVAS_WIDTH / 2)) - 100, (TRAJECTORY_Y_BALL_CAUGHT) + iRandT0);
  91. }
  92. } else if (pEndPoint.x > CANVAS_WIDTH / 2) {
  93. if (pEndPoint.y > (CANVAS_HEIGHT / 2)) {
  94. t0 = new createjs.Point(Math.floor(Math.random() * (CANVAS_WIDTH / 2) + 125) + 250, (TRAJECTORY_Y_BALL_CAUGHT) - iRandT0);
  95. } else {
  96. t0 = new createjs.Point(Math.floor(Math.random() * (_pEndPoint.x - _pStartPoint.x) + (CANVAS_WIDTH / 2) + iRandT0), (TRAJECTORY_Y_BALL_CAUGHT) + iRandT0);
  97. }
  98. } else {
  99. if (pEndPoint.x > (CANVAS_WIDTH / 2)) {
  100. t0 = new createjs.Point((CANVAS_WIDTH / 2) - 50, Math.floor(Math.random() * 200) + 100);
  101. } else {
  102. t0 = new createjs.Point((CANVAS_WIDTH / 2) + 50, Math.floor(Math.random() * 200) + 100);
  103. }
  104. }
  105. }
  106. } else if (_bMissed) {
  107. t0 = new createjs.Point(CANVAS_WIDTH_HALF + (END_POINT_X_MISSED_BALL - _oBall.x), CANVAS_HEIGHT_HALF + (END_POINT_Y_MISSED_BALL - _oBall.y));
  108. } else {
  109. var iVal = Math.random();
  110. if (iVal < 0.5) {
  111. t0 = new createjs.Point((CANVAS_WIDTH / 2) - 50, Math.floor(Math.random() * (CANVAS_HEIGHT / 2) - 100) + 100);
  112. } else {
  113. t0 = new createjs.Point((CANVAS_WIDTH / 2) + 50, Math.floor(Math.random() * (CANVAS_HEIGHT / 2) - 100) + 100);
  114. }
  115. }
  116. _aTrajectoryPoint = {start: pStartPoint, end: pEndPoint, traj: t0};
  117. };
  118. this._updateBall = function () {
  119. _iCntFrames += _fSpeed;
  120. if (_iCntFrames > _iMaxFrames) {
  121. if (!_bMissed || _bHitted) {
  122. if (_bHitted === true) {
  123. s_oGame._ballHitted(_oHitValue);
  124. } else {
  125. _bMissed = true;
  126. this.changeTrajectory({x: END_POINT_X_MISSED_BALL, y: END_POINT_Y_MISSED_BALL});
  127. }
  128. } else {
  129. s_oGame._ballMissed();
  130. _oBall.visible = false; //this is to resolve an issue when ball respawn
  131. _bUpdate = false;
  132. }
  133. s_oGame.changeStateTarget(true);
  134. playSound("drop_bounce_grass", 1, false);
  135. _iCntFrames = 0;
  136. }
  137. var fLerp;
  138. fLerp = easeLinear(_iCntFrames, 0, 1, _iMaxFrames);
  139. this.rolls();
  140. var pPos = getTrajectoryPoint(fLerp, _aTrajectoryPoint);
  141. _oBall.x = pPos.x;
  142. _oBall.y = pPos.y;
  143. if (_bHitted === true) {
  144. if (_oBall.scaleX >= 0) {
  145. _oBall.scaleX -= 0.03;
  146. _oBall.scaleY -= 0.03;
  147. }
  148. _fSpeed -= _fSpeedRate;
  149. } else {
  150. if (_oBall.scaleX < 1) {
  151. _oBall.scaleX += 0.03;
  152. _oBall.scaleY += 0.03;
  153. } else {
  154. _fSpeed += 0.2;
  155. }
  156. }
  157. };
  158. this.getValue = function () {
  159. return _oBall;
  160. };
  161. this.hitControl = function () {
  162. var bHit = false;
  163. if (_oBall.y >= PERFECT_HIT_Y - OFFSET_FOR_PERFECT_HIT && _oBall.y <= PERFECT_HIT_Y + OFFSET_FOR_PERFECT_HIT) {
  164. _bPerfect = true;
  165. bHit = true;
  166. } else if (_oBall.y >= ALMOST_MINUS && _oBall.y <= PERFECT_HIT_Y) {
  167. _bMinius = true;
  168. bHit = true;
  169. } else if (_oBall.y <= ALMOST_PLUS && _oBall.y >= PERFECT_HIT_Y) {
  170. _bPlus = true;
  171. bHit = true;
  172. }
  173. if (bHit) {
  174. _oHitValue = _oBall.y - PERFECT_HIT_Y;
  175. if (_oHitValue < 0) {
  176. _oHitValue *= -1;
  177. }
  178. }
  179. _bUpdate = true;
  180. };
  181. this.hitBall = function () {
  182. if (_bPerfect) {
  183. this.perfectHit();
  184. } else if (_bMinius) {
  185. this.miniusHit();
  186. } else if (_bPlus) {
  187. this.plusHit();
  188. }
  189. if (_bPerfect || _bMinius || _bPlus) {
  190. _bHitted = true;
  191. _pStartPoint.x = _oBall.x;
  192. _pStartPoint.y = _oBall.y;
  193. _fSpeed = STEP_SPEED_BALL + BEAT_FORCE;
  194. playSound("hit_ball", 1, false);
  195. this._calculateMid(_pStartPoint, _pEndPoint);
  196. s_oGame.setScore(SCORE_HIT - _oHitValue);
  197. }
  198. };
  199. this.perfectHit = function () {
  200. if (_aTrajectoryPoint.traj.x < CANVAS_WIDTH / 2) { //IF IS COMING FROM LEFT
  201. _iCntFrames = 0;
  202. _pEndPoint.x = END_POINT_X_PERFECT_LEFT;
  203. _pEndPoint.y = END_POINT_Y_PERFECT;
  204. } else {
  205. _iCntFrames = 0;
  206. _pEndPoint.x = END_POINT_X_PERFECT_RIGHT;
  207. _pEndPoint.y = END_POINT_Y_PERFECT;
  208. }
  209. };
  210. this.miniusHit = function () {
  211. if (_aTrajectoryPoint.traj.x < CANVAS_WIDTH / 2) { //IF IS COMING FROM LEFT
  212. _iCntFrames = 0;
  213. _pEndPoint.x = END_POINT_X_ALMOST_MINUS_RIGHT;
  214. _pEndPoint.y = END_POINT_Y_ALMOST_MINUS;
  215. } else {
  216. _iCntFrames = 0;
  217. _pEndPoint.x = END_POINT_X_ALMOST_MINUS_LEFT;
  218. _pEndPoint.y = END_POINT_Y_ALMOST_MINUS;
  219. }
  220. };
  221. this.plusHit = function () {
  222. if (_aTrajectoryPoint.traj.x < CANVAS_WIDTH / 2) { //IF IS COMING FROM LEFT
  223. _iCntFrames = 0;
  224. _pEndPoint.x = END_POINT_X_ALMOST_PLUS_RIGHT;
  225. _pEndPoint.y = END_POINT_Y_ALMOST_PLUS;
  226. } else {
  227. _iCntFrames = 0;
  228. _pEndPoint.x = END_POINT_X_ALMOST_PLUS_LEFT;
  229. _pEndPoint.y = END_POINT_Y_ALMOST_PLUS;
  230. }
  231. };
  232. this.rolls = function () {
  233. if (_fSpeed > 2) {
  234. _oBall.rotation += 10;
  235. } else if (_fSpeed > 1) {
  236. _iBufferTime++;
  237. if (_iBufferTime === 2) {
  238. _oBall.rotation += 8;
  239. _iBufferTime = 0;
  240. }
  241. } else if (_fSpeed > 0.5) {
  242. _iBufferTime++;
  243. if (_iBufferTime === 3) {
  244. _oBall.rotation += 4;
  245. _iBufferTime = 0;
  246. }
  247. }
  248. var oFuncRot = this._goToPrevFrame;
  249. if (_pStartPoint.y > _pEndPoint.y) {
  250. oFuncRot = this._goToNextFrame;
  251. }
  252. if (_fSpeed > 1.5) {
  253. oFuncRot();
  254. } else if (_fSpeed > 1) {
  255. _iBufferTime++;
  256. if (_iBufferTime > 1) {
  257. oFuncRot();
  258. _iBufferTime = 0;
  259. }
  260. } else if (_fSpeed > 0.5) {
  261. _iBufferTime++;
  262. if (_iBufferTime > 2) {
  263. oFuncRot();
  264. _iBufferTime = 0;
  265. }
  266. }
  267. };
  268. this._goToPrevFrame = function () {
  269. if (_iFrame === 0) {
  270. _iFrame = 20;
  271. _oBall.gotoAndStop(_iFrame);
  272. } else {
  273. _iFrame--;
  274. _oBall.gotoAndStop(_iFrame);
  275. }
  276. };
  277. this._goToNextFrame = function () {
  278. if (_iFrame === 21) {
  279. _iFrame = 1;
  280. _oBall.gotoAndStop(_iFrame);
  281. } else {
  282. _iFrame++;
  283. _oBall.gotoAndStop(_iFrame);
  284. }
  285. };
  286. this.setPosition = function (iX, iY) {
  287. _oBall.x = iX;
  288. _oBall.y = iY;
  289. };
  290. this.changeState = function (iVal) {
  291. _oBall.gotoAndStop(iVal);
  292. };
  293. this.update = function () {
  294. if (_bUpdate) {
  295. this._updateBall();
  296. }
  297. };
  298. this._init(oParentContainerBall);
  299. return this;
  300. }