背包的设计上主要采用的是mvc的设计模式
=============model层和control层===========
model层的思路主要是是定义装载背包的所有数据,和用control控制的。 主要也是增删改查了。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class InventoryModel {
//model层的物品 和控制层的东西
public int lineMax = 4;//行
public int rowMax = 5;//列
public List<ItemInventory> inventoryList = new List<ItemInventory>();
public void Swap(int index1,int index2) {
if (CheckNull(index1))
{
//判空
return;
}
if (CheckNull(index2))
{
//第二个格子是空的情况下
inventoryList[index2] = inventoryList[index1];
InitItemInventory(index1);
//inventoryList[index1].num = null;
}
else
{
ItemInventory item = inventoryList[index1];
inventoryList[index1] = inventoryList[index2];
inventoryList[index2] = item;
}
}
//将背包中的index格子 初始化
public void InitItemInventory(int index) {
//inventoryList.Insert(index,new ItemInventory());
inventoryList[index] = new ItemInventory();
}
//初始化整个背包
public void InitInventory() {
for (int i = 0; i < lineMax * rowMax;i++ )
{
inventoryList.Insert(i, new ItemInventory());
//InitItemInventory(i);
}
}
public bool CheckNull(int index) {
//背包中一项的数量 数量为0 的情况下判定格子为null
if (inventoryList[index].num == 0)
{
return true;
}
else
{
return false;
}
}
//查找list列表中是否有这个item
public int CheckItem(ItemInventory item)
{
for (int i = 0; i < lineMax * rowMax;i++ )
{
if(inventoryList[i].goodInfo.id == item.goodInfo.id){
//背包中存在这种物品的时候
return i;
}
}
return -1;//list中找不到物品的情况 返回-1
}
//背包上增加item
public void AddInventory(ItemInventory item) {
int index = CheckItem(item);
if (index &#