毕业设计的坑--unity游戏关于背包的设计(3)

本文探讨了毕业设计中Unity游戏的背包系统设计,采用MVC模式进行实现。模型层专注于数据存储与操作,包括背包物品的增删改查功能,而控制层负责协调各部分交互。
摘要由CSDN通过智能技术生成

背包的设计上主要采用的是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
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值