diesmal wirklich

This commit is contained in:
dhain 2022-05-19 16:03:20 +02:00
parent 25f51c2622
commit 2ff7fc2c12
20 changed files with 247 additions and 194 deletions

View file

@ -0,0 +1,56 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour {
#region Singleton
public static Inventory instance;
private void Awake() {
if(instance != null) {
Debug.LogWarning("More than one instance of Inventory found");
}
instance = this;
}
#endregion
public Dictionary<Item, int> items;
public const int inventorySpace = 28;
public delegate void onItemChanged();
public onItemChanged onItemChangedCallback;
private void Start() {
items ??= new Dictionary<Item, int>();
}
public void tempAddItem(Item item) {
AddItem(item, 1);
}
public void AddItem(Item item, int amount) {
if(items.Count >= inventorySpace) {
Debug.Log("Not enough inventory space!");
return;
}
if(!items.ContainsKey(item)) {
items.Add(item, amount);
} else {
items[item] += amount;
}
onItemChangedCallback?.Invoke();
}
public void removeItem(Item item, int amount) {
items.Add(item, -amount);
onItemChangedCallback?.Invoke();
}
}

View file

@ -4,27 +4,32 @@ using UnityEngine.UI;
public class InventorySlot : MonoBehaviour {
public Image icon;
private Item item;
private Item _item;
public void addItem(Item newItem) {
item = newItem;
public void AddItem(Item newItem) {
_item = newItem;
icon.sprite = item.defaultSprite;
icon.sprite = _item.defaultSprite;
icon.enabled = true;
}
public void clearSlot() {
item = null;
public void ClearSlot() {
_item = null;
icon.sprite = null;
icon.enabled = false;
}
public void removeItem() {
PlayerController.instance.inventory.Remove(item);
public void RemoveItem() {
Inventory.instance.items.Remove(_item);
}
public void useItem() {
//TODO: use item
Debug.Log("using " + item.displayName);
public void UseItem() {
if(_item.GetType() == typeof(UsableItem)) {
((UsableItem) _item).select();
Debug.Log("using " + _item.displayName);
} else {
Debug.Log("Item not usable " + _item.displayName);
}
}
}

View file

@ -1,18 +1,19 @@
using System.Linq;
using UnityEngine;
public class InventoryUI : MonoBehaviour {
public Transform itemsParent;
public GameObject inventoryUI;
private PlayerController playerController;
private InventorySlot[] slots;
private Inventory _inventory;
private InventorySlot[] _slots;
// Start is called before the first frame update
void Start() {
playerController = PlayerController.instance;
playerController.onItemChangedCallback += updateUI;
_inventory = Inventory.instance;
_inventory.onItemChangedCallback += UpdateUI;
slots = itemsParent.GetComponentsInChildren<InventorySlot>();
_slots = itemsParent.GetComponentsInChildren<InventorySlot>();
}
// Update is called once per frame
@ -22,12 +23,12 @@ public class InventoryUI : MonoBehaviour {
}
}
private void updateUI() {
for(int i = 0; i < slots.Length; i++) {
if(i < playerController.inventory.Count) {
// slots[i].addItem(playerController.inventory[i]); //TODO: dictionary "letztes" Item finden, Wie?!?!?!
private void UpdateUI() {
for(int i = 0; i < _slots.Length; i++) {
if(i < _inventory.items.Count) {
_slots[i].AddItem(_inventory.items.ElementAt(i).Key);
} else {
slots[i].clearSlot();
_slots[i].ClearSlot();
}
}
}

View file

@ -1,14 +1,10 @@
using System;
using UnityEngine;
//TODO: Auf ScriptableItem umschreiben!!!!!!!!!!
//
//https://www.youtube.com/watch?v=YLhj7SfaxSE
public class Item : MonoBehaviour, IComparable<Item> {
public readonly string displayName;
public readonly string description;
public readonly int id; //TODO: create an actual ID System that makes snens
public SpriteRenderer spriteRenderer;
public class Item : ScriptableObject, IComparable<Item> {
public string displayName;
public string description;
public int id; //TODO: create an actual ID System that makes snens
public Sprite selectedSprite;
public Sprite defaultSprite;
@ -16,7 +12,6 @@ public class Item : MonoBehaviour, IComparable<Item> {
this.displayName = displayName;
this.description = description;
this.id = id;
spriteRenderer.sprite ??= defaultSprite; // defaultSprite is set in UnityEditor
}
public int CompareTo(Item other) {

View file

@ -1,7 +0,0 @@
using UnityEngine;
namespace Items {
public class FishingRod : UsableItem {
public FishingRod() : base("Fishing Rod", "Can be used to fish fishy fish.", 1) { }
}
}

View file

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: b08796f3a5662aa43a460c7ddd6796b3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -1,9 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Items {
public class Hoe : UsableItem {
public Hoe() : base("Hoe", "Used to hoe the ground into farmland.", 2){}
}
}

View file

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: aa6ceecfb09ac614997702b2caf66e33
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -1,9 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Items {
public class Scythe : UsableItem {
public Scythe() : base("Scythe", "Used to cut down crops.", 3){}
}
}

View file

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 641048df0d7668f48a857c454e2bd320
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -1,9 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Items {
public class WateringCan : UsableItem {
public WateringCan():base("Watering Can", "Used to water planted crops.", 4){}
}
}

View file

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 7e19228f49932534e8af0218c51e5c63
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -1,9 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Items {
public class Wheat : Item {
public Wheat() : base("Wheat", "Wheat is a grass widely cultivated for its seed, a cereal grain which is a worldwide staple food.", 5){}
}
}

View file

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 207be0ed7f8f5714c97b7ac5deb8956d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -1,9 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Items {
public class WheatSeed : UsableItem {
public WheatSeed() : base("Wheat Seeds", "When planted on farmland, wheat will grow.", 6){}
}
}

View file

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 0808be748388ede4ba57db7d74401617
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -8,7 +8,7 @@ public class PlayerController : MonoBehaviour {
private void Awake() {
if(instance != null) {
Debug.LogWarning("More than one instance of PlayeController found");
Debug.LogWarning("More than one instance of PlayerController found");
}
instance = this;
@ -16,8 +16,7 @@ public class PlayerController : MonoBehaviour {
#endregion
public Dictionary<Item, int> inventory;
public readonly int inventorySpace = 28;
public Inventory inventory;
private int money;
private UsableItem selectedItem;
@ -25,40 +24,18 @@ public class PlayerController : MonoBehaviour {
// 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)) {
public void SetSelectedItem(UsableItem item) {
if(inventory.items.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();
}
}

View file

@ -15,6 +15,6 @@ public class UsableItem : Item, IUsable {
}
public void select() {
PlayerController.instance.setSelectedItem(this);
PlayerController.instance.SetSelectedItem(this);
}
}