Skip to content

Commit d59eee2

Browse files
committed
Clear screen animation
1 parent 0513dfc commit d59eee2

File tree

6 files changed

+60
-5
lines changed

6 files changed

+60
-5
lines changed

src/app/components/matrix/matrix.component.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { Component, OnInit } from '@angular/core';
2+
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
3+
import { GameState } from '@trungk18/interface/game-state';
24
import { Tile } from '@trungk18/interface/tile/tile';
5+
import { MatrixUtil } from '@trungk18/interface/utils/matrix';
36
import { TetrisQuery } from '@trungk18/state/tetris/tetris.query';
4-
import { Observable } from 'rxjs';
7+
import { combineLatest, Observable, of, timer } from 'rxjs';
8+
import { delay, map, startWith, switchMap, takeWhile } from 'rxjs/operators';
9+
@UntilDestroy()
510
@Component({
611
selector: 't-matrix',
712
templateUrl: './matrix.component.html',
@@ -12,6 +17,41 @@ export class MatrixComponent implements OnInit {
1217
constructor(private _tetrisQuery: TetrisQuery) {}
1318

1419
ngOnInit(): void {
15-
this.matrix$ = this._tetrisQuery.matrix$;
20+
this.matrix$ = this.getMatrix();
21+
}
22+
23+
getMatrix(): Observable<Tile[]> {
24+
return combineLatest([this._tetrisQuery.gameState$, this._tetrisQuery.matrix$]).pipe(
25+
untilDestroyed(this),
26+
switchMap(([gameState, matrix]) => {
27+
if (gameState !== GameState.Over && gameState !== GameState.Loading) {
28+
return of(matrix);
29+
}
30+
let newMatrix = [...matrix];
31+
let rowsLength = MatrixUtil.Height * 2;
32+
let animatedMatrix$: Observable<Tile[]> = timer(0, rowsLength).pipe(
33+
startWith(0),
34+
map((x) => x + 1),
35+
takeWhile((x) => x <= rowsLength+1),
36+
switchMap((idx) => {
37+
let gridIndex = idx - 1;
38+
if (gridIndex < MatrixUtil.Height) {
39+
newMatrix.splice(
40+
gridIndex * MatrixUtil.Width,
41+
MatrixUtil.Width,
42+
...MatrixUtil.FullRow
43+
);
44+
}
45+
if (gridIndex > MatrixUtil.Height&& gridIndex <= rowsLength) {
46+
let startIdx = (MatrixUtil.Height - (gridIndex - MatrixUtil.Height)) * MatrixUtil.Width;
47+
newMatrix.splice(startIdx, MatrixUtil.Width, ...MatrixUtil.EmptyRow);
48+
}
49+
50+
return of(newMatrix)
51+
})
52+
);
53+
return animatedMatrix$;
54+
})
55+
);
1656
}
1757
}

src/app/containers/angular-tetris/angular-tetris.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<div class="screen">
55
<div class="panel">
66
<t-matrix></t-matrix>
7-
<t-logo></t-logo>
7+
<t-logo *ngIf="isShowLogo$ | async"></t-logo>
88
<div class="state">
99
<t-point></t-point>
1010
<t-start-line></t-start-line>

src/app/containers/angular-tetris/angular-tetris.component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const KeyDown = 'document:keydown';
1515
})
1616
export class AngularTetrisComponent implements OnInit {
1717
drop$: Observable<boolean>;
18+
isShowLogo$: Observable<boolean>;
1819

1920
get hasCurrent() {
2021
return !!this._tetrisQuery.current;
@@ -30,6 +31,7 @@ export class AngularTetrisComponent implements OnInit {
3031

3132
ngOnInit(): void {
3233
this.drop$ = this._keyboardQuery.drop$;
34+
this.isShowLogo$ = this._tetrisQuery.isShowLogo$;
3335
}
3436

3537
keyboardMouseDown(key: string) {

src/app/interface/utils/matrix.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ export class MatrixUtil {
4747
return new Array(this.Width).fill(new EmptyTile());
4848
}
4949

50+
static get FullRow(): Tile[] {
51+
return new Array(this.Width).fill(new FilledTile());
52+
}
53+
5054
static Points = [100, 300, 700, 1500];
5155
static MaxPoint = 999999;
5256
static SpeedDelay = [800, 650, 500, 370, 250, 160];

src/app/state/tetris/tetris.query.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { Injectable } from '@angular/core';
22
import { Query } from '@datorama/akita';
33
import { TetrisStore, TetrisState } from './tetris.store';
44
import { GameState } from '@trungk18/interface/game-state';
5-
import { map } from 'rxjs/operators';
5+
import { map, delay } from 'rxjs/operators';
6+
import { combineLatest } from 'rxjs';
67

78
@Injectable({ providedIn: 'root' })
89
export class TetrisQuery extends Query<TetrisState> {
@@ -51,4 +52,11 @@ export class TetrisQuery extends Query<TetrisState> {
5152
clearedLines$ = this.select('clearedLines');
5253
initLine$ = this.select('initLine');
5354
speed$ = this.select('speed');
55+
isShowLogo$ = combineLatest(this.gameState$, this.select('current')).pipe(
56+
map(([state, current]) => {
57+
let isLoadingOrOver = state === GameState.Loading || state === GameState.Over;
58+
return isLoadingOrOver && !current;
59+
}),
60+
delay(1700)
61+
);
5462
}

src/app/state/tetris/tetris.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ export class TetrisService {
223223
private _onGameOver() {
224224
this.pause();
225225
this._store.update({
226-
gameState: GameState.Over
226+
gameState: GameState.Over,
227+
current: null
227228
});
228229
}
229230

0 commit comments

Comments
 (0)