unity进阶学习笔记:物品与背包类

在本文中,我们要完成一个物品类用于访问Json格式储存的游戏物品,以及一个背包类用作物品集合,作为游戏内的物品背包。

1 示例物品json文件

[
    {
    
    
        "id": 1,
        "name": "sword",
        "des": "This is a sword" ,
        "price": 100,
        "damage": 10,
        "HP": 0
    },

    {
    
    
        "id": 2,
        "name": "drug",
        "des": "This is a drug",
        "price": 20,
        "damage": 0,
        "HP": 10
    }

]

在上面的json文件中,我们定义了两个对象,都包含变量id, name, des, price, damage, HP。我们在之后开发中也可以根据具体游戏内容设置变量,不过注意保证同一个数组中各对象的变量类型和名称要尽量保持一致,方便后续解析。

我们将这个文件放在Asset下面的Resources文件下

2 物品管理器

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;


public class Item {
    
    
    public int id;
    public string name;
    public string des;
    public int price;
    public int damage;
    public int HP;
}



public class ItemManager
{
    
    
    private static ItemManager instance;
    public static ItemManager Instance {
    
    
        get {
    
    
            if (instance == null) {
    
    
                instance = new ItemManager();
            }
            return instance;
        }
    }

    private Item[] items;

    public ItemManager() {
    
    
        // load json document
        TextAsset json = Resources.Load<TextAsset>("Item");
        items = JsonMapper.ToObject<Item[]>(json.text);
    }

    // find cooresponding items from id
    public Item GetItem(int id) {
    
    
        foreach (Item item in items) {
    
    
            if (item.id == id) {
    
    
                return item;
            }
        }
        return null;
    }
}

物品管理器并不作为unity组件出现,只是完成了一个读取物品的逻辑控制类,因此没有继承MonoBehaviour。我们要首先定义物品类Item,其变量和json文件中的定义保持一致。物品管理类采用单例模式

在构造方法中,ItemManager从Resources文件夹读取"Item"文件,并使用LitJson的JsonMapper.ToObject方法将json解析为Item数组

在GetItem中,我们按照指定id遍历Item[ ]中各个物品对象,如果判断id符合返回该物品

背包管理器

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class InventoryItem {
    
    
    public int itemID;
    public int quantity = 1;
}

public class InventoryManager
{
    
    
    public static InventoryManager instance;
    public static InventoryManager Instance {
    
    
        get {
    
    
            if (instance == null) {
    
    
                instance = new InventoryManager();
            }
            return instance;
        }
    }

    public List<InventoryItem> Inventory = new List<InventoryItem>();

    // add items
    public void AddItem(int itemID, int quantity = 1) {
    
    
        // whether the object exists in the inventory
        foreach (InventoryItem tmpItem in Inventory) {
    
    
            if (tmpItem.itemID == itemID) {
    
    
                tmpItem.quantity += quantity;
                return;
            }

            InventoryItem item = new InventoryItem();
            item.itemID = itemID;
            item.quantity = quantity;
            Inventory.Add(item);
        }
    }

    // get items
    public InventoryItem GetItem(int itemID) {
    
    
        foreach (InventoryItem item in Inventory) {
    
    
            if (item.itemID == itemID) {
    
    
                return item;
            }
        }
        return null;
    }

    // remove items
    public void RemoveItem(int itemID, int quantity = 1) {
    
    
        for (int i= 0; i < Inventory.Count; i++) {
    
    
            InventoryItem item = Inventory[i];
            if (item.itemID == itemID && item.quantity > 0) {
    
    
                item.quantity -= quantity;
                if (item.quantity <= 0) {
    
    
                    Inventory.Remove(item);  
                }
            }
        }
    }


    // empty the inventory
    public void RemoveAllItems() {
    
    
        Inventory.Clear();
    }
}

我们首先创建InventoryItem类用于保存物品id和数量,作为一个背包格子。

在InventoryManager类中,我们实现了增加物品(AddItem),获取物品(GetItem),删除物品(RemoveItem),清空背包(RemoveAllItems)的功能,实现较为简单,这里只简述大概原理:

AddItem:判断物品是否存在背包,如果存在直接增加物品数量,不存在则添加新物品

GetItem:遍历物品列表,返回和参数itemID的ID一样的物品,如不存在返回null

RemoveItem:判断物品是否在背包,如存在将物品数量减去quantity,减完后如quantity小于等于零删除该背包格子

RemoveAllItems:清空列表

猜你喜欢

转载自blog.csdn.net/Raine_Yang/article/details/130710658