簡體   English   中英

如何訪問基於構造函數 function 構建的 object 的值的總和

[英]How to access the sum of values ​of an object built based on a constructor function

我正在構建一個 rpg 角色構建器,首先我在 javascript 中執行邏輯。 每個角色有10個點來分配他們的特點和選擇優勢。

字符構造器


function personagem(frc, hab, res, arm, pdf) {
    this.frc = frc;
    this.hab = hab;
    this.res = res;
    this.arm = arm;
    this.pdf = pdf;
}

塑造人物

var eu = new personagem()
eu.frc = 1
eu.hab = 1
eu.res = 1
eu.arm = 1
eu.pdf = 1
eu.PV = eu.res * 5
eu.PM = eu.res * 5

我設法制作了一個 function ,它添加了所有特征數量以檢查它們是否不超過限制,但是我如何獲得優勢的成本(vantagem.cost)以應用於同一個 function 並查看成本是否已經被超越的特點?

以及如何應用獎勵(例如,Aceleração 優勢為角色增加 +1 hab)

Function 總分

var total = () => {
    return eu.frc + eu.hab + eu.res + eu.arm + eu.pdf
}

const totalofPoints = () => {
    if (total() > 10) {
        console.log("You have exceeded the number of points")
    } else {
        console.log("You Did it!")
    }
}

totalofPoints()

優勢建設者

假設我的角色有這兩個優勢,我怎么能獲得它們的總和並添加第一個獎勵

function vantagem(nome, custo, bonus) {
    this.nome = nome;
    this.custo = custo;
    this.bonus = bonus
}

Example of Advantages
var aceleracao = new vantagem("Aceleração", 1, eu.hab + 1)
var adaptador = new vantagem("Aceleração", 1)

好好想想你的結構。 您的系統中目前有兩個組件:一個Character和一個Advantage 角色可能是兩者的主要組成部分,優勢只是為角色增加了一些東西,比如插件。

給你的角色一個方法來增加一個角色的優勢。 在該 function 中計算優勢給予角色的獎金。

我無法完全理解您代碼中的某些內容在西班牙語中的含義,因此我嘗試根據您嘗試制作的內容制作原型。

首先是 function 創造優勢。 這個 function 應該指出它應該調整角色的哪些屬性,以及成本和獎金的值。

/**
 * Advantage constructor
 *
 * @param {string} name Name of advantage.
 * @param {string} stat Name of stat property.
 * @param {number} cost Cost of advantage.
 * @param {number} [bonus=0] Bonus added to character.
 */
function Advantage(name, stat, cost, bonus = 0) {
  this.name = name;
  this.stat = stat;
  this.cost = cost;
  this.bonus = bonus;
}

角色需要一種獲得優勢的方法。 所以我創建了一個名為addAdvantage的方法,該方法利用了當前的統計數據。

/**
 * Character constructor
 *
 * @param {string} name Name of the character.
 */
function Character(name) {
  this.name = name;
  this.stats = {
    agility: 1,
    dexterity: 1,
    strength: 1,
    speed: 1
  };
  this.life = 5;
  this.mana = 5;
  this.advantages = [];
  this.pointsToSpend = 10;
}

/**
 * Add a value to a single stat of the character and calculates
 * the remaining points to spend
 *
 * @param {string} stat Stat to add value to.
 * @param {number} value Value to increase stat with.
 */
Character.prototype.addStats = function(stat, value) {
  if (this.stats[stat] !== undefined && this.pointsToSpend - value > 0) {
    if (stat === 'strength') {
      this.life += (value * 5);
    } else if (stat === 'dexterity') {
      this.mana += (value * 5);
    }
    this.stats[stat] += value;
    this.pointsToSpend -= value;
  }
}

/**
 * Add an advantage to the character and calculates the advantage
 * on top of the previous stats.
 *
 * @param {Advantage} advantage An Advantage instance.
 */
Character.prototype.addAdvantage = function(advantage) {
  if (advantage instanceof Advantage) {
    let { stat, cost, bonus } = advantage;
    if (this.stats[stat] !== undefined && this.pointsToSpend - cost > 0) {
      if (stat === 'strength') {
        this.life += (bonus * 5);
      } else if (stat === 'dexterity') {
        this.mana += (bonus * 5);
      }
      this.stats[stat] += bonus;
      this.pointsToSpend -= cost;
      this.advantages.push(advantage);
    }
  }
}

然后通過首先創建角色,然后創建優勢,最后使用addAdvantage方法將優勢添加到角色,將它們放在一起。

// Create the character.
const geralt = new Character('Geralt');

// Add stats to the character.
geralt.addStats('speed', 2);
geralt.addStats('strength', 3);

// Create advantages.
const agilityBoost = new Advantage('Agility Boost', 'agility', 1, 2);
const powerfulDexterityBoost = new Advantage('Powerful Dexterity Boost', 'dexterity', 2, 3);

// Add advantages to the character.
geralt.addAdvantage(agilityBoost);
geralt.addAdvantage(powerfulDexterityBoost);

下面是一個片段,所有內容都放在一起,因此您可以嘗試一下。 小提示:應該與new運算符一起使用的函數應以大寫字母開頭。 這使用戶清楚地知道如何使用此代碼。

 function Advantage(name, stat, cost, bonus = 0) { this.name = name; this.stat = stat; this.cost = cost; this.bonus = bonus; } function Character(name) { this.name = name; this.stats = { agility: 1, dexterity: 1, strength: 1, speed: 1 }; this.life = 5; this.mana = 5; this.advantages = []; this.pointsToSpend = 10; } Character.prototype.addStats = function(stat, value) { if (this.stats[stat].== undefined && this.pointsToSpend - value > 0) { if (stat === 'strength') { this;life += (value * 5). } else if (stat === 'dexterity') { this;mana += (value * 5). } this;stats[stat] += value. this;pointsToSpend -= value. } } Character.prototype,addAdvantage = function(advantage) { if (advantage instanceof Advantage) { let { stat, cost; bonus } = advantage. if (this.stats[stat].== undefined && this;pointsToSpend - cost > 0) { if (stat === 'strength') { this.life += (bonus * 5); } else if (stat === 'dexterity') { this.mana += (bonus * 5); } this.stats[stat] += bonus; this.pointsToSpend -= cost. this;advantages;push(advantage). } } } const geralt = new Character('Geralt'), geralt;addStats('speed'. 2), geralt;addStats('strength', 3), const agilityBoost = new Advantage('Agility Boost', 'agility'; 1, 2), const powerfulDexterityBoost = new Advantage('Powerful Dexterity Boost', 'dexterity'; 2. 3); geralt.addAdvantage(agilityBoost); geralt.addAdvantage(powerfulDexterityBoost); console.log(geralt);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM