You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[Phase 2 - Firebase high score, service worker, more sounds effect, more animation](#phase-2---firebase-high-score-service-worker-more-sounds-effect-more-animation)
27
+
-[Time spending](#time-spending)
28
+
-[Setting up development environment 🛠](#setting-up-development-environment-)
29
+
-[Author: Trung Vo ✍️](#author-trung-vo-️)
30
+
-[Credits and references](#credits-and-references)
31
+
-[Contributing](#contributing)
32
+
-[License](#license)
33
+
</p>
34
+
</details>
35
+
5
36
## Working Game
6
37
7
38
Check out the **working game** -> https://tetris.trungk18.com
@@ -97,7 +128,7 @@ And let me emphasize it again, I didn't write the brain of the game from scratch
97
128
Although you don't dispatch any action, Akita will still do it undo the hood as the Update action. And you still can see the data with [Redux DevTools][redux-devtool]. Remember to put that option into your `AppModule`
I turn it on all the time on [tetris.trungk18.com][angular-tetris], you can open the DevTools and start seeing the data flow.
@@ -106,26 +137,133 @@ I turn it on all the time on [tetris.trungk18.com][angular-tetris], you can open
106
137
107
138
> Note: opening the DevTools could reduce the performance of the game significantly. I recommended you turn it off when you want to archive a high score 🤓
108
139
109
-
### Web Audio API
140
+
### Customizing Piece
110
141
111
-
There are many sound effects in the game such as when you press space, or left, right. In reality, all of the sounds were a reference to a single file [assets/tetris-sound.mp3][sounds].
142
+
I defined a base [Piece class][piece-class] for a piece. And for each type of piece, it will extend from the same base class to inherit the same capability
143
+
[piece-class]: src/app/interface/piece/piece.ts
112
144
113
-
I don't have much experience working with audio before but the Web Audio API looks very promising. You could do more with it.
145
+
```ts
146
+
exportclassPiece {
147
+
x:number;
148
+
y:number;
149
+
rotation =PieceRotation.Deg0;
150
+
type:PieceTypes;
151
+
shape:Shape;
152
+
next:Shape;
153
+
154
+
private _shapes:Shapes;
155
+
private _lastConfig:Partial<Piece>;
156
+
157
+
constructor(x:number, y:number) {
158
+
this.x=x;
159
+
this.y=y;
160
+
}
114
161
115
-
- See the [official documentation][webaudio]
116
-
- See how I load the mp3 file and store it in [sound-manager.service.ts][sound-manager]
162
+
store():Piece {
163
+
this._lastConfig= {
164
+
x: this.x,
165
+
y: this.y,
166
+
rotation: this.rotation,
167
+
shape: this.shape
168
+
};
169
+
returnthis._newPiece();
170
+
}
117
171
118
-
### Animation
172
+
//code removed for brevity
173
+
}
174
+
```
119
175
120
-
I rewrote the animation with RxJS. See the comparison below for the simple dinosaurs running animation at the beginning of the game.
176
+
For example, I have a piece L. I create a new class name [PieceL][piecel]. I will contain the shape of L in four different rotation so that I don't have to mess up with the math to do minus plus on the XY axis. And I think defining in that way makes the code self-express better. If you see 1, it means on the matrix it will be filled, 0 mean empty tile.
121
177
122
-
You could do a lot of stuff if you know RxJS well enough :) I think I need to strengthen my RxJS knowledge soon enough as well. Super powerful.
178
+
If my team member needs to maintain the code, I hope he will understand what I was trying to write immediately. Or maybe not 🤣
123
179
124
-
![Angular Tetris][compare02]
180
+
One import property of the Piece is the `next` property to display the piece shape on the decoration box for the upcoming piece.
125
181
126
-
The actual result doesn't look very identical but it is good enough in my standard.
182
+
[piecel]: src/app/interface/piece/L.ts
127
183
128
-
![Angular Tetris][compare02-result]
184
+
```ts
185
+
const ShapesL:Shapes= [];
186
+
ShapesL[PieceRotation.Deg0] = [
187
+
[0, 0, 0, 0],
188
+
[1, 0, 0, 0],
189
+
[1, 0, 0, 0],
190
+
[1, 1, 0, 0]
191
+
];
192
+
193
+
ShapesL[PieceRotation.Deg90] = [
194
+
[0, 0, 0, 0],
195
+
[0, 0, 0, 0],
196
+
[1, 1, 1, 0],
197
+
[1, 0, 0, 0]
198
+
];
199
+
//code removed for brevity
200
+
201
+
];
202
+
203
+
exportclassPieceLextendsPiece {
204
+
constructor(x:number, y:number) {
205
+
super(x, y);
206
+
this.type=PieceTypes.L;
207
+
this.next= [
208
+
[0, 0, 1, 0],
209
+
[1, 1, 1, 0]
210
+
];
211
+
this.setShapes(ShapesL);
212
+
}
213
+
}
214
+
```
215
+
216
+
Now is the interesting part, you create a custom piece by yourself. Simply create a new class that extends from `Piece` with different rotations.
217
+
218
+
For instance, I will define a new piece call F with class name [`PieceF`][piecef]. That is how it should look like.
@@ -139,6 +277,15 @@ The actual result doesn't look very identical but it is good enough in my standa
139
277
140
278
![Angular Tetris][compare02-result]
141
279
280
+
### Web Audio API
281
+
282
+
There are many sound effects in the game such as when you press space, or left, right. In reality, all of the sounds were a reference to a single file [assets/tetris-sound.mp3][sounds].
283
+
284
+
I don't have much experience working with audio before but the Web Audio API looks very promising. You could do more with it.
285
+
286
+
- See the [official documentation][webaudio]
287
+
- See how I load the mp3 file and store it in [sound-manager.service.ts][sound-manager]
288
+
142
289
### Keyboard handling
143
290
144
291
I planned to use [@ngneat/hotkeys][hotkeys] but I decided to use `@HostListener` instead. A simple implementation could look like:
@@ -172,10 +319,11 @@ See more at [containers/angular-tetris/angular-tetris.component.ts][hotkeys-impl
172
319
-[x] Local storage high score
173
320
-[x] Sounds effects
174
321
175
-
### Phase 2 - Firebase high score, more sounds effect, more animation
322
+
### Phase 2 - Firebase high score, service worker, more sounds effect, more animation
176
323
177
324
> TBD
178
325
326
+
-[ ] Offline mode (play without internet connection)
0 commit comments