Skip to content

Commit

Permalink
refactor: move the stuff to object service
Browse files Browse the repository at this point in the history
  • Loading branch information
rengert committed Feb 2, 2024
1 parent 180d4b5 commit d38a1c6
Show file tree
Hide file tree
Showing 20 changed files with 282 additions and 181 deletions.
2 changes: 2 additions & 0 deletions src/app/components/pixijs/pixijs.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { GameScreenService } from '../../services/game-screen.service';
import { GameShipService } from '../../services/game-ship.service';
import { GameShotService } from '../../services/game-shot.service';
import { GameService } from '../../services/game.service';
import { ObjectService } from '../../services/object.service';

@Component({
selector: 'app-pixijs',
Expand All @@ -25,6 +26,7 @@ import { GameService } from '../../services/game.service';
GameShipService,
ExplosionService,
GameService,
ObjectService,
],
})
export class PixijsComponent implements OnInit {
Expand Down
3 changes: 3 additions & 0 deletions src/app/game-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface GameConfig extends Config {
ships: Record<ShipType, {
shotSpeed: number;
rocketSpeed: number;
energy: number;
}>,
}

Expand All @@ -30,10 +31,12 @@ export const GAME_CONFIG: GameConfig = {
[ShipType.ship]: {
rocketSpeed: 6,
shotSpeed: 1,
energy: 10,
},
[ShipType.enemy]: {
rocketSpeed: 4,
shotSpeed: 0.1,
energy: 1,
},
},
powerUpConfig: [
Expand Down
31 changes: 29 additions & 2 deletions src/app/models/pixijs/animated-game-sprite.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { AnimatedSprite, FrameObject, Sprite, Texture } from 'pixi.js';
import { AnimatedSprite, FrameObject, Texture } from 'pixi.js';
import { ExplosionService } from '../../services/explosion.service';
import { ObjectModelType } from '../../services/object.service';
import { hit } from '../../utils/sprite.util';
import { ObjectType } from './object-type.enum';

export class AnimatedGameSprite extends AnimatedSprite {
protected readonly speed: number = 1;

protected _energy: number | undefined;
reference: ObjectModelType | undefined;
destroying = false;
targetX?: number;

// eslint-disable-next-line max-params
constructor(
readonly type: ObjectType,
private readonly explosion: ExplosionService | null,
speed: number,
textures: Texture[] | FrameObject[],
autoUpdate?: boolean) {
Expand All @@ -15,7 +24,24 @@ export class AnimatedGameSprite extends AnimatedSprite {
this.speed = speed;
}

get energy(): number | undefined {
return this._energy;
}

set energy(value: number) {
this._energy = value;
}

explode(): void {
void this.explosion?.explode(this.x, this.y);
this.destroying = true;
}

override update(delta: number): void {
if (this.destroying && !this.destroyed) {
this.destroy();
return;
}
super.update(delta);

this.y += delta * this.speed;
Expand All @@ -26,10 +52,11 @@ export class AnimatedGameSprite extends AnimatedSprite {
}
}

hit(object2: Sprite): boolean {
hit(object2: ObjectModelType): boolean {
return hit(this, object2);
}


private getSpeed(targetX: number, x: number): number {
const distance = Math.abs(targetX - x);
const speed = distance / 2;
Expand Down
7 changes: 7 additions & 0 deletions src/app/models/pixijs/object-type.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export enum ObjectType {
ship = 'ship',
enemy = 'enemy',
meteor = 'meteor',
rocket = 'rocket',
collectable = 'collectable',
}
3 changes: 2 additions & 1 deletion src/app/models/pixijs/power-up-sprite.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FrameObject, Texture } from 'pixi.js';
import { PowerUpConfig } from '../power-up-config.model';
import { AnimatedGameSprite } from './animated-game-sprite';
import { ObjectType } from './object-type.enum';

export enum PowerUp {
speed,
Expand All @@ -12,7 +13,7 @@ export class PowerUpSprite extends AnimatedGameSprite {
readonly config: PowerUpConfig;

constructor(speed: number, textures: Texture[] | FrameObject[], config: PowerUpConfig) {
super(speed, textures);
super(ObjectType.collectable, null, speed, textures);

this.config = config;
}
Expand Down
14 changes: 10 additions & 4 deletions src/app/models/pixijs/rocket.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { FrameObject, Texture } from 'pixi.js';
import { ExplosionService } from '../../services/explosion.service';
import { AnimatedGameSprite } from './animated-game-sprite';
import { Ship } from './ship';
import { ObjectType } from './object-type.enum';

export class Rocket extends AnimatedGameSprite {
reference: Ship | undefined;
constructor(
explosion: ExplosionService,
speed: number,
textures: Texture[] | FrameObject[],
autoUpdate?: boolean) {
super(ObjectType.rocket, explosion, speed, textures, autoUpdate);

override hit(object2: AnimatedGameSprite): boolean {
return this.reference !== object2 && super.hit(object2);
this.energy = 1;
}
}
19 changes: 11 additions & 8 deletions src/app/models/pixijs/ship.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { FrameObject, Texture } from 'pixi.js';
import { GAME_CONFIG } from '../../game-constants';
import { ExplosionService } from '../../services/explosion.service';
import { GameShotService } from '../../services/game-shot.service';
import { AnimatedGameSprite } from './animated-game-sprite';
import { ObjectType } from './object-type.enum';
import { ShipType } from './ship-type.enum';

export class Ship extends AnimatedGameSprite {
Expand All @@ -10,28 +12,29 @@ export class Ship extends AnimatedGameSprite {
lastShot = 0;
autoFire = false;

#energy = 10;

private elapsed = 0;

// eslint-disable-next-line max-params
constructor(
readonly type: ShipType,
readonly shipType: ShipType,
private readonly shotService: GameShotService,
explosion: ExplosionService,
speed: number,
textures: Texture[] | FrameObject[],
autoUpdate?: boolean) {
super(speed, textures, autoUpdate);
super(shipType as unknown as ObjectType, explosion, speed, textures, autoUpdate);

this.shotSpeed = GAME_CONFIG.ships[this.type].shotSpeed;
this.energy = GAME_CONFIG.ships[this.shipType].energy;
this.shotSpeed = GAME_CONFIG.ships[this.shipType].shotSpeed;
}

set energy(value: number) {
this.#energy = Math.min(value, 10);
override set energy(value: number) {
super.energy = Math.min(value, GAME_CONFIG.ships[this.shipType].energy);
};

get energy(): number {
return this.#energy;
override get energy(): number {
return super.energy ?? 0;
}

shot(): void {
Expand Down
27 changes: 19 additions & 8 deletions src/app/models/pixijs/simple-game-sprite.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { Sprite, Texture } from 'pixi.js';
import { ExplosionService } from '../../services/explosion.service';
import { ObjectModelType } from '../../services/object.service';
import { hit } from '../../utils/sprite.util';
import { ObjectType } from './object-type.enum';

export class GameSprite extends Sprite {
private readonly ySpeed: number;
private readonly xSpeed: number;

power: number | undefined;
reference: ObjectModelType | undefined;
energy: number | undefined;
destroying = false;

constructor(speed: number, texture: Texture) {
constructor(
readonly type: ObjectType,
private readonly explosion: ExplosionService,
speed: number,
texture: Texture,
) {
super(texture);

this.ySpeed = Math.random() * speed;
Expand All @@ -20,11 +30,12 @@ export class GameSprite extends Sprite {
this.x += delta * this.xSpeed;
}

hit(object2: Sprite): boolean {
const event = hit(this, object2);
if (event && this.power !== undefined) {
this.power -= 1;
}
return event;
hit(object2: ObjectModelType): boolean {
return hit(this, object2);
}

explode(): void {
void this.explosion.explode(this.x, this.y);
this.destroying = true;
}
}
15 changes: 2 additions & 13 deletions src/app/services/base.service.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
import { inject } from '@angular/core';
import { AnimatedSprite } from 'pixi.js';
import { ApplicationService } from './application.service';
import { ExplosionService } from './explosion.service';
import { ObjectService } from './object.service';

export abstract class BaseService {
private explosion = inject(ExplosionService);

protected readonly application = inject(ApplicationService);

protected explode(
x: number,
y: number,
oncomplete: (explosion: AnimatedSprite) => void = (): void => {
},
): void {
void this.explosion.explode(this.application.stage, x, y, oncomplete);
}
protected readonly object = inject(ObjectService);
}
10 changes: 6 additions & 4 deletions src/app/services/explosion.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Injectable } from '@angular/core';
import { AnimatedSprite, Assets, Container, Spritesheet, Texture } from 'pixi.js';
import { inject, Injectable } from '@angular/core';
import { AnimatedSprite, Assets, Spritesheet, Texture } from 'pixi.js';
import { ApplicationService } from './application.service';

@Injectable({ providedIn: 'root' })
export class ExplosionService {
private explosionSprite: Spritesheet | undefined;

private readonly application = inject(ApplicationService);

private async getAnimationSprite(): Promise<AnimatedSprite> {
if (!this.explosionSprite) {
this.explosionSprite = await Assets.load<Spritesheet>('assets/game/explosion.json');
Expand All @@ -14,7 +17,6 @@ export class ExplosionService {
}

async explode(
stage: Container,
x: number,
y: number,
oncomplete: (explosion: AnimatedSprite) => void = (): void => {
Expand All @@ -30,7 +32,7 @@ export class ExplosionService {
oncomplete(explosion);
explosion.destroy();
};
stage.addChild(explosion);
this.application.stage.addChild(explosion);
explosion.play();
}
}
32 changes: 23 additions & 9 deletions src/app/services/game-collectable.service.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import { Injectable } from '@angular/core';
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';

interface Dictionary<T> {
[key: string]: T;
}

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

constructor(private readonly application: ApplicationService) {
constructor(
private readonly application: ApplicationService,
private readonly shipService: GameShipService,
objectService: ObjectService,
) {
super();

objectService.onDestroyed(ObjectType.enemy, (enemy, by) => this.spawn(enemy, by));
}

async init(): Promise<void> {
Expand All @@ -28,15 +38,18 @@ export class GameCollectableService {
}
}

spawn(x: number, y: number): void {
private spawn({ x, y }: ObjectModelType, { type, reference }: ObjectModelType): void {
if (type !== ObjectType.ship && reference?.type !== ObjectType.ship) {
return;
}
const rand = Math.random();
if (rand > 0.2) {
return;
}
const value = Math.floor(Math.random() * GAME_CONFIG.powerUpConfig.length);
const type = GAME_CONFIG.powerUpConfig[value];
const texture = this.animations[type.type];
const powerUp = new PowerUpSprite(1, texture, type);
const powerUpType = GAME_CONFIG.powerUpConfig[value];
const texture = this.animations[powerUpType.type];
const powerUp = new PowerUpSprite(1, texture, powerUpType);
powerUp.animationSpeed = 0.167;
powerUp.play();
powerUp.anchor.set(0.5);
Expand All @@ -46,8 +59,9 @@ export class GameCollectableService {
this.collectables.push(powerUp);
}

collect(ship: Ship | undefined): void {
if (!ship || ship.destroyed) {
update(): void {
const ship = this.shipService.instance;
if (ship.destroyed) {
return;
}

Expand Down
Loading

0 comments on commit d38a1c6

Please sign in to comment.