Unity3D自学笔记——UGUI背包系统(六)角色换装及属性动态改变

  • Post author:
  • Post category:其他



角色换装及属性动态改变

同样利用了Attribute设计,其实角色这里已经很简单了

效果图

白属性

这里写图片描述

装备剑, 攻击力+10

这里写图片描述

装备匕首,攻击力+8

这里写图片描述

装备三级匕首,攻击加24

这里写图片描述

同样角色属性的改变是通过之前写的PlayerStatus进行赋值

大致示意图

这里写图片描述

思路

通过装备按钮,更改PlayerStatus的装备属性,并重新计算角色总属性,然后触发PlayerStatus的换装事件,通知角色属性面板刷新。

弹出框修改

同样,界面绑定都从ItemStaus取值,在装备按钮事件中首先改变PlayerStatus的对应属性,这里是Weapon

public class UIItemToolTip : UIScene {
    private Image m_Icon;
    private Text m_NameText;
    private Text m_LvText;
    private Text m_TypeText;
    private Text m_DescriptionText;
    private Text m_AttributeText;
    private Text m_BtnDressOnText;
    private Text m_BtnDropText;
    private Text m_RedPriceText;
    private Text m_BluePriceText;

    private UISceneWidget m_BtnClose;
    private UISceneWidget m_BtnDressOn;
    private UISceneWidget m_BtnDrop;
    private ItemStatus m_ItemStatus;
    private PlayerStatus m_PlayerStatus;

    void Awake()
    {
        this.m_Icon = UIHelper.FindChild<Image>(transform, "imgIcon/Image");
        this.m_NameText = UIHelper.FindChild<Text>(transform, "txtName");
        this.m_LvText = UIHelper.FindChild<Text>(transform, "txtLv");
        this.m_TypeText = UIHelper.FindChild<Text>(transform, "txtType");
        this.m_DescriptionText = UIHelper.FindChild<Text>(transform, "txtDescription");
        this.m_AttributeText = UIHelper.FindChild<Text>(transform, "imgAttribute/txtAttribute");
        this.m_BtnDressOnText = UIHelper.FindChild<Text>(transform, "btnDressOn/Text");
        this.m_BtnDropText = UIHelper.FindChild<Text>(transform, "btnDrop/Text");
        this.m_BluePriceText = UIHelper.FindChild<Text>(transform, "txtBluePrice");
        this.m_RedPriceText = UIHelper.FindChild<Text>(transform, "txtRedPrice");
        GameObject player = GameObject.FindGameObjectWithTag("Player");
        this.m_PlayerStatus = player.GetComponent<PlayerStatus>();
    }

    protected override void Start()
    {
        base.Start();
        m_BtnClose = this.GetWidget("btnClose");
        if(m_BtnClose != null)
            m_BtnClose.PointerClick += BtnClosePointerClick;
        m_BtnDressOn = this.GetWidget("btnDressOn");
        if(m_BtnDressOn != null)
            m_BtnDressOn.PointerClick += BtnDressOnPointerClick;
        m_BtnDrop = this.GetWidget("btnDrop");
        if(m_BtnDrop != null)
            m_BtnDrop.PointerClick += BtnDropPointerClick;
    }

    public void Show(ItemStatus itemStatus, bool isLeft)
    {
        this.SetVisible(true);
        this.m_ItemStatus = itemStatus;
        this.m_Icon.overrideSprite = Resources.Load<Sprite>(m_ItemStatus.IconPath);
        this.m_NameText.text = m_ItemStatus.ItemName;
        this.m_DescriptionText.text = m_ItemStatus.Description;
        this.m_LvText.text = string.Format("等级: {0}", m_ItemStatus.Lv);
        this.m_TypeText.text = m_ItemStatus.ItemTypeString;
        this.m_AttributeText.text = m_ItemStatus.AttributeString;
        this.m_BtnDressOnText.text = GetButtonDressonText(m_ItemStatus.ItemType);
        this.m_BtnDropText.text = "丢弃";
        GetPriceText(m_ItemStatus.Price);
        Vector3 pos = transform.localPosition;

        if (isLeft)
        {
            transform.localPosition = new Vector3(-Mathf.Abs(pos.x), pos.y, pos.z);
        }
        else
        {
            transform.localPosition = new Vector3(Mathf.Abs(pos.x), pos.y, pos.z);
        }
    }

    private void BtnClosePointerClick(PointerEventData obj)
    {
        this.SetVisible(false);
    }

    private void BtnDropPointerClick(PointerEventData obj)
    {

    }
//换装按钮,给PlayerStatus进行对应属性赋值
    private void BtnDressOnPointerClick(PointerEventData obj)
    {
        switch (m_ItemStatus.ItemType)
        {
            case ItemType.Weapon:
                this.m_PlayerStatus.Weapon = this.m_ItemStatus;
                break;
            case ItemType.Armor:
                this.m_PlayerStatus.Armor = this.m_ItemStatus;
                break;
            case ItemType.Necklace:
                this.m_PlayerStatus.Necklace = this.m_ItemStatus;
                break;
            case ItemType.Ring:
                this.m_PlayerStatus.Ring = this.m_ItemStatus;
                break;
        }
        this.SetVisible(false);
    }

