Generified ElementStorage.cs and ElementStorageSlot.cs
This commit is contained in:
parent
df3fad8c51
commit
e160867e7e
18 changed files with 210 additions and 212 deletions
46
Assets/Scripts/ElementStorage.cs
Normal file
46
Assets/Scripts/ElementStorage.cs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class ElementStorage<T> : MonoBehaviour {
|
||||
public Dictionary<T, int> elements;
|
||||
public T[] startElements;
|
||||
|
||||
/**
|
||||
* Methods can be added to this and they will get called every time onItemChangedCallback gets Invoked
|
||||
*/
|
||||
public delegate void OnElementChanged();
|
||||
public OnElementChanged onElementChangedCallback;
|
||||
|
||||
private void Start() {
|
||||
elements ??= new Dictionary<T, int>();
|
||||
foreach(T element in startElements) {
|
||||
AddElement(element, 1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the specified amount of elements to the Element Storage
|
||||
*/
|
||||
public virtual void AddElement(T element, int amount) {
|
||||
if(!elements.ContainsKey(element)) {
|
||||
elements.Add(element, amount);
|
||||
} else {
|
||||
elements[element] += amount;
|
||||
}
|
||||
|
||||
onElementChangedCallback?.Invoke();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the specified amount of elements in the Element Storage
|
||||
*/
|
||||
public virtual void RemoveElement(T element, int amount) {
|
||||
if(elements[element]-amount <= 0) {
|
||||
elements.Remove(element);
|
||||
} else {
|
||||
elements[element] -= amount;
|
||||
}
|
||||
|
||||
onElementChangedCallback?.Invoke();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue