import { UpgradeSettings } from "../Data/GameSettings"; import { Player } from "../Unit/Player/Player"; import { UpgradeType } from "./UpgradeType"; export class Upgrader { private player: Player; private typeToAction: Map void> = new Map void>(); private typeToLevel: Map = new Map(); private typeToMaxLevel: Map = new Map(); public constructor(player: Player, settings: UpgradeSettings) { this.player = player; this.setTypeMaps(UpgradeType.WeaponLength, this.upgradeWeaponLength.bind(this), settings.maxWeaponLengthUpgrades); this.setTypeMaps(UpgradeType.WeaponDamage, this.upgradeWeaponDamage, settings.maxWeaponDamageUpgrades); } public upgradeSkill(type: UpgradeType): void { if (!this.typeToAction.has(type)) throw new Error("Upgrade does not have " + type); if (this.isMaxLevel(type)) throw new Error("Upgrade is already at max level " + type); this.typeToAction.get(type)(); const level: number = this.typeToLevel.get(type); this.typeToLevel.set(type, level + 1); } public getAvailableUpgrades(): Set { const availableUpgrades: Set = new Set(); for (const key of this.typeToAction.keys()) { if (!this.isMaxLevel(key)) { availableUpgrades.add(key); } } return availableUpgrades; } private setTypeMaps(upgradeType: UpgradeType, action: () => void, maxLevel: number): void { this.typeToAction.set(upgradeType, action); this.typeToLevel.set(upgradeType, 0); this.typeToMaxLevel.set(upgradeType, maxLevel); } private upgradeWeaponLength(): void { this.player.Weapon.upgradeWeaponLength(); } private upgradeWeaponDamage(): void { this.player.Weapon.upgradeWeaponDamage(); } private isMaxLevel(type: UpgradeType): boolean { return this.typeToMaxLevel.get(type) <= this.typeToLevel.get(type); } }