畢業設計的坑--unity遊戲關於揹包的設計(3)
阿新 • • 發佈:2019-02-15
揹包的設計上主要採用的是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 == -1) { //揹包上不存在這個物品 int nullItemIndex = FindNull(); if (nullItemIndex == -1) { //揹包滿了的情況下 } else { //揹包沒滿 inventoryList[nullItemIndex] = item; } } else { //揹包上有這個物品的情況下 向index處疊加物品的量 inventoryList[index].num += item.num; } } //查詢揹包裡面空的格子 public int FindNull() { for (int i = 0; i < lineMax * rowMax; i++) { if (inventoryList[i].num == 0) { //找到這個格子的物品是空的 return i; } } return -1;//揹包滿了的情況返回-1 } //刪除index格子上的東西 public void Delete(int index) { InitItemInventory(index); } } //揹包中每一個格子 public class ItemInventory { public GoodInfo goodInfo;//這個揹包的物品 public int num;//揹包中一項的數量 數量為0 的情況下判定格子為null public ItemInventory(){ goodInfo = new GoodInfo(); num = 0; } public ItemInventory(GoodInfo goodInfo, int num) { this.goodInfo = goodInfo; this.num = num; } }
=====================view層通過改變揹包的資料生成視覺化====================
======================其中的生成的每個子項可以拖動====================using UnityEngine; using System.Collections; using System.Collections.Generic; public class InventoryView : MonoBehaviour { public static InventoryView _indstance; public GameObject grid;//揹包上的一個格子 public InventoryModel inventory; public List<GameObject> inventortGrids = new List<GameObject>();//儲存生成的格子 private float width; private float height; void Start () { _indstance = this; inventory = new InventoryModel(); //test Debug.Log("start"); inventory.InitInventory();//初始化 //ItemInventory item1 = new ItemInventory(GoodsInfo._instance.getInfoById(1000), 3); //ItemInventory item2 = new ItemInventory(GoodsInfo._instance.getInfoById(1000), 5); //ItemInventory item3 = new ItemInventory(GoodsInfo._instance.getInfoById(2002), 5); //inventory.AddInventory(item1); //inventory.AddInventory(item2); //inventory.AddInventory(item3); for (int i = 2011; i <= 2011 + 10; i++) { inventory.AddInventory(new ItemInventory(GoodsInfo._instance.getInfoById(i), 5)); } //inventory.Swap(0, 1); width = grid.GetComponent<UISprite>().localSize.x; height = grid.GetComponent<UISprite>().localSize.y; InitInventoryView(); } //根據model類生成相關的格子 形成揹包 public void InitInventoryView() { int lineMax = inventory.lineMax;//行 int rowMax = inventory.rowMax;//列 int k = 0; for (int i = 0; i < lineMax;i++ ) { for (int j = 0; j < rowMax;j++ ) { //設定格子位置的初始座標 GameObject gObjGrid = Instantiate(grid, new Vector3(0,0,0), Quaternion.identity) as GameObject; gObjGrid.transform.SetParent(this.transform); gObjGrid.transform.localScale = new Vector3(1, 1, 1); gObjGrid.transform.localPosition = new Vector3(j * width, -i * height, 0); gObjGrid.GetComponent<InventoryVireGrid>().gridId = k;//設定格子的index gObjGrid.transform.Find("InventroyItemGoods").GetComponent<InventoryViewItem>().itemId = k;//設定物品的id gObjGrid.transform.Find("InventroyItemGoods").gameObject.SetActive(false); //update揹包UI ItemInventory itemInventory = inventory.inventoryList[k];//當前格子對應的model層的物品 if (itemInventory.num != 0) { ShowItem(gObjGrid.transform, itemInventory); } inventortGrids.Insert(k, gObjGrid); k++; } } } // 重新重新整理揹包顯示(物品位置發生變化時) public void ShowItems() { int lineMax = inventory.lineMax;//行 int rowMax = inventory.rowMax;//列 //for (int i = 0; i < lineMax; i++) //{ // for (int j = 0; j < rowMax; j++) // { // ItemInventory itemInventory = inventory.inventoryList[i * lineMax + j];//當前格子對應的model層的物品 // if (itemInventory.num != 0) // { // //inventortGrids[i * lineMax + j].transform.GetComponent<InventoryViewItem>().itemId = i * lineMax + j; // ShowItem(inventortGrids[i * lineMax + j].transform, itemInventory); // } // } //} for (int i = 0; i < lineMax * rowMax;i++ ) { ItemInventory itemInventory = inventory.inventoryList[i];//當前格子對應的model層的物品 if (itemInventory.num != 0) { ShowItem(inventortGrids[i].transform, itemInventory); } else { UnShowItem(inventortGrids[i].transform); } } } // 顯示物品格子 private void ShowItem(Transform itemGrid, ItemInventory itemInventory) { itemGrid.Find("InventroyItemGoods").gameObject.SetActive(true); itemGrid.Find("InventroyItemGoods/LabelNum").GetComponent<UILabel>().text = itemInventory.num+""; itemGrid.Find("InventroyItemGoods").GetComponent<UISprite>().spriteName = itemInventory.goodInfo.altesName; } private void UnShowItem(Transform itemGrid) { itemGrid.Find("InventroyItemGoods").gameObject.SetActive(false); } public void AddItem() { ItemInventory item3 = new ItemInventory(GoodsInfo._instance.getInfoById(2001), 5); inventory.AddInventory(item3); ShowItems(); } public void AddItem(int infoId) { ItemInventory item3 = new ItemInventory(GoodsInfo._instance.getInfoById(infoId), 1); inventory.AddInventory(item3); ShowItems(); } }
using UnityEngine; using System.Collections; public class InventoryViewItem : UIDragDropItem { public int itemId;//當前格子物品所在的格子的id void Start () { base.Start(); } protected override void OnDragDropStart() { base.OnDragDropStart(); } protected override void OnDragDropRelease(GameObject surface) { base.OnDragDropRelease(surface); if (surface != null) { if (surface.tag == Tags.backPackGoods) { //Debug.Log("碰到有物品的格子"); //碰到有物品的格子 進行格子交換 InventoryView._indstance.inventory.Swap(itemId, surface.GetComponent<InventoryViewItem>().itemId);//model層的改變 //全域性更新揹包ui InventoryView._indstance.ShowItems(); } else if (surface.tag == Tags.backPackItem) { //拖到空的格子上 Debug.Log("拖到空的格子上了" + surface.GetComponent<InventoryVireGrid>().gridId); InventoryView._indstance.inventory.Swap(itemId, surface.GetComponent<InventoryVireGrid>().gridId);//model層的改變 InventoryView._indstance.ShowItems(); } else if (surface.tag == Tags.equip || surface.tag == Tags.equipGoods) { //拖到沒有裝備的空格子上 //InventoryView._indstance.inventory.Equals(); NewEquipSystemView.equipView.equipModel.Equip(InventoryView._indstance.inventory.inventoryList[this.itemId].goodInfo); InventoryView._indstance.inventory.Delete(this.itemId); NewEquipSystemView.equipView.updateUi(); InventoryView._indstance.ShowItems(); } //else if(surface.tag == Tags.equipGoods){ // //拖到有裝備的格子上 //} } this.transform.localPosition = Vector3.zero; } public void OnMouseOver() { //Debug.Log("mouse 在上面"); BackPackItemGoodsDes._instance.ShowLabel(InventoryView._indstance.inventory.inventoryList[itemId].goodInfo); } public void OnMouseHoverOut() { //Debug.Log("mouse 離開了"); BackPackItemGoodsDes._instance.HideLabel(); } }
這些東西都寫的很粗糙。。。。。。 還不如來看看我發的原始碼讓你執行下,來的舒服.......