Foerming/Assets/Scripts/ElementStorage.cs
dhain acd602c85a fixed first Shop Slot
started on shop switcher button (for animal shop)
2022-06-23 15:01:13 +02:00

47 lines
1.3 KiB
C#

using System.Collections.Generic;
using UnityEngine;
public class ElementStorage<T> : MonoBehaviour {
private Dictionary<T, int> _elements;
public Dictionary<T, int> Elements => _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();
}
}