@kikosko

Как создать фильтр по прототипу?

В классе "Team" есть метод "kick", его суть состоит в том, чтобы случайно выбрать цель, если она жива, соответственно, нанести урон. Как сделать, что бы каждый персонаж (archer, mage, swordsman) сначала искал цель своего класса,(то есть archer "kick" archer, swordsman "kick" swordsman и тд) и если его свойство "isAlive" = "false", то попытался поразить случайную цель ? У меня получатся ошибка "TypeError: sameTypeUnit.__proto__ is not a function" при условии поиска типа.
//constructor for creating a unit
function Unit(maxHealth, basicDamage,type) {
    this.maxHealth = maxHealth;
    this.currentHealth = 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 */
Unit.prototype.isAlive = function () {
    return this.currentHealth > 0;
};
/* a method that
 shows the level of health*/
Unit.prototype.getFormattedHealth = function () {
    return this.currentHealth + "/" + this.maxHealth + " HP";
};
/*a method that returns the base damage of the hero and damage to the
 weapon (if it is set)*/
Unit.prototype.getDamage = function () {
    return this.basicDamage;
};
/* The method of hitting
 the hero for the chosen purpose*/
Unit.prototype.kick = function (target) {
    if (this.isAlive()) {
        target.currentHealth = Math.max(0,
            target.currentHealth - this.getDamage());
        console.log(this.type + " hit " + this.type);
    }
    return this;
};
/*method for showing all the characteristics of the hero and changes
 with them*/
Unit.prototype.toString = function () {
    return "Type - " + this.type + ", is alive - " +
        this.isAlive() + ", " + this.getFormattedHealth() +
        ', hero current damage - ' + this.getDamage() + ' points';
};
/*the constructors of the main types of units  which we will use*/
function Archer(maxHealth, basicDamage) {
    Unit.apply(this, arguments);
    this.type = "archer";
}
function Swordsman(maxHealth, basicDamage) {
    Unit.apply(this, arguments);
    this.type = "swordsman";
}
function Mage(maxHealth, basicDamage) {
    Unit.apply(this, arguments);
    this.type = "mage";
}
Archer.prototype = Object.create(Unit.prototype);
Swordsman.prototype = Object.create(Unit.prototype);
Mage.prototype = Object.create(Unit.prototype);
/*We create units of which we will then write to the teams.
 Three units per team*/
var archer = new Archer(60, 5);
var swordsman = new Swordsman(100, 10);
var mage = new Mage(40, 15);

var troll = new Archer(70, 5);
var orc = new Swordsman(150, 10);
var druid = new Mage(50, 15);

/*class for creating teams*/
function Team(name) {
    this.name = name;
    this.members = [];
    this.activeMember = 0;
}
/*method for adding a new unit with an arbitrary number of units*/
Team.prototype.addMember = function (...members) {
    this.members.push(...members);
}
/*method of life of the team, if all participants have
"currentHealth" <0 then this method = "false"*/
Team.prototype.isAlive = function () {
    return this.members.some(n => n.isAlive());
};
/*Damage method similar to that in "Unit" in it the choice
is made - who is attacking whom (the team members take turns
attacking the random participant of the other team)*/
Team.prototype.kick = function(targetTeam) {

    var sameTypeUnit = null;
    for(var i=0;i<this.members.length;i++) {
        sameTypeUnit = this.members[i];
    }
    if (sameTypeUnit.__proto__(sameTypeUnit) && sameTypeUnit.isAlive()) {
        while (sameTypeUnit.isAlive()) {
            sameTypeUnit.kick(sameTypeUnit);
        }
    }else if (!sameTypeUnit.isAlive()) {
        var m = targetTeam.members.filter(n => n.isAlive())
        target = m[Math.random() * m.length | 0];
        if (target && this.isAlive()) {
            var active = null;

            do  {
                active = this.members[this.activeMember];
                this.activeMember = (this.activeMember + 1)
                    % this.members.length;
            } while (!active.isAlive());

            active.kick(target);
        }
    }
};
/*method to output information about the team*/
Team.prototype.toString = function () {
    var res = "Name of team - " + this.name +  '\n'
        + "life of a team : " + this.isAlive() + '\n'
        +"members :\n";
    for (var i=0; i<this.members.length; i++)
        res += this.members[i]+"\n";
    return  res;
};
/*create team 1 and add units to it*/
var team1 =  new Team('Alliance');
team1.addMember(archer,swordsman,mage);
/*create team 2 and add units to it*/
var team2 = new Team('Orcs');
team2.addMember(troll,orc,druid);

/*class that organizes a battle between two teams until
 "currentHealth" of all units in the team will not be zero*/
function Game(team1, team2) {
    this.team1 = team1;
    this.team2 = team2;
}
/*the method in which the battle occurs until the
"isAlive" property of all participants of one of the commands
 is equal to "false"*/
Game.prototype.battle = function() {
    if (!this.team1.isAlive() || !this.team2.isAlive()) {
        if (this.team1.isAlive()) {
            alert("Team 1 is win");
        }
        if (this.team2.isAlive()) {
            alert("Team 2 is win");
        }
        console.log(`THE BATTLE IS END :
        ${this.team1.toString()}
        ${this.team2.toString()}
      ${this.team1.name} - ${this.team1.members.length} - 
${this.team1.members.map(n => n.currentHealth)}
      ${this.team2.name} - ${this.team2.members.length} -
${this.team2.members.map(n => n.currentHealth)}  
    `);
        return;
    }
    team1.kick(team2);
    team2.kick(team1);
    requestAnimationFrame(this.battle.bind(this));
};
var game = new Game(team1, team2);
game.battle();
  • Вопрос задан
  • 67 просмотров
Решения вопроса 1
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы