application-amicale/screens/Tetris/GameLogic.js

238 lines
6.9 KiB
JavaScript
Raw Normal View History

2020-03-15 18:44:32 +01:00
// @flow
2020-03-23 11:32:50 +01:00
import Piece from "./Piece";
import ScoreManager from "./ScoreManager";
import GridManager from "./GridManager";
2020-03-15 18:44:32 +01:00
export default class GameLogic {
2020-03-17 11:00:45 +01:00
static levelTicks = [
1000,
800,
600,
400,
300,
200,
150,
100,
];
#scoreManager: ScoreManager;
#gridManager: GridManager;
2020-03-15 18:44:32 +01:00
#height: number;
#width: number;
2020-03-15 18:44:32 +01:00
#gameRunning: boolean;
#gamePaused: boolean;
#gameTime: number;
2020-03-15 18:44:32 +01:00
#currentObject: Piece;
2020-03-15 18:44:32 +01:00
#gameTick: number;
#gameTickInterval: IntervalID;
#gameTimeInterval: IntervalID;
2020-03-15 18:44:32 +01:00
#pressInInterval: TimeoutID;
#isPressedIn: boolean;
#autoRepeatActivationDelay: number;
#autoRepeatDelay: number;
2020-03-17 00:24:57 +01:00
#nextPieces: Array<Piece>;
#nextPiecesCount: number;
2020-03-17 00:24:57 +01:00
#onTick: Function;
#onClock: Function;
2020-03-15 19:28:41 +01:00
endCallback: Function;
2020-03-15 18:44:32 +01:00
#colors: Object;
2020-03-15 20:34:20 +01:00
constructor(height: number, width: number, colors: Object) {
this.#height = height;
this.#width = width;
this.#gameRunning = false;
this.#gamePaused = false;
this.#colors = colors;
this.#autoRepeatActivationDelay = 300;
this.#autoRepeatDelay = 50;
this.#nextPieces = [];
this.#nextPiecesCount = 3;
this.#scoreManager = new ScoreManager();
this.#gridManager = new GridManager(this.getWidth(), this.getHeight(), this.#colors);
2020-03-15 18:44:32 +01:00
}
getHeight(): number {
return this.#height;
2020-03-15 18:44:32 +01:00
}
getWidth(): number {
return this.#width;
2020-03-15 18:44:32 +01:00
}
getCurrentGrid() {
return this.#gridManager.getCurrentGrid();
}
2020-03-15 18:44:32 +01:00
isGameRunning(): boolean {
return this.#gameRunning;
2020-03-15 18:44:32 +01:00
}
2020-03-16 19:10:32 +01:00
isGamePaused(): boolean {
return this.#gamePaused;
2020-03-16 19:10:32 +01:00
}
onFreeze() {
this.#gridManager.freezeTetromino(this.#currentObject, this.#scoreManager);
this.createTetromino();
2020-03-15 18:44:32 +01:00
}
2020-03-16 20:10:54 +01:00
setNewGameTick(level: number) {
2020-03-17 11:00:45 +01:00
if (level >= GameLogic.levelTicks.length)
2020-03-16 20:10:54 +01:00
return;
this.#gameTick = GameLogic.levelTicks[level];
clearInterval(this.#gameTickInterval);
this.#gameTickInterval = setInterval(this.#onTick, this.#gameTick);
2020-03-16 20:10:54 +01:00
}
2020-03-15 18:44:32 +01:00
onTick(callback: Function) {
this.#currentObject.tryMove(0, 1,
this.#gridManager.getCurrentGrid(), this.getWidth(), this.getHeight(),
2020-03-23 11:32:50 +01:00
() => this.onFreeze());
callback(
this.#scoreManager.getScore(),
this.#scoreManager.getLevel(),
this.#gridManager.getCurrentGrid());
if (this.#scoreManager.canLevelUp())
this.setNewGameTick(this.#scoreManager.getLevel());
2020-03-16 19:40:52 +01:00
}
onClock(callback: Function) {
this.#gameTime++;
callback(this.#gameTime);
2020-03-15 18:44:32 +01:00
}
2020-03-16 19:10:32 +01:00
canUseInput() {
return this.#gameRunning && !this.#gamePaused
2020-03-16 19:10:32 +01:00
}
2020-03-15 18:44:32 +01:00
rightPressed(callback: Function) {
this.#isPressedIn = true;
2020-03-17 00:24:57 +01:00
this.movePressedRepeat(true, callback, 1, 0);
2020-03-15 18:44:32 +01:00
}
2020-03-17 00:24:57 +01:00
leftPressedIn(callback: Function) {
this.#isPressedIn = true;
2020-03-17 00:24:57 +01:00
this.movePressedRepeat(true, callback, -1, 0);
}
2020-03-16 19:10:32 +01:00
2020-03-17 00:24:57 +01:00
downPressedIn(callback: Function) {
this.#isPressedIn = true;
2020-03-17 00:24:57 +01:00
this.movePressedRepeat(true, callback, 0, 1);
2020-03-16 19:26:42 +01:00
}
2020-03-17 00:24:57 +01:00
movePressedRepeat(isInitial: boolean, callback: Function, x: number, y: number) {
if (!this.canUseInput() || !this.#isPressedIn)
2020-03-16 19:26:42 +01:00
return;
const moved = this.#currentObject.tryMove(x, y,
this.#gridManager.getCurrentGrid(), this.getWidth(), this.getHeight(),
2020-03-23 11:32:50 +01:00
() => this.onFreeze());
if (moved) {
2020-03-17 00:24:57 +01:00
if (y === 1) {
this.#scoreManager.incrementScore();
callback(this.#gridManager.getCurrentGrid(), this.#scoreManager.getScore());
2020-03-17 00:24:57 +01:00
} else
callback(this.#gridManager.getCurrentGrid());
2020-03-16 19:26:42 +01:00
}
this.#pressInInterval = setTimeout(() =>
this.movePressedRepeat(false, callback, x, y),
isInitial ? this.#autoRepeatActivationDelay : this.#autoRepeatDelay
);
2020-03-17 00:24:57 +01:00
}
pressedOut() {
this.#isPressedIn = false;
clearTimeout(this.#pressInInterval);
2020-03-15 18:44:32 +01:00
}
2020-03-15 19:28:41 +01:00
rotatePressed(callback: Function) {
2020-03-16 19:10:32 +01:00
if (!this.canUseInput())
return;
if (this.#currentObject.tryRotate(this.#gridManager.getCurrentGrid(), this.getWidth(), this.getHeight()))
callback(this.#gridManager.getCurrentGrid());
}
getNextPiecesPreviews() {
let finalArray = [];
for (let i = 0; i < this.#nextPieces.length; i++) {
finalArray.push(this.#gridManager.getEmptyGrid(4, 4));
this.#nextPieces[i].toGrid(finalArray[i], true);
}
return finalArray;
2020-03-15 19:28:41 +01:00
}
2020-03-17 14:22:49 +01:00
recoverNextPiece() {
this.#currentObject = this.#nextPieces.shift();
2020-03-17 14:22:49 +01:00
this.generateNextPieces();
}
generateNextPieces() {
while (this.#nextPieces.length < this.#nextPiecesCount) {
this.#nextPieces.push(new Piece(this.#colors));
2020-03-17 14:22:49 +01:00
}
}
2020-03-15 18:44:32 +01:00
createTetromino() {
2020-03-17 00:24:57 +01:00
this.pressedOut();
2020-03-17 14:22:49 +01:00
this.recoverNextPiece();
if (!this.#currentObject.isPositionValid(this.#gridManager.getCurrentGrid(), this.getWidth(), this.getHeight()))
2020-03-16 19:10:32 +01:00
this.endGame(false);
2020-03-15 18:44:32 +01:00
}
2020-03-16 19:10:32 +01:00
togglePause() {
if (!this.#gameRunning)
2020-03-16 19:10:32 +01:00
return;
this.#gamePaused = !this.#gamePaused;
if (this.#gamePaused) {
clearInterval(this.#gameTickInterval);
clearInterval(this.#gameTimeInterval);
2020-03-16 19:10:32 +01:00
} else {
this.#gameTickInterval = setInterval(this.#onTick, this.#gameTick);
this.#gameTimeInterval = setInterval(this.#onClock, 1000);
2020-03-16 19:10:32 +01:00
}
}
endGame(isRestart: boolean) {
this.#gameRunning = false;
this.#gamePaused = false;
clearInterval(this.#gameTickInterval);
clearInterval(this.#gameTimeInterval);
this.endCallback(this.#gameTime, this.#scoreManager.getScore(), isRestart);
2020-03-15 18:44:32 +01:00
}
2020-03-16 19:40:52 +01:00
startGame(tickCallback: Function, clockCallback: Function, endCallback: Function) {
if (this.#gameRunning)
2020-03-16 19:10:32 +01:00
this.endGame(true);
this.#gameRunning = true;
this.#gamePaused = false;
this.#gameTime = 0;
this.#scoreManager = new ScoreManager();
this.#gameTick = GameLogic.levelTicks[this.#scoreManager.getLevel()];
this.#gridManager = new GridManager(this.getWidth(), this.getHeight(), this.#colors);
this.#nextPieces = [];
2020-03-17 14:22:49 +01:00
this.generateNextPieces();
2020-03-15 18:44:32 +01:00
this.createTetromino();
tickCallback(
this.#scoreManager.getScore(),
this.#scoreManager.getLevel(),
this.#gridManager.getCurrentGrid());
clockCallback(this.#gameTime);
this.#onTick = this.onTick.bind(this, tickCallback);
this.#onClock = this.onClock.bind(this, clockCallback);
this.#gameTickInterval = setInterval(this.#onTick, this.#gameTick);
this.#gameTimeInterval = setInterval(this.#onClock, 1000);
2020-03-15 19:28:41 +01:00
this.endCallback = endCallback;
2020-03-15 18:44:32 +01:00
}
}