    private string GetButtonDressonText(ItemType itemType)
    {
        string result = string.Empty;
        switch (itemType)
        {
            case ItemType.Armor:
            case ItemType.Weapon:
            case ItemType.Ring:
            case ItemType.Necklace:
                result = "装备";
                break;
            case ItemType.Potion:
                result = "使用";
                break;
        }
        return result;
    }

    private void GetPriceText(int price)
    {
        int sellPrice = (int)(price * 0.8); //售价打八折
        m_RedPriceText.text = (sellPrice / 100).ToString();
        m_BluePriceText.text = (sellPrice % 100).ToString();
    }
}

PlayerStatus修改

增加装备的AttributeNode,由外部更换,增加换装事件

public class PlayerStatus : MonoBehaviour {
    private UserEntity m_User;
    private ItemStatus m_Weapon;
    private ItemStatus m_Armor;
    private ItemStatus m_Necklace;
    private ItemStatus m_Ring;
    public double m_MultipleExp = 1.18;
    public int m_StartExp = 40;
    public string PlayerName { get; set; }
    public int Lv { get; set; }
    public int LvMax { get; set; }
    public double Exp { get; set; }
    public double ExpMax { get; set;}
    public double HpRegenTime { get; set; }
    public double MpRegenTime { get; set; }
    public ItemStatus Weapon
    {
        get
        {
            return this.m_Weapon;
        }
        set
        {
            //卸下当前装备
            if (this.m_Weapon != null)
                statusEquip.RemoveNode(this.m_Weapon.status);

            //穿上新装备
            this.m_Weapon = value;
            //更新装备数据
            InitStatusEquip();
            //更新角色数据
            status.Calculate();
            //触发换装事件
            if(ChangedEquip != null)
            {
                ChangedEquip();
            }
        }
    }
    public ItemStatus Armor { get; set; }
    public ItemStatus Necklace { get; set; }
    public ItemStatus Ring { get; set; }

    private AttributeNode statusGrowth = new AttributeNode();
    private AttributeNode statusEquip = new AttributeNode();
    private AttributeNode statusBuff = new AttributeNode();
    public AttributeRoot status = new AttributeRoot();

    public event Action ChangedEquip;

    void Start()
    {

    }
    public void Load(UserEntity user)
    {
        this.m_User = user;
        this.Lv = this.m_User.Lv;
        this.PlayerName = this.m_User.Name;
        this.Exp = this.m_User.Exp;
        this.ExpMax = m_StartExp * m_MultipleExp * this.Lv;
        this.LvMax = this.m_User.Hero.MaxLv;
        this.HpRegenTime = this.m_User.HpRegenTime;
        this.MpRegenTime = this.m_User.MpRegenTime;

        InitAllStatus();
    }
    private void InitAllStatus()
    {
        InitStatusGrowth();
        InitStatusEquip();
        InitStatus(true);
    }

    private void InitStatusGrowth()
    {
        this.statusGrowth.Hp = this.Lv * this.m_User.Hero.HpGrowth;
        this.statusGrowth.Mp = this.Lv * this.m_User.Hero.MpGrowth;
        this.statusGrowth.Atk = this.Lv * this.m_User.Hero.AtkGrowth;
        this.statusGrowth.Def = this.Lv * this.m_User.Hero.DefGrowth;
        this.statusGrowth.Spd = this.Lv * this.m_User.Hero.SpdGrowth;
        this.statusGrowth.Hit = this.Lv * this.m_User.Hero.HitGrowth;
        this.statusGrowth.CriticalRate = this.Lv * this.m_User.Hero.CriticalRateGrowth;
        this.statusGrowth.AtkSpd = this.Lv * this.m_User.Hero.AtkSpdGrowth;
        this.statusGrowth.AtkRange = this.Lv * this.m_User.Hero.AtkRangeGrowth;
        this.statusGrowth.MoveSpd = this.Lv * this.m_User.Hero.MoveSpdGrowth;
    }

//装备的AttributeNode
    private void InitStatusEquip()
    {
        if(this.m_Weapon != null)
            this.statusEquip.AddNode(this.m_Weapon.status);
    }
    private void InitStatus(bool isInit)
    {
        this.status.Hp = this.m_User.Hero.Hp;
        this.status.Mp = this.m_User.Hero.Mp;
        this.status.Atk = this.m_User.Hero.Atk;
        this.status.Def = this.m_User.Hero.Def;
        this.status.Spd = this.m_User.Hero.Spd;
        this.status.Hit = this.m_User.Hero.Hit;
        this.status.CriticalRate = this.m_User.Hero.CriticalRate;
        this.status.AtkSpd = this.m_User.Hero.AtkSpd;
        this.status.AtkRange = this.m_User.Hero.AtkRange;
        this.status.MoveSpd = this.m_User.Hero.MoveSpd;

        this.status.AddNode(this.statusGrowth);
        this.status.AddNode(this.statusEquip);
        this.status.AddNode(this.statusBuff);
        this.status.Calculate(isInit);
    }
}

