From 7d718141e7d329b42149579a34404b9a34413f57 Mon Sep 17 00:00:00 2001 From: Arnaud Vergnet Date: Tue, 17 Mar 2020 00:24:57 +0100 Subject: [PATCH] Added touch and hold controls --- screens/Tetris/GameLogic.js | 51 ++++++++++++++++++++++------------ screens/Tetris/TetrisScreen.js | 10 +++++-- 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/screens/Tetris/GameLogic.js b/screens/Tetris/GameLogic.js index 8aed8fb..e0b00a8 100644 --- a/screens/Tetris/GameLogic.js +++ b/screens/Tetris/GameLogic.js @@ -47,6 +47,12 @@ export default class GameLogic { gameTickInterval: IntervalID; gameTimeInterval: IntervalID; + pressInInterval: TimeoutID; + isPressedIn: boolean; + autoRepeatActivationDelay: number; + autoRepeatDelay: number; + + onTick: Function; onClock: Function; endCallback: Function; @@ -59,6 +65,8 @@ export default class GameLogic { this.gameRunning = false; this.gamePaused = false; this.colors = colors; + this.autoRepeatActivationDelay = 300; + this.autoRepeatDelay = 100; } getHeight(): number { @@ -187,7 +195,7 @@ export default class GameLogic { tryRotateTetromino() { this.currentObject.rotate(true); - if (!this.isTetrominoPositionValid()){ + if (!this.isTetrominoPositionValid()) { this.currentObject.rotate(false); return false; } @@ -221,29 +229,37 @@ export default class GameLogic { } rightPressed(callback: Function) { - if (!this.canUseInput()) - return; - - if (this.tryMoveTetromino(1, 0)) - callback(this.getFinalGrid()); + this.isPressedIn = true; + this.movePressedRepeat(true, callback, 1, 0); } - leftPressed(callback: Function) { - if (!this.canUseInput()) - return; - - if (this.tryMoveTetromino(-1, 0)) - callback(this.getFinalGrid()); + leftPressedIn(callback: Function) { + this.isPressedIn = true; + this.movePressedRepeat(true, callback, -1, 0); } - downPressed(callback: Function) { - if (!this.canUseInput()) + downPressedIn(callback: Function) { + this.isPressedIn = true; + this.movePressedRepeat(true, callback, 0, 1); + } + + movePressedRepeat(isInitial: boolean, callback: Function, x: number, y: number) { + if (!this.canUseInput() || !this.isPressedIn) return; - if (this.tryMoveTetromino(0, 1)){ - this.score++; - callback(this.getFinalGrid(), this.score); + if (this.tryMoveTetromino(x, y)) { + if (y === 1) { + this.score++; + callback(this.getFinalGrid(), this.score); + } else + callback(this.getFinalGrid()); } + this.pressInInterval = setTimeout(() => this.movePressedRepeat(false, callback, x, y), isInitial ? this.autoRepeatActivationDelay : this.autoRepeatDelay); + } + + pressedOut() { + this.isPressedIn = false; + clearTimeout(this.pressInInterval); } rotatePressed(callback: Function) { @@ -255,6 +271,7 @@ export default class GameLogic { } createTetromino() { + this.pressedOut(); let shape = Math.floor(Math.random() * 7); this.currentObject = new Tetromino(shape, this.colors); if (!this.isTetrominoPositionValid()) diff --git a/screens/Tetris/TetrisScreen.js b/screens/Tetris/TetrisScreen.js index a4937c9..ceb3ac6 100644 --- a/screens/Tetris/TetrisScreen.js +++ b/screens/Tetris/TetrisScreen.js @@ -259,18 +259,22 @@ class TetrisScreen extends React.Component { this.logic.leftPressed(this.updateGrid)} + onPress={() => this.logic.pressedOut()} + onPressIn={() => this.logic.leftPressedIn(this.updateGrid)} + /> this.logic.rightPressed(this.updateGrid)} + onPress={() => this.logic.pressedOut()} + onPressIn={() => this.logic.rightPressed(this.updateGrid)} /> this.logic.downPressed(this.updateGridScore)} + onPressIn={() => this.logic.downPressedIn(this.updateGridScore)} + onPress={() => this.logic.pressedOut()} style={{marginLeft: 'auto'}} />