Compare commits

...

3 commits

Author SHA1 Message Date
dbe03a2a2d Fixed level 6 missing 2020-03-17 00:25:44 +01:00
7d718141e7 Added touch and hold controls 2020-03-17 00:24:57 +01:00
79e72784d1 Improved piece rotation 2020-03-16 23:36:01 +01:00
3 changed files with 203 additions and 69 deletions

View file

@ -23,11 +23,11 @@ export default class GameLogic {
'3': 500,
'4': 700,
'5': 1000,
'7': 1500,
'8': 2000,
'9': 3000,
'10': 4000,
'11': 5000,
'6': 1500,
'7': 2000,
'8': 3000,
'9': 4000,
'10': 5000,
};
currentGrid: Array<Array<Object>>;
@ -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,8 +195,11 @@ 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) {
@ -218,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) {
@ -252,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())

View file

@ -91,7 +91,6 @@ 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())
@ -260,18 +259,22 @@ class TetrisScreen extends React.Component<Props, State> {
<IconButton
icon="arrow-left"
size={40}
onPress={() => this.logic.leftPressed(this.updateGrid)}
onPress={() => this.logic.pressedOut()}
onPressIn={() => this.logic.leftPressedIn(this.updateGrid)}
/>
<IconButton
icon="arrow-right"
size={40}
onPress={() => this.logic.rightPressed(this.updateGrid)}
onPress={() => this.logic.pressedOut()}
onPressIn={() => this.logic.rightPressed(this.updateGrid)}
/>
</View>
<IconButton
icon="arrow-down"
size={40}
onPress={() => this.logic.downPressed(this.updateGridScore)}
onPressIn={() => this.logic.downPressedIn(this.updateGridScore)}
onPress={() => this.logic.pressedOut()}
style={{marginLeft: 'auto'}}
/>
</View>

View file

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