CGame.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. function CGame(oData) {
  2. var _oBgLevel;
  3. var _oInterface;
  4. var _oGameField;
  5. var _oBlock;
  6. var _oContainerGame;
  7. var _oDirFunc;
  8. var _oEdges;
  9. var _iScore;
  10. var _iNextType;
  11. var _iLevel;
  12. var _iLines;
  13. var _iLevelLines;
  14. var _bPressedKeys;
  15. var _bPause;
  16. var _bInput;
  17. var _bKeyDown = false;
  18. var _bKeyDir = false;
  19. var _fTimeRefresh;
  20. var _fMaxTimeRefresh;
  21. var _fTimeRefreshDirection;
  22. var _aSpawnBlocksOccurence;
  23. this._init = function () {
  24. setVolume("soundtrack", SOUNDTRACK_VOLUME_IN_GAME);
  25. this.setPause(true);
  26. _oBgLevel = createBitmap(s_oSpriteLibrary.getSprite("bg_game"));
  27. s_oStage.addChild(_oBgLevel);
  28. _oEdges = new CEdges();
  29. this.createGameContainer();
  30. _bPressedKeys = false;
  31. _oGameField = new CGameField();
  32. _iScore = 0;
  33. _iLevel = 0;
  34. _iLines = 0;
  35. _iLevelLines = 0;
  36. _fMaxTimeRefresh = TIME_REFRESH_GAME;
  37. _fTimeRefresh = _fMaxTimeRefresh;
  38. _aSpawnBlocksOccurence = new Array();
  39. for (var i = 0; i < BLOCKS_TYPE.length; i++) {
  40. for (var j = 0; j < BLOCKS_OCCURENCE[i]; i++) {
  41. _aSpawnBlocksOccurence.push(i);
  42. }
  43. }
  44. var iRandType = Math.floor(Math.random() * _aSpawnBlocksOccurence.length);
  45. this.createBlock(_aSpawnBlocksOccurence[iRandType]);
  46. this.nextType();
  47. _oInterface = new CInterface(_iNextType);
  48. _oInterface.refreshLevel(_iLevel + 1);
  49. this.canInput(true);
  50. _oEdges.createIEdge();
  51. $(s_oMain).trigger("start_level", 1);
  52. _oInterface.showHelpPanel();
  53. if (!s_bMobile) {
  54. document.onkeydown = onKeyDown;
  55. document.onkeyup = onKeyUp;
  56. } else {
  57. }
  58. };
  59. this.createGameContainer = function () {
  60. _oContainerGame = new createjs.Container();
  61. s_oStage.addChild(_oContainerGame);
  62. };
  63. this.nextType = function () {
  64. var iRandType = Math.floor(Math.random() * _aSpawnBlocksOccurence.length);
  65. _iNextType = _aSpawnBlocksOccurence[iRandType];
  66. };
  67. this.setPause = function (bVal) {
  68. _bPause = bVal;
  69. };
  70. this._onExitHelpPanel = function () {
  71. this.setPause(false);
  72. _oInterface.unloadHelp();
  73. };
  74. this.onExit = function () {
  75. setVolume("soundtrack", 1);
  76. s_oGame.unload();
  77. s_oMain.gotoMenu();
  78. $(s_oMain).trigger("end_level", 1);
  79. $(s_oMain).trigger("end_session", _iScore);
  80. $(s_oMain).trigger("show_interlevel_ad");
  81. };
  82. this.restartGame = function () {
  83. _oGameField.unload();
  84. _oGameField = null;
  85. _oContainerGame.removeAllChildren();
  86. _oBlock = null;
  87. _bKeyDir = false;
  88. this.nextType();
  89. _oInterface.refreshNextBlock(_iNextType);
  90. _oGameField = new CGameField();
  91. var iRandType = Math.floor(Math.random() * _aSpawnBlocksOccurence.length);
  92. this.createBlock(_aSpawnBlocksOccurence[iRandType]);
  93. _fMaxTimeRefresh = TIME_REFRESH_GAME;
  94. _iScore = 0;
  95. _iLevel = 0;
  96. _iLines = 0;
  97. _iLevelLines = 0;
  98. _oInterface.refreshLevel(_iLevel + 1);
  99. _oInterface.refreshLines(_iLines);
  100. _oInterface.refreshScore(_iScore);
  101. _bPause = false;
  102. };
  103. this.unload = function () {
  104. if (s_bMobile) {
  105. } else {
  106. document.onkeydown = null;
  107. document.onkeyup = null;
  108. }
  109. _oGameField.unload();
  110. _oGameField = null;
  111. _oInterface.unload();
  112. createjs.Tween.removeAllTweens();
  113. s_oStage.removeAllChildren();
  114. };
  115. function onKeyDown(evt) {
  116. if(_oBlock.getReplace()){
  117. return;
  118. }
  119. if (!_bPressedKeys && !_bPause && !s_oGameField.isAnimFullLines()) {
  120. if (evt.keyCode === 37) {
  121. s_oGame.onLeft();
  122. _bPressedKeys = true;
  123. } else if (evt.keyCode === 39) {
  124. s_oGame.onRight();
  125. _bPressedKeys = true;
  126. } else if (evt.keyCode === 38) {
  127. s_oGame.onUp();
  128. _bPressedKeys = true;
  129. }
  130. }
  131. if (evt.keyCode === 40 && _bKeyDown === false && !_bPause) {
  132. s_oGame.onDown();
  133. }
  134. evt.preventDefault();
  135. return false;
  136. }
  137. this.onLeft = function () {
  138. if (_oBlock.getReplace()) {
  139. return;
  140. }
  141. _oDirFunc = s_oGame.onLeft;
  142. _bKeyDir = true;
  143. _fTimeRefreshDirection = TIME_REFRESH_DIRECTION;
  144. if (_oBlock.getCol() > 0) {
  145. var _bHitSomething = _oGameField.checkDirection(_oBlock, LEFT);
  146. if (!_bHitSomething) {
  147. _oBlock.setCol(_oBlock.getCol() - 1);
  148. _oBlock.refreshCellPosition();
  149. }
  150. }
  151. };
  152. this.onRight = function () {
  153. if (_oBlock.getReplace()) {
  154. return;
  155. }
  156. _oDirFunc = s_oGame.onRight;
  157. _bKeyDir = true;
  158. _fTimeRefreshDirection = TIME_REFRESH_DIRECTION;
  159. if (_oBlock.getCol() < GRID_X - _oBlock.getWidth()) {
  160. var _bHitSomething = _oGameField.checkDirection(_oBlock, RIGHT);
  161. if (!_bHitSomething) {
  162. _oBlock.setCol(_oBlock.getCol() + 1);
  163. _oBlock.refreshCellPosition();
  164. }
  165. }
  166. };
  167. this.onUp = function () {
  168. playSound("shift_piece", 1, false);
  169. _oBlock.setOrientation(_oBlock.getOrientation() + 90);
  170. _oBlock.refreshCellPosition();
  171. };
  172. this.onDown = function () {
  173. _bKeyDown = true;
  174. if (!_oBlock.getReplace()) {
  175. _oBlock.down();
  176. }
  177. _fTimeRefresh = TIME_REFRESH_GAME_KEY_DOWN;
  178. };
  179. this.calculateScore = function (iFullLines) {
  180. this.addScore(SCORE_LINE[iFullLines - 1] * (_iLevel + 1));
  181. };
  182. this.checkForNewLevel = function (iFullLines) {
  183. _iLines += iFullLines;
  184. _iLevelLines += iFullLines;
  185. _oInterface.refreshLines(_iLines);
  186. if (_iLevelLines >= LEVEL_UP_LINES) {
  187. _iLevel++;
  188. _iLevelLines = _iLevelLines - LEVEL_UP_LINES;
  189. _oInterface.refreshLevel(_iLevel + 1);
  190. var fTempRefresh = _fMaxTimeRefresh - STEP_DECREASE;
  191. if (fTempRefresh >= MIN_REFRESH_GAME) {
  192. _fMaxTimeRefresh = fTempRefresh;
  193. }
  194. STEP_DECREASE -= 0.05;
  195. if (STEP_DECREASE < 0.05) {
  196. STEP_DECREASE = 0.05;
  197. }
  198. }
  199. };
  200. this.canInput = function (bVal) {
  201. _bInput = bVal;
  202. };
  203. this.addScore = function (iAddScore) {
  204. _iScore += iAddScore;
  205. _oInterface.refreshScore(_iScore);
  206. };
  207. function onKeyUp(evt) {
  208. if (_bPressedKeys && !_bPause && !s_oGameField.isAnimFullLines()) {
  209. if (evt.keyCode === 37) {
  210. _bPressedKeys = false;
  211. s_oGame.dirKeyUp();
  212. } else if (evt.keyCode === 39) {
  213. _bPressedKeys = false;
  214. s_oGame.dirKeyUp();
  215. } else if (evt.keyCode === 38) {
  216. _bPressedKeys = false;
  217. } else if (evt.keyCode === 80) {
  218. _bPressedKeys = false;
  219. } else if (evt.keyCode === 32) {
  220. _bPressedKeys = false;
  221. }
  222. }
  223. if (evt.keyCode === 40 && _bKeyDown === true) {
  224. s_oGame.onDownKeyUp();
  225. }
  226. evt.preventDefault();
  227. return false;
  228. }
  229. this.dirKeyUp = function () {
  230. _bKeyDir = false;
  231. };
  232. this.onDownKeyUp = function () {
  233. _bKeyDown = false;
  234. _fTimeRefresh = _fMaxTimeRefresh;
  235. };
  236. this.createBlock = function (iType) {
  237. var oSpriteCell = s_oSpriteLibrary.getSprite("cell_" + iType);
  238. _oBlock = new CBlock(iType, oSpriteCell, _oContainerGame);
  239. };
  240. this.getContainerGame = function () {
  241. return _oContainerGame;
  242. };
  243. this.gameOver = function () {
  244. this.setPause(true);
  245. s_aSounds["game_over"].on('end', function(){
  246. setVolume("soundtrack", SOUNDTRACK_VOLUME_IN_GAME);
  247. });
  248. playSound("game_over", 1, false);
  249. setVolume("soundtrack", 0);
  250. $(s_oMain).trigger("end_level", 1);
  251. _oInterface.gameOver(_iScore, _iLevel + 1, _iLines);
  252. };
  253. this.keysDirectionPress = function () {
  254. if (_bKeyDir) {
  255. _oDirFunc();
  256. }
  257. };
  258. this.update = function () {
  259. if (_bPause === false) {
  260. if (s_oGameField.isAnimFullLines()) {
  261. return;
  262. }
  263. if (_oBlock.getReplace() === true) {
  264. _oBlock = null;
  265. this.createBlock(_iNextType);
  266. this.nextType();
  267. _oInterface.refreshNextBlock(_iNextType);
  268. if (s_oGameField.checkDirection(_oBlock)) {
  269. this.gameOver();
  270. return;
  271. }
  272. }
  273. if (_fTimeRefresh < 0) {
  274. _oBlock.down();
  275. if (!_bKeyDown) {
  276. _fTimeRefresh = _fMaxTimeRefresh * 1000;
  277. } else {
  278. _fTimeRefresh = TIME_REFRESH_GAME_KEY_DOWN;
  279. }
  280. } else {
  281. _fTimeRefresh -= s_iTimeElaps;
  282. }
  283. if (_fTimeRefreshDirection < 0) {
  284. _fTimeRefreshDirection = TIME_REFRESH_DIRECTION;
  285. this.keysDirectionPress();
  286. } else {
  287. if (_bKeyDir) {
  288. _fTimeRefreshDirection -= FPS_TIME;
  289. }
  290. }
  291. }
  292. };
  293. s_oGame = this;
  294. LEVEL_UP_LINES = oData.level_up_lines;
  295. MIN_REFRESH_GAME = oData.min_refresh_game;
  296. SCORE_LINE = oData.score_line;
  297. TIME_REFRESH_GAME = oData.start_refresh_game;
  298. STEP_DECREASE = oData.step_decrease_refresh_game;
  299. BLOCKS_OCCURENCE = oData.blocks_occurence;
  300. NUM_LEVEL_FOR_ADS = oData.num_levels_for_ads;
  301. TIME_REFRESH_GAME_KEY_DOWN = MIN_REFRESH_GAME;
  302. this._init();
  303. }
  304. var s_oGame;
  305. var s_oScrollStage;