diff --git a/assets/Scripts/Game/Unit/Enemy/EnemyManager.ts b/assets/Scripts/Game/Unit/Enemy/EnemyManager.ts index b16f31d..a3cc726 100644 --- a/assets/Scripts/Game/Unit/Enemy/EnemyManager.ts +++ b/assets/Scripts/Game/Unit/Enemy/EnemyManager.ts @@ -30,7 +30,7 @@ export class EnemyManager extends Component { this.individualEnemySpawner = new IndividualEnemySpawner(this.enemySpawner, EnemyType.Basic); this.circularEnemySpawner = new CircularEnemySpawner(this.enemySpawner, 30, EnemyType.Basic); - this.waveEnemySpawner = new WaveEnemySpawner(this.enemySpawner, 30, EnemyType.Basic); + this.waveEnemySpawner = new WaveEnemySpawner(this.enemySpawner, 30, 10, EnemyType.Basic); this.movementTypeToMover.set(EnemyMovementType.Follow, new FollowTargetEnemyMover(targetNode)); this.movementTypeToMover.set(EnemyMovementType.Launch, new LaunchToTargetEnemyMover(targetNode)); diff --git a/assets/Scripts/Game/Unit/Enemy/InvididualEnemySpawner.ts b/assets/Scripts/Game/Unit/Enemy/InvididualEnemySpawner.ts index c696626..b7932b5 100644 --- a/assets/Scripts/Game/Unit/Enemy/InvididualEnemySpawner.ts +++ b/assets/Scripts/Game/Unit/Enemy/InvididualEnemySpawner.ts @@ -7,7 +7,9 @@ import { EnemyType } from "./EnemyType"; export class InvididualEnemySpawner { private spawnTimer: GameTimer = new GameTimer(1); - public constructor(private enemySpawner: EnemySpawner, enemyType: EnemyType) {} + + public constructor(private enemySpawner: EnemySpawner, private enemyType: EnemyType) {} + public gameTick(deltaTime: number): void { this.spawnTimer.gameTick(deltaTime); if (this.spawnTimer.tryFinishPeriod()) { diff --git a/assets/Scripts/Game/Unit/Enemy/WaveEnemySpawner.ts b/assets/Scripts/Game/Unit/Enemy/WaveEnemySpawner.ts index 62bc012..0025c07 100644 --- a/assets/Scripts/Game/Unit/Enemy/WaveEnemySpawner.ts +++ b/assets/Scripts/Game/Unit/Enemy/WaveEnemySpawner.ts @@ -1,27 +1,64 @@ +import { randomRange } from "cc"; import { GameTimer } from "../../../Services/GameTimer"; import { randomPositiveOrNegative } from "../../../Services/Utils/MathUtils"; +import { Enemy } from "./Enemy"; import { EnemyMovementType } from "./EnemyMovementType"; import { EnemySpawner } from "./EnemySpawner"; import { EnemyType } from "./EnemyType"; export class WaveEnemySpawner { private spawnTimer: GameTimer = new GameTimer(5); - public constructor(private enemySpawner: EnemySpawner, private enemiesToSpawn: number, private enemyType: EnemyType) {} + private groups: EnemyGroupWithLifetime[] = []; + public constructor( + private enemySpawner: EnemySpawner, + private enemiesToSpawn: number, + private enemyLifeTime: number, + private enemyType: EnemyType + ) {} public gameTick(deltaTime: number): void { this.spawnTimer.gameTick(deltaTime); - if (this.spawnTimer.tryFinishPeriod()) { - const angle: number = (2 * Math.PI) / this.enemiesToSpawn; + this.tryRemoveExpiredEnemies(deltaTime); + this.trySpawnNewGroup(); + } - const defaultPosX: number = 200 * randomPositiveOrNegative(); - const defaultPosY: number = 200 * randomPositiveOrNegative(); + private tryRemoveExpiredEnemies(deltaTime: number): void { + for (let i = this.groups.length - 1; 0 <= i; i--) { + const group: EnemyGroupWithLifetime = this.groups[i]; + group.lifeTimeLeft -= deltaTime; + if (group.lifeTimeLeft <= 0) { + for (const enemy of group.enemies) { + this.enemySpawner.returnEnemy(enemy); + } - for (let i = 0; i < this.enemiesToSpawn; i++) { - const posX: number = defaultPosX + 10 * i; - const posY: number = defaultPosY + 10 * (i % 2); - this.enemySpawner.spawnNewEnemy(posX, posY, EnemyMovementType.Launch); + this.groups.splice(i, 1); } } } + + private trySpawnNewGroup(): void { + if (this.spawnTimer.tryFinishPeriod()) { + const defaultPosX: number = 200 * randomPositiveOrNegative(); + const defaultPosY: number = 200 * randomPositiveOrNegative(); + + const enemies: Enemy[] = []; + const side: number = Math.ceil(Math.sqrt(this.enemiesToSpawn)); + for (let i = 0; i < this.enemiesToSpawn; i++) { + const randomOffsetX: number = randomRange(-20, 20); + const randomOffsetY: number = randomRange(-20, 20); + const posX: number = defaultPosX + randomOffsetX + 50 * (i % side); + const posY: number = defaultPosY + randomOffsetY + 50 * Math.floor(i / side); + const enemy = this.enemySpawner.spawnNewEnemy(posX, posY, EnemyMovementType.Launch); + enemies.push(enemy); + } + + this.groups.push({ enemies, lifeTimeLeft: this.enemyLifeTime }); + } + } +} + +class EnemyGroupWithLifetime { + public enemies: Enemy[]; + public lifeTimeLeft: number; }