RPG游戏黑暗之光Part4:物品模块

RPG游戏黑暗之光Part4:物品模块

 

(由于电脑出了点问题重装以后好多写好的文档没有了,所以说各位一定要好好备份啊!这里博主微(ku)笑(B)着回来填坑啦~

RPG游戏主要组成中道具的构成是很大一部分的内容,其中从属性分类包括药品,武器(上装,饰品,宝石等),药品等。

这里RPG游戏游戏人物自身需要有个背包系统,商店(武器,药品)也要有自己的商品系统,这里我们先分后和的来介绍。

 

1. 人物背包系统

 

 

不同类型物品的使用不同,这里给大家看下我的物品内容,简单的介绍一下

道具id,道具名称,道具在图集中的名称,属性,增加的hpmpspeedattackdefense,售价,回收价,简介(鼠标停留在道具ui显示的内容)

 

 

自定义一个类ObjectInfo包含这些内容,用另一个类读取txt文本保存到字典中,主要代码如下。

public class ObjectsText : MonoBehaviour {
    
public static ObjectsText _instance;
//这里的这一行就是unity默认txt的类,官方文档有介绍
    public TextAsset objectsText;
    public Dictionary<int, ObjectInfo> objectDictionary = new Dictionary<int, ObjectInfo>();
 
    private void Awake()
    {
        _instance = this;
        ReadText();
        ObjectInfo info = new ObjectInfo();
    }
    public ObjectInfo GetObjectById(int id)
    {
        ObjectInfo info = new ObjectInfo();
        objectDictionary.TryGetValue(id, out info);
        return info;
    }
    public void ReadText()
    {
        string text = objectsText.text;
        string[] objectsInfo = text.Split('\n');
        foreach(string x in objectsInfo)
        {
            string[] objectInfo = x.Split(';');
            ObjectInfo Info = new ObjectInfo();
            Info.id = int.Parse(objectInfo[0]);
            Info.obj_name = objectInfo[1];
            Info.icon_name = objectInfo[2];
            switch (objectInfo[3])
            {
                case "Drug":
                    Info.type = ObjectType.Drug;
                    break;
                case "Headgear":
                    Info.type = ObjectType.Headgear;
                    break;
                case "Armor":
                    Info.type = ObjectType.Armor;
                    break;
                case "Right_Hand":
                    Info.type = ObjectType.Right_Hand;
                    break;
                case "Left_Hand":
                    Info.type = ObjectType.Left_Hand;
                    break;
                case "Shoe":
                    Info.type = ObjectType.Shoe;
                    break;
                case "Accessory":
                    Info.type = ObjectType.Accessory;
                    break;
                case "Material":
                    Info.type = ObjectType.Material;
                    break;
            }
            Info.hp = int.Parse(objectInfo[4]);
            Info.mp = int.Parse(objectInfo[5]);
            Info.attack = int.Parse(objectInfo[6]);
            Info.defense = int.Parse(objectInfo[7]);
            Info.speed = int.Parse(objectInfo[8]);
            Info.buy_price = int.Parse(objectInfo[9]);
            Info.sell_price = int.Parse(objectInfo[10]);
            Info.obj_info = objectInfo[11];
            objectDictionary.Add(Info.id, Info);
        }
 
    }
}


在其他脚本中直接可以利用ObjectsText._instance.objectDictionary获取字典内容。

 

 

 

通过导入的ngui包可以轻易拖拽出一个ui背包界面,分为背包,背包框,背包框内物品,这里我们利用物品id的唯一性来设置背包框属性和物品属性,用以之后的背包内物品的增减。

 

这里注意ui控件 脚本中写onmouseover有问题,可以在视图窗添加eventtrigger来解决这一问题。

最上层背包系统的代码

public bool EarnGoods(int id)
    {
 
    ObjectInfo info = ObjectsText._instance.GetObjectById(id);
        print("生成id为:" + id);
        //增加要考虑全空的情况,有的情况,满的情况
        foreach (InventoryItemGird x in ItemGirds)
        {
            if (x.item.GetComponent<InventoryItem>().ItemCount == 0)
            {
                x.SetGird(info);
                return true;
            }
            else
            {
                if (x.id == id)
                {
                    x.SetGird(info);
                    return true;
                }
                else
                {
                    continue;
                }
            }
        }
        return false;
}


接下来的背包框的代码最为重要,不仅仅需要涉及到物品使用,物品拖拽,还要考虑使用情况。

public int id = 0;
    public GameObject item;
    public GameObject originalItem;
    private bool flag = false;
public GameObject preItem;
 
    public void Start()
    {
        originalItem = item;
    }
    public void SetGird(ObjectInfo info)
    {
        id = info.id;
        //这里不是一个初始化
        //说到底还是prefab这里初始化的问题
        //这里获取一次物品以后GetItemCount永远不为0,相当于修改prefab内容
 
        if (item.GetComponent<InventoryItem>().ItemCount == 0)
        {
            //这里增加了老的但是内容没有修改
            //一开始没有在等号左边加上item导致获取的内容不正确
            item = NGUITools.AddChild(gameObject, item);
        }
        item.GetComponent<InventoryItem>().AddItem(info);
    }
 
    public void MoveItem(GameObject surface)
    {
        ObjectInfo info = item.GetComponent<InventoryItem>().objectInfo;
        surface.GetComponent<InventoryItemGird>().SetGird(info);
        Destroy(item);
        item = originalItem;
        id = 0;
    }


 

这里的背包框的内容属性设置是通过上层的背包系统获取的。而其中最主要的设置就是对下一层物品的新建,这里默认在视图窗拖拽一个物品,但是并不在物品中显示,且id设置为0,该背包框有物品在的时候替换,其他时间为该物品。

物品模块如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class InventoryItem : UIDragDropItem
{
    private UILabel label;//
    private int itemCount = 0;
    private UISprite sprite;
    private Vector3 originalPos;//
    public GameObject gb;//放的是显示info的内容
    public ObjectInfo objectInfo;
    public string SpriteName
    {
        get { return sprite.spriteName; }
        set { sprite.spriteName = value; }
    }
    public int ItemCount
    {
        get { return itemCount; }
        set { itemCount = value; }
    }
    //这里还要修正 接下来是武器的修改
    public void UseItem()
    {
        GameObject player = GameObject.FindGameObjectWithTag("Player");
        if (objectInfo.type == ObjectType.Drug)
        {
            PlayerStatus status = player.GetComponent<PlayerStatus>();
            status.hp += objectInfo.hp;
            if (status.hp > status.hpMax)
                status.hp = status.hpMax;
            status.mp += objectInfo.mp;
            if (status.mp > status.mpMax)
                status.mp = status.mpMax;
            status.attack += objectInfo.attack;
            status.defense += objectInfo.defense;
            status.speed += objectInfo.speed;
            //如果药品为1destroy不然只是-1
            if (ItemCount == 1)
            {
GameObject item = GetComponentInParent<InventoryItemGird> ().preItem;
                Destroy(GetComponentInParent<InventoryItemGird>().item);
                GetComponentInParent<InventoryItemGird>().item = item;
                GetComponentInParent<InventoryItemGird>().id = 0;
            }
            else
            {
                ItemCount--;
                label.text = itemCount + "";
            }
        }
        else if(objectInfo.type == ObjectType.Material)
        {
 
        }
        else
        {
            string type = objectInfo.type.ToString();
            GameObject gb2 = GameObject.Find("UI Root").transform.Find("Equip").Find(type).gameObject;
            if (gb2.transform.childCount == 0)
            {
                GameObject object1 = NGUITools.AddChild(gb2, gameObject);
                object1.GetComponent<InventoryItem>().AddItem(objectInfo);
 
                //删除原来的道具Done
                if (ItemCount == 1)
                {
GameObject item = GetComponentInParent<InventoryItemGird> ().preItem;
Destroy(GetComponentInParent<InventoryItemGird>().item);
GetComponentInParent<InventoryItemGird>().item = item;
GetComponentInParent<InventoryItemGird>().id = 0;
                }
                else
                {
                    ItemCount--;
                    label.text = itemCount + "";
                }
            }
            else
            {
                ObjectInfo info = gb2.GetComponentInChildren<InventoryItem>().objectInfo;
                player.GetComponent<PlayerStatus>().hpMax -= info.hp;
                player.GetComponent<PlayerStatus>().mpMax -= info.mp;
                player.GetComponent<PlayerStatus>().attack -= info.attack;
                player.GetComponent<PlayerStatus>().defense -= info.defense;
                player.GetComponent<PlayerStatus>().speed -= info.speed;
                Destroy(gb2.GetComponentInChildren<InventoryItem>().gameObject);
                print(info.obj_info);
                GameObject object1 = NGUITools.AddChild(gb2, gameObject);
                object1.GetComponent<InventoryItem>().AddItem(objectInfo);
                if (ItemCount == 1)
                {
GameObject item = GetComponentInParent<InventoryItemGird> ().preItem;
Destroy(GetComponentInParent<InventoryItemGird>().item);
GetComponentInParent<InventoryItemGird>().item = item;
GetComponentInParent<InventoryItemGird>().id = 0;
                }
                else
                {
                    ItemCount--;
                    label.text = itemCount + "";
                }
                GameObject.Find("UI Root").transform.Find("Inventory").GetComponent<Inventory>().EarnGoods(info.id);
            }
            player.GetComponent<PlayerStatus>().hpMax += objectInfo.hp;
            player.GetComponent<PlayerStatus>().mpMax += objectInfo.mp;
            player.GetComponent<PlayerStatus>().attack += objectInfo.attack;
            player.GetComponent<PlayerStatus>().defense += objectInfo.defense;
            player.GetComponent<PlayerStatus>().speed += objectInfo.speed;
        }
    }
    protected override void OnDragDropRelease(GameObject surface)
    {
        gb.SetActive(false);
        base.OnDragDropRelease(surface);
        if (surface == null)
        {
            transform.position = originalPos;
        }
        else if (surface.tag == "InventoryItemGird")
        {
            if (surface.transform.childCount == 0)
            {
                GetComponentInParent<InventoryItemGird>().MoveItem(surface);
            }
            else
            {
                InventoryItem itm = surface.GetComponentInChildren<InventoryItem>();
                int count1 = itm.ItemCount;
                string str1 = itm.SpriteName;
                ObjectInfo info1 = itm.objectInfo;
                string text1 = itm.gb.GetComponentInChildren<UILabel>().text;
                itm.SetItem(ItemCount, SpriteName, objectInfo, gb.GetComponentInChildren<UILabel>().text);
                SetItem(count1, str1, info1, text1);
            }
        }
        else if (surface.tag == "InventoryItem" && surface.GetComponentInParent<InventoryItemGird>()!=null)
        {
            print(surface.tag);
            InventoryItem itm = surface.GetComponent<InventoryItem>();
            int count1 = itm.ItemCount;
            string str1 = itm.SpriteName;
            ObjectInfo info1 = itm.objectInfo;
            string text1 = itm.gb.GetComponentInChildren<UILabel>().text;
            itm.SetItem(ItemCount, SpriteName, objectInfo, gb.GetComponentInChildren<UILabel>().text);
            SetItem(count1, str1, info1, text1);
        }
        else
        {
            transform.position = originalPos;
        }
    }
    public void SetItem(int Count, string str, ObjectInfo info, string text)
    {
        itemCount = Count;
        sprite.spriteName = str;
        objectInfo = info;
        label.text = itemCount + "";
        GetComponentInParent<InventoryItemGird>().id = info.id;
        gb.GetComponentInChildren<UILabel>().text = text;
        transform.position = originalPos;
    }
    public void AddItem(ObjectInfo info)
    {
        objectInfo = info;
        label = GetComponentInChildren<UILabel>();
        itemCount++;
        label.text = itemCount + "";
        sprite = GetComponent<UISprite>();
        sprite.spriteName = info.icon_name;
        originalPos = transform.position;
    }
 
}


每个物品包含一个显示info的label子对象,还有拖拽以后不在背包框中返回原位置,其余位置有物品还要将两个物品替换等操作,当然其中涉及到的主要是该类继承自UIDragDropItem类,只有这样才可以实现ui的拖拽,在里面protected override void OnDragDropRelease(GameObject surface)这就是拖拽结束鼠标松开调用的代码,在其中的修改就是对物品的一系列操作。

 

这里本章节结束啦,你问我还有药品装备商店,用户自己的装备装备呢,少年郎这些东西都是举一反三的没必要照搬教条自己摸索出来的错了也没事这只会加深自己的印象,反正博主后来涉及的道具操作就不按照学习案例中照搬喽,相信自己你行的

 

猜你喜欢

转载自blog.csdn.net/icesony/article/details/78599551