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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. console.log(this.#gameTime);
  91. callback(this.#gameTime);
  92. }
  93. canUseInput() {
  94. return this.#gameRunning && !this.#gamePaused
  95. }
  96. rightPressed(callback: Function) {
  97. this.#isPressedIn = true;
  98. this.movePressedRepeat(true, callback, 1, 0);
  99. }
  100. leftPressedIn(callback: Function) {
  101. this.#isPressedIn = true;
  102. this.movePressedRepeat(true, callback, -1, 0);
  103. }
  104. downPressedIn(callback: Function) {
  105. this.#isPressedIn = true;
  106. this.movePressedRepeat(true, callback, 0, 1);
  107. }
  108. movePressedRepeat(isInitial: boolean, callback: Function, x: number, y: number) {
  109. if (!this.canUseInput() || !this.#isPressedIn)
  110. return;
  111. const moved = this.#currentObject.tryMove(x, y,
  112. this.#gridManager.getCurrentGrid(), this.getWidth(), this.getHeight(),
  113. () => this.onFreeze());
  114. if (moved) {
  115. if (y === 1) {
  116. this.#scoreManager.incrementScore();
  117. callback(this.#gridManager.getCurrentGrid(), this.#scoreManager.getScore());
  118. } else
  119. callback(this.#gridManager.getCurrentGrid());
  120. }
  121. this.#pressInInterval = setTimeout(() =>
  122. this.movePressedRepeat(false, callback, x, y),
  123. isInitial ? this.#autoRepeatActivationDelay : this.#autoRepeatDelay
  124. );
  125. }
  126. pressedOut() {
  127. this.#isPressedIn = false;
  128. clearTimeout(this.#pressInInterval);
  129. }
  130. rotatePressed(callback: Function) {
  131. if (!this.canUseInput())
  132. return;
  133. if (this.#currentObject.tryRotate(this.#gridManager.getCurrentGrid(), this.getWidth(), this.getHeight()))
  134. callback(this.#gridManager.getCurrentGrid());
  135. }
  136. getNextPiecesPreviews() {
  137. let finalArray = [];
  138. for (let i = 0; i < this.#nextPieces.length; i++) {
  139. const gridSize = this.#nextPieces[i].getCurrentShape().getCurrentShape()[0].length;
  140. finalArray.push(this.#gridManager.getEmptyGrid(gridSize, gridSize));
  141. this.#nextPieces[i].toGrid(finalArray[i], true);
  142. }
  143. return finalArray;
  144. }
  145. recoverNextPiece() {
  146. this.#currentObject = this.#nextPieces.shift();
  147. this.generateNextPieces();
  148. }
  149. generateNextPieces() {
  150. while (this.#nextPieces.length < this.#nextPiecesCount) {
  151. this.#nextPieces.push(new Piece(this.#theme));
  152. }
  153. }
  154. createTetromino() {
  155. this.pressedOut();
  156. this.recoverNextPiece();
  157. if (!this.#currentObject.isPositionValid(this.#gridManager.getCurrentGrid(), this.getWidth(), this.getHeight()))
  158. this.endGame(false);
  159. }
  160. togglePause() {
  161. if (!this.#gameRunning)
  162. return;
  163. this.#gamePaused = !this.#gamePaused;
  164. if (this.#gamePaused) {
  165. clearInterval(this.#gameTickInterval);
  166. clearInterval(this.#gameTimeInterval);
  167. } else {
  168. this.#gameTickInterval = setInterval(this.#onTick, this.#gameTick);
  169. this.#gameTimeInterval = setInterval(this.#onClock, 1000);
  170. }
  171. }
  172. endGame(isRestart: boolean) {
  173. this.#gameRunning = false;
  174. this.#gamePaused = false;
  175. clearInterval(this.#gameTickInterval);
  176. clearInterval(this.#gameTimeInterval);
  177. this.endCallback(this.#gameTime, this.#scoreManager.getScore(), isRestart);
  178. }
  179. startGame(tickCallback: Function, clockCallback: Function, endCallback: Function) {
  180. if (this.#gameRunning)
  181. this.endGame(true);
  182. this.#gameRunning = true;
  183. this.#gamePaused = false;
  184. this.#gameTime = 0;
  185. this.#scoreManager = new ScoreManager();
  186. this.#gameTick = GameLogic.levelTicks[this.#scoreManager.getLevel()];
  187. this.#gridManager = new GridManager(this.getWidth(), this.getHeight(), this.#theme);
  188. this.#nextPieces = [];
  189. this.generateNextPieces();
  190. this.createTetromino();
  191. tickCallback(
  192. this.#scoreManager.getScore(),
  193. this.#scoreManager.getLevel(),
  194. this.#gridManager.getCurrentGrid());
  195. clockCallback(this.#gameTime);
  196. this.#onTick = this.onTick.bind(this, tickCallback);
  197. this.#onClock = this.onClock.bind(this, clockCallback);
  198. this.#gameTickInterval = setInterval(this.#onTick, this.#gameTick);
  199. this.#gameTimeInterval = setInterval(this.#onClock, 1000);
  200. this.endCallback = endCallback;
  201. }
  202. }