Skip to content

Commit

Permalink
feat: meteor has power
Browse files Browse the repository at this point in the history
  • Loading branch information
rengert committed Jan 13, 2024
1 parent d111e38 commit e4a52d2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/app/models/pixijs/simple-game-sprite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export class GameSprite extends Sprite {
private readonly ySpeed: number;
private readonly xSpeed: number;

power: number | undefined;

constructor(speed: number, texture: Texture) {
super(texture);

Expand All @@ -19,6 +21,10 @@ export class GameSprite extends Sprite {
}

hit(object2: Sprite): boolean {
return hit(this, object2);
const event = hit(this, object2);
if (event && this.power !== undefined) {
this.power -= 1;
}
return event;
}
}
12 changes: 9 additions & 3 deletions src/app/services/game-meteor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ export class GameMeteorService extends BaseService {
update(delta: number, level: number): void {
this.elapsed += delta;

this.#meteors = this.#meteors.filter(meteor => !meteor.destroyed);
this.#meteors
.filter(meteor => meteor.y > this.application.screen.height + 50)
.forEach(meteor => meteor.destroy());
this.#meteors = this.#meteors.filter(enemy => !enemy.destroyed);
this.#meteors = this.#meteors.filter(meteor => !meteor.destroyed);

this.#meteors.forEach(enemy => enemy.update(delta));
this.#meteors.forEach(meteor => meteor.update(delta));

const check = Math.floor(this.elapsed);
if (((check % Math.floor(60 / (GAME_CONFIG.meteor.autoSpawnSpeed + (0.1 * (level - 1))))) === 0)
Expand All @@ -48,10 +49,14 @@ export class GameMeteorService extends BaseService {

hit(shots: AnimatedGameSprite[]): number {
for (const shot of shots.filter(s => !s.destroyed)) {
const hit = this.meteors.find(enemy => !enemy.destroyed && shot.hit(enemy));
const hit = this.meteors.find(meteor => !meteor.destroyed && meteor.hit(shot));
if (hit) {
this.explode(hit.x, hit.y);
shot.destroy();

if (hit.power === 0) {
hit.destroy();
}
}
}

Expand Down Expand Up @@ -82,6 +87,7 @@ export class GameMeteorService extends BaseService {
meteor.y = 10;
meteor.width += Math.random() * 20;
meteor.height += Math.random() * 20;
meteor.power = 10;
this.#meteors.push(meteor);
this.application.stage.addChild(meteor);
}
Expand Down

0 comments on commit e4a52d2

Please sign in to comment.