有没有办法让我的对象 "update" 成为基于公式的变量?

Is there a way to make my objects "update" their formula based variables?

我有一种在我的游戏中创建 "spirit" 对象的方法。我创建了精神 class 的一个实例,然后为我想要制作的任何精神调用一个方法。示例:

public void risp(){
species = "Risp";
eletype = "plant";
baseattack=4; basespeed=4; baseevasion=5; basearmor=2; basemagres=3; basehealth = 13+2*strength; basemana = 13+intelligence;
attack = (int) (baseattack+.2*4*strength+strength); speed = (int) (basespeed+4*.2*dexterity); health = 13+2*strength; mana = 13+intelligence; 

armor = (int) (basearmor+2*.2*strength); armit = 0; magres = (int) (basemagres+.2*3*intelligence); mrmit = 0; evasion = (int) (dexterity+.25*5*baseevasion); cooperation = 12; accuracy = 10+dexterity;           
fire = 5; water = 35; air = 35; ice = 25; mud = 40; rock = 20; toxic = 25; energy = 10; dark = 15; death = 15; light = 20; plant = 0; psychic = 5;
ability1 = "prick"; ability2 = "photosynethesize"; ability3 = "regrowth"; ability4 = "None Equipped";
spiritimage = new Image(risptexture);
spiritimage2 = new Image(risptexture);
spiritxp = 0; spiritlvl = 1;

}

问题是,在我的菜鸟中,我为一些值设置了公式,期望变量会在使用对象时更新。这似乎不是这种情况,考虑一下,它只是使用初始化的值是有道理的。

所以,当我升级我的 "risp" 并使其基础攻击更高时,比如 10 - 它不在乎。 Risp 仍然有它的初始攻击值,结果是 6,当我想要它应该是 12.

我创建的代表 risp 的对象存储在一个包含我的 "spirit characters".

的数组列表中

我不知道如何更新基于攻击、速度、生命值等公式的变量

我知道我可以编写代码

if(spirit.species = "Risp") { 
attack = (int) (baseattack+.2*4*strength+strength); 

以及其他变量,然后在我更改 risps 的基值时调用它。但那将是.. 这么多打字。我有很多精神。我想尽量避免这种情况。有更简单的方法吗?

如果您在 Java 工作,您总是可以让不同的角色具有不同的 类,并让 getter 为当前统计数据执行计算。

例如:

public class Risp {
    private int baseattack, basespeed; // ... and so on
    // also have a constructor that sets all base stats

    public void setStrength(int strength) {
        this.strength = strength;
    }

    public String getSpecies() {
        return "Risp";
    }
    public int getAttack() {
        return (int) (baseattack+.2*4*strength+strength);
    }
    // remaining setters & getters omitted
}

然后当您需要使用这些值时:

Risp risp = new Risp();
risp.getAttack(); // Returns attack with base stats
risp.setStrength(120);
risp.getAttack(); // Returns attack with base stats, but w/ strength 120