角色属性面板

监听换装事件,更新数据

public class UIPlayerDetail : UIScene {
    private Text m_TxtLv;
    private Text m_TxtName;
    private Text m_TxtHp;
    private Text m_TxtMp;
    private Text m_TxtAtk;
    private Text m_TxtDef;
    private Text m_TxtAtkSpd;
    private Text m_TxtAtkRange;
    private Text m_TxtCriticalRate;
    private Text m_TxtMoveSpd;
    private GameObject m_Weapon;
    private GameObject m_EquipItemPrefab;
    private PlayerStatus m_PlayerStatus;
    protected override void Start()
    {
        base.Start();
        InitWidget();
    }

    private void InitWidget()
    {
        this.m_TxtLv = UIHelper.FindChild<Text>(transform, "imgTitle/txtLv");
        this.m_TxtName = UIHelper.FindChild<Text>(transform, "imgTitle/txtName");
        this.m_TxtHp = UIHelper.FindChild<Text>(transform, "imgLeft/pnlAttribute/pnlHp/txtValue");
        this.m_TxtMp = UIHelper.FindChild<Text>(transform, "imgLeft/pnlAttribute/pnlMp/txtValue");
        this.m_TxtAtk = UIHelper.FindChild<Text>(transform, "imgLeft/pnlAttribute/pnlAtk/txtValue");
        this.m_TxtDef = UIHelper.FindChild<Text>(transform, "imgLeft/pnlAttribute/pnlDef/txtValue");
        this.m_TxtAtkSpd = UIHelper.FindChild<Text>(transform, "imgLeft/pnlAttribute/pnlAtkSpd/txtValue");
        this.m_TxtAtkRange = UIHelper.FindChild<Text>(transform, "imgLeft/pnlAttribute/pnlAtkRange/txtValue");
        this.m_TxtCriticalRate = UIHelper.FindChild<Text>(transform, "imgLeft/pnlAttribute/pnlCriticalRate/txtValue");
        this.m_TxtMoveSpd = UIHelper.FindChild<Text>(transform, "imgLeft/pnlAttribute/pnlMoveSpd/txtValue");
        this.m_EquipItemPrefab = (GameObject)Resources.Load("Inventory/EquipItem");
        this.m_Weapon = UIHelper.FindChild(transform, "imgLeft/pnlEquip/imgWeapon");
        GameObject player = GameObject.FindGameObjectWithTag("Player");
        this.m_PlayerStatus = player.GetComponent<PlayerStatus>();
        this.m_PlayerStatus.ChangedEquip += PlayerStatusChangedEquip;
        InitData();
    }

    private void InitData()
    {
        this.m_TxtLv.text = string.Format(".{0}", this.m_PlayerStatus.Lv.ToString());
        this.m_TxtName.text = this.m_PlayerStatus.PlayerName;
        this.m_TxtHp.text = this.m_PlayerStatus.status.HpMax.ToString();
        this.m_TxtMp.text = this.m_PlayerStatus.status.MpMax.ToString();
        this.m_TxtAtk.text = this.m_PlayerStatus.status.AtkMax.ToString();
        this.m_TxtDef.text = this.m_PlayerStatus.status.DefMax.ToString();
        this.m_TxtAtkSpd.text = this.m_PlayerStatus.status.AtkSpdMax.ToString();
        this.m_TxtAtkRange.text = this.m_PlayerStatus.status.AtkRangeMax.ToString();
        this.m_TxtCriticalRate.text = this.m_PlayerStatus.status.CriticalRateMax.ToString();
        this.m_TxtMoveSpd.text = this.m_PlayerStatus.status.MoveSpdMax.ToString();
        if (this.m_PlayerStatus.Weapon != null)
        {
            AddEquip(m_Weapon);
        }
    }

    private void AddEquip(GameObject equipGrid)
    {
        if (equipGrid.transform.childCount > 0)
        {
            Image equipImg = equipGrid.transform.GetChild(0).GetComponent<Image>();
            equipImg.overrideSprite = m_PlayerStatus.Weapon.Icon;
        }
        else
        {
            GameObject equipGo = (GameObject)GameObject.Instantiate(this.m_EquipItemPrefab);
            UIEquipItem equipItem = equipGo.GetComponent<UIEquipItem>();
            equipItem.SetEquip(m_PlayerStatus.Weapon);
            equipGo.transform.SetParent(equipGrid.transform);
            equipGo.transform.localPosition = Vector3.zero;
            equipGo.transform.localScale = Vector3.one;
        }
    }

//监听换装事件,刷新数据
    private void PlayerStatusChangedEquip()
    {
        InitData();
    }
}



版权声明:本文为alistair_chow原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。