Skip to content

Commit

Permalink
fix: collectables should be collectable
Browse files Browse the repository at this point in the history
  • Loading branch information
rengert committed Feb 3, 2024
1 parent 6d52abe commit e6ae549
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
5 changes: 5 additions & 0 deletions src/app/models/pixijs/power-up-sprite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
30 changes: 14 additions & 16 deletions src/app/services/game-collectable.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -14,21 +14,20 @@ interface Dictionary<T> {

@Injectable()
export class GameCollectableService extends UpdatableService {
private collectables: PowerUpSprite[] = [];
private readonly animations: Dictionary<Texture[]> = {};

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<void> {
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<Spritesheet>(config.assetUrl);
Expand Down Expand Up @@ -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 {
//
}
}

0 comments on commit e6ae549

Please sign in to comment.