基于Unity3D(UGUI)的背包系统<二>

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Say__Yes/article/details/72188613

这篇文章接着上一章的背包系统,上文主要介绍了这个背包系统的效果和总的大概。接下来的就是具体的实现了!!(实现嘛,当然少不了代码咯)


在这里我继续利用上一篇文章中的UML图,从UML图的左边部分讲述,也就是装备物品数据的获取和存储方面。好,先放上上文中的UML图,也方便大家查阅(具体介绍看上一篇文章哈)。
这里写图片描述

一:存储物品的Json文件(轻量级)。我把每一个物品的信息都存储在一个Json文件中,然后再从Json中解析出每一个物品的各种信息(补充:由于Json文件太长,我就只给出了大概三分之一的物品信息,其他物品类似):

[
    {
        "id": 1,
        "name": "经典血瓶",
        "type": "Consumable",
        "quality": "Common",
        "description": "这是用来加血的",
        "capacity": 20,
        "buyPrice": 10,
        "sellPrice": 5,
        "hp": 10,
        "mp": 0,
        "sprite": "Sprites/Items/hp"
    },
    {
        "id": 2,
        "name": "经典蓝瓶",
        "type": "Consumable",
        "quality": "Common",
        "description": "这是用来加蓝的",
        "capacity": 20,
        "buyPrice": 10,
        "sellPrice": 5,
        "hp": 0,
        "mp":10,
        "sprite": "Sprites/Items/mp"
    },
    {
        "id":3,
        "name": "胸甲",
        "type": "Equipment",
        "quality": "Rare",
        "description": "这是用来增加防御力之类的",
        "capacity": 20,
        "buyPrice": 20,
        "sellPrice": 10,
        "sprite": "Sprites/Items/armor",
        "strength":20,
        "intellect":10,
        "agility":10,
        "stamina": 10,
        "equipType":"Chest"
    },
    {
        "id":4,
        "name": "经典款工具背包",
        "type": "Equipment",
        "quality": "Epic",
        "description": "这个背包好沉啊,肯定值钱",
        "capacity": 20,
        "buyPrice": 25,
        "sellPrice": 15,
        "sprite": "Sprites/Items/bag",
        "strength":25,
        "intellect":20,
        "agility":10,
        "stamina": 5,
        "equipType":"Shoulder"
    },
    {
        "id":5,
        "name": "大力皮腰带",
        "type": "Equipment",
        "quality": "Uncommon",
        "description": "这个腰带真特么带劲",
        "capacity": 10,
        "buyPrice": 25,
        "sellPrice": 15,
        "sprite": "Sprites/Items/belts",
        "strength":25,
        "intellect":10,
        "agility":10,
        "stamina": 10,
        "equipType":"Belt"
    }
    ]
//如上篇文章所述,每一个物品都有自己的分类,共有四大类:消耗品类,装备类,材料类和武器类。每个物品都有共有的一些属性,ID,name,购买价格和售卖价格等。但是物品所属的分类不同就说明它有不同于其他物品的功能。这就是为何要把物品分类的原因所在。补充,如果有些单词不懂大家自己查哈,还有其中每个物品的Sprite属性是我存放它对应的图片精灵所在的目录,它是可变化的。

二:对应Json文件解析出来的数据的Item类。既然有了Json文件,先不说解析哈(后面说),那就先给对应的一个类,用来存储其解析出来的数据。PS:下面Item代码中有一个得到提示框(上篇文章中的那个黑框)的方法,是为了显示提示框的内容,会再后续处理UI的代码中调用,可先不用细看。

using UnityEngine;
using System.Collections;
/// <summary>
/// 物品基类
/// </summary>
public class Item {
    public int ID { get; set; }
    public string Name { get; set; }
    public ItemType Type { get; set; }
    public ItemQuality Quality { get; set; }
    public string Description { get; set; }
    public int Capacity { get; set; }//容量
    public int BuyPrice { get; set; }
    public int SellPrice { get; set; }
    public string Sprite { get; set; }//用于后期查找UI精灵路径

    public Item() 
    {
        this.ID = -1;//表示这是一个空的物品类
    }

    public Item(int id,string name,ItemType type,ItemQuality quality,string description,int capaticy,int buyPrice,int sellPrice,string sprite)
    {
        this.ID = id;
        this.Name = name;
        this.Type = type;
        this.Quality = quality;
        this.Description = description;
        this.Capacity = capaticy;
        this.BuyPrice = buyPrice;
        this.SellPrice = sellPrice;
        this.Sprite = sprite;
    }

