67 lines
2.4 KiB
TypeScript
Raw Normal View History

2022-11-30 11:44:20 +01:00
import { Node, Vec2 } from "cc";
2023-01-09 11:09:06 +01:00
import { Empty } from "../../../Menu/ModalWindows/Upgrades/UpgradesModalWindow";
import { ISignal } from "../../../Services/EventSystem/ISignal";
import { GameTimer } from "../../../Services/GameTimer";
import { delay } from "../../../Services/Utils/AsyncUtils";
import { WaveLauncherSettings } from "../../Data/GameSettings";
import { IProjectileLauncherSignaler } from "../IProjectileLauncherSignaler";
import { ProjectileCollision } from "../ProjectileCollision";
2022-12-21 08:51:39 +01:00
import { ProjectileData } from "./ProjectileData";
2022-12-22 12:08:00 +01:00
import { ProjectileLauncher } from "./ProjectileLauncher";
2022-11-30 11:44:20 +01:00
2022-12-22 12:08:00 +01:00
export class WaveProjectileLauncher implements IProjectileLauncherSignaler {
2022-11-30 11:44:20 +01:00
private currentUpgrade = 0;
private wavesToShootPerUpgrade = 0;
2022-12-20 15:17:57 +01:00
private fireTimer: GameTimer;
private wavesToShoot: number;
private wavesDelayMs: number;
2022-11-30 11:44:20 +01:00
2022-12-13 11:58:40 +01:00
public constructor(
private launcher: ProjectileLauncher,
2022-12-20 14:37:26 +01:00
private playerNode: Node,
private directions: Vec2[],
2022-12-13 11:58:40 +01:00
settings: WaveLauncherSettings,
projectileData: ProjectileData
) {
2022-11-30 11:44:20 +01:00
this.wavesToShootPerUpgrade = settings.wavesToShootPerUpgrade;
2022-12-20 15:17:57 +01:00
this.fireTimer = new GameTimer(settings.launcher.cooldown);
this.wavesToShoot = settings.launcher.wavesToShoot;
this.wavesDelayMs = settings.launcher.wavesDelayMs;
launcher.init(settings.launcher.projectileLifetime, settings.launcher.projectileSpeed, projectileData.damage, projectileData.pierces);
2022-11-30 11:44:20 +01:00
}
public get ProjectileCollisionEvent(): ISignal<ProjectileCollision> {
return this.launcher.ProjectileCollisionEvent;
}
2022-12-22 12:08:00 +01:00
public get ProjectileLaunchedEvent(): ISignal {
return this.launcher.ProjectileLaunchedEvent;
}
2022-11-30 11:44:20 +01:00
public gameTick(deltaTime: number): void {
if (this.currentUpgrade == 0) return;
2022-12-20 15:17:57 +01:00
this.launcher.gameTick(deltaTime);
this.fireTimer.gameTick(deltaTime);
if (this.fireTimer.tryFinishPeriod()) {
this.fireProjectiles();
}
2022-11-30 11:44:20 +01:00
}
public upgrade(): void {
this.currentUpgrade++;
2022-12-20 15:17:57 +01:00
this.wavesToShoot += this.wavesToShootPerUpgrade;
}
private async fireProjectiles(): Promise<void> {
for (let i = 0; i < this.wavesToShoot; i++) {
this.launcher.fireProjectiles(this.playerNode.worldPosition, this.directions);
await delay(this.wavesDelayMs);
}
2022-11-30 11:44:20 +01:00
}
}