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

View file

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

View file

@ -10,156 +10,35 @@ export default class Tetromino {
'L': 6,
};
static shapes = [
[
[
[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],
],
static shapes = {
0: [
[1, 1, 1, 1]
],
[
[
[0, 0, 1, 0],
[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],
],
1: [
[1, 1],
[1, 1]
],
[
[
[0, 0, 0, 0],
[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],
],
2: [
[0, 1, 0],
[1, 1, 1],
],
[
[
[0, 1, 0, 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],
],
3: [
[0, 1, 1],
[1, 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;
@ -171,8 +50,8 @@ export default class Tetromino {
constructor(type: Tetromino.types, colors: Object) {
this.currentType = type;
this.currentShape = Tetromino.shapes[type];
this.currentRotation = 0;
this.currentShape = Tetromino.shapes[this.currentRotation][type];
this.position = {x: 0, y: 0};
if (this.currentType === Tetromino.types.O)
this.position.x = 4;
@ -206,15 +85,25 @@ export default class Tetromino {
}
rotate(isForward) {
if (isForward)
this.currentRotation++;
else
this.currentRotation--;
this.currentRotation++;
if (this.currentRotation > 3)
this.currentRotation = 0;
else if (this.currentRotation < 0)
this.currentRotation = 3;
this.currentShape = Tetromino.shapes[this.currentRotation][this.currentType];
if (this.currentRotation === 0) {
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) {