Application Android et IOS pour l'amicale des élèves
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GameLogic.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // @flow
  2. import Piece from "./Piece";
  3. import ScoreManager from "./ScoreManager";
  4. import GridManager from "./GridManager";
  5. import type {CustomTheme} from "../../../managers/ThemeManager";
  6. export default class GameLogic {
  7. static levelTicks = [
  8. 1000,
  9. 800,
  10. 600,
  11. 400,
  12. 300,
  13. 200,
  14. 150,
  15. 100,
  16. ];
  17. #scoreManager: ScoreManager;
  18. #gridManager: GridManager;
  19. #height: number;
  20. #width: number;
  21. #gameRunning: boolean;
  22. #gamePaused: boolean;
  23. #gameTime: number;
  24. #currentObject: Piece;
  25. #gameTick: number;
  26. #gameTickInterval: IntervalID;
  27. #gameTimeInterval: IntervalID;
  28. #pressInInterval: TimeoutID;
  29. #isPressedIn: boolean;
  30. #autoRepeatActivationDelay: number;
  31. #autoRepeatDelay: number;
  32. #nextPieces: Array<Piece>;
  33. #nextPiecesCount: number;
  34. #onTick: Function;
  35. #onClock: Function;
  36. endCallback: Function;
  37. #theme: CustomTheme;
  38. constructor(height: number, width: number, theme: CustomTheme) {
  39. this.#height = height;
  40. this.#width = width;
  41. this.#gameRunning = false;
  42. this.#gamePaused = false;
  43. this.#theme = theme;
  44. this.#autoRepeatActivationDelay = 300;
  45. this.#autoRepeatDelay = 50;
  46. this.#nextPieces = [];
  47. this.#nextPiecesCount = 3;
  48. this.#scoreManager = new ScoreManager();
  49. this.#gridManager = new GridManager(this.getWidth(), this.getHeight(), this.#theme);
  50. }
  51. getHeight(): number {
  52. return this.#height;
  53. }
  54. getWidth(): number {
  55. return this.#width;
  56. }
  57. getCurrentGrid() {
  58. return this.#gridManager.getCurrentGrid();
  59. }
  60. isGameRunning(): boolean {
  61. return this.#gameRunning;
  62. }
  63. isGamePaused(): boolean {
  64. return this.#gamePaused;
  65. }
  66. onFreeze() {
  67. this.#gridManager.freezeTetromino(this.#currentObject, this.#scoreManager);
  68. this.createTetromino();
  69. }
  70. setNewGameTick(level: number) {
  71. if (level >= GameLogic.levelTicks.length)
  72. return;
  73. this.#gameTick = GameLogic.levelTicks[level];
  74. clearInterval(this.#gameTickInterval);
  75. this.#gameTickInterval = setInterval(this.#onTick, this.#gameTick);
  76. }
  77. onTick(callback: Function) {
  78. this.#currentObject.tryMove(0, 1,
  79. this.#gridManager.getCurrentGrid(), this.getWidth(), this.getHeight(),
  80. () => this.onFreeze());
  81. callback(
  82. this.#scoreManager.getScore(),
  83. this.#scoreManager.getLevel(),
  84. this.#gridManager.getCurrentGrid());
  85. if (this.#scoreManager.canLevelUp())
  86. this.setNewGameTick(this.#scoreManager.getLevel());
  87. }
  88. onClock(callback: Function) {
  89. this.#gameTime++;
  90. callback(this.#gameTime);
  91. }
  92. canUseInput() {
  93. return this.#gameRunning && !this.#gamePaused
  94. }
  95. rightPressed(callback: Function) {
  96. this.#isPressedIn = true;
  97. this.movePressedRepeat(true, callback, 1, 0);
  98. }
  99. leftPressedIn(callback: Function) {
  100. this.#isPressedIn = true;
  101. this.movePressedRepeat(true, callback, -1, 0);
  102. }
  103. downPressedIn(callback: Function) {
  104. this.#isPressedIn = true;
  105. this.movePressedRepeat(true, callback, 0, 1);
  106. }
  107. movePressedRepeat(isInitial: boolean, callback: Function, x: number, y: number) {
  108. if (!this.canUseInput() || !this.#isPressedIn)
  109. return;
  110. const moved = this.#currentObject.tryMove(x, y,
  111. this.#gridManager.getCurrentGrid(), this.getWidth(), this.getHeight(),
  112. () => this.onFreeze());
  113. if (moved) {
  114. if (y === 1) {
  115. this.#scoreManager.incrementScore();
  116. callback(this.#gridManager.getCurrentGrid(), this.#scoreManager.getScore());
  117. } else
  118. callback(this.#gridManager.getCurrentGrid());
  119. }
  120. this.#pressInInterval = setTimeout(() =>
  121. this.movePressedRepeat(false, callback, x, y),
  122. isInitial ? this.#autoRepeatActivationDelay : this.#autoRepeatDelay
  123. );
  124. }
  125. pressedOut() {
  126. this.#isPressedIn = false;
  127. clearTimeout(this.#pressInInterval);
  128. }
  129. rotatePressed(callback: Function) {
  130. if (!this.canUseInput())
  131. return;
  132. if (this.#currentObject.tryRotate(this.#gridManager.getCurrentGrid(), this.getWidth(), this.getHeight()))
  133. callback(this.#gridManager.getCurrentGrid());
  134. }
  135. getNextPiecesPreviews() {
  136. let finalArray = [];
  137. for (let i = 0; i < this.#nextPieces.length; i++) {
  138. finalArray.push(this.#gridManager.getEmptyGrid(4, 4));
  139. this.#nextPieces[i].toGrid(finalArray[i], true);
  140. }
  141. return finalArray;
  142. }
  143. recoverNextPiece() {
  144. this.#currentObject = this.#nextPieces.shift();
  145. this.generateNextPieces();
  146. }
  147. generateNextPieces() {
  148. while (this.#nextPieces.length < this.#nextPiecesCount) {
  149. this.#nextPieces.push(new Piece(this.#theme));
  150. }
  151. }
  152. createTetromino() {
  153. this.pressedOut();
  154. this.recoverNextPiece();
  155. if (!this.#currentObject.isPositionValid(this.#gridManager.getCurrentGrid(), this.getWidth(), this.getHeight()))
  156. this.endGame(false);
  157. }
  158. togglePause() {
  159. if (!this.#gameRunning)
  160. return;
  161. this.#gamePaused = !this.#gamePaused;
  162. if (this.#gamePaused) {
  163. clearInterval(this.#gameTickInterval);
  164. clearInterval(this.#gameTimeInterval);
  165. } else {
  166. this.#gameTickInterval = setInterval(this.#onTick, this.#gameTick);
  167. this.#gameTimeInterval = setInterval(this.#onClock, 1000);
  168. }
  169. }
  170. endGame(isRestart: boolean) {
  171. this.#gameRunning = false;
  172. this.#gamePaused = false;
  173. clearInterval(this.#gameTickInterval);
  174. clearInterval(this.#gameTimeInterval);
  175. this.endCallback(this.#gameTime, this.#scoreManager.getScore(), isRestart);
  176. }
  177. startGame(tickCallback: Function, clockCallback: Function, endCallback: Function) {
  178. if (this.#gameRunning)
  179. this.endGame(true);
  180. this.#gameRunning = true;
  181. this.#gamePaused = false;
  182. this.#gameTime = 0;
  183. this.#scoreManager = new ScoreManager();
  184. this.#gameTick = GameLogic.levelTicks[this.#scoreManager.getLevel()];
  185. this.#gridManager = new GridManager(this.getWidth(), this.getHeight(), this.#theme);
  186. this.#nextPieces = [];
  187. this.generateNextPieces();
  188. this.createTetromino();
  189. tickCallback(
  190. this.#scoreManager.getScore(),
  191. this.#scoreManager.getLevel(),
  192. this.#gridManager.getCurrentGrid());
  193. clockCallback(this.#gameTime);
  194. this.#onTick = this.onTick.bind(this, tickCallback);
  195. this.#onClock = this.onClock.bind(this, clockCallback);
  196. this.#gameTickInterval = setInterval(this.#onTick, this.#gameTick);
  197. this.#gameTimeInterval = setInterval(this.#onClock, 1000);
  198. this.endCallback = endCallback;
  199. }
  200. }