1
0
Fork 0
spacetac/src/core/ShipLevel.ts

120 lines
2.9 KiB
TypeScript
Raw Permalink Normal View History

2017-09-24 22:23:22 +00:00
module TK.SpaceTac {
2017-03-17 00:07:00 +00:00
/**
* Level and experience system for a ship, with enabled upgrades.
2017-03-17 00:07:00 +00:00
*/
export class ShipLevel {
private level = 1
private experience = 0
private upgrades: string[] = []
2017-03-17 00:07:00 +00:00
/**
* Get current level
*/
get(): number {
return this.level;
}
2017-05-09 17:19:26 +00:00
/**
* Get the current experience points
*/
getExperience(): number {
return this.experience;
}
/**
* Get the activated upgrades
*/
getUpgrades(): string[] {
return acopy(this.upgrades);
}
2017-03-17 00:07:00 +00:00
/**
* Get the next experience goal to reach, to gain one level
*/
getNextGoal(): number {
return isum(imap(irange(this.level), i => (i + 1) * 100));
}
/**
* Force experience gain, to reach a given level
*/
forceLevel(level: number): void {
2017-03-17 00:07:00 +00:00
while (this.level < level) {
this.forceLevelUp();
}
}
/**
* Force a level up
*/
forceLevelUp(): void {
let old_level = this.level;
this.addExperience(this.getNextGoal() - this.experience);
this.checkLevelUp();
if (old_level >= this.level) {
// security against infinite loops
throw new Error("No effective level up");
2017-03-17 00:07:00 +00:00
}
}
/**
* Check for level-up
*
* Returns true if level changed
*/
checkLevelUp(): boolean {
let changed = false;
while (this.experience >= this.getNextGoal()) {
2017-03-17 00:07:00 +00:00
this.level++;
changed = true;
}
return changed;
}
/**
* Add experience points
*/
addExperience(points: number): void {
2017-03-17 00:07:00 +00:00
this.experience += points;
}
/**
* Get upgrade points given by current level
*
* This does not deduce activated upgrades usage
2017-03-17 00:07:00 +00:00
*/
getUpgradePoints(): number {
2018-03-26 15:30:43 +00:00
return this.level > 1 ? (1 + 2 * (this.level - 1)) : 0;
2017-03-17 00:07:00 +00:00
}
/**
* (De)Activate an upgrade
*
* This does not check the upgrade points needed
*/
2018-03-06 14:39:48 +00:00
activateUpgrade(upgrade: ShipUpgrade, active: boolean): void {
if (active) {
add(this.upgrades, upgrade.code);
} else {
remove(this.upgrades, upgrade.code);
}
}
/**
* Check if an upgrade is active
*/
2018-03-06 14:39:48 +00:00
hasUpgrade(upgrade: ShipUpgrade): boolean {
return contains(this.upgrades, upgrade.code);
}
/**
* Clear all activated upgrades
*/
clearUpgrades(): void {
this.upgrades = [];
}
2017-03-17 00:07:00 +00:00
}
}