From 3d54e764bcdc4369e1bb29d41b3e0b2c18f667ca Mon Sep 17 00:00:00 2001 From: NTG_Lenovo Date: Mon, 21 Jul 2025 19:51:11 +0900 Subject: [PATCH] =?UTF-8?q?DDD-64=20=EC=95=84=EC=9D=B4=ED=85=9C=20?= =?UTF-8?q?=EC=9D=B8=EB=B2=A4=ED=86=A0=EB=A6=AC=20=EC=8B=9C=EC=8A=A4?= =?UTF-8?q?=ED=85=9C=20=EA=B8=B0=EB=B3=B8=20=EA=B5=AC=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../_Scripts/GameData/InventoryItemData.cs | 14 +++ .../_Scripts/GameData/InventoryManager.cs | 97 +++++++++++++++++++ Assets/_DDD/_Scripts/GameEvent/GameEvents.cs | 5 + .../GenerateGoogleSheet/Core/DataSo.cs | 2 + 4 files changed, 118 insertions(+) create mode 100644 Assets/_DDD/_Scripts/GameData/InventoryItemData.cs create mode 100644 Assets/_DDD/_Scripts/GameData/InventoryManager.cs diff --git a/Assets/_DDD/_Scripts/GameData/InventoryItemData.cs b/Assets/_DDD/_Scripts/GameData/InventoryItemData.cs new file mode 100644 index 000000000..2f73efa4b --- /dev/null +++ b/Assets/_DDD/_Scripts/GameData/InventoryItemData.cs @@ -0,0 +1,14 @@ +namespace DDD +{ + public class InventoryItemData : IId + { + public string Id { get; set; } + public int Quantity { get; set; } + + public InventoryItemData(string id, int quantity) + { + Id = id; + Quantity = quantity; + } + } +} \ No newline at end of file diff --git a/Assets/_DDD/_Scripts/GameData/InventoryManager.cs b/Assets/_DDD/_Scripts/GameData/InventoryManager.cs new file mode 100644 index 000000000..6f4c83014 --- /dev/null +++ b/Assets/_DDD/_Scripts/GameData/InventoryManager.cs @@ -0,0 +1,97 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using UnityEngine; + +namespace DDD +{ + public class InventoryManager : Singleton, IManager + { + private Dictionary _itemDataLookup; + private Dictionary _inventoryItemDatas; + + public void PreInit() + { + + } + + public Task Init() + { + return Task.CompletedTask; + } + + public void PostInit() + { + InitializeItemData(); + } + + private void InitializeItemData() + { + var itemDataSo = DataManager.Instance.ItemDataSo; + + Debug.Assert(itemDataSo != null, "itemDataSo != null"); + + _itemDataLookup = itemDataSo.GetDataList() + .Where(item => !string.IsNullOrEmpty(item.Id)) + .ToDictionary(item => item.Id, item => item); + + _inventoryItemDatas = new Dictionary(itemDataSo.GetDataCount()); + } + + public bool AddItem(string id, int quantity = 1) + { + if (!_itemDataLookup.ContainsKey(id)) + { + Debug.LogError($"[Inventory] 등록되지 않은 아이템 ID: {id}"); + return false; + } + + if (_inventoryItemDatas.TryGetValue(id, out var itemData)) + { + itemData.Quantity += quantity; + } + else + { + _inventoryItemDatas[id] = new InventoryItemData(id, quantity); + } + + EventBus.Broadcast(new InventoryChangedEvent()); + return true; + } + + public bool RemoveItem(string id, int quantity = 1) + { + if (!_inventoryItemDatas.TryGetValue(id, out var itemData)) + { + Debug.LogError($"[Inventory] 등록되지 않은 아이템 ID: {id}"); + return false; + } + + if (itemData.Quantity < quantity) + { + Debug.LogWarning($"[Inventory] 보유 수량보다 삭제하는 수량이 더 많습니다 " + + $"{id}, 보유 수량 : {itemData.Quantity}, 삭제 수량 : {quantity}"); + return false; + } + + itemData.Quantity -= quantity; + + if (itemData.Quantity <= 0) + { + _inventoryItemDatas.Remove(id); + } + + EventBus.Broadcast(new InventoryChangedEvent()); + return true; + } + + public IReadOnlyDictionary InventoryItems => _inventoryItemDatas; + public bool TryGetItemData(string id, out ItemData itemData) => _itemDataLookup.TryGetValue(id, out itemData); + public int GetItemCount(string id) => _inventoryItemDatas.TryGetValue(id, out var itemData) ? itemData.Quantity : 0; + public ItemData GetItemDataByIdOrNull(string id) + { + _itemDataLookup.TryGetValue(id, out var itemData); + return itemData; + } + } +} \ No newline at end of file diff --git a/Assets/_DDD/_Scripts/GameEvent/GameEvents.cs b/Assets/_DDD/_Scripts/GameEvent/GameEvents.cs index 9f26685c4..5e838add3 100644 --- a/Assets/_DDD/_Scripts/GameEvent/GameEvents.cs +++ b/Assets/_DDD/_Scripts/GameEvent/GameEvents.cs @@ -20,4 +20,9 @@ public class InteractionEvent : IEvent public GameObject Causer; public GameObject Target; } + + public class InventoryChangedEvent : IEvent + { + + } } diff --git a/Assets/_DDD/_Scripts/GenerateGoogleSheet/Core/DataSo.cs b/Assets/_DDD/_Scripts/GenerateGoogleSheet/Core/DataSo.cs index 88d39826a..8598c3e71 100644 --- a/Assets/_DDD/_Scripts/GenerateGoogleSheet/Core/DataSo.cs +++ b/Assets/_DDD/_Scripts/GenerateGoogleSheet/Core/DataSo.cs @@ -20,5 +20,7 @@ public List GetDataList() { return Datas; } + + public int GetDataCount() => Datas.Count; } } \ No newline at end of file