Foerming/Assets/Scripts/PlayerController.cs
d-hain aacdd80fdf added Inventory UI
added logic to inventory
 * it is very possible to be reworked in the near future
 * With
   - ScriptableItem
   - an own Inventory class maybe
   - if necessary no Dictionary anymore
2022-05-19 02:30:21 +02:00

64 lines
No EOL
1.6 KiB
C#

using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
#region Singleton
public static PlayerController instance;
private void Awake() {
if(instance != null) {
Debug.LogWarning("More than one instance of PlayeController found");
}
instance = this;
}
#endregion
public Dictionary<Item, int> inventory;
public readonly int inventorySpace = 28;
private int money;
private UsableItem selectedItem;
public int startMoney = 100;
// Start is called before the first frame update
void Start() {
inventory ??= new Dictionary<Item, int>();
money = startMoney;
}
// Update is called once per frame
void Update() { }
public void setSelectedItem(UsableItem item) {
if(inventory.ContainsKey(item)) {
selectedItem = item;
Cursor.SetCursor(item.defaultSprite.texture, Vector2.zero, CursorMode.Auto);
} else {
Debug.Log("An item requested to select isn't in the inventory" + item);
}
}
public delegate void onItemChanged();
public onItemChanged onItemChangedCallback;
public void addItem(Item item, int amount) {
if(inventory.Count >= inventorySpace) {
Debug.Log("Not enough inventory space!");
return;
}
inventory.Add(item, amount);
onItemChangedCallback?.Invoke();
}
public void removeItem(Item item, int amount) {
inventory.Add(item, -amount);
onItemChangedCallback?.Invoke();
}
}