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
This commit is contained in:
parent
433158e658
commit
aacdd80fdf
103 changed files with 17775 additions and 58 deletions
30
Assets/Scripts/InventorySlot.cs
Normal file
30
Assets/Scripts/InventorySlot.cs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class InventorySlot : MonoBehaviour {
|
||||
public Image icon;
|
||||
|
||||
private Item item;
|
||||
|
||||
public void addItem(Item newItem) {
|
||||
item = newItem;
|
||||
|
||||
icon.sprite = item.defaultSprite;
|
||||
icon.enabled = true;
|
||||
}
|
||||
|
||||
public void clearSlot() {
|
||||
item = null;
|
||||
icon.sprite = null;
|
||||
icon.enabled = false;
|
||||
}
|
||||
|
||||
public void removeItem() {
|
||||
PlayerController.instance.inventory.Remove(item);
|
||||
}
|
||||
|
||||
public void useItem() {
|
||||
//TODO: use item
|
||||
Debug.Log("using " + item.displayName);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/InventorySlot.cs.meta
Normal file
11
Assets/Scripts/InventorySlot.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 92863b15e86b5b94e9a331f3c97dddca
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
34
Assets/Scripts/InventoryUI.cs
Normal file
34
Assets/Scripts/InventoryUI.cs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
using UnityEngine;
|
||||
|
||||
public class InventoryUI : MonoBehaviour {
|
||||
|
||||
public Transform itemsParent;
|
||||
public GameObject inventoryUI;
|
||||
private PlayerController playerController;
|
||||
private InventorySlot[] slots;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start() {
|
||||
playerController = PlayerController.instance;
|
||||
playerController.onItemChangedCallback += updateUI;
|
||||
|
||||
slots = itemsParent.GetComponentsInChildren<InventorySlot>();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update() {
|
||||
if(Input.GetButtonDown("Inventory")) {
|
||||
inventoryUI.SetActive(!inventoryUI.activeSelf);
|
||||
}
|
||||
}
|
||||
|
||||
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?!?!?!
|
||||
} else {
|
||||
slots[i].clearSlot();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/InventoryUI.cs.meta
Normal file
11
Assets/Scripts/InventoryUI.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 07c8392e636669644a90cebd609eaf5a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -1,10 +1,13 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
//TODO: Auf ScriptableItem umschreiben!!!!!!!!!!
|
||||
//
|
||||
//https://www.youtube.com/watch?v=YLhj7SfaxSE
|
||||
public class Item : MonoBehaviour, IComparable<Item> {
|
||||
private readonly string displayName;
|
||||
private readonly string description;
|
||||
private readonly int id; //TODO: create an actual ID System that makes snens
|
||||
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 Sprite selectedSprite;
|
||||
public Sprite defaultSprite;
|
||||
|
|
|
|||
|
|
@ -1,42 +1,64 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PlayerController : MonoBehaviour {
|
||||
private Dictionary<Item, int> inventory;
|
||||
private int money;
|
||||
private UsableItem selectedItem;
|
||||
|
||||
private static PlayerController instance;
|
||||
|
||||
public int startMoney = 100;
|
||||
#region Singleton
|
||||
|
||||
public static PlayerController getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static PlayerController instance;
|
||||
|
||||
private void Awake() {
|
||||
if(instance != null) {
|
||||
Debug.LogWarning("More than one instance of PlayeController found");
|
||||
}
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
inventory ??= new Dictionary<Item, int>();
|
||||
money = startMoney;
|
||||
instance = this;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
#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)) {
|
||||
if(inventory.ContainsKey(item)) {
|
||||
selectedItem = item;
|
||||
Cursor.SetCursor(item.defaultSprite.texture, Vector2.zero, CursorMode.Auto);
|
||||
Cursor.SetCursor(item.defaultSprite.texture, Vector2.zero, CursorMode.Auto);
|
||||
} else {
|
||||
Debug.Log("An item requested to select isn't in the inventory" + item);
|
||||
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();
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/PlayerController.cs.meta
Normal file
11
Assets/Scripts/PlayerController.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b18feac5270242d4a85c78ce72972d56
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
11
Assets/Scripts/TileBehaviour.cs.meta
Normal file
11
Assets/Scripts/TileBehaviour.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 3fd8bc1d313319d4f89f11548ccb1b6a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Scripts/Tiles.meta
Normal file
8
Assets/Scripts/Tiles.meta
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 5e750377f5419b7409015c1dac98c88e
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
11
Assets/Scripts/Tiles/BaseTile.cs.meta
Normal file
11
Assets/Scripts/Tiles/BaseTile.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2619a266d1f1c0c468e81e5bfda1cd78
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
11
Assets/Scripts/Tiles/FarmlandTile.cs.meta
Normal file
11
Assets/Scripts/Tiles/FarmlandTile.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 5b78d95148d53bd4aa70e3d0d595ab93
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 9a992c82c9da0cd4f8759894ddfd5fb3
|
||||
guid: af8926946929c0644a4fbbe9d92b2729
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
11
Assets/Scripts/Tiles/WaterTile.cs.meta
Normal file
11
Assets/Scripts/Tiles/WaterTile.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: e1921bfec5aa63e44a32c0492c021809
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -2,26 +2,19 @@ using System.Collections;
|
|||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class UsableItem : Item, IUsable
|
||||
{
|
||||
public class UsableItem : Item, IUsable {
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
|
||||
void Start() {
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
void Update() {
|
||||
}
|
||||
|
||||
public UsableItem(string displayName, string description, int id) : base(displayName, description, id)
|
||||
{
|
||||
}
|
||||
|
||||
public void select()
|
||||
{
|
||||
PlayerController.getInstance().setSelectedItem(this);
|
||||
}
|
||||
public UsableItem(string displayName, string description, int id) : base(displayName, description, id) {
|
||||
}
|
||||
|
||||
public void select() {
|
||||
PlayerController.instance.setSelectedItem(this);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/UsableItem.cs.meta
Normal file
11
Assets/Scripts/UsableItem.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 761f645f009328845bc7851753024e92
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
Add table
Add a link
Reference in a new issue