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

119 lines
4.8 KiB
TypeScript
Raw Normal View History

2022-12-19 14:50:24 +00:00
import { Component, Node, random, randomRange, Vec3, _decorator } from "cc";
2022-12-19 13:24:29 +00:00
2022-12-06 09:00:18 +00:00
import { EnemyManagerSettings } from "../../Data/GameSettings";
2022-12-19 14:50:24 +00:00
import { GoldSpawner } from "../../Gold/GoldSpawner";
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";
2022-12-12 12:12:07 +00:00
import { DelayedEnemySpawner } from "./EnemySpawner/DelayedEnemySpawner";
2022-12-06 09:00:18 +00:00
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-19 14:50:24 +00:00
@property(GoldSpawner) private goldSpawner: GoldSpawner;
2022-11-16 11:26:20 +00:00
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-12 12:12:07 +00:00
private spawners: DelayedEnemySpawner[] = [];
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 13:26:53 +00:00
for (const individualSpawnerSettings of settings.individualEnemySpawners) {
const individualSpawner = new IndividualEnemySpawner(this.enemySpawner, individualSpawnerSettings);
this.spawners.push(individualSpawner);
}
for (const circularSpawnerSettings of settings.circularEnemySpawners) {
const circularSpawner = new CircularEnemySpawner(this.enemySpawner, circularSpawnerSettings);
this.spawners.push(circularSpawner);
}
2022-12-12 12:12:07 +00:00
for (const waveSpawnerSettings of settings.waveEnemySpawners) {
const waveSpawner = new WaveEnemySpawner(this.enemySpawner, waveSpawnerSettings);
this.spawners.push(waveSpawner);
}
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();
2022-12-19 14:50:24 +00:00
this.goldSpawner.init();
2022-11-16 11:26:20 +00:00
}
public gameTick(deltaTime: number): void {
2022-12-12 12:12:07 +00:00
for (const spawner of this.spawners) {
spawner.gameTick(deltaTime);
}
2022-12-05 14:13:42 +00:00
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);
2022-12-13 17:56:00 +00:00
enemy.LifetimeEndedEvent.off(this.onEnemyLifetimeEnded);
2022-12-19 13:24:29 +00:00
for (let index = 0; index < enemy.XPReward; index++) {
const position: Vec3 = enemy.node.worldPosition;
position.x += randomRange(-10, 10);
position.y += randomRange(-10, 10);
this.xpSpawner.spawnXp(position, 1);
}
2022-12-19 14:50:24 +00:00
if (0 < enemy.GoldReward) {
if (enemy.GoldReward < 1) {
if (random() < enemy.GoldReward) {
this.goldSpawner.spawn(enemy.node.worldPosition);
}
} else {
for (let i = 0; i < enemy.GoldReward; i++) {
const position: Vec3 = enemy.node.worldPosition;
position.x += randomRange(-10, 10);
position.y += randomRange(-10, 10);
this.goldSpawner.spawn(position);
}
}
}
2022-12-05 11:19:46 +00:00
}
2022-12-13 17:56:00 +00:00
private onEnemyLifetimeEnded(enemy: Enemy): void {
enemy.DeathEvent.off(this.onEnemyDied);
enemy.LifetimeEndedEvent.off(this.onEnemyLifetimeEnded);
}
2022-12-12 11:04:31 +00:00
private onEnemyAdded(enemy: Enemy): void {
enemy.DeathEvent.on(this.onEnemyDied, this);
2022-12-13 17:56:00 +00:00
enemy.LifetimeEndedEvent.on(this.onEnemyLifetimeEnded, this);
2022-12-12 11:04:31 +00:00
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
}
}