    /// <summary>
    /// 物品类型
    /// </summary>
    public enum ItemType
    {
        Consumable,//消耗品
        Equipment,//装备
        Weapon,//武器
        Material //材料
    }
    /// <summary>
    /// 品质
    /// </summary>
    public enum ItemQuality
    {
        Common,//一般的
        Uncommon,//不寻常的
        Rare,//稀有的
        Epic,//史诗级的
        Legendary,//传奇的
        Artifact//手工的
    }

    //得到提示框应该显示的内容
    public virtual string GetToolTipText() 
    {
        string strItemType = "";
        switch (Type)
        {
            case ItemType.Consumable:
                strItemType = "消耗品";
                break;
            case ItemType.Equipment:
                strItemType = "装备";
                break;
            case ItemType.Weapon:
                strItemType = "武器";
                break;
            case ItemType.Material:
                strItemType = "材料";
                break;
        }

        string strItemQuality = "";
        switch (Quality)
        {
            case ItemQuality.Common:
                strItemQuality = "一般的";
                break;
            case ItemQuality.Uncommon:
                strItemQuality = "不寻常的";
                break;
            case ItemQuality.Rare:
                strItemQuality = "稀有的";
                break;
            case ItemQuality.Epic:
                strItemQuality = "史诗级的";
                break;
            case ItemQuality.Legendary:
                strItemQuality = "传奇的";
                break;
            case ItemQuality.Artifact:
                strItemQuality = "手工的";
                break;
        }

        string color = ""; //用于设置提示框各个不同内容的颜色
        switch (Quality)
        {
            case ItemQuality.Common:
                color = "white";//白色
                break;
            case ItemQuality.Uncommon:
                color = "lime";//绿黄色
                break;
            case ItemQuality.Rare:
                color = "navy";//深蓝色
                break;
            case ItemQuality.Epic:
                color = "magenta";//洋红色
                break;
            case ItemQuality.Legendary:
                color = "orange";//橙黄色
                break;
            case ItemQuality.Artifact:
                color = "red";//红色
                break;
        }
        string text = string.Format("<color={0}>{1}</color>\n<color=yellow><size=10>介绍:{2}</size></color>\n<color=red><size=12>容量:{3}</size></color>\n<color=green><size=12>物品类型:{4}</size></color>\n<color=blue><size=12>物品质量:{5}</size></color>\n<color=orange>购买价格$:{6}</color>\n<color=red>出售价格$:{7}</color>", color, Name, Description, Capacity, strItemType, strItemQuality, BuyPrice, SellPrice);
        return text;
    }
}

三:消耗品类(consumable)。一般是用来加血或者加蓝的。上面讲了,每一物品Item按照Type都有不同的分类,这是物品分类之一。它是继承自上面的Item类的,因为分类只是对每个Item(物品)做了更多属性的补充。下面这段代码中的两个重载方法同样和上面Item类的方法作用类似,在这里和下文就不在赘述了。

using UnityEngine;
using System.Collections;
/// <summary>
/// 消耗品类
/// </summary>
public class Consumable : Item{
    public int Hp { get; set; }
    public int Mp { get;set; }

    public Consumable(int id,string name,ItemType type,ItemQuality quality,string description,int capaticy,int buyPrice,int sellPrice,string sprite,int hp,int mp):base(id,name,type,quality,description,capaticy,buyPrice,sellPrice,sprite){
        this.Hp = hp;
        this.Mp = mp;
    }

    //对父方法Item.GetToolTipText()进行重写
    public override string GetToolTipText()
    {
        string text = base.GetToolTipText();//调用父类的GetToolTipText()方法
        string newText = string.Format("{0}\n<color=red>加血:{1}HP</color>\n<color=yellow>加魔法:{2}MP</color>", text, Hp, Mp);
        return newText;
    }
    public override string ToString()
    {
        string str = "";
        str += ID;
        str += Name;
        str += Type;
        str += Quality;
        str += Description;
        str += Capacity;
        str += BuyPrice;
        str += SellPrice;
        str += Sprite;
        str += Hp;
        str += Mp;
        return str;
    }
}

四:装备类(Equipment)。角色的服饰,能加一些体力,智力敏捷什么的。介绍和上面的消耗品类差不多,就直接上代码了哈。

using UnityEngine;
using System.Collections;
/// <summary>
/// 装备类
/// </summary>
public class Equipment : Item{
    public int Strength { get; set; }//力量
    public int Intellect { get; set; }//智力
    public int Agility { get; set; }//敏捷
    public int Stamina { get; set; }//体力
    public EquipmentType EquipType { get; set; }//装备类型

