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

73 lines
2.9 KiB
TypeScript
Raw Normal View History

2022-11-16 11:26:20 +00:00
import { Component, Node, _decorator } from "cc";
2022-11-28 11:19:04 +00:00
import { XPSpawner } from "../../XP/XPSpawner";
2022-12-05 11:19:46 +00:00
import { CircularEnemySpawner } from "./CircularEnemySpawner";
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-11-16 11:26:20 +00:00
import { EnemyMover } from "./EnemyMover";
import { EnemySpawner } from "./EnemySpawner";
2022-12-05 11:19:46 +00:00
import { EnemyType } from "./EnemyType";
2022-12-05 14:13:42 +00:00
import { FollowTargetEnemyMover } from "./FollowTargetEnemyMover";
2022-12-05 11:19:46 +00:00
import { InvididualEnemySpawner as IndividualEnemySpawner } from "./InvididualEnemySpawner";
2022-12-05 14:13:42 +00:00
import { LaunchToTargetEnemyMover } from "./LaunchToTargetEnemyMover";
import { WaveEnemySpawner } from "./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-11-16 11:26:20 +00:00
public init(targetNode: Node): void {
2022-11-16 13:04:23 +00:00
this.enemySpawner.init(targetNode);
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);
this.individualEnemySpawner = new IndividualEnemySpawner(this.enemySpawner, EnemyType.Basic);
2022-12-05 14:13:42 +00:00
this.circularEnemySpawner = new CircularEnemySpawner(this.enemySpawner, 30, EnemyType.Basic);
2022-12-06 08:15:05 +00:00
this.waveEnemySpawner = new WaveEnemySpawner(this.enemySpawner, 30, 10, EnemyType.Basic);
2022-12-05 14:13:42 +00:00
this.movementTypeToMover.set(EnemyMovementType.Follow, new FollowTargetEnemyMover(targetNode));
this.movementTypeToMover.set(EnemyMovementType.Launch, new LaunchToTargetEnemyMover(targetNode));
2022-11-16 11:26:20 +00:00
this.xpSpawner.init();
}
public gameTick(deltaTime: number): void {
2022-12-05 14:13:42 +00:00
//this.individualEnemySpawner.gameTick(deltaTime);
//this.circularEnemySpawner.gameTick(deltaTime);
this.waveEnemySpawner.gameTick(deltaTime);
for (const kvp of this.movementTypeToMover) {
kvp[1].gameTick(deltaTime);
}
2022-11-16 11:26:20 +00:00
}
private onEnemyAdded(enemy: Enemy): void {
enemy.DeathEvent.on(this.onEnemyDied, this);
2022-12-05 14:13:42 +00:00
this.getEnemyMover(enemy).addEnemy(enemy);
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
}
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
}
}