Я хочу реализовать метод "heal" (target) в классе "Mage". Когда я реализую его в цикле, он не работает. В симуляции битвы лучник мечника и мечника лучника, волшебник должен восстанавливает здоровье лучника.
function Unit(maxHealth, basicDamage, type) {
this._maxHealth = maxHealth;
this._currentHeaalth = maxHealth
this._basicDamage = basicDamage;
this._type = type;
/*method for showing the status of life, true if the "health" is greater
than 0 and false if equal to or lower */
this._isAlive = function() {
return this._currentHeaalth > 0
};
this._getFormattedHealth = function() {
/* a method that
shows the level of health*/
shows the level of health
return this._currentHeaalth + '/' + this._maxHealth + ' HP';
};
/*a method that returns the base damage of the heroand damage to the
weapon (if it is set)*/
this._getDamage = function() {
return (this.weapon ? this.weapon._getDamage() : 0)
this._basicDamage;
};
this._kick = function(target) {
/* The method of hitting
the hero for the chosen purpose*/
if (this._isAlive()) {
target._currentHeaalth = Math.max(0, target._currentHeaalth -
this._getDamage());
}
return this;
};
/*method for showing all the characteristics of the hero and changes
with them*/
this.toString = function() {
return "Type - " + this._type + ", is alive - " + this._isAlive() +
this._getFormattedHealth() + ', hero current damage - ' +
this._getDamage();
}
}
}
function Archer(maxHealth, basicDamage) {
Unit.apply(this, arguments);
this._type = "archer";
}
var archer = new Archer(50, 5);
console.log(archer.toString());
function Swordsman(maxHealth, basicDamage) {
Unit.apply(this, arguments);
this._type = "swordsman";
}
var swordsman = new Swordsman(100, 10);
function Mage(maxHealth, basicDamage) {
Unit.apply(this, arguments);
this._type = "mage";
this.heal = function(target) { /*a method of treatment that works when the
archer's health falls below his maximum health*/
if (this._currentHeaalth < this._maxHealth) {
target._currentHeaalth = Math.max(this._maxHealth,
target._currentHeaalth + 50);
}
return this;
}
}
var mage = new Mage(40, 15);
// cycle until someone dies
while (archer._isAlive() && swordsman._isAlive()) {
archer._kick(swordsman);
mage.heal(archer);
swordsman._kick(archer);
}
console.log(archer.toString());
console.log(swordsman.toString());