管理结构中变量的更简单方法

时间:2017-03-01 14:49:18

标签: c# unity3d

我仍然对Unity中的编程很陌生,我目前正在尝试使用基于拼写的平台游戏。

我的问题如下: 我有一个包含6个浮点数的结构(这个数字会随着我实现新的法术比例而增加)而且我需要为这个结构中的每个浮点数添加另一个浮点数。我只有6个元素,我通过添加每个浮点数手动完成它,但是当我打算扩展这个结构时,我需要一个更好的解决方案然后这个粗暴无用的实现。

  

此外,下面的代码显示了一个咒语的示例,我是   同时工作两个,所以有两倍的线   这么简单的代码

            //Adding proporties to controller
        manager.GetComponent<Controller> ().firstSpell.damage += adder.damage;
        manager.GetComponent<Controller> ().firstSpell.force += adder.force;
        manager.GetComponent<Controller> ().firstSpell.cooldown += adder.cooldown;
        manager.GetComponent<Controller> ().firstSpell.damageOverTime += adder.damageOverTime;
        manager.GetComponent<Controller> ().firstSpell.size += adder.size;
        manager.GetComponent<Controller> ().firstSpell.gravity += adder.gravity;

        manager.GetComponent<Controller> ().firstSpell.damage = Mathf.Clamp (manager.GetComponent<Controller> ().firstSpell.damage, Spells.damageMin, Spells.damageMax);
        manager.GetComponent<Controller> ().firstSpell.force = Mathf.Clamp (manager.GetComponent<Controller> ().firstSpell.force, Spells.forceMin, Spells.forceMax);
        manager.GetComponent<Controller> ().firstSpell.cooldown = Mathf.Clamp (manager.GetComponent<Controller> ().firstSpell.cooldown, Spells.cooldownMin, Spells.cooldownMax);
        manager.GetComponent<Controller> ().firstSpell.damageOverTime = Mathf.Clamp (manager.GetComponent<Controller> ().firstSpell.damageOverTime, Spells.damageOverTimeMin, Spells.damageOverTimeMax);
        manager.GetComponent<Controller> ().firstSpell.size = Mathf.Clamp (manager.GetComponent<Controller> ().firstSpell.size, Spells.sizeMin, Spells.sizeMax);
        manager.GetComponent<Controller> ().firstSpell.gravity = Mathf.Clamp (manager.GetComponent<Controller> ().firstSpell.gravity, Spells.gravityMin, Spells.gravityMax);

正如您在上面的代码中所看到的,我也必须将这些值钳制起来。有没有一种简单的方法来添加两个相同类型的结构变量(并且还在路上限制它们的值)?

修改 这就是我改变它的方式,现在有一个公共类:

public class spell{
    public float damage;
    public float force;
    public float cooldown;
    public float damageOverTime;
    public float size;
    public float gravity;

    public void addValues(spell other){
        damage += other.damage;
        force += other.force;
        cooldown += other.cooldown;
        damageOverTime += other.damageOverTime;
        size += other.size;
        gravity += other.gravity;
    }

    public void clampValues(){
        damage = Mathf.Clamp(damage, Boundaries.damageMin, Boundaries.damageMax);
        force = Mathf.Clamp(force , Boundaries.forceMin, Boundaries.forceMax);
        cooldown = Mathf.Clamp(cooldown, Boundaries.cooldownMin, Boundaries.cooldownMax);
        damageOverTime = Mathf.Clamp(damageOverTime, Boundaries.damageOverTimeMin, Boundaries.damageOverTimeMax);
        size = Mathf.Clamp(size, Boundaries.sizeMin, Boundaries.sizeMax);
        gravity = Mathf.Clamp(gravity, Boundaries.gravityMin, Boundaries.gravityMax);
    }
}

这就是我用它做的事情:

Controller managerController = manager.GetComponent<Controller> ();
managerController.firstSpell.addValues (adder);
managerController.firstSpell.clampValues ();

0 个答案:

没有答案