brick-engine-cli 1.0.10 → 1.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/templates/src/index.ts +46 -49
package/package.json
CHANGED
package/templates/src/index.ts
CHANGED
|
@@ -1,16 +1,29 @@
|
|
|
1
|
-
import { Game, FontSize, ControlKey, ControlEventType, Sound, FontAlign, FontVerticalAlign, Color, Coordinate,
|
|
1
|
+
import { Game, FontSize, ControlKey, ControlEventType, Sound, FontAlign, FontVerticalAlign, Color, Coordinate, Cell } from 'brick-engine-js';
|
|
2
2
|
|
|
3
3
|
export default class MyGame extends Game {
|
|
4
|
-
private
|
|
4
|
+
private initialPlayerCell: Cell = {
|
|
5
|
+
value: 1,
|
|
6
|
+
color: Color.CYAN,
|
|
7
|
+
coordinate: {
|
|
8
|
+
x: 5,
|
|
9
|
+
y: 17,
|
|
10
|
+
},
|
|
11
|
+
};
|
|
12
|
+
private initialTickInterval = 200;
|
|
13
|
+
|
|
14
|
+
private player: Cell = this.initialPlayerCell;
|
|
5
15
|
private enemies: Coordinate[] = [];
|
|
6
16
|
private spawnRate = 5; // Ticks between spawns
|
|
7
|
-
private initTickInterval = 200;
|
|
8
17
|
|
|
9
18
|
/**
|
|
10
19
|
* Called once after the engine and its modules are fully initialized.
|
|
11
20
|
*/
|
|
12
21
|
setupGame(): void {
|
|
13
|
-
const { control, session } = this.modules;
|
|
22
|
+
const { control, session, grid, score, time } = this.modules;
|
|
23
|
+
|
|
24
|
+
// Reset game local state when starting/restarting.
|
|
25
|
+
this.player = this.initialPlayerCell;
|
|
26
|
+
time.setTickInterval(this.initialTickInterval);
|
|
14
27
|
|
|
15
28
|
// Register how game data should be serialized and deserialized for the session
|
|
16
29
|
session.register({
|
|
@@ -19,43 +32,36 @@ export default class MyGame extends Game {
|
|
|
19
32
|
// Recieves from LocalStorage and restore the game state
|
|
20
33
|
deserialize: (data: string) => {
|
|
21
34
|
if (!data) return;
|
|
22
|
-
const {
|
|
23
|
-
this.
|
|
35
|
+
const { player, enemies } = JSON.parse(data);
|
|
36
|
+
this.player = player;
|
|
24
37
|
this.enemies = enemies;
|
|
25
38
|
},
|
|
26
39
|
// Prepare data to be saved to LocalStorage
|
|
27
40
|
serialize: () => {
|
|
28
41
|
return JSON.stringify({
|
|
29
|
-
|
|
42
|
+
player: this.player,
|
|
30
43
|
enemies: this.enemies,
|
|
31
44
|
});
|
|
32
45
|
},
|
|
33
46
|
});
|
|
34
47
|
|
|
35
|
-
this.onStart();
|
|
36
|
-
|
|
37
48
|
// Move Left
|
|
38
49
|
control.subscribeForPlayingScreen(ControlKey.LEFT, ControlEventType.PRESSED, () => {
|
|
39
|
-
this.
|
|
50
|
+
const newCell = grid.moveCellLeft(this.player);
|
|
51
|
+
if (newCell) {
|
|
52
|
+
this.player = newCell;
|
|
53
|
+
}
|
|
40
54
|
});
|
|
41
55
|
|
|
42
56
|
// Move Right
|
|
43
57
|
control.subscribeForPlayingScreen(ControlKey.RIGHT, ControlEventType.PRESSED, () => {
|
|
44
|
-
this.
|
|
58
|
+
const newCell = grid.moveCellRight(this.player);
|
|
59
|
+
if (newCell) {
|
|
60
|
+
this.player = newCell;
|
|
61
|
+
}
|
|
45
62
|
});
|
|
46
63
|
}
|
|
47
64
|
|
|
48
|
-
private movePlayer(dx: number): void {
|
|
49
|
-
const { grid, sound } = this.modules;
|
|
50
|
-
|
|
51
|
-
const newX = this.playerX + dx;
|
|
52
|
-
|
|
53
|
-
if (grid.isCoordinateValid({ x: newX, y: grid.bottomRow })) {
|
|
54
|
-
this.playerX = newX;
|
|
55
|
-
sound.play(Sound.KEY_PRESS);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
65
|
/**
|
|
60
66
|
* Logic update on every game "tick".
|
|
61
67
|
*/
|
|
@@ -72,8 +78,7 @@ export default class MyGame extends Game {
|
|
|
72
78
|
}
|
|
73
79
|
|
|
74
80
|
// 3. Check collisions
|
|
75
|
-
const
|
|
76
|
-
const collision = this.enemies.find(enemy => enemy.x === this.playerX && enemy.y === playerY);
|
|
81
|
+
const collision = this.enemies.find(enemy => enemy.x === this.player.coordinate.x && enemy.y === this.player.coordinate.y);
|
|
77
82
|
|
|
78
83
|
if (collision) {
|
|
79
84
|
sound.play(Sound.EXPLOSION);
|
|
@@ -85,30 +90,33 @@ export default class MyGame extends Game {
|
|
|
85
90
|
grid.resetGrid();
|
|
86
91
|
|
|
87
92
|
// Draw Player
|
|
88
|
-
grid
|
|
89
|
-
|
|
90
|
-
color: Color.CYAN,
|
|
91
|
-
value: 1,
|
|
92
|
-
});
|
|
93
|
+
// Cell is a class that represents a single cell in the grid
|
|
94
|
+
grid.stampCell(this.player);
|
|
93
95
|
|
|
94
96
|
// Draw Enemies
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
97
|
+
// Piece is a class that represents a collection of cells in the grid
|
|
98
|
+
grid.stampPiece(
|
|
99
|
+
this.enemies.map(coordinate => {
|
|
100
|
+
return {
|
|
101
|
+
coordinate,
|
|
102
|
+
color: Color.RED,
|
|
103
|
+
value: 2,
|
|
104
|
+
};
|
|
105
|
+
}),
|
|
106
|
+
);
|
|
102
107
|
|
|
103
108
|
// 5. Increase score and difficulty
|
|
104
109
|
score.increaseScore(1);
|
|
105
110
|
|
|
106
|
-
// Increase speed every 50 points
|
|
111
|
+
// Increase speed every 50 points, limit to 10 levels
|
|
107
112
|
if (score.score > 0 && score.score % 50 === 0 && score.level < score.maxLevel) {
|
|
108
|
-
|
|
113
|
+
// Set tick interval to 200ms minus 15ms per level
|
|
114
|
+
time.setTickInterval(this.initialTickInterval - 15 * score.level);
|
|
109
115
|
|
|
116
|
+
// Increase level
|
|
110
117
|
score.increaseLevel(1);
|
|
111
118
|
|
|
119
|
+
// Play sound based on level
|
|
112
120
|
if (score.level < 6) {
|
|
113
121
|
sound.play(Sound.SCORE_2);
|
|
114
122
|
} else {
|
|
@@ -155,15 +163,4 @@ export default class MyGame extends Game {
|
|
|
155
163
|
text.setTextSize(FontSize.MEDIUM);
|
|
156
164
|
text.textOnDisplay('PRESS START', { x: 0.5, y: 0.8 });
|
|
157
165
|
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Reset game local state when starting/restarting.
|
|
161
|
-
*/
|
|
162
|
-
onStart(): void {
|
|
163
|
-
const { score, time } = this.modules;
|
|
164
|
-
|
|
165
|
-
this.playerX = 5;
|
|
166
|
-
this.enemies = [];
|
|
167
|
-
time.setTickInterval(this.initTickInterval - 15 * score.level);
|
|
168
|
-
}
|
|
169
166
|
}
|