Untitled
import getRandomInt from '../utils'; import Energy from '../Energy'; import Archetype, { Mage } from '../Archetypes'; import Fighter, { SimpleFighter } from '../Fighter'; import Race, { Elf } from '../Races'; export default class Character implements Fighter { private _name: string; private _race: Race; private _archetype: Archetype; private _maxLifePoints: number; private _lifePoints: number; private _strength: number; private _defense: number; private _dexterity: number; private _energy: Energy; constructor(name: string) { this._name = name; this._dexterity = getRandomInt(1, 10); this._strength = getRandomInt(1, 10); this._defense = getRandomInt(1, 10); this._race = new Elf(name, this._dexterity); this._archetype = new Mage(name); this._maxLifePoints = this._race.maxLifePoints / 2; this._lifePoints = this._maxLifePoints; this._energy = { type_: this._archetype.energyType, amount: getRandomInt(1, 10), }; } get race(): Race { return this._race; } get archetype(): Archetype { return this._archetype; } get lifePoints(): number { return this._lifePoints; } get strength(): number { return this._strength; } get defense(): number { return this._defense; } get dexterity(): number { return this._dexterity; } get energy(): Energy { return { ...this._energy }; } receiveDamage(attackPoints: number): number { const damage = attackPoints - this._defense; this._lifePoints -= damage > 0 ? damage : 1; if (this._lifePoints <= 0) { this._lifePoints = -1; } return this._lifePoints; } attack(enemy: Fighter | SimpleFighter): void { enemy.receiveDamage(this._strength); } levelUp(): void { const increment = getRandomInt(1, 10); this._strength += increment; this._dexterity += increment; this._defense += increment; this._energy.amount = 10; this._maxLifePoints += increment; if (this._maxLifePoints > this._race.maxLifePoints) { this._maxLifePoints = this._race.maxLifePoints; } this._lifePoints = this._maxLifePoints; }
Leave a Comment