Application Android et IOS pour l'amicale des élèves https://play.google.com/store/apps/details?id=fr.amicaleinsat.application
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 6.9KB

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