Unity可扩展、灵活的战斗框架实现2 数值和属性

文章讨论了如何在复杂游戏中升级属性管理,从单一的基础类型到UnityGAS中的Attribute和AttributeValue设计,提出了一种更灵活的属性计算方式,包括基础属性、衍生属性和加成机制的改进,以支持动态变化和多英雄配置。

在一些简单的游戏中,属性可以直接使用基础类型来定义,比如 int hp=100,float speed=50等等,但当战斗的复杂性提升,加入buff、装备等变量后,属性要根据buff的增加、删除等进行频繁的改变,又假如有些计算过程需要对基础值或附加值的百分比进行修正,那么简单的数值--属性系统就难以实现这些功能。

在dota2中,属性我们可以区分为基础属性(力量,敏捷,智力),以及由基础属性计算得到的属性(最大生命值,最大法力值,生命恢复,法力恢复,攻击力,攻击速度等等)

在UNITY GAS 的示例中,作者的设计是分为两个类Attribute和AttributeValue

AttributeValue是最基本的数值,里面四个字段,Attribute存储自己所属的属性的引用,Modifier用于添加临时的修正(如装备、buff等)

public struct AttributeValue
    {
        public AttributeScriptableObject Attribute;
        public float BaseValue;
        public float CurrentValue;
        public AttributeModifier Modifier;
    }

    [Serializable]
    public struct AttributeModifier
    {
        public float Add;
        public float Multiply;
        public float Override;
        public AttributeModifier Combine(AttributeModifier other)
        {
            other.Add += Add;
            other.Multiply += Multiply;
            other.Override = Override;
            return other;
        }
    }

AttributeScriptableObject

属性的基类,继承ScriptableObject,用于生成属性配置的实例存储。

 public class AttributeScriptableObject : ScriptableObject
    {
        /// <summary>
        /// Friendly name of this attribute.  Used for dislpay purposes only.
        /// </summary>
        public string Name;

        public event EventHandler PreAttributeChange;

        public void OnPreAttributeChange(object sender, PreAttributeChangeEventArgs e)
        {
            EventHandler handler = PreAttributeChange;
            PreAttributeChange?.Invoke(sender, e);
        }

        public virtual AttributeValue CalculateInitialValue(AttributeValue attributeValue, List<AttributeValue> otherAttributeValues)
        {
            return attributeValue;
        }

        public virtual AttributeValue CalculateCurrentAttributeValue(AttributeValue attributeValue, List<AttributeValue> o
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值