    public Equipment(int id, string name, ItemType type, ItemQuality quality, string description, int capaticy, int buyPrice, int sellPrice, string sprite,int strength, int intellect,int agility,int stamina,EquipmentType equipType) : base(id, name, type, quality, description, capaticy, buyPrice, sellPrice,sprite) 
    {
        this.Strength = strength;
        this.Intellect = intellect;
        this.Agility = agility;
        this.Stamina = stamina;
        this.EquipType = equipType;
    }

    public enum EquipmentType 
    {
        None,      //不能装备
        Head,      //头部
        Neck,      //脖子
        Ring,       //戒指
        Leg,        //腿部
        Chest,    //胸部
        Bracer,    //护腕
        Boots,     //鞋子
        Shoulder,//肩部
        Belt,       //腰带
        OffHand //副手
    }

    //对父方法Item.GetToolTipText()进行重写
    public override string GetToolTipText()
    {
        string strEquipType = "";
        switch (EquipType)
        {
            case EquipmentType.Head:
                strEquipType = "头部的";
                break;
            case EquipmentType.Neck:
                strEquipType = "脖子的";
                break;
            case EquipmentType.Ring:
                strEquipType = "戒指";
                break;
            case EquipmentType.Leg:
                strEquipType = "腿部的";
                break;
            case EquipmentType.Chest:
                strEquipType = "胸部的";
                break;
            case EquipmentType.Bracer:
                strEquipType = "护腕";
                break;
            case EquipmentType.Boots:
                strEquipType = "靴子";
                break;
            case EquipmentType.Shoulder:
                strEquipType = "肩部的";
                break;
            case EquipmentType.Belt:
                strEquipType = "腰带";
                break;
            case EquipmentType.OffHand:
                strEquipType = "副手装备";
                break;
        }

        string text = base.GetToolTipText();//调用父类的GetToolTipText()方法
        string newText = string.Format("{0}\n<color=green>力量:{1}</color>\n<color=yellow>智力:{2}</color>\n<color=white>敏捷:{3}</color>\n<color=blue>体力:{4}</color>\n<color=red>装备类型:{5}</color>", text, Strength, Intellect, Agility, Stamina, strEquipType);
        return newText;
    }
}

五:材料类(Material)。这个简单,只实现了父类的构造方法。没有特殊的属性加成,仅仅用来锻造其他装备用的。

using UnityEngine;
using System.Collections;
/// <summary>
/// 材料类,直接继承自Item基类即可
/// </summary>
public class Material : Item {
    public Material(int id, string name, ItemType type, ItemQuality quality, string description, int capaticy, int buyPrice, int sellPrice, string sprite) : base(id, name, type, quality, description, capaticy, buyPrice, sellPrice, sprite) { }
}

六:武器类(Weapon)。这个可以加伤害,不同的武器,伤害必然不同嘛。另外,武器也有主手武器和副手武器之分,也就是说明该武器放在玩家左手还是右手而已。

using UnityEngine;
using System.Collections;
/// <summary>
/// 武器类
/// </summary>
public class Weapon : Item{
    public int Damage { get; set; }//伤害
    public WeaponType WpType { get; set; }//武器类型

    public Weapon(int id, string name, ItemType type, ItemQuality quality, string description, int capaticy, int buyPrice, int sellPrice, string sprite, int damage, WeaponType wpType)
        : base(id, name, type, quality, description, capaticy, buyPrice, sellPrice,sprite)
    {
        this.Damage = damage;
        this.WpType = wpType;
    }
    /// <summary>
    /// 武器类型
    /// </summary>
    public enum WeaponType
    {
        Null,//没有武器
        OffHand,//副手武器
        MainHand//主手武器
    }

    //对父方法Item.GetToolTipText()进行重写
    public override string GetToolTipText()
    {
        string strWeaponType = "";
        switch (WpType)
        {
            case WeaponType.OffHand:
                strWeaponType = "副手武器";
                break;
            case WeaponType.MainHand:
                strWeaponType = "主手武器";
                break;
        }

        string text = base.GetToolTipText();//调用父类的GetToolTipText()方法
        string newText = string.Format("{0}\n<color=red>伤害值:{1}</color>\n<color=yellow>武器类型:{2}</color>", text, Damage, strWeaponType);
        return newText;
    }
}

好了,终于解决了装备物品的数据存储和分类方面了。下一篇就开始转向UI逻辑处理方面,以及UI和该篇中的数据两者之间的使用了。

猜你喜欢

转载自blog.csdn.net/Say__Yes/article/details/72188613