我想用“踢”方法实现英雄逃避的逻辑。对于英雄,设置了一定数量的“逃避”属性,在此基础上,其逃避百分比取决于该属性,例如“逃避”弓箭手为0.6,即有60%的几率规避“反踢”。如何正确实施此逻辑以及消息的结果,显示损坏或逃避



function Unit(maxHealth, basicDamage, evasion, type) {

    this.maxHealth = maxHealth;
    this.currentHeaalth = maxHealth;
    this.basicDamage = basicDamage;
    this.evasion = evasion;
    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
    };
    /* a method that
     shows the level of health*/
    this.getFormattedHealth = function () {
        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;
    };
    /* The method of hitting
     the hero for the chosen purpose*/
    this.kick = function (target) {
            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() + ' points' +
            ", hero evasion - " + this.evasion;
    }
}
function Archer(maxHealth, basicDamage, evasion) {
    Unit.apply(this, arguments);
    this.type = "archer";
}
var archer = new Archer(60, 5, 0.6);
function Swordsman(maxHealth, basicDamage, evasion) {
    Unit.apply(this, arguments);
    this.type = "swordsman";
}
var swordsman = new Swordsman(100, 10, 0.3)
while (archer.isAlive() && swordsman.isAlive()) {
    archer.kick(swordsman);
    swordsman.kick(archer);
}
console.log(archer.toString());
console.log(swordsman.toString());

最佳答案

编写一个函数,该函数根据逃避情况用10位0和1填充数字,并返回随机的1或0:



let evasion = 0.4,
  probability = function() {
  var notRandomNumbers = [],
    maxEvasion = 0;
  if ((evasion + '').split('.')[0] == 1 && (evasion + '').split('.')[1] == 0) {
    maxEvasion = 10;
  } else {
    maxEvasion = (evasion + '').split('.')[1];
  }
  for (var i = 0; i < maxEvasion; i++) {
    notRandomNumbers.push(1);
  }
  for (var i = 0; i < 10 - maxEvasion; i++) {
    notRandomNumbers.push(0);
  }
  var idx = Math.floor(Math.random() * notRandomNumbers.length);
  return notRandomNumbers[idx];
}

console.log('1 )', probability());
console.log('2 )', probability());
console.log('3 )', probability());
console.log('4 )', probability());





kick功能中使用它。

this.kick = function(target) {
  if (this.isAlive()) {
    target.currentHeaalth = Math.max(
      0,
      target.currentHeaalth - this.probability() * this.getDamage()
    );
  }
};


完整的代码是这样的:



function Unit(maxHealth, basicDamage, evasion, type) {
  this.maxHealth = maxHealth;
  this.currentHeaalth = maxHealth;
  this.basicDamage = basicDamage;
  this.evasion = evasion;
  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;
  };
  /* a method that
		   shows the level of health*/
  this.getFormattedHealth = function() {
    return this.currentHeaalth + "/" + this.maxHealth + " HP";
  };

  this.probability = function() {
    var notRandomNumbers = [],
      maxEvasion = 0;
    if (
      (this.evasion + "").split(".")[0] == 1 &&
      (this.evasion + "").split(".")[1] == 0
    ) {
      maxEvasion = 10;
    } else {
      maxEvasion = (this.evasion + "").split(".")[1];
    }
    for (var i = 0; i < maxEvasion; i++) {
      notRandomNumbers.push(1);
    }
    for (var i = 0; i < 10 - maxEvasion; i++) {
      notRandomNumbers.push(0);
    }
    var idx = Math.floor(Math.random() * notRandomNumbers.length);
    return notRandomNumbers[idx];
  };

  /*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;
  };
  /* The method of hitting
		   the hero for the chosen purpose*/
  this.kick = function(target) {
    if (this.isAlive()) {
      target.currentHeaalth = Math.max(
        0,
        target.currentHeaalth - this.probability() * this.getDamage()
      );
    }
  };
  /*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() +
      " points" +
      ", hero evasion - " +
      this.evasion
    );
  };
}

function Archer(maxHealth, basicDamage, evasion) {
  Unit.apply(this, arguments);
  this.type = "archer";
}
var archer = new Archer(60, 5, 0.6);

function Swordsman(maxHealth, basicDamage, evasion) {
  Unit.apply(this, arguments);
  this.type = "swordsman";
}
var swordsman = new Swordsman(100, 10, 0.3);
while (archer.isAlive() && swordsman.isAlive()) {
  archer.kick(swordsman);
  swordsman.kick(archer);
}

console.log(archer.toString());
console.log(swordsman.toString());





希望这对您有所帮助。

09-28 02:49