application-amicale/screens/Tetris/GameLogic.js

235 lines
6.8 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;
gameRunning: boolean;
2020-03-16 19:10:32 +01:00
gamePaused: boolean;
2020-03-15 18:44:32 +01:00
gameTime: number;
2020-03-23 11:32:50 +01:00
currentObject: Piece;
2020-03-15 18:44:32 +01:00
gameTick: number;
gameTickInterval: IntervalID;
2020-03-16 19:40:52 +01:00
gameTimeInterval: IntervalID;
2020-03-15 18:44:32 +01:00
2020-03-17 00:24:57 +01:00
pressInInterval: TimeoutID;
isPressedIn: boolean;
autoRepeatActivationDelay: number;
autoRepeatDelay: number;
2020-03-23 11:32:50 +01:00
nextPieces: Array<Piece>;
2020-03-17 14:22:49 +01:00
nextPiecesCount: number;
2020-03-17 00:24:57 +01:00
2020-03-15 18:44:32 +01:00
onTick: Function;
2020-03-16 19:40:52 +01:00
onClock: Function;
2020-03-15 19:28:41 +01:00
endCallback: Function;
2020-03-15 18:44:32 +01:00
2020-03-15 20:34:20 +01:00
colors: Object;
constructor(height: number, width: number, colors: Object) {
2020-03-15 18:44:32 +01:00
this.height = height;
this.width = width;
this.gameRunning = false;
2020-03-16 19:10:32 +01:00
this.gamePaused = false;
2020-03-15 20:34:20 +01:00
this.colors = colors;
2020-03-17 00:24:57 +01:00
this.autoRepeatActivationDelay = 300;
2020-03-17 11:12:55 +01:00
this.autoRepeatDelay = 50;
2020-03-17 14:22:49 +01:00
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;
}
getWidth(): number {
return this.width;
}
getCurrentGrid() {
return this.gridManager.getCurrentGrid();
}
2020-03-15 18:44:32 +01:00
isGameRunning(): boolean {
return this.gameRunning;
}
2020-03-16 19:10:32 +01:00
isGamePaused(): boolean {
return this.gamePaused;
}
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-15 18:44:32 +01:00
onTick(callback: Function) {
2020-03-23 11:32:50 +01:00
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.getFinalGrid(this.currentObject));
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-15 18:44:32 +01:00
rightPressed(callback: Function) {
2020-03-17 00:24:57 +01:00
this.isPressedIn = true;
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;
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;
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;
2020-03-23 11:32:50 +01:00
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.getFinalGrid(this.currentObject), this.scoreManager.getScore());
2020-03-17 00:24:57 +01:00
} else
callback(this.gridManager.getFinalGrid(this.currentObject));
2020-03-16 19:26:42 +01:00
}
2020-03-17 00:24:57 +01:00
this.pressInInterval = setTimeout(() => this.movePressedRepeat(false, callback, x, y), isInitial ? this.autoRepeatActivationDelay : this.autoRepeatDelay);
}
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.getFinalGrid(this.currentObject));
}
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();
this.generateNextPieces();
}
generateNextPieces() {
while (this.nextPieces.length < this.nextPiecesCount) {
2020-03-23 11:32:50 +01:00
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)
return;
this.gamePaused = !this.gamePaused;
if (this.gamePaused) {
clearInterval(this.gameTickInterval);
2020-03-16 19:40:52 +01:00
clearInterval(this.gameTimeInterval);
2020-03-16 19:10:32 +01:00
} else {
this.gameTickInterval = setInterval(this.onTick, this.gameTick);
2020-03-16 19:40:52 +01:00
this.gameTimeInterval = setInterval(this.onClock, 1000);
2020-03-16 19:10:32 +01:00
}
}
endGame(isRestart: boolean) {
2020-03-15 19:28:41 +01:00
this.gameRunning = false;
2020-03-16 19:10:32 +01:00
this.gamePaused = false;
2020-03-15 18:44:32 +01:00
clearInterval(this.gameTickInterval);
2020-03-16 19:40:52 +01:00
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) {
2020-03-15 18:44:32 +01:00
if (this.gameRunning)
2020-03-16 19:10:32 +01:00
this.endGame(true);
2020-03-15 18:44:32 +01:00
this.gameRunning = true;
2020-03-16 19:10:32 +01:00
this.gamePaused = false;
2020-03-15 18:44:32 +01:00
this.gameTime = 0;
this.scoreManager = new ScoreManager();
this.gameTick = GameLogic.levelTicks[this.scoreManager.getLevel()];
this.gridManager = new GridManager(this.getWidth(), this.getHeight(), this.colors);
2020-03-17 14:22:49 +01:00
this.nextPieces = [];
this.generateNextPieces();
2020-03-15 18:44:32 +01:00
this.createTetromino();
tickCallback(
this.scoreManager.getScore(),
this.scoreManager.getLevel(),
this.gridManager.getFinalGrid(this.currentObject));
2020-03-16 19:40:52 +01:00
clockCallback(this.gameTime);
2020-03-15 19:28:41 +01:00
this.onTick = this.onTick.bind(this, tickCallback);
2020-03-16 19:40:52 +01:00
this.onClock = this.onClock.bind(this, clockCallback);
2020-03-15 18:44:32 +01:00
this.gameTickInterval = setInterval(this.onTick, this.gameTick);
2020-03-16 19:40:52 +01:00
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
}
}