70 lines
2.6 KiB
TypeScript
Raw Normal View History

2023-01-02 09:17:11 +01:00
import { Node, Vec2 } from "cc";
2023-01-09 11:09:06 +01:00
import { ISignal } from "../../../Services/EventSystem/ISignal";
import { Signal } from "../../../Services/EventSystem/Signal";
import { GameTimer } from "../../../Services/GameTimer";
import { roundToOneDecimal } from "../../../Services/Utils/MathUtils";
import { HaloLauncherSettings } from "../../Data/GameSettings";
import { IProjectileLauncherSignaler } from "../IProjectileLauncherSignaler";
import { ProjectileCollision } from "../ProjectileCollision";
2022-12-21 08:51:39 +01:00
import { ProjectileData } from "./ProjectileData";
2023-01-02 09:17:11 +01:00
import { ProjectileLauncher } from "./ProjectileLauncher";
2022-11-30 11:44:20 +01:00
2022-12-22 12:08:00 +01:00
export class HaloProjectileLauncher implements IProjectileLauncherSignaler {
2022-11-30 11:44:20 +01:00
private currentUpgrade = 0;
private defaultCooldown = 0;
private cooldownDivisorPerUpgrade = 0;
2022-12-20 14:37:26 +01:00
private directions: Vec2[] = [];
2022-12-20 15:17:57 +01:00
private fireTimer = new GameTimer(0);
2022-12-20 14:37:26 +01:00
2023-01-02 09:17:11 +01:00
private projectilesLaunchedEvent = new Signal();
2022-12-20 14:37:26 +01:00
public constructor(
private launcher: ProjectileLauncher,
private playerNode: Node,
settings: HaloLauncherSettings,
projectileData: ProjectileData
) {
2022-11-30 11:44:20 +01:00
this.defaultCooldown = settings.launcher.cooldown;
this.cooldownDivisorPerUpgrade = settings.cooldownDivisorPerUpgrade;
const angle: number = (2 * Math.PI) / settings.projectilesToSpawn;
for (let i = 0; i < settings.projectilesToSpawn; i++) {
const x: number = roundToOneDecimal(Math.sin(angle * i));
const y: number = roundToOneDecimal(Math.cos(angle * i));
2022-12-20 14:37:26 +01:00
this.directions.push(new Vec2(x, y).normalize());
2022-11-30 11:44:20 +01:00
}
2022-12-20 15:17:57 +01:00
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;
}
2023-01-02 09:17:11 +01:00
public get ProjectilesLaunchedEvent(): ISignal {
return this.projectilesLaunchedEvent;
}
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.launcher.fireProjectiles(this.playerNode.worldPosition, this.directions);
2023-01-02 09:17:11 +01:00
this.projectilesLaunchedEvent.trigger();
2022-12-20 15:17:57 +01:00
}
2022-11-30 11:44:20 +01:00
}
public upgrade(): void {
this.currentUpgrade++;
2022-12-20 15:17:57 +01:00
this.fireTimer = new GameTimer(this.defaultCooldown / (this.cooldownDivisorPerUpgrade * this.currentUpgrade));
2022-11-30 11:44:20 +01:00
}
}