Compare commits
3 commits
b29973189f
...
dbe03a2a2d
| Author | SHA1 | Date | |
|---|---|---|---|
| dbe03a2a2d | |||
| 7d718141e7 | |||
| 79e72784d1 |
3 changed files with 203 additions and 69 deletions
|
|
@ -23,11 +23,11 @@ export default class GameLogic {
|
||||||
'3': 500,
|
'3': 500,
|
||||||
'4': 700,
|
'4': 700,
|
||||||
'5': 1000,
|
'5': 1000,
|
||||||
'7': 1500,
|
'6': 1500,
|
||||||
'8': 2000,
|
'7': 2000,
|
||||||
'9': 3000,
|
'8': 3000,
|
||||||
'10': 4000,
|
'9': 4000,
|
||||||
'11': 5000,
|
'10': 5000,
|
||||||
};
|
};
|
||||||
|
|
||||||
currentGrid: Array<Array<Object>>;
|
currentGrid: Array<Array<Object>>;
|
||||||
|
|
@ -47,6 +47,12 @@ 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;
|
||||||
|
|
@ -59,6 +65,8 @@ 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 {
|
||||||
|
|
@ -187,8 +195,11 @@ 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) {
|
||||||
|
|
@ -218,29 +229,37 @@ export default class GameLogic {
|
||||||
}
|
}
|
||||||
|
|
||||||
rightPressed(callback: Function) {
|
rightPressed(callback: Function) {
|
||||||
if (!this.canUseInput())
|
this.isPressedIn = true;
|
||||||
return;
|
this.movePressedRepeat(true, callback, 1, 0);
|
||||||
|
|
||||||
if (this.tryMoveTetromino(1, 0))
|
|
||||||
callback(this.getFinalGrid());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
leftPressed(callback: Function) {
|
leftPressedIn(callback: Function) {
|
||||||
if (!this.canUseInput())
|
this.isPressedIn = true;
|
||||||
return;
|
this.movePressedRepeat(true, callback, -1, 0);
|
||||||
|
|
||||||
if (this.tryMoveTetromino(-1, 0))
|
|
||||||
callback(this.getFinalGrid());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
downPressed(callback: Function) {
|
downPressedIn(callback: Function) {
|
||||||
if (!this.canUseInput())
|
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(0, 1)){
|
if (this.tryMoveTetromino(x, y)) {
|
||||||
|
if (y === 1) {
|
||||||
this.score++;
|
this.score++;
|
||||||
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() {
|
||||||
|
this.isPressedIn = false;
|
||||||
|
clearTimeout(this.pressInInterval);
|
||||||
}
|
}
|
||||||
|
|
||||||
rotatePressed(callback: Function) {
|
rotatePressed(callback: Function) {
|
||||||
|
|
@ -252,6 +271,7 @@ 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())
|
||||||
|
|
|
||||||
|
|
@ -91,7 +91,6 @@ 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())
|
||||||
|
|
@ -260,18 +259,22 @@ class TetrisScreen extends React.Component<Props, State> {
|
||||||
<IconButton
|
<IconButton
|
||||||
icon="arrow-left"
|
icon="arrow-left"
|
||||||
size={40}
|
size={40}
|
||||||
onPress={() => this.logic.leftPressed(this.updateGrid)}
|
onPress={() => this.logic.pressedOut()}
|
||||||
|
onPressIn={() => this.logic.leftPressedIn(this.updateGrid)}
|
||||||
|
|
||||||
/>
|
/>
|
||||||
<IconButton
|
<IconButton
|
||||||
icon="arrow-right"
|
icon="arrow-right"
|
||||||
size={40}
|
size={40}
|
||||||
onPress={() => this.logic.rightPressed(this.updateGrid)}
|
onPress={() => this.logic.pressedOut()}
|
||||||
|
onPressIn={() => this.logic.rightPressed(this.updateGrid)}
|
||||||
/>
|
/>
|
||||||
</View>
|
</View>
|
||||||
<IconButton
|
<IconButton
|
||||||
icon="arrow-down"
|
icon="arrow-down"
|
||||||
size={40}
|
size={40}
|
||||||
onPress={() => this.logic.downPressed(this.updateGridScore)}
|
onPressIn={() => this.logic.downPressedIn(this.updateGridScore)}
|
||||||
|
onPress={() => this.logic.pressedOut()}
|
||||||
style={{marginLeft: 'auto'}}
|
style={{marginLeft: 'auto'}}
|
||||||
/>
|
/>
|
||||||
</View>
|
</View>
|
||||||
|
|
|
||||||
|
|
@ -10,35 +10,156 @@ 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, 1],
|
[1, 1],
|
||||||
[1, 1]
|
|
||||||
],
|
],
|
||||||
2: [
|
[
|
||||||
[0, 1, 0],
|
[0, 1, 0],
|
||||||
[1, 1, 1],
|
[1, 1, 1],
|
||||||
|
[0, 0, 0],
|
||||||
],
|
],
|
||||||
3: [
|
[
|
||||||
[0, 1, 1],
|
[0, 1, 1],
|
||||||
[1, 1, 0],
|
[1, 1, 0],
|
||||||
|
[0, 0, 0],
|
||||||
],
|
],
|
||||||
4: [
|
[
|
||||||
[1, 1, 0],
|
[1, 1, 0],
|
||||||
[0, 1, 1],
|
[0, 1, 1],
|
||||||
|
[0, 0, 0],
|
||||||
],
|
],
|
||||||
5: [
|
[
|
||||||
[1, 0, 0],
|
[1, 0, 0],
|
||||||
[1, 1, 1],
|
[1, 1, 1],
|
||||||
|
[0, 0, 0],
|
||||||
],
|
],
|
||||||
6: [
|
[
|
||||||
[0, 0, 1],
|
[0, 0, 1],
|
||||||
[1, 1, 1],
|
[1, 1, 1],
|
||||||
|
[0, 0, 0],
|
||||||
],
|
],
|
||||||
};
|
],
|
||||||
|
[
|
||||||
|
[
|
||||||
|
[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],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[
|
||||||
|
[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],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[
|
||||||
|
[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],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
static colors: Object;
|
static colors: Object;
|
||||||
|
|
||||||
|
|
@ -50,8 +171,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;
|
||||||
|
|
@ -85,25 +206,15 @@ 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)
|
||||||
if (this.currentRotation === 0) {
|
this.currentRotation = 3;
|
||||||
this.currentShape = Tetromino.shapes[this.currentType];
|
this.currentShape = Tetromino.shapes[this.currentRotation][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) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue