From e6ae54902ee3caca96be08ea4e6ff7fb139a2ef0 Mon Sep 17 00:00:00 2001 From: Thomas Renger Date: Sat, 3 Feb 2024 09:16:41 +0100 Subject: [PATCH] fix: collectables should be collectable --- src/app/models/pixijs/power-up-sprite.ts | 5 ++++ src/app/services/game-collectable.service.ts | 30 +++++++++----------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/app/models/pixijs/power-up-sprite.ts b/src/app/models/pixijs/power-up-sprite.ts index 1deac58..c75672f 100644 --- a/src/app/models/pixijs/power-up-sprite.ts +++ b/src/app/models/pixijs/power-up-sprite.ts @@ -16,5 +16,10 @@ export class PowerUpSprite extends AnimatedGameSprite { super(ObjectType.collectable, null, speed, textures); this.config = config; + this.energy = 1; + } + + override explode(): void { + this.destroying = true; } } diff --git a/src/app/services/game-collectable.service.ts b/src/app/services/game-collectable.service.ts index 4a07b91..cd7280f 100644 --- a/src/app/services/game-collectable.service.ts +++ b/src/app/services/game-collectable.service.ts @@ -3,8 +3,8 @@ import { Assets, Spritesheet, Texture } from 'pixi.js'; import { GAME_CONFIG } from '../game-constants'; import { ObjectType } from '../models/pixijs/object-type.enum'; import { PowerUpSprite } from '../models/pixijs/power-up-sprite'; +import { Ship } from '../models/pixijs/ship'; import { ApplicationService } from './application.service'; -import { GameShipService } from './game-ship.service'; import { ObjectModelType, ObjectService } from './object.service'; import { UpdatableService } from './updatable.service'; @@ -14,21 +14,20 @@ interface Dictionary { @Injectable() export class GameCollectableService extends UpdatableService { - private collectables: PowerUpSprite[] = []; private readonly animations: Dictionary = {}; constructor( private readonly application: ApplicationService, - private readonly shipService: GameShipService, + private readonly object: ObjectService, objectService: ObjectService, ) { super(); objectService.onDestroyed(ObjectType.enemy, (enemy, by) => this.spawn(enemy, by)); + objectService.onDestroyed(ObjectType.collectable, (powerUp, by) => this.collect(powerUp, by)); } async init(): Promise { - this.collectables.forEach(collectable => collectable.destroy()); if (Object.values(this.animations).length === 0) { for (const config of GAME_CONFIG.powerUpConfig) { const powerUp = await Assets.load(config.assetUrl); @@ -57,22 +56,21 @@ export class GameCollectableService extends UpdatableService { powerUp.y = y; powerUp.power = 0; this.application.stage.addChild(powerUp); - this.collectables.push(powerUp); + this.object.add(powerUp); } - update(): void { - const ship = this.shipService.instance; - if (ship.destroyed) { + collect(object: ObjectModelType, by: ObjectModelType): void { + if (by.type !== ObjectType.ship) { return; } + const ship = by as unknown as Ship; + const powerUp = object as unknown as PowerUpSprite; + ship.shotSpeed += powerUp.config.powerUp.speed; + ship.shotPower += powerUp.config.powerUp.shot; + ship.energy += powerUp.config.powerUp.energy; + } - const powerUp = this.collectables.find(collectable => !collectable.destroyed && ship.hit(collectable)); - if (powerUp) { - ship.shotSpeed += powerUp.config.powerUp.speed; - ship.shotPower += powerUp.config.powerUp.shot; - ship.energy += powerUp.config.powerUp.energy; - powerUp.destroy(); - this.collectables = this.collectables.filter(collectable => !collectable.destroyed); - } + update(): void { + // } }