CGame.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. function CGame(oData, iPlayerTeam, iOpponentTeam) {
  2. var _idleBatterWait = 0;
  3. var _idleBatterStrike = 0;
  4. var _idlePitcher = 0;
  5. var _iScore = 0;
  6. var _iBallThrowed = 0;
  7. var _iRandDir = 0;
  8. var _iPressedDir = -1;
  9. var _iLives;
  10. var _bUpdate;
  11. var _bWaiting = false; //0 waiting state, 1 striking state
  12. var _bBallThrowed = false;
  13. var _bMissed = false;
  14. var _bInput = true;
  15. var _bBeat = false;
  16. var _bStartGame;
  17. var _oInterface;
  18. var _oEndPanel = null;
  19. var _oParent;
  20. var _oContainerGame;
  21. var _oBatter;
  22. var _oBowler;
  23. var _oTarget;
  24. var _oPole;
  25. this._init = function (iPlayerTeam, iOpponentTeam) {
  26. $(s_oMain).trigger("start_session");
  27. setVolume("soundtrack", 0.35);
  28. playSound("crowd_cheering", 1, true);
  29. var oBg = createBitmap(s_oSpriteLibrary.getSprite('bg_game'));
  30. s_oStage.addChild(oBg);
  31. _oContainerGame = new createjs.Container();
  32. s_oStage.addChild(_oContainerGame);
  33. _oInterface = new CInterface();
  34. _oBowler = new CBowler(_oContainerGame, iOpponentTeam);
  35. var oSpritePole = s_oSpriteLibrary.getSprite("pole_1");
  36. _oPole = new CPole(CANVAS_WIDTH_HALF - 8, CANVAS_HEIGHT - 812, oSpritePole, _oContainerGame);
  37. var oSpriteTarget = s_oSpriteLibrary.getSprite("ball_target");
  38. _oTarget = new CBallTarget(-100, -100, oSpriteTarget, _oContainerGame);
  39. _oTarget.setVisible(false);
  40. s_oBall = new CBall(_oContainerGame);
  41. this.ballResetPos();
  42. _iLives = LIVES;
  43. _oBatter = new CBatter(_oContainerGame, iPlayerTeam);
  44. var oSpritePole = s_oSpriteLibrary.getSprite("pole_0");
  45. _oPole = new CPole(CANVAS_WIDTH_HALF, CANVAS_HEIGHT - 193, oSpritePole, _oContainerGame);
  46. if (s_bMobile === false) {
  47. document.onkeydown = onKeyDown;
  48. } else {
  49. _oInterface.createController();
  50. _oInterface.refreshButtonPos(s_iOffsetX, s_iOffsetY);
  51. _oInterface.createHitArea();
  52. }
  53. _bStartGame = true;
  54. _oInterface.createHelpPanel();
  55. };
  56. this.launchCountdown = function () {
  57. var oMissed = createBitmap(s_oSpriteLibrary.getSprite('3'));
  58. oMissed.x = CANVAS_WIDTH / 2;
  59. oMissed.y = CANVAS_HEIGHT / 2;
  60. oMissed.regX = 40;
  61. oMissed.regY = 84;
  62. _oContainerGame.addChild(oMissed);
  63. playSound("countdown1", 1, false);
  64. createjs.Tween.get(oMissed).to({scaleX: 1.3, scaleY: 1.3}, (500), createjs.Ease.cubicOut).wait(500).call(function () {
  65. this.visible = false;
  66. var oMissed = createBitmap(s_oSpriteLibrary.getSprite('2'));
  67. oMissed.x = CANVAS_WIDTH / 2;
  68. oMissed.y = CANVAS_HEIGHT / 2;
  69. oMissed.regX = 40;
  70. oMissed.regY = 84;
  71. oMissed.scaleX = 0.7;
  72. oMissed.scaleY = 0.7;
  73. _oContainerGame.addChild(oMissed);
  74. playSound("countdown1", 1, false);
  75. createjs.Tween.get(oMissed).to({scaleX: 1.3, scaleY: 1.3}, (500), createjs.Ease.cubicOut).wait(500).call(function () {
  76. _oContainerGame.removeChild(this);
  77. var oMissed = createBitmap(s_oSpriteLibrary.getSprite('1'));
  78. oMissed.x = CANVAS_WIDTH / 2;
  79. oMissed.y = CANVAS_HEIGHT / 2;
  80. oMissed.regX = 40;
  81. oMissed.regY = 84;
  82. oMissed.scaleX = 0.7;
  83. oMissed.scaleY = 0.7;
  84. _oContainerGame.addChild(oMissed);
  85. playSound("countdown1", 1, 0);
  86. createjs.Tween.get(oMissed).to({scaleX: 1.3, scaleY: 1.3}, (500), createjs.Ease.cubicOut).wait(500).call(function () {
  87. _oContainerGame.removeChild(this);
  88. _oInterface.createAnimText(TEXT_START, 48, false, null, 300, function () {
  89. _oContainerGame.removeChild(this);
  90. _bUpdate = true;
  91. _oInterface.setHitAreaVisible(false);
  92. });
  93. playSound("countdown2", 1, false);
  94. });
  95. });
  96. });
  97. };
  98. this._onExitHelpPanel = function () {
  99. _oInterface.onExitFromHelp();
  100. this.launchCountdown();
  101. $(s_oMain).trigger("start_level", 1);
  102. };
  103. function onKeyDown(evt) {
  104. if (_bInput === true) {
  105. if (!_bBeat) {
  106. if (evt.keyCode === 37) {
  107. s_oGame.hitLeft();
  108. } else if (evt.keyCode === 38) {
  109. s_oGame.hitCenter();
  110. } else if (evt.keyCode === 39) {
  111. s_oGame.hitRight();
  112. }
  113. }
  114. }
  115. evt.preventDefault();
  116. return false;
  117. }
  118. this.hitLeft = function () {
  119. _iPressedDir = HIT_LEFT;
  120. s_oGame._strike();
  121. s_oGame.beatDirection();
  122. };
  123. this.hitCenter = function () {
  124. _iPressedDir = HIT_CENTER;
  125. s_oGame._strike();
  126. s_oGame.beatDirection();
  127. };
  128. this.hitRight = function () {
  129. _iPressedDir = HIT_RIGHT;
  130. s_oGame._strike();
  131. s_oGame.beatDirection();
  132. };
  133. this._strike = function () {
  134. if (!_bWaiting) {
  135. _idleBatterWait = 0;
  136. } else {
  137. _idleBatterStrike = 0;
  138. }
  139. _bWaiting = !_bWaiting;
  140. _bBeat = true;
  141. _oInterface.setHitAreaVisible(true);
  142. };
  143. this._ballMissed = function () {
  144. _bUpdate = false;
  145. _bMissed = true;
  146. playSound("crowd_ohhh", 1, false);
  147. _oInterface.createAnimText(TEXT_BOWLED, 48, false, null, 300, this.afterBallMissed);
  148. _oTarget.setVisible(false);
  149. _iLives--;
  150. _oInterface.refreshLivesText(_iLives);
  151. };
  152. this.afterBallMissed = function () {
  153. _idleBatterWait = 0;
  154. if (_bWaiting) {
  155. s_oGame._strike();
  156. }
  157. s_oGame.changeStateTarget(false);
  158. _bUpdate = true;
  159. _bBeat = false;
  160. _oInterface.setHitAreaVisible(false);
  161. };
  162. this.getMissed = function () {
  163. return _bMissed;
  164. };
  165. this.setScore = function (iValue) {
  166. _iScore += Math.floor(iValue);
  167. _oInterface.viewScore(_iScore);
  168. };
  169. this.unload = function () {
  170. if (s_bMobile === false) {
  171. document.onkeydown = null;
  172. }
  173. _oInterface.unload();
  174. if (_oEndPanel !== null) {
  175. _oEndPanel.unload();
  176. }
  177. createjs.Tween.removeAllTweens();
  178. s_oStage.removeAllChildren();
  179. };
  180. this.onExit = function () {
  181. this.unload();
  182. s_oMain.gotoMenu();
  183. setVolume("soundtrack", 1);
  184. stopSound("crowd_cheering");
  185. $(s_oMain).trigger("end_level", 1);
  186. $(s_oMain).trigger("show_interlevel_ad");
  187. $(s_oMain).trigger("end_session", _iScore);
  188. };
  189. this.gameOver = function () {
  190. _bUpdate = false;
  191. _bInput = false;
  192. _oEndPanel = CEndPanel(s_oSpriteLibrary.getSprite('msg_box'));
  193. _oEndPanel.show(_iScore);
  194. };
  195. this._ballHitted = function (fHitValue) {
  196. _bUpdate = false;
  197. s_oBall.hideBall();
  198. var bStrobe = false;
  199. playSound("applauses", 1, false);
  200. var iPoint = SCORE_HIT - fHitValue;
  201. var szText = TEXT_CONGRATULATION[0];
  202. if (iPoint >= POINT_TEXT_EXCELLENT) {
  203. szText = TEXT_CONGRATULATION[2];
  204. bStrobe = true;
  205. } else if (iPoint > POINT_TEXT_GREAT) {
  206. szText = TEXT_CONGRATULATION[1];
  207. }
  208. _oInterface.createAnimText(szText, 48, bStrobe, TEXT_EXCELLENT_COLOR, 300, this.afterBallHit);
  209. };
  210. this.afterBallHit = function () {
  211. s_oGame._restart();
  212. s_oBall.addVelocity();
  213. _bUpdate = true;
  214. };
  215. this.changeStateTarget = function (bVal) {
  216. _oTarget.changeState(bVal);
  217. };
  218. this.randomDir = function () {
  219. _iRandDir = Math.floor(Math.random() * LAUNCH_DIR_OFFSET_RANGE.length);
  220. };
  221. this.ballResetPos = function () {
  222. this.randomDir();
  223. var iRandRangeX = (Math.random() * (LAUNCH_DIR_OFFSET_RANGE[_iRandDir].max - LAUNCH_DIR_OFFSET_RANGE[_iRandDir].min)) + LAUNCH_DIR_OFFSET_RANGE[_iRandDir].min;
  224. var pEndPosRandom = {x: END_POINT_X_THROWN + iRandRangeX, y: END_POINT_Y_THROWN};
  225. PERFECT_HIT_X = pEndPosRandom.x;
  226. _oTarget.setPosition(pEndPosRandom.x, pEndPosRandom.y);
  227. s_oBall.reset(pEndPosRandom);
  228. };
  229. this.pause = function (bVal) {
  230. _bStartGame = !bVal;
  231. createjs.Ticker.paused = bVal;
  232. };
  233. this._restart = function () {
  234. if (_iLives <= 0) {
  235. this.gameOver();
  236. } else {
  237. this.ballResetPos();
  238. _oTarget.setVisible(false);
  239. s_oGame.changeStateTarget(false);
  240. s_bBounce = true;
  241. _bBallThrowed = false;
  242. _bMissed = false;
  243. this._strike();
  244. _bBeat = false;
  245. _oInterface.setHitAreaVisible(false);
  246. }
  247. };
  248. this.resetGame = function () {
  249. _iLives = LIVES;
  250. _oInterface.refreshLivesText(_iLives);
  251. _iScore = 0;
  252. _oInterface.viewScore(_iScore);
  253. _bUpdate = true;
  254. _bWaiting = true;
  255. _bInput = true;
  256. _bBeat = false;
  257. this._restart();
  258. $(s_oMain).trigger("restart_level", 1);
  259. };
  260. this.ballMissedRestart = function () {
  261. if (_iLives <= 0) {
  262. this.gameOver();
  263. } else {
  264. this.ballResetPos();
  265. _bBallThrowed = false;
  266. if (_idleBatterStrike > 0) {
  267. this._strike();
  268. }
  269. _bMissed = false;
  270. _bBeat = false;
  271. _oInterface.setHitAreaVisible(false);
  272. }
  273. };
  274. this.beatDirection = function () {
  275. if (_iPressedDir === _iRandDir) {
  276. s_oBall.hitControl();
  277. }
  278. };
  279. this.update = function () {
  280. if (_bUpdate && _bStartGame) {
  281. if (!_bWaiting) {
  282. //waiting the pitcher
  283. if ((_idleBatterWait + 1) < NUM_SPRITE_BATTING) {
  284. _oBatter.viewBatter(_idleBatterWait + 1, _bWaiting);
  285. _idleBatterWait++;
  286. } else {
  287. _idleBatterWait = 0;
  288. _oBatter.viewBatter(_idleBatterWait, _bWaiting);
  289. }
  290. } else {
  291. //striking BATTER
  292. if ((_idleBatterStrike + 1) < NUM_SPRITE_BATTING) {
  293. _oBatter.viewBatter(_idleBatterStrike + 1, _bWaiting);
  294. _idleBatterStrike++;
  295. } else {
  296. _oBatter.viewBatter(_idleBatterStrike, _bWaiting);
  297. }
  298. if (_idleBatterStrike === 8) {
  299. s_oBall.hitBall();
  300. }
  301. }
  302. if (_bMissed === true) {
  303. this.ballMissedRestart();
  304. }
  305. if (!_bBallThrowed || _idlePitcher + 1 < NUM_SPRITE_BOWLER) {
  306. _oBowler.hideBowler(_idlePitcher);
  307. if (_idlePitcher + 1 < NUM_SPRITE_BOWLER) {
  308. _oBowler.viewBowler(_idlePitcher + 1);
  309. _idlePitcher++;
  310. } else {
  311. _idlePitcher = 0;
  312. }
  313. if (_idlePitcher === 28) {
  314. _iBallThrowed++;
  315. s_oBall.viewBall();
  316. _oTarget.setVisible(true);
  317. _bBallThrowed = true;
  318. }
  319. }
  320. if (_bBallThrowed) {
  321. s_oBall.update();
  322. }
  323. }
  324. };
  325. s_oGame = this;
  326. _oParent = this;
  327. LIVES = oData.lives;
  328. OFFSET_FOR_HIT = oData.offset_hit;
  329. OFFSET_FOR_PERFECT_HIT = oData.offset_perfect_hit;
  330. STEP_SPEED_BALL = oData.start_speed_ball;
  331. BALL_VELOCITY_ADDED = oData.ball_velocity_added;
  332. MAX_BALL_VELOCITY = oData.max_ball_velocity;
  333. SCORE_HIT = oData.score_ball_hit;
  334. NUM_LEVEL_FOR_ADS = oData.num_levels_for_ads;
  335. ALMOST_MINUS = PERFECT_HIT_Y - OFFSET_FOR_HIT;
  336. ALMOST_PLUS = PERFECT_HIT_Y + OFFSET_FOR_HIT;
  337. POINT_TEXT_EXCELLENT = SCORE_HIT - OFFSET_FOR_PERFECT_HIT;
  338. this._init(iPlayerTeam, iOpponentTeam);
  339. }
  340. var s_oGame;