Slash-The-Hordes/assets/Scripts/Game/Unit/Enemy/EnemyManager.ts

76 lines
3.2 KiB
TypeScript
Raw Normal View History

2022-11-16 11:26:20 +00:00
import { Component, Node, _decorator } from "cc";
2022-12-06 09:00:18 +00:00
import { EnemyManagerSettings } from "../../Data/GameSettings";
2022-11-28 11:19:04 +00:00
import { XPSpawner } from "../../XP/XPSpawner";
2022-11-16 11:26:20 +00:00
import { Enemy } from "./Enemy";
2022-12-05 14:13:42 +00:00
import { EnemyMovementType } from "./EnemyMovementType";
2022-12-06 09:00:18 +00:00
import { EnemyMover } from "./EnemyMover/EnemyMover";
import { FollowTargetEnemyMover } from "./EnemyMover/FollowTargetEnemyMover";
2022-12-12 11:04:31 +00:00
import { PeriodicFollowTargetEnemyMover } from "./EnemyMover/PeriodicFollowTargetEnemyMover";
2022-12-06 09:00:18 +00:00
import { WaveEnemyMover } from "./EnemyMover/WaveEnemyMover";
import { CircularEnemySpawner } from "./EnemySpawner/CircularEnemySpawner";
import { EnemySpawner } from "./EnemySpawner/EnemySpawner";
import { IndividualEnemySpawner } from "./EnemySpawner/IndividualEnemySpawner";
import { WaveEnemySpawner } from "./EnemySpawner/WaveEnemySpawner";
2022-11-16 11:26:20 +00:00
const { ccclass, property } = _decorator;
@ccclass("EnemyManager")
export class EnemyManager extends Component {
@property(EnemySpawner) private enemySpawner: EnemySpawner;
@property(XPSpawner) private xpSpawner: XPSpawner;
2022-12-05 14:13:42 +00:00
private movementTypeToMover: Map<EnemyMovementType, EnemyMover> = new Map<EnemyMovementType, EnemyMover>();
2022-11-16 11:26:20 +00:00
2022-12-05 11:19:46 +00:00
private individualEnemySpawner: IndividualEnemySpawner;
private circularEnemySpawner: CircularEnemySpawner;
2022-12-05 14:13:42 +00:00
private waveEnemySpawner: WaveEnemySpawner;
2022-12-05 11:19:46 +00:00
2022-12-06 09:00:18 +00:00
public init(targetNode: Node, settings: EnemyManagerSettings): void {
2022-12-12 11:46:17 +00:00
this.enemySpawner.init(targetNode, settings.enemies);
2022-11-16 11:26:20 +00:00
this.enemySpawner.EnemyAddedEvent.on(this.onEnemyAdded, this);
2022-12-05 11:19:46 +00:00
this.enemySpawner.enemyRemovedEvent.on(this.onRemoveEnemy, this);
2022-12-12 11:46:17 +00:00
this.individualEnemySpawner = new IndividualEnemySpawner(this.enemySpawner, "Basic");
this.circularEnemySpawner = new CircularEnemySpawner(this.enemySpawner, 30, "Basic");
2022-12-06 09:00:18 +00:00
this.waveEnemySpawner = new WaveEnemySpawner(this.enemySpawner, settings.waveEnemySpawner);
2022-12-05 14:13:42 +00:00
this.movementTypeToMover.set(EnemyMovementType.Follow, new FollowTargetEnemyMover(targetNode));
2022-12-06 09:00:18 +00:00
this.movementTypeToMover.set(EnemyMovementType.Launch, new WaveEnemyMover(targetNode));
2022-12-12 11:04:31 +00:00
this.movementTypeToMover.set(EnemyMovementType.PeriodicFollow, new PeriodicFollowTargetEnemyMover(targetNode, 5, 5));
2022-11-16 11:26:20 +00:00
this.xpSpawner.init();
}
public gameTick(deltaTime: number): void {
2022-12-06 09:00:18 +00:00
this.individualEnemySpawner.gameTick(deltaTime);
this.circularEnemySpawner.gameTick(deltaTime);
2022-12-05 14:13:42 +00:00
this.waveEnemySpawner.gameTick(deltaTime);
for (const kvp of this.movementTypeToMover) {
kvp[1].gameTick(deltaTime);
}
2022-11-16 11:26:20 +00:00
}
private onEnemyDied(enemy: Enemy): void {
enemy.DeathEvent.off(this.onEnemyDied);
this.xpSpawner.spawnXp(enemy.node.worldPosition, 1);
2022-12-05 11:19:46 +00:00
}
2022-12-12 11:04:31 +00:00
private onEnemyAdded(enemy: Enemy): void {
enemy.DeathEvent.on(this.onEnemyDied, this);
this.getEnemyMover(enemy).addEnemy(enemy);
}
2022-12-05 11:19:46 +00:00
private onRemoveEnemy(enemy: Enemy): void {
2022-12-05 14:13:42 +00:00
this.getEnemyMover(enemy).removeEnemy(enemy);
}
private getEnemyMover(enemy: Enemy): EnemyMover {
if (this.movementTypeToMover.has(enemy.MovementType)) {
return this.movementTypeToMover.get(enemy.MovementType);
}
throw new Error("Does not have mover of type " + enemy.MovementType);
2022-11-16 11:26:20 +00:00
}
}