mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2024-12-26 03:38:58 +00:00
Wave lifetime
This commit is contained in:
parent
d109519190
commit
8e38cbfb27
@ -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));
|
||||
|
@ -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()) {
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
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 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 posX: number = defaultPosX + 10 * i;
|
||||
const posY: number = defaultPosY + 10 * (i % 2);
|
||||
this.enemySpawner.spawnNewEnemy(posX, posY, EnemyMovementType.Launch);
|
||||
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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user