global virtual inventory

This commit is contained in:
Aggtaa
2026-01-20 23:32:47 +03:00
parent 29899842e4
commit 8bf4d080b5
11 changed files with 213 additions and 81 deletions
+8 -10
View File
@@ -9,8 +9,6 @@ public abstract class IInventory
{
private List<Action> changeCallbacks = new List<Action>();
public abstract string Name { get; }
public abstract IReadOnlyDictionary<string, float> Items { get; }
public abstract float Capacity { get; set; }
@@ -59,7 +57,7 @@ public abstract class IInventory
changeCallbacks.Remove(callback);
}
private void InvokeOnChange()
protected void InvokeOnChange()
{
// Debug.Log(
// $"Resources: {String.Join("; ", resources.Values.ToArray().Select(r => r.Resource.Name + "=" + r.Amount + (r.Unlocked ? "" : "(locked)")))}"
@@ -79,12 +77,10 @@ public abstract class IInventory
[Serializable]
public class Inventory2 : IInventory, IDataPersistence<InventoryData>
{
private NameCallback nameCallback;
private Dictionary<string, float> items = new Dictionary<string, float>();
public Inventory2(float capacity, NameCallback nameCallback)
public Inventory2(float capacity)
{
this.nameCallback = nameCallback;
_capacity = capacity;
}
@@ -92,12 +88,14 @@ public class Inventory2 : IInventory, IDataPersistence<InventoryData>
private float _capacity;
public override string Name => nameCallback();
public override float Capacity
{
get { return _capacity; }
set { _capacity = value; }
set
{
_capacity = value;
InvokeOnChange();
}
}
public override string ToString()
@@ -172,7 +170,7 @@ public class Inventory2 : IInventory, IDataPersistence<InventoryData>
public Inventory2 Clone()
{
var clone = new Inventory2(Capacity, nameCallback);
var clone = new Inventory2(Capacity);
foreach (var kvp in items)
clone.items.Add(kvp.Key, kvp.Value);
+2 -14
View File
@@ -2,24 +2,12 @@ using System;
using UnityEngine;
// class, not interface so Unity shows fields in Editor
public class InventoryHolder : MonoBehaviour
public abstract class InventoryHolder : MonoBehaviour
{
[SerializeField]
private new string name;
public readonly IInventory Inventory;
public abstract IInventory Inventory { get; }
public string Name => name == null || name == "" ? gameObject.name : name;
public float Capacity;
public InventoryHolder()
{
Inventory = new Inventory2(0, () => Name);
}
void Start()
{
Inventory.Capacity = Capacity;
}
}
+33
View File
@@ -0,0 +1,33 @@
using System.Collections.Generic;
public class ResourceRegistry
{
public static ResourceRegistry Instance { get; private set; }
private readonly List<Resource> _items = new List<Resource>();
public Resource[] Items => _items.ToArray();
public readonly Metal Metal = new Metal();
public readonly Electronics Electronics = new Electronics();
public readonly Energy Energy = new Energy();
public readonly Money Money = new Money();
static ResourceRegistry()
{
Instance = new ResourceRegistry();
}
private ResourceRegistry()
{
RegisterResource(Metal, true);
RegisterResource(Electronics, true); // TODO lock back
RegisterResource(Energy, true); // TODO lock back
RegisterResource(Money, true); // TODO lock back
}
private void RegisterResource(Resource resource, bool unlocked = false)
{
_items.Add(resource);
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: ab0e490a1eacfaf4791ced60ce6a934b
+22 -1
View File
@@ -1,8 +1,29 @@
using System;
using UnityEngine;
public class ResourceSource : MonoBehaviour
public class ResourceSource : InventoryHolder
{
[SerializeReference]
public Resource Resource;
public InventoryHolder ParentInventory;
public float InitialCapacity;
private readonly IInventory _inventory;
public override IInventory Inventory => _inventory;
public ResourceSource()
{
_inventory = new Inventory2(InitialCapacity);
}
void Start()
{
_inventory.Capacity = InitialCapacity;
if (ParentInventory != null && ParentInventory.Inventory is VirtualInventory)
((VirtualInventory)ParentInventory.Inventory).RegisterChild(_inventory);
}
}
+5 -34
View File
@@ -1,45 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UIElements;
public class ResourceRegistry
{
public static ResourceRegistry Instance { get; private set; }
private readonly List<Resource> _items = new List<Resource>();
public Resource[] Items => _items.ToArray();
public readonly Metal Metal = new Metal();
public readonly Electronics Electronics = new Electronics();
public readonly Energy Energy = new Energy();
public readonly Money Money = new Money();
static ResourceRegistry()
{
Instance = new ResourceRegistry();
}
private ResourceRegistry()
{
RegisterResource(Metal, true);
RegisterResource(Electronics, true); // TODO lock back
RegisterResource(Energy, true); // TODO lock back
RegisterResource(Money, true); // TODO lock back
}
private void RegisterResource(Resource resource, bool unlocked = false)
{
_items.Add(resource);
}
}
public class ResourceStorage : InventoryHolder
{
private readonly IInventory _inventory;
public override IInventory Inventory => _inventory;
public ResourceStorage()
{
Capacity = -1;
_inventory = new VirtualInventory();
}
}
+96
View File
@@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
internal class ChildInventotyWithCallback
{
public readonly IInventory inventory;
public readonly Action callback;
public ChildInventotyWithCallback(IInventory inventory, Action callback)
{
this.inventory = inventory;
this.callback = callback;
}
}
[Serializable]
public class VirtualInventory : IInventory
{
private readonly List<ChildInventotyWithCallback> _children =
new List<ChildInventotyWithCallback>();
public override IReadOnlyDictionary<string, float> Items
{
get
{
var items = new Dictionary<string, float>();
foreach (var child in _children)
{
foreach (var childItem in child.inventory.Items)
{
if (!items.ContainsKey(childItem.Key))
items.Add(childItem.Key, childItem.Value);
else
items[childItem.Key] += childItem.Value;
}
}
return items;
}
}
public override float Capacity
{
get => _children.Sum(c => c.inventory.Capacity);
set { throw new NotSupportedException(); }
}
public void RegisterChild(IInventory child)
{
Debug.Log($"Registered child {child}");
var reg = new ChildInventotyWithCallback(child, () => OnChildChange(child));
_children.Add(reg);
child.RegisterOnChangeCallback(reg.callback);
}
public void UnregisterChild(IInventory child)
{
int index = _children.FindIndex(c => c.inventory == child);
if (index < 0)
return;
child.UnregisterOnChangeCallback(_children[index].callback);
_children.RemoveAt(index);
}
private void OnChildChange(IInventory child)
{
Debug.Log($"child has changed {child}");
base.InvokeOnChange();
}
public override float GetAmount(string item)
{
float amount = 0;
Items.TryGetValue(item, out amount);
return amount;
}
protected override float DoAdd(string item, float amount, bool allowPartialAdd = false)
{
throw new NotSupportedException();
}
protected override int DoClear()
{
throw new NotSupportedException();
}
protected override float DoRemove(string item, float amount, bool allowPartialRemoval = false)
{
throw new NotSupportedException();
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: fdbf256004ee20a45be934c4508a24c7