global virtual inventory
This commit is contained in:
parent
29899842e4
commit
8bf4d080b5
|
|
@ -15,9 +15,8 @@ GameObject:
|
||||||
- component: {fileID: 819912288095587636}
|
- component: {fileID: 819912288095587636}
|
||||||
- component: {fileID: 1421388845701231023}
|
- component: {fileID: 1421388845701231023}
|
||||||
- component: {fileID: 4527755210678078285}
|
- component: {fileID: 4527755210678078285}
|
||||||
- component: {fileID: 7057847397855985585}
|
|
||||||
m_Layer: 0
|
m_Layer: 0
|
||||||
m_Name: MetalMine
|
m_Name: ResourceSource
|
||||||
m_TagString: Untagged
|
m_TagString: Untagged
|
||||||
m_Icon: {fileID: 0}
|
m_Icon: {fileID: 0}
|
||||||
m_NavMeshLayer: 0
|
m_NavMeshLayer: 0
|
||||||
|
|
@ -113,8 +112,11 @@ MonoBehaviour:
|
||||||
m_Script: {fileID: 11500000, guid: c06e7972b6bac34468cda352d1c07c7a, type: 3}
|
m_Script: {fileID: 11500000, guid: c06e7972b6bac34468cda352d1c07c7a, type: 3}
|
||||||
m_Name:
|
m_Name:
|
||||||
m_EditorClassIdentifier: Assembly-CSharp::ResourceSource
|
m_EditorClassIdentifier: Assembly-CSharp::ResourceSource
|
||||||
|
name:
|
||||||
Resource:
|
Resource:
|
||||||
rid: 7834219818361815538
|
rid: 7834219818361815538
|
||||||
|
ParentInventory: {fileID: 0}
|
||||||
|
InitialCapacity: 0
|
||||||
references:
|
references:
|
||||||
version: 2
|
version: 2
|
||||||
RefIds:
|
RefIds:
|
||||||
|
|
@ -218,17 +220,4 @@ MonoBehaviour:
|
||||||
m_EditorClassIdentifier: Assembly-CSharp::ResourceGenerator
|
m_EditorClassIdentifier: Assembly-CSharp::ResourceGenerator
|
||||||
Interval: 2000
|
Interval: 2000
|
||||||
Power: 3
|
Power: 3
|
||||||
<TargetInventory>k__BackingField: {fileID: 7057847397855985585}
|
<TargetInventory>k__BackingField: {fileID: 4019719181363577730}
|
||||||
--- !u!114 &7057847397855985585
|
|
||||||
MonoBehaviour:
|
|
||||||
m_ObjectHideFlags: 0
|
|
||||||
m_CorrespondingSourceObject: {fileID: 0}
|
|
||||||
m_PrefabInstance: {fileID: 0}
|
|
||||||
m_PrefabAsset: {fileID: 0}
|
|
||||||
m_GameObject: {fileID: 558136326881420228}
|
|
||||||
m_Enabled: 1
|
|
||||||
m_EditorHideFlags: 0
|
|
||||||
m_Script: {fileID: 11500000, guid: b6e85c222b8627a4287c8dc63bf5cfe6, type: 3}
|
|
||||||
m_Name:
|
|
||||||
m_EditorClassIdentifier: Assembly-CSharp::InventoryHolder
|
|
||||||
name:
|
|
||||||
|
|
@ -157,12 +157,20 @@ PrefabInstance:
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
- target: {fileID: 4019719181363577730, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
- target: {fileID: 4019719181363577730, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
||||||
propertyPath: Resource
|
propertyPath: Resource
|
||||||
value: 7834219818361815541
|
value: 7834219818361815585
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
- target: {fileID: 4019719181363577730, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
- target: {fileID: 4019719181363577730, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
||||||
propertyPath: resource
|
propertyPath: resource
|
||||||
value: 7834219806948065471
|
value: 7834219806948065471
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
|
- target: {fileID: 4019719181363577730, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
||||||
|
propertyPath: InitialCapacity
|
||||||
|
value: 200
|
||||||
|
objectReference: {fileID: 0}
|
||||||
|
- target: {fileID: 4019719181363577730, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
||||||
|
propertyPath: ParentInventory
|
||||||
|
value:
|
||||||
|
objectReference: {fileID: 403904197}
|
||||||
- target: {fileID: 4019719181363577730, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
- target: {fileID: 4019719181363577730, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
||||||
propertyPath: <Label>k__BackingField
|
propertyPath: <Label>k__BackingField
|
||||||
value: Power Plant
|
value: Power Plant
|
||||||
|
|
@ -187,6 +195,10 @@ PrefabInstance:
|
||||||
propertyPath: 'managedReferences[7834219818361815541]'
|
propertyPath: 'managedReferences[7834219818361815541]'
|
||||||
value: Assembly-CSharp Energy
|
value: Assembly-CSharp Energy
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
|
- target: {fileID: 4019719181363577730, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
||||||
|
propertyPath: 'managedReferences[7834219818361815585]'
|
||||||
|
value: Assembly-CSharp Energy
|
||||||
|
objectReference: {fileID: 0}
|
||||||
- target: {fileID: 4527755210678078285, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
- target: {fileID: 4527755210678078285, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
||||||
propertyPath: Power
|
propertyPath: Power
|
||||||
value: 7
|
value: 7
|
||||||
|
|
@ -247,7 +259,8 @@ PrefabInstance:
|
||||||
propertyPath: name
|
propertyPath: name
|
||||||
value: Power Plant
|
value: Power Plant
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
m_RemovedComponents: []
|
m_RemovedComponents:
|
||||||
|
- {fileID: 7057847397855985585, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
||||||
m_RemovedGameObjects: []
|
m_RemovedGameObjects: []
|
||||||
m_AddedGameObjects: []
|
m_AddedGameObjects: []
|
||||||
m_AddedComponents: []
|
m_AddedComponents: []
|
||||||
|
|
@ -327,7 +340,6 @@ MonoBehaviour:
|
||||||
m_Name:
|
m_Name:
|
||||||
m_EditorClassIdentifier: '::'
|
m_EditorClassIdentifier: '::'
|
||||||
name:
|
name:
|
||||||
Capacity: -1
|
|
||||||
--- !u!114 &403904198
|
--- !u!114 &403904198
|
||||||
MonoBehaviour:
|
MonoBehaviour:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
|
|
@ -539,12 +551,16 @@ PrefabInstance:
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
- target: {fileID: 4019719181363577730, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
- target: {fileID: 4019719181363577730, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
||||||
propertyPath: Resource
|
propertyPath: Resource
|
||||||
value: 7834219818361815539
|
value: 7834219818361815584
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
- target: {fileID: 4019719181363577730, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
- target: {fileID: 4019719181363577730, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
||||||
propertyPath: resource
|
propertyPath: resource
|
||||||
value: 7834219806948065469
|
value: 7834219806948065469
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
|
- target: {fileID: 4019719181363577730, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
||||||
|
propertyPath: ParentInventory
|
||||||
|
value:
|
||||||
|
objectReference: {fileID: 403904197}
|
||||||
- target: {fileID: 4019719181363577730, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
- target: {fileID: 4019719181363577730, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
||||||
propertyPath: <Label>k__BackingField
|
propertyPath: <Label>k__BackingField
|
||||||
value: Power Plant
|
value: Power Plant
|
||||||
|
|
@ -569,6 +585,10 @@ PrefabInstance:
|
||||||
propertyPath: 'managedReferences[7834219818361815539]'
|
propertyPath: 'managedReferences[7834219818361815539]'
|
||||||
value: Assembly-CSharp Electronics
|
value: Assembly-CSharp Electronics
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
|
- target: {fileID: 4019719181363577730, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
||||||
|
propertyPath: 'managedReferences[7834219818361815584]'
|
||||||
|
value: Assembly-CSharp Electronics
|
||||||
|
objectReference: {fileID: 0}
|
||||||
- target: {fileID: 4527755210678078285, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
- target: {fileID: 4527755210678078285, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
||||||
propertyPath: Interval
|
propertyPath: Interval
|
||||||
value: 0
|
value: 0
|
||||||
|
|
@ -817,12 +837,20 @@ PrefabInstance:
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
- target: {fileID: 4019719181363577730, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
- target: {fileID: 4019719181363577730, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
||||||
propertyPath: Resource
|
propertyPath: Resource
|
||||||
value: 7834219818361815540
|
value: 7834219818361815583
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
- target: {fileID: 4019719181363577730, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
- target: {fileID: 4019719181363577730, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
||||||
propertyPath: resource
|
propertyPath: resource
|
||||||
value: 7834219806948065470
|
value: 7834219806948065470
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
|
- target: {fileID: 4019719181363577730, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
||||||
|
propertyPath: InitialCapacity
|
||||||
|
value: 500
|
||||||
|
objectReference: {fileID: 0}
|
||||||
|
- target: {fileID: 4019719181363577730, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
||||||
|
propertyPath: ParentInventory
|
||||||
|
value:
|
||||||
|
objectReference: {fileID: 403904197}
|
||||||
- target: {fileID: 4019719181363577730, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
- target: {fileID: 4019719181363577730, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
||||||
propertyPath: <Label>k__BackingField
|
propertyPath: <Label>k__BackingField
|
||||||
value: Metal Mine
|
value: Metal Mine
|
||||||
|
|
@ -847,6 +875,10 @@ PrefabInstance:
|
||||||
propertyPath: 'managedReferences[7834219818361815540]'
|
propertyPath: 'managedReferences[7834219818361815540]'
|
||||||
value: Assembly-CSharp Metal
|
value: Assembly-CSharp Metal
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
|
- target: {fileID: 4019719181363577730, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
||||||
|
propertyPath: 'managedReferences[7834219818361815583]'
|
||||||
|
value: Assembly-CSharp Metal
|
||||||
|
objectReference: {fileID: 0}
|
||||||
- target: {fileID: 4851214142334336292, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
- target: {fileID: 4851214142334336292, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
||||||
propertyPath: m_LocalPosition.x
|
propertyPath: m_LocalPosition.x
|
||||||
value: 0
|
value: 0
|
||||||
|
|
@ -897,7 +929,7 @@ PrefabInstance:
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
- target: {fileID: 7057847397855985585, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
- target: {fileID: 7057847397855985585, guid: cd4f08f126f359d48ba53df6ace98e6c, type: 3}
|
||||||
propertyPath: Capacity
|
propertyPath: Capacity
|
||||||
value: 50
|
value: 500
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
m_RemovedComponents: []
|
m_RemovedComponents: []
|
||||||
m_RemovedGameObjects: []
|
m_RemovedGameObjects: []
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,6 @@ public abstract class IInventory
|
||||||
{
|
{
|
||||||
private List<Action> changeCallbacks = new List<Action>();
|
private List<Action> changeCallbacks = new List<Action>();
|
||||||
|
|
||||||
public abstract string Name { get; }
|
|
||||||
|
|
||||||
public abstract IReadOnlyDictionary<string, float> Items { get; }
|
public abstract IReadOnlyDictionary<string, float> Items { get; }
|
||||||
|
|
||||||
public abstract float Capacity { get; set; }
|
public abstract float Capacity { get; set; }
|
||||||
|
|
@ -59,7 +57,7 @@ public abstract class IInventory
|
||||||
changeCallbacks.Remove(callback);
|
changeCallbacks.Remove(callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void InvokeOnChange()
|
protected void InvokeOnChange()
|
||||||
{
|
{
|
||||||
// Debug.Log(
|
// Debug.Log(
|
||||||
// $"Resources: {String.Join("; ", resources.Values.ToArray().Select(r => r.Resource.Name + "=" + r.Amount + (r.Unlocked ? "" : "(locked)")))}"
|
// $"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]
|
[Serializable]
|
||||||
public class Inventory2 : IInventory, IDataPersistence<InventoryData>
|
public class Inventory2 : IInventory, IDataPersistence<InventoryData>
|
||||||
{
|
{
|
||||||
private NameCallback nameCallback;
|
|
||||||
private Dictionary<string, float> items = new Dictionary<string, float>();
|
private Dictionary<string, float> items = new Dictionary<string, float>();
|
||||||
|
|
||||||
public Inventory2(float capacity, NameCallback nameCallback)
|
public Inventory2(float capacity)
|
||||||
{
|
{
|
||||||
this.nameCallback = nameCallback;
|
|
||||||
_capacity = capacity;
|
_capacity = capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -92,12 +88,14 @@ public class Inventory2 : IInventory, IDataPersistence<InventoryData>
|
||||||
|
|
||||||
private float _capacity;
|
private float _capacity;
|
||||||
|
|
||||||
public override string Name => nameCallback();
|
|
||||||
|
|
||||||
public override float Capacity
|
public override float Capacity
|
||||||
{
|
{
|
||||||
get { return _capacity; }
|
get { return _capacity; }
|
||||||
set { _capacity = value; }
|
set
|
||||||
|
{
|
||||||
|
_capacity = value;
|
||||||
|
InvokeOnChange();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
|
|
@ -172,7 +170,7 @@ public class Inventory2 : IInventory, IDataPersistence<InventoryData>
|
||||||
|
|
||||||
public Inventory2 Clone()
|
public Inventory2 Clone()
|
||||||
{
|
{
|
||||||
var clone = new Inventory2(Capacity, nameCallback);
|
var clone = new Inventory2(Capacity);
|
||||||
foreach (var kvp in items)
|
foreach (var kvp in items)
|
||||||
clone.items.Add(kvp.Key, kvp.Value);
|
clone.items.Add(kvp.Key, kvp.Value);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,24 +2,12 @@ using System;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
// class, not interface so Unity shows fields in Editor
|
// class, not interface so Unity shows fields in Editor
|
||||||
public class InventoryHolder : MonoBehaviour
|
public abstract class InventoryHolder : MonoBehaviour
|
||||||
{
|
{
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
private new string name;
|
private new string name;
|
||||||
|
|
||||||
public readonly IInventory Inventory;
|
public abstract IInventory Inventory { get; }
|
||||||
|
|
||||||
public string Name => name == null || name == "" ? gameObject.name : name;
|
public string Name => name == null || name == "" ? gameObject.name : name;
|
||||||
|
|
||||||
public float Capacity;
|
|
||||||
|
|
||||||
public InventoryHolder()
|
|
||||||
{
|
|
||||||
Inventory = new Inventory2(0, () => Name);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Start()
|
|
||||||
{
|
|
||||||
Inventory.Capacity = Capacity;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: ab0e490a1eacfaf4791ced60ce6a934b
|
||||||
|
|
@ -1,8 +1,29 @@
|
||||||
using System;
|
using System;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
public class ResourceSource : MonoBehaviour
|
public class ResourceSource : InventoryHolder
|
||||||
{
|
{
|
||||||
[SerializeReference]
|
[SerializeReference]
|
||||||
public Resource Resource;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,45 +1,16 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.UIElements;
|
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
|
public class ResourceStorage : InventoryHolder
|
||||||
{
|
{
|
||||||
|
private readonly IInventory _inventory;
|
||||||
|
|
||||||
|
public override IInventory Inventory => _inventory;
|
||||||
|
|
||||||
public ResourceStorage()
|
public ResourceStorage()
|
||||||
{
|
{
|
||||||
Capacity = -1;
|
_inventory = new VirtualInventory();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: fdbf256004ee20a45be934c4508a24c7
|
||||||
Loading…
Reference in New Issue