Slash-The-Hordes/assets/Scripts/Game/Upgrades/Upgrader.ts

85 lines
3.5 KiB
TypeScript
Raw Normal View History

2022-11-28 10:49:16 +00:00
import { UpgradeSettings } from "../Data/GameSettings";
import { Player } from "../Unit/Player/Player";
2023-01-09 10:09:06 +00:00
import { HaloProjectileLauncher } from "../Projectile/ProjectileLauncher/HaloProjectileLauncher";
import { WaveProjectileLauncher } from "../Projectile/ProjectileLauncher/WaveProjectileLauncher";
2022-11-23 08:01:01 +00:00
import { UpgradeType } from "./UpgradeType";
export class Upgrader {
private typeToAction: Map<UpgradeType, () => void> = new Map<UpgradeType, () => void>();
private typeToLevel: Map<UpgradeType, number> = new Map<UpgradeType, number>();
2022-11-24 12:17:05 +00:00
private typeToMaxLevel: Map<UpgradeType, number> = new Map<UpgradeType, number>();
2022-11-23 08:01:01 +00:00
2022-11-30 10:44:20 +00:00
public constructor(
private player: Player,
2022-12-01 11:16:52 +00:00
private horizontalProjectileLauncher: WaveProjectileLauncher,
2022-11-30 10:44:20 +00:00
private haloProjectileLauncher: HaloProjectileLauncher,
2022-12-12 13:40:38 +00:00
private diagonalProjectileLauncher: WaveProjectileLauncher,
2022-11-30 10:44:20 +00:00
settings: UpgradeSettings
) {
2022-11-28 10:49:16 +00:00
this.setTypeMaps(UpgradeType.WeaponLength, this.upgradeWeaponLength.bind(this), settings.maxWeaponLengthUpgrades);
2022-11-28 13:34:34 +00:00
this.setTypeMaps(UpgradeType.WeaponDamage, this.upgradeWeaponDamage.bind(this), settings.maxWeaponDamageUpgrades);
2022-11-30 15:04:48 +00:00
this.setTypeMaps(
UpgradeType.HorizontalProjectile,
this.upgradeHorizontalProjectileLauncher.bind(this),
settings.maxHorizontalProjectileUpgrades
);
2022-12-12 13:40:38 +00:00
this.setTypeMaps(UpgradeType.DiagonalProjectile, this.upgradeDiagonalProjectileLauncher.bind(this), settings.maxDiagonalProjectileUpgrades);
2022-11-30 10:44:20 +00:00
this.setTypeMaps(UpgradeType.HaloProjectlie, this.upgradeHaloProjectileLauncher.bind(this), settings.maxHaloProjectileUpgrades);
2022-11-28 13:34:34 +00:00
this.setTypeMaps(UpgradeType.Regeneration, this.upgradeRegeneration.bind(this), settings.maxRegenerationUpgrades);
2022-11-23 08:01:01 +00:00
}
public upgradeSkill(type: UpgradeType): void {
if (!this.typeToAction.has(type)) throw new Error("Upgrade does not have " + type);
2022-11-28 10:49:16 +00:00
if (this.isMaxLevel(type)) throw new Error("Upgrade is already at max level " + type);
2022-11-25 11:00:09 +00:00
2022-11-23 08:01:01 +00:00
this.typeToAction.get(type)();
2022-11-28 10:49:16 +00:00
const level: number = this.typeToLevel.get(type);
this.typeToLevel.set(type, level + 1);
2022-11-23 08:01:01 +00:00
}
2022-11-25 11:00:09 +00:00
public getAvailableUpgrades(): Set<UpgradeType> {
const availableUpgrades: Set<UpgradeType> = new Set<UpgradeType>();
for (const key of this.typeToAction.keys()) {
if (!this.isMaxLevel(key)) {
availableUpgrades.add(key);
}
}
return availableUpgrades;
}
2022-11-24 12:17:05 +00:00
private setTypeMaps(upgradeType: UpgradeType, action: () => void, maxLevel: number): void {
this.typeToAction.set(upgradeType, action);
this.typeToLevel.set(upgradeType, 0);
this.typeToMaxLevel.set(upgradeType, maxLevel);
2022-11-23 08:01:01 +00:00
}
private upgradeWeaponLength(): void {
this.player.Weapon.upgradeWeaponLength();
}
private upgradeWeaponDamage(): void {
this.player.Weapon.upgradeWeaponDamage();
}
2022-11-25 11:00:09 +00:00
2022-11-30 15:04:48 +00:00
private upgradeHorizontalProjectileLauncher(): void {
this.horizontalProjectileLauncher.upgrade();
2022-11-30 10:44:20 +00:00
}
2022-12-12 13:40:38 +00:00
private upgradeDiagonalProjectileLauncher(): void {
this.diagonalProjectileLauncher.upgrade();
}
2022-11-30 10:44:20 +00:00
private upgradeHaloProjectileLauncher(): void {
this.haloProjectileLauncher.upgrade();
}
2022-11-28 13:34:34 +00:00
private upgradeRegeneration(): void {
this.player.Regeneration.upgrade();
}
2022-11-25 11:00:09 +00:00
private isMaxLevel(type: UpgradeType): boolean {
return this.typeToMaxLevel.get(type) <= this.typeToLevel.get(type);
}
2022-11-23 08:01:01 +00:00
}