搭建框架
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d3a7044b333aacb4286fef636118cfec
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8a4ee71d02fa8cd40aceb7f62a6efd89
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,16 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 物品
|
||||
/// </summary>
|
||||
public class DataItem {
|
||||
/// <summary> 名字 </summary>
|
||||
public string name;
|
||||
/// <summary> 图片 </summary>
|
||||
public Sprite sprite;
|
||||
|
||||
/// <summary> 堆叠数量 </summary>
|
||||
public virtual int MaxStack => 999;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 427a33328910b764b9014f337267f950
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,76 @@
|
||||
using System.Linq;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 物品库存
|
||||
/// </summary>
|
||||
public class Inventory {
|
||||
/// <summary> 插槽列表 </summary>
|
||||
public List<InventorySlot> slots = new List<InventorySlot>();
|
||||
|
||||
/// <summary> 添加物品 </summary>
|
||||
public void AddItem(InventorySlot slot) {
|
||||
if (MergeToSlots(slot)) { return; }
|
||||
// 如果还有剩余,按照MaxStack分批放入空位
|
||||
while (slot.count > 0) { slot.count = AddToSlots(slot); }
|
||||
}
|
||||
/// <summary> 添加到插槽 </summary>
|
||||
public int AddToSlots(InventorySlot slot) {
|
||||
InventorySlot emptySlot = slots.FirstOrDefault(s => s.item == null);
|
||||
if (emptySlot == null) { Debug.Log("库存没有空位了!"); return 0; }
|
||||
// 填充插槽
|
||||
int addCount = Mathf.Min(slot.count, slot.MaxStack);
|
||||
emptySlot.Settings(slot.item, addCount);
|
||||
// 计算余量
|
||||
return slot.count - addCount;
|
||||
}
|
||||
/// <summary> 合并到插槽 </summary>
|
||||
public bool MergeToSlots(InventorySlot slot) {
|
||||
int remain = slot.count;
|
||||
// 查询所有可合并的插槽
|
||||
var sameSlots = slots.Where(slot => slot.Merge(slot)).ToList();
|
||||
// 合并到已有插槽
|
||||
sameSlots.ForEach(obj => MergeToSlots(obj, ref remain));
|
||||
slot.count = remain;
|
||||
return remain == 0;
|
||||
}
|
||||
/// <summary> 合并到插槽 </summary>
|
||||
public void MergeToSlots(InventorySlot slot, ref int remain) {
|
||||
int canAdd = slot.MaxStack - slot.count;
|
||||
int add = Mathf.Min(canAdd, remain);
|
||||
slot.count += add;
|
||||
remain -= add;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 库存插槽
|
||||
/// </summary>
|
||||
public class InventorySlot {
|
||||
/// <summary> 数量 </summary>
|
||||
public int count;
|
||||
/// <summary> 物品 </summary>
|
||||
public DataItem item;
|
||||
|
||||
/// <summary> 堆叠数量 </summary>
|
||||
public virtual int MaxStack => item != null ? item.MaxStack : 0;
|
||||
|
||||
public InventorySlot() { }
|
||||
|
||||
public InventorySlot(DataItem item, int count) {
|
||||
this.item = item;
|
||||
this.count = count;
|
||||
}
|
||||
public void Settings(DataItem item, int count) {
|
||||
this.item = item;
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
/// <summary> 是否允许合并 </summary>
|
||||
public bool Merge(InventorySlot slot) {
|
||||
if (item == null || slot == null || slot.item == null) { return false; }
|
||||
if (count >= item.MaxStack) { return false; }
|
||||
return slot.item.name == item.name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 299fd3a7463d8ef46ab2221eca3eeae7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9871787cca5a9e54fab450c7d3674d69
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,18 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 物品 - 常量
|
||||
/// </summary>
|
||||
[CreateAssetMenu(menuName = "MuHua/物品系统/物品")]
|
||||
public class ConstItem : ScriptableObject {
|
||||
/// <summary> 图片 </summary>
|
||||
public Sprite sprite;
|
||||
|
||||
/// <summary> 数据转换 </summary>
|
||||
public DataItem To() {
|
||||
DataItem item = new DataItem { name = name, sprite = sprite };
|
||||
return item;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 66da6092ac51f6a47a0c3d896f845816
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user