brick-engine-cli 1.0.10 → 1.0.12

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/dist/index.js CHANGED
@@ -8,11 +8,14 @@ const commander_1 = require("commander");
8
8
  const init_1 = require("./commands/init");
9
9
  const publish_1 = require("./commands/publish");
10
10
  const chalk_1 = __importDefault(require("chalk"));
11
+ const fs_1 = require("fs");
12
+ const path_1 = __importDefault(require("path"));
11
13
  function run() {
14
+ const pkg = JSON.parse((0, fs_1.readFileSync)(path_1.default.join(__dirname, "../package.json"), "utf-8"));
12
15
  commander_1.program
13
16
  .name("brick-engine-cli")
14
17
  .description("CLI to scaffold Brick Engine projects")
15
- .version("1.0.8");
18
+ .version(pkg.version);
16
19
  commander_1.program
17
20
  .command("init <name>")
18
21
  .description("Initialize a new Brick Engine project")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brick-engine-cli",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "CLI to scaffold Brick Game projects",
5
5
  "main": "dist/index.js",
6
6
  "bin": {
package/src/index.ts CHANGED
@@ -3,12 +3,17 @@ import { initCommand } from "./commands/init";
3
3
  import { publishCommand } from "./commands/publish";
4
4
  import chalk from "chalk";
5
5
  import { readFileSync } from "fs";
6
+ import path from "path";
6
7
 
7
8
  export function run() {
9
+ const pkg = JSON.parse(
10
+ readFileSync(path.join(__dirname, "../package.json"), "utf-8"),
11
+ );
12
+
8
13
  program
9
14
  .name("brick-engine-cli")
10
15
  .description("CLI to scaffold Brick Engine projects")
11
- .version("1.0.8");
16
+ .version(pkg.version);
12
17
 
13
18
  program
14
19
  .command("init <name>")
@@ -1,16 +1,29 @@
1
- import { Game, FontSize, ControlKey, ControlEventType, Sound, FontAlign, FontVerticalAlign, Color, Coordinate, StateProperty } from 'brick-engine-js';
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 playerX = 5;
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 { playerX, enemies } = JSON.parse(data);
23
- this.playerX = playerX;
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
- playerX: this.playerX,
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.movePlayer(-1);
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.movePlayer(1);
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 playerY = grid.bottomRow;
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.stampCell({
89
- coordinate: { x: this.playerX, y: playerY },
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
- this.enemies.forEach(enemy => {
96
- grid.stampCell({
97
- coordinate: enemy,
98
- color: Color.RED,
99
- value: 2,
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
- time.setTickInterval(this.initTickInterval - 15 * score.level);
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
  }