Skip to content

Commit

Permalink
Merge bb289b2 into 086ca8b
Browse files Browse the repository at this point in the history
  • Loading branch information
robertrypula committed Nov 25, 2018
2 parents 086ca8b + bb289b2 commit acf8cbe
Show file tree
Hide file tree
Showing 18 changed files with 443 additions and 133 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ was inspired by Redux and was written from scratch in TypeScript.

**NOTE:** This project is still not finished. More details in the TODO section below.

[![Api client browser](https://cdn.rypula.pl/simple-tetris/api-client-browser.gif)](https://cdn.rypula.pl/simple-tetris/api-client-browser.gif)

[Play online](https://cdn.rypula.pl/simple-tetris/v1.2.0-rc/api-client-browser.html) - use WSAD keys on Desktop or on-screen buttons on Mobile devices.

[![Api client browser](https://cdn.rypula.pl/simple-tetris/api-client-browser.gif)](https://cdn.rypula.pl/simple-tetris/api-client-browser.gif)

Simplest code example:

```html
Expand Down Expand Up @@ -131,14 +131,22 @@ that simplifies basic input and output of the text based games.
- when tetrimino will hit the 'ground' trigger new tetrimino (probably in **1.5.x**)
- detect and remove completely filled lines (probably in **1.6.x**)
- detect game over / pause game (probably in **1.7.x**)
- (?) more like scores, next piece etc.
- ...
- add separate Enum for Keys and remove KEY_CODE_* constants (breaking change, probably in **2.0.0**)
- (?) remove `terminal-game-io` from typescript definitions of _src/lib/ascii-runner.ts_ file and move this lib back to devDeps (breaking change, probably in **2.0.0**)
- updates in interfaces like TetriminoRotation, TetriminoType instead of index, etc (breaking change, probably in **2.0.0**)
- add separate Enum for Keys and remove KEY_CODE_* constants (breaking change, probably in **2.0.0**)
- soft drop (probably in **2.x.x**)
- remove completely filled line 'blink' effect (probably in **2.x.x**)
- block colors (probably in **2.x.x**)
- score (probably in **2.x.x**)
- next piece preview (probably in **2.x.x**)
- ...

### 1.2.0 - ?? November 2018
- [todo] collision detection both with borders and other blocks
- updates in internal 'Simple Redux' implementation
- tetriminos now rotates clockwise
- fields in IAsciiRunnerOptions are now optional
- bugfix related to keyName parameter usage in ascii-runner.ts
- bugfix related to terminal-game-io dependency issue

Expand Down
4 changes: 2 additions & 2 deletions src/lib/ascii-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { render } from './ascii-renderer';
import * as fromGame from './game';

export interface IAsciiRunnerOptions {
createStoreOptions: fromGame.ICreateStoreOptions;
domElementId: string;
createStoreOptions?: fromGame.ICreateStoreOptions;
domElementId?: string;
}

export class AsciiRunner {
Expand Down
56 changes: 56 additions & 0 deletions src/lib/game/actions/game-loop.actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2018 Robert Rypuła - https://github.com/robertrypula

import { isNotCollidingSelector } from '../selectors/game.selectors';
import { IThunkAction } from '../simple-redux';
import { Store } from '../store';
import * as fromTetriminoActions from './tetrimino.actions';

export const KEY_CODE_HARD_DROP_ACTION = 'KEY_CODE_HARD_DROP_ACTION';
export const KEY_CODE_LEFT_ACTION = 'KEY_CODE_LEFT_ACTION';
export const KEY_CODE_RIGHT_ACTION = 'KEY_CODE_RIGHT_ACTION';
export const KEY_CODE_ROTATE_ACTION = 'KEY_CODE_ROTATE_ACTION';

/*tslint:disable:max-classes-per-file*/

export class KeyCodeHardDropAction implements IThunkAction<Store> {
public readonly type = KEY_CODE_HARD_DROP_ACTION;

public executeThunk(store: Store): void {
let offsetY = 0;

// TODO find 'the bootom' in loop via isNotCollidingSelector(0, ++offsetY)
if (isNotCollidingSelector(0, ++offsetY)(store.getState())) {
store.dispatch(new fromTetriminoActions.MoveDownAction({ offsetY }));
}
}
}

export class KeyCodeLeftAction implements IThunkAction<Store> {
public readonly type = KEY_CODE_LEFT_ACTION;

public executeThunk(store: Store): void {
if (isNotCollidingSelector(-1)(store.getState())) {
store.dispatch(new fromTetriminoActions.MoveLeftAction());
}
}
}

export class KeyCodeRightAction implements IThunkAction<Store> {
public readonly type = KEY_CODE_RIGHT_ACTION;

public executeThunk(store: Store): void {
if (isNotCollidingSelector(1)(store.getState())) {
store.dispatch(new fromTetriminoActions.MoveRightAction());
}
}
}

export class KeyCodeRotateAction implements IThunkAction<Store> {
public readonly type = KEY_CODE_ROTATE_ACTION;

public executeThunk(store: Store): void {
if (isNotCollidingSelector(0, 0, 1)(store.getState())) {
store.dispatch(new fromTetriminoActions.RotateAction());
}
}
}
8 changes: 4 additions & 4 deletions src/lib/game/actions/game.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { TETRIMINO_LIST } from '../constants';
import { IThunkAction } from '../simple-redux';
import { Store } from '../store';
import { getRandomInt } from '../utilities';
import { InitializeMatrixAction } from './matrix.actions';
import { InitNewAction } from './tetrimino.actions';
import * as fromMatrixActions from './matrix.actions';
import * as fromTetriminoActions from './tetrimino.actions';

export const INITIALIZE_GAME_ACTION = 'INITIALIZE_GAME_ACTION';

Expand All @@ -24,12 +24,12 @@ export class InitializeGameAction implements IThunkAction<Store> {
public executeThunk(store: Store): void {
const { matrixSizeX, matrixSizeY } = this.payload;

store.dispatch(new InitializeMatrixAction({
store.dispatch(new fromMatrixActions.InitializeMatrixAction({
sizeX: matrixSizeX,
sizeY: matrixSizeY
}));

store.dispatch(new InitNewAction({
store.dispatch(new fromTetriminoActions.InitNewAction({
index: getRandomInt(0, TETRIMINO_LIST.length - 1),
matrixSizeX
}));
Expand Down
18 changes: 9 additions & 9 deletions src/lib/game/actions/tetrimino.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@

import { IAction } from '../simple-redux';

export const HARD_DROP = 'HARD_DROP';
export const INIT_NEW = 'INIT_NEW';
export const MOVE_DOWN = 'MOVE_DOWN';
export const MOVE_LEFT = 'MOVE_LEFT';
export const MOVE_RIGHT = 'MOVE_RIGHT';
export const ROTATE = 'ROTATE';

/*tslint:disable:max-classes-per-file*/

export class HardDropAction implements IAction {
public readonly type = HARD_DROP;
export class InitNewAction implements IAction {
public readonly type = INIT_NEW;

public constructor(
public payload: {
matrixSizeY: number
index: number,
matrixSizeX: number
}
) { }
}

export class InitNewAction implements IAction {
public readonly type = INIT_NEW;
export class MoveDownAction implements IAction {
public readonly type = MOVE_DOWN;

public constructor(
public payload: {
index: number,
matrixSizeX: number
offsetY: number
}
) { }
}
Expand All @@ -44,8 +44,8 @@ export class RotateAction implements IAction {
}

export type TetriminoActionsUnion =
HardDropAction |
InitNewAction |
MoveDownAction |
MoveLeftAction |
MoveRightAction |
RotateAction;
83 changes: 42 additions & 41 deletions src/lib/game/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TetriminoList } from './models/tetrimino.model';
export const DEFAULT_MATRIX_SIZE_X = 10;
export const DEFAULT_MATRIX_SIZE_Y = 20;

export const KEY_CODE_HARD_DROP = 0;
export const KEY_CODE_HARD_DROP = 0; // TODO move all KeyCodes to 'Key' Enum (v2.0.0)
export const KEY_CODE_LEFT = 1;
export const KEY_CODE_RIGHT = 2;
export const KEY_CODE_ROTATE = 3;
Expand All @@ -14,49 +14,50 @@ export const TETRIMINO_ROTATIONS = 4;
export const TETRIMINO_SIZE_X = 4;
export const TETRIMINO_SIZE_Y = 4;

const _ = false;
const X = true;
export const _ = false;
export const f = false;
export const X = true;

export const TETRIMINO_LIST: TetriminoList = [
[
// piece I
// piece I - index 0
[
_, _, _, _, // rotation 0
X, X, X, X,
_, _, _, _,
_, _, _, _
],
[
_, X, _, _, // rotation 1
_, X, _, _,
_, X, _, _,
_, X, _, _
_, _, X, _, // rotation 1
_, _, X, _,
_, _, X, _,
_, _, X, _
],
[
_, _, _, _, // rotation 2
X, X, X, X,
_, _, _, _,
X, X, X, X,
_, _, _, _
],
[
_, _, X, _, // rotation 3
_, _, X, _,
_, _, X, _,
_, _, X, _
_, X, _, _, // rotation 3
_, X, _, _,
_, X, _, _,
_, X, _, _
]
],
[
// piece J
// piece J - index 1
[
_, _, _, _, // rotation 0
X, X, X, _,
_, _, X, _,
_, _, _, _
],
[
_, X, X, _, // rotation 1
_, X, _, _,
_, X, _, _, // rotation 1
_, X, _, _,
X, X, _, _,
_, _, _, _
],
[
Expand All @@ -66,24 +67,24 @@ export const TETRIMINO_LIST: TetriminoList = [
_, _, _, _
],
[
_, X, _, _, // rotation 3
_, X, X, _, // rotation 3
_, X, _, _,
_, X, _, _,
X, X, _, _,
_, _, _, _
]
],
[
// piece L
// piece L - index 2
[
_, _, _, _, // rotation 0
X, X, X, _,
X, _, _, _,
_, _, _, _
],
[
_, X, _, _, // rotation 1
X, X, _, _, // rotation 1
_, X, _, _,
_, X, _, _,
_, X, X, _,
_, _, _, _
],
[
Expand All @@ -93,14 +94,14 @@ export const TETRIMINO_LIST: TetriminoList = [
_, _, _, _
],
[
X, X, _, _, // rotation 3
_, X, _, _,
_, X, _, _, // rotation 3
_, X, _, _,
_, X, X, _,
_, _, _, _
]
],
[
// piece O
// piece O - index 3
[
_, _, _, _, // rotation 0
_, X, X, _,
Expand All @@ -127,17 +128,17 @@ export const TETRIMINO_LIST: TetriminoList = [
]
],
[
// piece S
// piece S - index 4
[
_, _, _, _, // rotation 0
_, X, X, _,
X, X, _, _,
_, _, _, _
],
[
_, X, _, _, // rotation 1
_, X, X, _,
_, _, X, _,
X, _, _, _, // rotation 1
X, X, _, _,
_, X, _, _,
_, _, _, _
],
[
Expand All @@ -147,14 +148,14 @@ export const TETRIMINO_LIST: TetriminoList = [
_, _, _, _
],
[
X, _, _, _, // rotation 3
X, X, _, _,
_, X, _, _,
_, X, _, _, // rotation 3
_, X, X, _,
_, _, X, _,
_, _, _, _
]
],
[
// piece T
// piece T - index 5
[
_, _, _, _, // rotation 0
X, X, X, _,
Expand All @@ -163,7 +164,7 @@ export const TETRIMINO_LIST: TetriminoList = [
],
[
_, X, _, _, // rotation 1
_, X, X, _,
X, X, _, _,
_, X, _, _,
_, _, _, _
],
Expand All @@ -175,23 +176,23 @@ export const TETRIMINO_LIST: TetriminoList = [
],
[
_, X, _, _, // rotation 3
X, X, _, _,
_, X, X, _,
_, X, _, _,
_, _, _, _
]
],
[
// piece Z
// piece Z - index 6
[
_, _, _, _, // rotation 0
X, X, _, _,
_, X, X, _,
_, _, _, _
],
[
_, _, X, _, // rotation 1
_, X, X, _,
_, X, _, _,
_, X, _, _, // rotation 1
X, X, _, _,
X, _, _, _,
_, _, _, _
],
[
Expand All @@ -201,9 +202,9 @@ export const TETRIMINO_LIST: TetriminoList = [
_, _, _, _
],
[
_, X, _, _, // rotation 3
X, X, _, _,
X, _, _, _,
_, _, X, _, // rotation 3
_, X, X, _,
_, X, _, _,
_, _, _, _
]
]
Expand Down

0 comments on commit acf8cbe

Please sign in to comment.