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.ts 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Copyright (c) 2019 - 2020 Arnaud Vergnet.
  3. *
  4. * This file is part of Campus INSAT.
  5. *
  6. * Campus INSAT is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Campus INSAT is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import Piece from './Piece';
  20. import ScoreManager from './ScoreManager';
  21. import GridManager from './GridManager';
  22. import type {GridType} from '../components/GridComponent';
  23. export type TickCallbackType = (
  24. score: number,
  25. level: number,
  26. grid: GridType,
  27. ) => void;
  28. export type ClockCallbackType = (time: number) => void;
  29. export type EndCallbackType = (
  30. time: number,
  31. score: number,
  32. isRestart: boolean,
  33. ) => void;
  34. export type MovementCallbackType = (grid: GridType, score?: number) => void;
  35. export default class GameLogic {
  36. static levelTicks = [1000, 800, 600, 400, 300, 200, 150, 100];
  37. scoreManager: ScoreManager;
  38. gridManager: GridManager;
  39. height: number;
  40. width: number;
  41. gameRunning: boolean;
  42. gamePaused: boolean;
  43. gameTime: number;
  44. currentObject?: Piece;
  45. gameTick: number;
  46. gameTickInterval?: NodeJS.Timeout;
  47. gameTimeInterval?: NodeJS.Timeout;
  48. pressInInterval?: NodeJS.Timeout;
  49. isPressedIn: boolean;
  50. autoRepeatActivationDelay: number;
  51. autoRepeatDelay: number;
  52. nextPieces: Array<Piece>;
  53. nextPiecesCount: number;
  54. tickCallback?: TickCallbackType;
  55. clockCallback?: ClockCallbackType;
  56. endCallback?: EndCallbackType;
  57. theme: ReactNativePaper.Theme;
  58. constructor(height: number, width: number, theme: ReactNativePaper.Theme) {
  59. this.gameTime = 0;
  60. this.gameTick = 0;
  61. this.isPressedIn = false;
  62. this.height = height;
  63. this.width = width;
  64. this.gameRunning = false;
  65. this.gamePaused = false;
  66. this.theme = theme;
  67. this.autoRepeatActivationDelay = 300;
  68. this.autoRepeatDelay = 50;
  69. this.nextPieces = [];
  70. this.nextPiecesCount = 3;
  71. this.scoreManager = new ScoreManager();
  72. this.gridManager = new GridManager(
  73. this.getWidth(),
  74. this.getHeight(),
  75. this.theme,
  76. );
  77. }
  78. getHeight(): number {
  79. return this.height;
  80. }
  81. getWidth(): number {
  82. return this.width;
  83. }
  84. getCurrentGrid(): GridType {
  85. return this.gridManager.getCurrentGrid();
  86. }
  87. isGamePaused(): boolean {
  88. return this.gamePaused;
  89. }
  90. onFreeze = () => {
  91. if (this.currentObject) {
  92. this.gridManager.freezeTetromino(this.currentObject, this.scoreManager);
  93. }
  94. this.createTetromino();
  95. };
  96. setNewGameTick(level: number) {
  97. if (level >= GameLogic.levelTicks.length) {
  98. return;
  99. }
  100. this.gameTick = GameLogic.levelTicks[level];
  101. this.stopTick();
  102. this.startTick();
  103. }
  104. startClock() {
  105. this.gameTimeInterval = setInterval(() => {
  106. this.onClock(this.clockCallback);
  107. }, 1000);
  108. }
  109. startTick() {
  110. this.gameTickInterval = setInterval(() => {
  111. this.onTick(this.tickCallback);
  112. }, this.gameTick);
  113. }
  114. stopClock() {
  115. if (this.gameTimeInterval) {
  116. clearInterval(this.gameTimeInterval);
  117. }
  118. }
  119. stopTick() {
  120. if (this.gameTickInterval) {
  121. clearInterval(this.gameTickInterval);
  122. }
  123. }
  124. stopGameTime() {
  125. this.stopClock();
  126. this.stopTick();
  127. }
  128. startGameTime() {
  129. this.startClock();
  130. this.startTick();
  131. }
  132. onTick(callback?: TickCallbackType) {
  133. if (this.currentObject) {
  134. this.currentObject.tryMove(
  135. 0,
  136. 1,
  137. this.gridManager.getCurrentGrid(),
  138. this.getWidth(),
  139. this.getHeight(),
  140. this.onFreeze,
  141. );
  142. }
  143. if (callback) {
  144. callback(
  145. this.scoreManager.getScore(),
  146. this.scoreManager.getLevel(),
  147. this.gridManager.getCurrentGrid(),
  148. );
  149. }
  150. if (this.scoreManager.canLevelUp()) {
  151. this.setNewGameTick(this.scoreManager.getLevel());
  152. }
  153. }
  154. onClock(callback?: ClockCallbackType) {
  155. this.gameTime += 1;
  156. if (callback) {
  157. callback(this.gameTime);
  158. }
  159. }
  160. canUseInput(): boolean {
  161. return this.gameRunning && !this.gamePaused;
  162. }
  163. rightPressed(callback: MovementCallbackType) {
  164. this.isPressedIn = true;
  165. this.movePressedRepeat(true, callback, 1, 0);
  166. }
  167. leftPressedIn(callback: MovementCallbackType) {
  168. this.isPressedIn = true;
  169. this.movePressedRepeat(true, callback, -1, 0);
  170. }
  171. downPressedIn(callback: MovementCallbackType) {
  172. this.isPressedIn = true;
  173. this.movePressedRepeat(true, callback, 0, 1);
  174. }
  175. movePressedRepeat(
  176. isInitial: boolean,
  177. callback: MovementCallbackType,
  178. x: number,
  179. y: number,
  180. ) {
  181. if (!this.canUseInput() || !this.isPressedIn) {
  182. return;
  183. }
  184. const moved =
  185. this.currentObject &&
  186. this.currentObject.tryMove(
  187. x,
  188. y,
  189. this.gridManager.getCurrentGrid(),
  190. this.getWidth(),
  191. this.getHeight(),
  192. this.onFreeze,
  193. );
  194. if (moved) {
  195. if (y === 1) {
  196. this.scoreManager.incrementScore();
  197. callback(
  198. this.gridManager.getCurrentGrid(),
  199. this.scoreManager.getScore(),
  200. );
  201. } else {
  202. callback(this.gridManager.getCurrentGrid());
  203. }
  204. }
  205. this.pressInInterval = setTimeout(
  206. () => {
  207. this.movePressedRepeat(false, callback, x, y);
  208. },
  209. isInitial ? this.autoRepeatActivationDelay : this.autoRepeatDelay,
  210. );
  211. }
  212. pressedOut() {
  213. this.isPressedIn = false;
  214. if (this.pressInInterval) {
  215. clearTimeout(this.pressInInterval);
  216. }
  217. }
  218. rotatePressed(callback: MovementCallbackType) {
  219. if (!this.canUseInput()) {
  220. return;
  221. }
  222. if (
  223. this.currentObject &&
  224. this.currentObject.tryRotate(
  225. this.gridManager.getCurrentGrid(),
  226. this.getWidth(),
  227. this.getHeight(),
  228. )
  229. ) {
  230. callback(this.gridManager.getCurrentGrid());
  231. }
  232. }
  233. getNextPiecesPreviews(): Array<GridType> {
  234. const finalArray = [];
  235. for (let i = 0; i < this.nextPieces.length; i += 1) {
  236. const gridSize = this.nextPieces[i].getCurrentShape().getCurrentShape()[0]
  237. .length;
  238. finalArray.push(this.gridManager.getEmptyGrid(gridSize, gridSize));
  239. this.nextPieces[i].toGrid(finalArray[i], true);
  240. }
  241. return finalArray;
  242. }
  243. recoverNextPiece() {
  244. const next = this.nextPieces.shift();
  245. if (next) {
  246. this.currentObject = next;
  247. }
  248. this.generateNextPieces();
  249. }
  250. generateNextPieces() {
  251. while (this.nextPieces.length < this.nextPiecesCount) {
  252. this.nextPieces.push(new Piece(this.theme));
  253. }
  254. }
  255. createTetromino() {
  256. this.pressedOut();
  257. this.recoverNextPiece();
  258. if (
  259. this.currentObject &&
  260. !this.currentObject.isPositionValid(
  261. this.gridManager.getCurrentGrid(),
  262. this.getWidth(),
  263. this.getHeight(),
  264. )
  265. ) {
  266. this.endGame(false);
  267. }
  268. }
  269. togglePause() {
  270. if (!this.gameRunning) {
  271. return;
  272. }
  273. this.gamePaused = !this.gamePaused;
  274. if (this.gamePaused) {
  275. this.stopGameTime();
  276. } else {
  277. this.startGameTime();
  278. }
  279. }
  280. endGame(isRestart: boolean) {
  281. this.gameRunning = false;
  282. this.gamePaused = false;
  283. this.stopGameTime();
  284. if (this.endCallback) {
  285. this.endCallback(this.gameTime, this.scoreManager.getScore(), isRestart);
  286. }
  287. }
  288. startGame(
  289. tickCallback: TickCallbackType,
  290. clockCallback: ClockCallbackType,
  291. endCallback: EndCallbackType,
  292. ) {
  293. if (this.gameRunning) {
  294. this.endGame(true);
  295. }
  296. this.gameRunning = true;
  297. this.gamePaused = false;
  298. this.gameTime = 0;
  299. this.scoreManager = new ScoreManager();
  300. this.gameTick = GameLogic.levelTicks[this.scoreManager.getLevel()];
  301. this.gridManager = new GridManager(
  302. this.getWidth(),
  303. this.getHeight(),
  304. this.theme,
  305. );
  306. this.nextPieces = [];
  307. this.generateNextPieces();
  308. this.createTetromino();
  309. tickCallback(
  310. this.scoreManager.getScore(),
  311. this.scoreManager.getLevel(),
  312. this.gridManager.getCurrentGrid(),
  313. );
  314. clockCallback(this.gameTime);
  315. this.startTick();
  316. this.startClock();
  317. this.tickCallback = tickCallback;
  318. this.clockCallback = clockCallback;
  319. this.endCallback = endCallback;
  320. }
  321. }