Compare commits

..

No commits in common. "dbe03a2a2d48aba0a2eff793aa0380dd4b525dd0" and "b29973189f092c42c40a7a51caf908a1af3d79ce" have entirely different histories.

3 changed files with 72 additions and 206 deletions

View file

@ -23,11 +23,11 @@ export default class GameLogic {
'3': 500, '3': 500,
'4': 700, '4': 700,
'5': 1000, '5': 1000,
'6': 1500, '7': 1500,
'7': 2000, '8': 2000,
'8': 3000, '9': 3000,
'9': 4000, '10': 4000,
'10': 5000, '11': 5000,
}; };
currentGrid: Array<Array<Object>>; currentGrid: Array<Array<Object>>;
@ -47,12 +47,6 @@ export default class GameLogic {
gameTickInterval: IntervalID; gameTickInterval: IntervalID;
gameTimeInterval: IntervalID; gameTimeInterval: IntervalID;
pressInInterval: TimeoutID;
isPressedIn: boolean;
autoRepeatActivationDelay: number;
autoRepeatDelay: number;
onTick: Function; onTick: Function;
onClock: Function; onClock: Function;
endCallback: Function; endCallback: Function;
@ -65,8 +59,6 @@ export default class GameLogic {
this.gameRunning = false; this.gameRunning = false;
this.gamePaused = false; this.gamePaused = false;
this.colors = colors; this.colors = colors;
this.autoRepeatActivationDelay = 300;
this.autoRepeatDelay = 100;
} }
getHeight(): number { getHeight(): number {
@ -195,11 +187,8 @@ export default class GameLogic {
tryRotateTetromino() { tryRotateTetromino() {
this.currentObject.rotate(true); this.currentObject.rotate(true);
if (!this.isTetrominoPositionValid()) { if (!this.isTetrominoPositionValid())
this.currentObject.rotate(false); this.currentObject.rotate(false);
return false;
}
return true;
} }
setNewGameTick(level: number) { setNewGameTick(level: number) {
@ -229,37 +218,29 @@ export default class GameLogic {
} }
rightPressed(callback: Function) { rightPressed(callback: Function) {
this.isPressedIn = true; if (!this.canUseInput())
this.movePressedRepeat(true, callback, 1, 0);
}
leftPressedIn(callback: Function) {
this.isPressedIn = true;
this.movePressedRepeat(true, callback, -1, 0);
}
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; return;
if (this.tryMoveTetromino(x, y)) { if (this.tryMoveTetromino(1, 0))
if (y === 1) { callback(this.getFinalGrid());
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() { leftPressed(callback: Function) {
this.isPressedIn = false; if (!this.canUseInput())
clearTimeout(this.pressInInterval); return;
if (this.tryMoveTetromino(-1, 0))
callback(this.getFinalGrid());
}
downPressed(callback: Function) {
if (!this.canUseInput())
return;
if (this.tryMoveTetromino(0, 1)){
this.score++;
callback(this.getFinalGrid(), this.score);
}
} }
rotatePressed(callback: Function) { rotatePressed(callback: Function) {
@ -271,7 +252,6 @@ export default class GameLogic {
} }
createTetromino() { createTetromino() {
this.pressedOut();
let shape = Math.floor(Math.random() * 7); let shape = Math.floor(Math.random() * 7);
this.currentObject = new Tetromino(shape, this.colors); this.currentObject = new Tetromino(shape, this.colors);
if (!this.isTetrominoPositionValid()) if (!this.isTetrominoPositionValid())

View file

@ -91,6 +91,7 @@ class TetrisScreen extends React.Component<Props, State> {
date.setMinutes(0); date.setMinutes(0);
date.setSeconds(seconds); date.setSeconds(seconds);
let format; let format;
console.log(date);
if (date.getHours()) if (date.getHours())
format = date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds(); format = date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
else if (date.getMinutes()) else if (date.getMinutes())
@ -259,22 +260,18 @@ class TetrisScreen extends React.Component<Props, State> {
<IconButton <IconButton
icon="arrow-left" icon="arrow-left"
size={40} size={40}
onPress={() => this.logic.pressedOut()} onPress={() => this.logic.leftPressed(this.updateGrid)}
onPressIn={() => this.logic.leftPressedIn(this.updateGrid)}
/> />
<IconButton <IconButton
icon="arrow-right" icon="arrow-right"
size={40} size={40}
onPress={() => this.logic.pressedOut()} onPress={() => this.logic.rightPressed(this.updateGrid)}
onPressIn={() => this.logic.rightPressed(this.updateGrid)}
/> />
</View> </View>
<IconButton <IconButton
icon="arrow-down" icon="arrow-down"
size={40} size={40}
onPressIn={() => this.logic.downPressedIn(this.updateGridScore)} onPress={() => this.logic.downPressed(this.updateGridScore)}
onPress={() => this.logic.pressedOut()}
style={{marginLeft: 'auto'}} style={{marginLeft: 'auto'}}
/> />
</View> </View>

View file

@ -10,156 +10,35 @@ export default class Tetromino {
'L': 6, 'L': 6,
}; };
static shapes = [ static shapes = {
[ 0: [
[ [1, 1, 1, 1]
[0, 0, 0, 0],
[1, 1, 1, 1],
[0, 0, 0, 0],
[0, 0, 0, 0],
],
[
[1, 1],
[1, 1],
],
[
[0, 1, 0],
[1, 1, 1],
[0, 0, 0],
],
[
[0, 1, 1],
[1, 1, 0],
[0, 0, 0],
],
[
[1, 1, 0],
[0, 1, 1],
[0, 0, 0],
],
[
[1, 0, 0],
[1, 1, 1],
[0, 0, 0],
],
[
[0, 0, 1],
[1, 1, 1],
[0, 0, 0],
],
], ],
[ 1: [
[ [1, 1],
[0, 0, 1, 0], [1, 1]
[0, 0, 1, 0],
[0, 0, 1, 0],
[0, 0, 1, 0],
],
[
[1, 1],
[1, 1],
],
[
[0, 1, 0],
[0, 1, 1],
[0, 1, 0],
],
[
[0, 1, 0],
[0, 1, 1],
[0, 0, 1],
],
[
[0, 0, 1],
[0, 1, 1],
[0, 1, 0],
],
[
[0, 1, 1],
[0, 1, 0],
[0, 1, 0],
],
[
[0, 1, 0],
[0, 1, 0],
[0, 1, 1],
],
], ],
[ 2: [
[ [0, 1, 0],
[0, 0, 0, 0], [1, 1, 1],
[0, 0, 0, 0],
[1, 1, 1, 1],
[0, 0, 0, 0],
],
[
[1, 1],
[1, 1],
],
[
[0, 0, 0],
[1, 1, 1],
[0, 1, 0],
],
[
[0, 0, 0],
[0, 1, 1],
[1, 1, 0],
],
[
[0, 0, 0],
[1, 1, 0],
[0, 1, 1],
],
[
[0, 0, 0],
[1, 1, 1],
[0, 0, 1],
],
[
[0, 0, 0],
[1, 1, 1],
[1, 0, 0],
],
], ],
[ 3: [
[ [0, 1, 1],
[0, 1, 0, 0], [1, 1, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
],
[
[1, 1],
[1, 1],
],
[
[0, 1, 0],
[1, 1, 0],
[0, 1, 0],
],
[
[1, 0, 0],
[1, 1, 0],
[0, 1, 0],
],
[
[0, 1, 0],
[1, 1, 0],
[1, 0, 0],
],
[
[0, 1, 0],
[0, 1, 0],
[1, 1, 0],
],
[
[1, 1, 0],
[0, 1, 0],
[0, 1, 0],
],
], ],
]; 4: [
[1, 1, 0],
[0, 1, 1],
],
5: [
[1, 0, 0],
[1, 1, 1],
],
6: [
[0, 0, 1],
[1, 1, 1],
],
};
static colors: Object; static colors: Object;
@ -171,8 +50,8 @@ export default class Tetromino {
constructor(type: Tetromino.types, colors: Object) { constructor(type: Tetromino.types, colors: Object) {
this.currentType = type; this.currentType = type;
this.currentShape = Tetromino.shapes[type];
this.currentRotation = 0; this.currentRotation = 0;
this.currentShape = Tetromino.shapes[this.currentRotation][type];
this.position = {x: 0, y: 0}; this.position = {x: 0, y: 0};
if (this.currentType === Tetromino.types.O) if (this.currentType === Tetromino.types.O)
this.position.x = 4; this.position.x = 4;
@ -206,15 +85,25 @@ export default class Tetromino {
} }
rotate(isForward) { rotate(isForward) {
if (isForward) this.currentRotation++;
this.currentRotation++;
else
this.currentRotation--;
if (this.currentRotation > 3) if (this.currentRotation > 3)
this.currentRotation = 0; this.currentRotation = 0;
else if (this.currentRotation < 0)
this.currentRotation = 3; if (this.currentRotation === 0) {
this.currentShape = Tetromino.shapes[this.currentRotation][this.currentType]; this.currentShape = Tetromino.shapes[this.currentType];
} else {
let result = [];
for(let i = 0; i < this.currentShape[0].length; i++) {
let row = this.currentShape.map(e => e[i]);
if (isForward)
result.push(row.reverse());
else
result.push(row);
}
this.currentShape = result;
}
} }
move(x: number, y: number) { move(x: number, y: number) {