内容纲要

重写了背包管理类和装备管理类,创建了一个管理基类所有关于物品的管理类都继承自此方法

public class Mgr //所有管理类的基类
{
    public virtual void AddItem(Item item)
    {

    }
    public virtual void LoadData(GameObject itemPre ,int BagSize)
    {

    }
    public virtual void LoadData(GameObject slotPre, GameObject itemPre, int bagSize)
    {

    }
    public virtual int GetListCount()
    {
        return 0;
    }
    public virtual Item GetItem( int id)
    {
        return null;
    }

    public virtual void DelateItem(int id)
    {

    }

重构了现有的背包管理类和装备管理类

1. 装备管理类

public class EquipmentMgr :Mgr{

    public static EquipmentMgr instance = new EquipmentMgr();

    public List<Item> Equipments = new List<Item>();

    public EquipmentMgr()
    {

    }

    public override  Item GetItem(int id)
    {
        return Equipments[id];   
    }

    public override void LoadData( GameObject itemPre, int bagSize)
    {

        Transform menueTransform = GameObject.FindGameObjectWithTag(Tags.MENUE).transform;
        Transform EquipmentTransform = menueTransform.Find("Equipment");
        //加载槽位
        for (int i = 0; i < EquipmentTransform.childCount; i++)
        {
            Transform child = EquipmentTransform.GetChild(i);
            child.GetComponent<SlotSwitch>().Index = i;
            child.GetComponent<SlotSwitch>().type = SlotTypes.EQUIPMENT;

        }

        //加载物品
        for (int i = 0; i < Equipments.Count; i++)
        {
            ItemInfoMgr.EquipmentInfo info = ItemInfoMgr.instance.GetItemInfo(Equipments[i].typeId) as ItemInfoMgr.EquipmentInfo;
            Transform slot = EquipmentTransform.Find(info.slot);
            GameObject item = GameObject.Instantiate(itemPre);
            item.transform.SetParent(slot);
            item.transform.localScale = Vector3.one;
            item.GetComponent<ItemView>().id = Equipments[i].id;
            item.GetComponent<Toggle>().group = EquipmentTransform.GetComponent<ToggleGroup>();
            item.GetComponent<ItemView>().Show();

        }

    }

    public override void AddItem(Item item)
    {
        Equipments.Add(item);
        item.id = Equipments.Count - 1;

        ItemInfoMgr.EquipmentInfo iteminfo = (ItemInfoMgr.EquipmentInfo)ItemInfoMgr.instance.GetItemInfo(item.typeId);
        PlayerProperty.instance.CurMoveSpeed = (1 + (float)iteminfo.speedAdd/100)*PlayerProperty.instance.DefaultMoveSpeed;
        PlayerProperty.instance.Atk += iteminfo.atk;
        PlayerProperty.instance.Defence += iteminfo.defence;

    }

    public override void DelateItem(int id)
    {
        ItemInfoMgr.EquipmentInfo iteminfo = (ItemInfoMgr.EquipmentInfo)ItemInfoMgr.instance.GetItemInfo(Equipments[id].typeId);
        PlayerProperty.instance.CurMoveSpeed -= (float)iteminfo.speedAdd / 100 * PlayerProperty.instance.DefaultMoveSpeed;
        PlayerProperty.instance.Atk -= iteminfo.atk;
        PlayerProperty.instance.Defence -= iteminfo.defence;

        Equipments.RemoveAt(id);
    }
    public override int GetListCount()
    {
        return Equipments.Count;
    }
}

在涉及到背包更新的时候对玩家属性做检测,所以在添加物品方法和删除物品方法中做了判断。

2. 背包管理类

public class BagMgr : Mgr{

    public static BagMgr instance = new BagMgr();

    public BagMgr()
    {

        AddItem(new Item( 0,1,0,SlotTypes.BAG));
        AddItem(new Item( 1,1,1, SlotTypes.BAG));
        AddItem(new Item( 2,1,2, SlotTypes.BAG));
        AddItem(new Item( 3,1,3, SlotTypes.BAG));
        AddItem(new Item( 4,1,5, SlotTypes.BAG));
        AddItem(new Item( 3, 3, 5, SlotTypes.BAG));

    }

    public int BagSize;

    public List<Item> Items = new List<Item>();
    public override void  LoadData(GameObject slotPre, GameObject itemPre,int bagSize)
    {
        this.BagSize = bagSize;
        Transform menueTransform = GameObject.FindGameObjectWithTag(Tags.MENUE).transform;
        Transform bagTransform = menueTransform.Find("Bag");

        //加载槽位
        for (int i = 0; i < BagSize; i++)
        {
            GameObject slot = GameObject.Instantiate(slotPre);
            slot.GetComponent<SlotSwitch>().Index = i;
            slot.GetComponent<SlotSwitch>().type = SlotTypes.BAG;
            slot.transform.SetParent(bagTransform);
            slot.transform.localScale = Vector3.one;

        }
        //加载物品
        for (int i = 0; i < Items.Count; i++)
        {

            GameObject item = GameObject.Instantiate(itemPre);
            item.transform.SetParent(bagTransform.GetChild(Items[i].slotIndex));
            item.transform.localScale = Vector3.one;
            item.GetComponent<ItemView>().id = Items[i].id;
            item.GetComponent<ItemView>().SlotType = SlotTypes.BAG;
            item.GetComponent<Toggle>().group = bagTransform.GetComponent<ToggleGroup>();
            item.GetComponent<ItemView>().Show();

        }

    }

    public override Item GetItem(int id)
    {
        return Items[id];

    }
    /// <summary>
    /// 玩家获得物品
    /// </summary>
    /// <param name="item"></param>
    public override void AddItem(Item item)
    {
      Item  target =   Items.Find(find => {

          return (find.typeId == item.typeId 
          && ItemInfoMgr.instance.GetItemInfo(item.typeId).canMultiple);
      });
        if (target != null)
        {
            target.num+= item.num;
        }
        else
        {
            Items.Add(item);
            item.id = Items.Count - 1;

        }

    }
    /// <summary>
    /// 玩家使用或丢弃物品
    /// </summary>
    /// <param name="item"></param>
    public override void DelateItem(int id)
    {

        Items.RemoveAt(id);

    }

    public override int GetListCount()
    {
        return Items.Count;
    }

}

1 对 “190622”的想法;

  1. Pingback: buy viagra

发表评论