Wave lifetime

This commit is contained in:
Martin 2022-12-06 09:15:05 +01:00
parent d109519190
commit 8e38cbfb27
3 changed files with 50 additions and 11 deletions

View File

@ -30,7 +30,7 @@ export class EnemyManager extends Component {
this.individualEnemySpawner = new IndividualEnemySpawner(this.enemySpawner, EnemyType.Basic); this.individualEnemySpawner = new IndividualEnemySpawner(this.enemySpawner, EnemyType.Basic);
this.circularEnemySpawner = new CircularEnemySpawner(this.enemySpawner, 30, 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.Follow, new FollowTargetEnemyMover(targetNode));
this.movementTypeToMover.set(EnemyMovementType.Launch, new LaunchToTargetEnemyMover(targetNode)); this.movementTypeToMover.set(EnemyMovementType.Launch, new LaunchToTargetEnemyMover(targetNode));

View File

@ -7,7 +7,9 @@ import { EnemyType } from "./EnemyType";
export class InvididualEnemySpawner { export class InvididualEnemySpawner {
private spawnTimer: GameTimer = new GameTimer(1); 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 { public gameTick(deltaTime: number): void {
this.spawnTimer.gameTick(deltaTime); this.spawnTimer.gameTick(deltaTime);
if (this.spawnTimer.tryFinishPeriod()) { if (this.spawnTimer.tryFinishPeriod()) {

View File

@ -1,27 +1,64 @@
import { randomRange } from "cc";
import { GameTimer } from "../../../Services/GameTimer"; import { GameTimer } from "../../../Services/GameTimer";
import { randomPositiveOrNegative } from "../../../Services/Utils/MathUtils"; import { randomPositiveOrNegative } from "../../../Services/Utils/MathUtils";
import { Enemy } from "./Enemy";
import { EnemyMovementType } from "./EnemyMovementType"; import { EnemyMovementType } from "./EnemyMovementType";
import { EnemySpawner } from "./EnemySpawner"; import { EnemySpawner } from "./EnemySpawner";
import { EnemyType } from "./EnemyType"; import { EnemyType } from "./EnemyType";
export class WaveEnemySpawner { export class WaveEnemySpawner {
private spawnTimer: GameTimer = new GameTimer(5); 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 { public gameTick(deltaTime: number): void {
this.spawnTimer.gameTick(deltaTime); this.spawnTimer.gameTick(deltaTime);
if (this.spawnTimer.tryFinishPeriod()) { this.tryRemoveExpiredEnemies(deltaTime);
const angle: number = (2 * Math.PI) / this.enemiesToSpawn; this.trySpawnNewGroup();
}
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);
}
this.groups.splice(i, 1);
}
}
}
private trySpawnNewGroup(): void {
if (this.spawnTimer.tryFinishPeriod()) {
const defaultPosX: number = 200 * randomPositiveOrNegative(); const defaultPosX: number = 200 * randomPositiveOrNegative();
const defaultPosY: 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++) { for (let i = 0; i < this.enemiesToSpawn; i++) {
const posX: number = defaultPosX + 10 * i; const randomOffsetX: number = randomRange(-20, 20);
const posY: number = defaultPosY + 10 * (i % 2); const randomOffsetY: number = randomRange(-20, 20);
this.enemySpawner.spawnNewEnemy(posX, posY, EnemyMovementType.Launch); 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;
}