如何使用class属性正确创建条件?

时间:2018-05-31 20:50:23

标签: javascript

如何在“Team”类中更改方法“kick”的逻辑。他在团队中选择了一个随机目标(如果她还活着)并对她造成伤害,所以两队之间的战斗(“team1”和“team2”在“Game”类中)发生。必须首先选择相同类型的目标,然后使用“踢”:“theam1”中的“archer”从“theam2”中“踢”“archer”然后返回。如果“弓箭手”死亡,那么偶然选择目标。

伪代码,我的意思是:

var simpletypeUnit = this.members.type[i];
if(simpletypeUnit.type == target.type && this.isAlive) {
  simpletypeUnit.kick(target)
}else {
 //random target selection 
}

//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);
    }
    console.log(this.type + " hit " + target.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 = [];
}
/*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());
};
/*The method of random choice of the target, if the hero is alive,
then the target is randomly chosen and damage is done using
the "kick" method*/
Team.prototype.selectRandomTarget = function (targetTeam) {
    var numberOfMembers = targetTeam.members.length;
    var target = null;
    while(target == null || !target.isAlive())
    {
        var randomIndex = Math.floor(Math.random() * numberOfMembers);
        target = targetTeam.members[randomIndex];
    }
    return target;
};
/*method of damage, we go through the array of  "members" and if the target
is alive, then we accidentally deal damage using the method "selectRandomTarget"*/
Team.prototype.kick = function(targetTeam) {
    console.log(`\nTeam ${this.name} is attacking`);

    for(var i=0; i < this.members.length; i++) {
        var singleMember = this.members[i];

        if(!this.isAlive()) break;
        if(!singleMember.isAlive()) continue;
        var target = this.selectRandomTarget(targetTeam);
        singleMember.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();

1 个答案:

答案 0 :(得分:1)

您可以对代码添加这两个小调整。如果我理解了这个问题,那就应该做你需要的了。

Unit.prototype.SelectSameType = function(targetTeam)
{ //Iterate through all the players and look for at least one of the same type that's alive
  for(let i = 0; i < targetTeam.members.length; i++)
  {
    let target = targetTeam.members[i];
    if(target.type === this.type && target.isAlive()) return target;
  }
  return null;
}
Team.prototype.kick = function(targetTeam) {
    console.log(`\nTeam ${this.name} is attacking`);

    for(var i=0; i < this.members.length; i++) {
        var singleMember = this.members[i];

        if(!this.isAlive() || !targetTeam.isAlive()) break;
        if(!singleMember.isAlive()) continue;

        //Check for the same type first
        var target = singleMember.SelectSameType(targetTeam)
        if(target === null) target = this.selectRandomTarget(targetTeam);

        singleMember.kick(target);

    }
};