reworked inv

This commit is contained in:
j.mei7
2022-02-21 21:16:29 +01:00
parent faf55bfb89
commit 65d6af578d
19 changed files with 364 additions and 253 deletions

View File

@@ -910,15 +910,16 @@ GameObject:
serializedVersion: 6
m_Component:
- component: {fileID: 310196954}
- component: {fileID: 310196952}
- component: {fileID: 310196957}
- component: {fileID: 310196958}
- component: {fileID: 310196959}
- component: {fileID: 310196953}
- component: {fileID: 310196956}
- component: {fileID: 310196951}
- component: {fileID: 310196955}
- component: {fileID: 310196961}
- component: {fileID: 310196962}
- component: {fileID: 310196960}
- component: {fileID: 310196959}
- component: {fileID: 310196958}
- component: {fileID: 310196957}
- component: {fileID: 310196963}
m_Layer: 3
m_Name: Player
m_TagString: Player
@@ -947,19 +948,6 @@ Rigidbody2D:
m_SleepingMode: 1
m_CollisionDetection: 1
m_Constraints: 4
--- !u!114 &310196952
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 310196950}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5eabb8b018254b445a53ca18ab08171b, type: 3}
m_Name:
m_EditorClassIdentifier:
moveSpeed: 5
--- !u!212 &310196953
SpriteRenderer:
m_ObjectHideFlags: 0
@@ -1028,26 +1016,6 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 11
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!95 &310196955
Animator:
serializedVersion: 4
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 310196950}
m_Enabled: 1
m_Avatar: {fileID: 0}
m_Controller: {fileID: 9100000, guid: 90bb3dd39fb871f45afc69781292e62c, type: 2}
m_CullingMode: 0
m_UpdateMode: 0
m_ApplyRootMotion: 0
m_LinearVelocityBlending: 0
m_StabilizeFeet: 0
m_WarningMessage:
m_HasTransformHierarchy: 1
m_AllowConstantClipSamplingOptimization: 1
m_KeepAnimatorControllerStateOnDisable: 0
--- !u!70 &310196956
CapsuleCollider2D:
m_ObjectHideFlags: 0
@@ -1073,11 +1041,29 @@ MonoBehaviour:
m_GameObject: {fileID: 310196950}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 194170eed9e58d74584e7e11747aea0b, type: 3}
m_Script: {fileID: 11500000, guid: 259b4dba2ac00aa4983a6562c5f4eed5, type: 3}
m_Name:
m_EditorClassIdentifier:
calendar: {fileID: 1667673220}
health: 1
food: 1
drink: 1
healthModifier: 30
foodModifier: 0.4
drinkModifier: 0.6
--- !u!114 &310196958
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 310196950}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5eabb8b018254b445a53ca18ab08171b, type: 3}
m_Name:
m_EditorClassIdentifier:
moveSpeed: 5
--- !u!114 &310196959
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
@@ -1091,7 +1077,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
interactionText: {fileID: 87507994}
interactionProgressImg: {fileID: 138750307}
--- !u!114 &310196959
--- !u!114 &310196960
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
@@ -1100,16 +1086,11 @@ MonoBehaviour:
m_GameObject: {fileID: 310196950}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 259b4dba2ac00aa4983a6562c5f4eed5, type: 3}
m_Script: {fileID: 11500000, guid: 194170eed9e58d74584e7e11747aea0b, type: 3}
m_Name:
m_EditorClassIdentifier:
health: 1
food: 1
drink: 1
healthModifier: 30
foodModifier: 0.4
drinkModifier: 0.6
--- !u!114 &310196960
calendar: {fileID: 1667673220}
--- !u!114 &310196961
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
@@ -1121,6 +1102,42 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 5ee4c18f008d122488cbb74c779ef5c4, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &310196962
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 310196950}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ee2fcb6d299019740bbede78bbc6a1d7, type: 3}
m_Name:
m_EditorClassIdentifier:
testItemType: {fileID: 11400000, guid: e04039a6cbed5e5439b605aeee90485f, type: 2}
numberOfSlots: 20
maxSpaceOfSlots: 60
inventory: []
--- !u!95 &310196963
Animator:
serializedVersion: 4
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 310196950}
m_Enabled: 1
m_Avatar: {fileID: 0}
m_Controller: {fileID: 9100000, guid: 90bb3dd39fb871f45afc69781292e62c, type: 2}
m_CullingMode: 0
m_UpdateMode: 0
m_ApplyRootMotion: 0
m_LinearVelocityBlending: 0
m_StabilizeFeet: 0
m_WarningMessage:
m_HasTransformHierarchy: 1
m_AllowConstantClipSamplingOptimization: 1
m_KeepAnimatorControllerStateOnDisable: 0
--- !u!1 &360525550
GameObject:
m_ObjectHideFlags: 0

View File

@@ -7,4 +7,9 @@ public class FoodItem : Item
public int health;
public int water;
public int regeneration;
public override void OnSelect()
{
throw new System.NotImplementedException();
}
}

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
@@ -21,114 +22,76 @@ public class Inventory : MonoBehaviour
/// -------------------------------------------------------------------------------///
/// </summary>
public Item testItemType;
public delegate void OnItemChanged();
public OnItemChanged onItemChangedCallback; //
public static Action OnInventoryChanged;
public static Action OnItemAdded;
public static Action OnItemRemoved;
[Header("Inventory")]
[SerializeField]
int numberOfSlots = 20;
[SerializeField]
int maxSpaceOfSlots = 10;
[SerializeField]
List<Slot> inventory = new List<Slot>();
public List<Slot> getInventory { get => inventory;}
public void createEmptyInventory(int numberOfSlots, int maxSpaceOfSlots = 10)
public void AddSlots(int _numberOfSlots)
{
// Initializes the inventory with a specific number of slots and slotsize.
// !!!Has to be called bevore adding any items!!!
// Example createEmptyInventory(5, 10) : 5 Slots with space for 10 items each
inventory = new List<Slot>();
for (int i = 0; i < numberOfSlots; i++)
for (int i = 0; i < _numberOfSlots; i++)
{
inventory.Add(new Slot(maxSpaceOfSlots));
inventory.Add(new Slot());
}
if (onItemChangedCallback != null)
onItemChangedCallback.Invoke();
OnInventoryChanged?.Invoke();
}
public void addInventorySlots(int numberOfSlots, int maxSpaceOfSlots = 10)
Slot GetEmptySlot()
{
// Adds a specific number of slots and slotsize to the inventory.
// Example addInventorySlots(5, 10) : Adds 5 Slots with space for 10 items each
for (int i = 0; i < numberOfSlots; i++)
foreach (Slot slot in inventory)
{
inventory.Add(new Slot(maxSpaceOfSlots));
if (slot.GetItem() == null)
return slot;
}
if (onItemChangedCallback != null)
onItemChangedCallback.Invoke();
return null;
}
public int addItemAt(int index, Item itemType,int count)
Slot GetSlotByItem(Item item)
{
// Adds a number (count = 7) of items of type itemType (itemType = Stone) to an item slot(index = 3).
//Returns the number of items that could not be added because of unsufficent space in the specefied slot
//or Return -1 if the index is out of bound or the item slot is already filled with a different itemType.
//
// Example inventory.addItemAt(3,Stone,7);
if (indexIsInRange(index) && (inventory[index].ItemType == null || inventory[index].ItemType.id == itemType.id))
foreach (Slot slot in inventory)
{
if (inventory[index].ItemType == null)
if (slot.GetItem() != null)
{
inventory[index].ItemType = Instantiate(itemType); // Set the itemType if the slot was empty.
if (slot.GetItem().id == item.id)
return slot;
}
if (inventory[index].MaxItems == inventory[index].Count)
{
if (onItemChangedCallback != null)
onItemChangedCallback.Invoke();
return count; // Can't add any items if the slot is full.
}
else if (inventory[index].MaxItems >= inventory[index].Count + count)
{
inventory[index].addItem(count); // Adds all items if there is enought space.
if(onItemChangedCallback != null)
onItemChangedCallback.Invoke();
return 0;
} else
{
int rest = count - inventory[index].MaxItems - inventory[index].Count;
inventory[index].addItem(inventory[index].MaxItems - inventory[index].Count); // Adds the number of items until the slot is full and returns the number of items that didn't fit.
if (onItemChangedCallback != null)
onItemChangedCallback.Invoke();
return rest;
}
}
return null;
}
int GetRest(int count, Slot slot)
{
if (CountFitsInSlot(count, slot))
{
return 0;
}
else
{
return -1;//Wrong item or index not in range.
return (count - (maxSpaceOfSlots - slot.GetCount()));
}
}
public int removeItemAt(int index, int count)
bool CountFitsInSlot(int count, Slot slot)
{
// Removes a number of items (count = 5) from a specific slot (index = 4).
// Example inventory.removeItemAt(4,5)
if (indexIsInRange(index))
int leftSize = maxSpaceOfSlots - slot.GetCount();
if (leftSize > count) // left size > count
{
if (inventory[index].ItemType == null)
{
return count;// Can't remove any items if the slot is empty.
}
else if (inventory[index].Count > count)
{
inventory[index].removeItem(count);// Removes the number of items if there are more or equal items in the slot.
return 0;
}
else if (inventory[index].Count <= count)
{
int rest = count - inventory[index].Count;
inventory[index].Count = 0; // Removes all the items from the slot and returns the number of items that could not be removed.
inventory[index].ItemType = null; // When the slot is empty the itemType can also be removed.
return rest;
}
else
{
return -1; // Something went wrong (you should never end up in here).
}
return true;
}
else
{
return -1;//Index not in range.
return false;
}
}
private bool indexIsInRange(int index)
bool indexIsInRange(int index)
{
// Returns true if a given index is in the bounds of the inventory.
// Example (maxSize = 10) index = -10 : false , index = 100: false, index = 7 : true
@@ -142,4 +105,126 @@ public class Inventory : MonoBehaviour
return false;
}
}
public int Remove(Item itemType, int count, int invIndex = -1)
{
Slot slot = null;
Item item = Instantiate(itemType);
if (item == null)
return -1;
// Get Slot
if (invIndex > -1)
{
if (indexIsInRange(invIndex))
slot = inventory[invIndex];
}
else
{
slot = GetSlotByItem(item);
}
if (slot == null)
return -1;
// remove
if (slot.GetItem() != null && slot.GetItem().id == item.id) // Wenn im Slot schon das gleiche item ist
{
int rest = 0;
if (slot.GetCount() >= count)
{
slot.RemoveCount(count);
}
else
{
rest = count - slot.GetCount();
slot.Clear();
}
OnInventoryChanged?.Invoke();
OnItemRemoved?.Invoke();
return rest;
}
else
return -1;
}
public int Add(Item itemType, int count, int invIndex = -1)
{
int rest = 0;
Slot slot = null;
Item item = Instantiate(itemType);
if (item == null)
return -1;
// Get Slot
if (invIndex > -1)
{
if(indexIsInRange(invIndex))
slot = inventory[invIndex];
}
else
{
slot = GetSlotByItem(item);
if (slot == null)
slot = GetEmptySlot();
}
if (slot == null)
return -1;
// add
if (slot.GetItem() != null && slot.GetItem().id == item.id) // Wenn im Slot schon das selbe item ist
{
if (CountFitsInSlot(count, slot))
{
slot.AddCount(count);
}
else
{
rest = GetRest(count, slot);
slot.Set(item, maxSpaceOfSlots);
}
}
else if (slot.GetItem() == null)
{
if (CountFitsInSlot(count, slot))
{
slot.Set(item, count);
}
else
{
rest = GetRest(count, slot);
slot.Set(item, maxSpaceOfSlots);
}
}
else
return -1;
OnInventoryChanged?.Invoke();
OnItemAdded?.Invoke();
return rest;
}
void Start()
{
AddSlots(numberOfSlots);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
Debug.Log(Add(testItemType, 8, 4));
}
if (Input.GetKeyDown(KeyCode.T))
{
Debug.Log(Remove(testItemType, 8, 4));
}
}
}

View File

@@ -4,7 +4,7 @@ using UnityEngine;
public class InventoryController : MonoBehaviour
{
[SerializeField] Item item1;// not needed
/*[SerializeField] Item item1;// not needed
[SerializeField] Item item2;// not needed
public Inventory inventory;
[SerializeField] UI_Inventory uiInventory;
@@ -18,12 +18,11 @@ public class InventoryController : MonoBehaviour
inventory.addItemAt(3, item2, 15);
inventory.addItemAt(4, item1, 3);
/*
Debug.Log(inventory.addItemAt(0, item2, 15));
Debug.Log(inventory.getInventory[0].Count);
Debug.Log(inventory.removeItemAt(0, 10));
Debug.Log(inventory.getInventory[0].Count);
*/
uiInventory.setInventory(inventory);
}
void Start()
@@ -35,5 +34,5 @@ public class InventoryController : MonoBehaviour
void Update()
{
}
}*/
}

View File

@@ -3,10 +3,20 @@ using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "Item", menuName = "Items/Item", order = 1)]
public class Item : ScriptableObject
public abstract class Item : ScriptableObject
{
public new string name;
public int id;
public bool isStackable;
public Sprite sprite;
public bool isSelected;
public abstract void OnSelect();
public void Select()
{
isSelected = !isSelected;
OnSelect();
}
}

View File

@@ -1,18 +0,0 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: de55e2e3081d4ee4a9d0737e718a5b3b, type: 3}
m_Name: Lumber
m_EditorClassIdentifier:
name: Lumber
id: 3
isStackable: 1
sprite: {fileID: 21300000, guid: 636c61591a702914dadb6abf17d92f6f, type: 3}

View File

@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 90fd8b1738596514f860a4ce5adaba88
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,18 +0,0 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: de55e2e3081d4ee4a9d0737e718a5b3b, type: 3}
m_Name: Stone
m_EditorClassIdentifier:
name: Stone
id: 0
isStackable: 1
sprite: {fileID: 21300000, guid: 475f050511847c0499c1ca4f47fcaefa, type: 3}

View File

@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: e04039a6cbed5e5439b605aeee90485f
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,18 +0,0 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: de55e2e3081d4ee4a9d0737e718a5b3b, type: 3}
m_Name: Wood
m_EditorClassIdentifier:
name: Wood
id: 2
isStackable: 1
sprite: {fileID: 21300000, guid: 30728ae26107dfe438d180ab175fdaca, type: 3}

View File

@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 8ac3fa2505f8292498c5c20cf4fea84d
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,11 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "ResourceItem", menuName = "Items/ResourceItem", order = 2)]
public class ResourceItem : Item
{
public override void OnSelect()
{
throw new System.NotImplementedException();
}
}

View File

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

View File

@@ -1,54 +1,47 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class Slot
{
/// <summary>
/// Holds an itemType the number of items and the number of maxItems
/// this has no logic so everything has to be done from the outside
/// [Get/Set]
/// ItemType
/// Count
/// MaxItems
/// [Methods]
/// void addItem(int count = 1)
/// void removeItem(int count = 1)
/// void clear()
/// </summary>
[SerializeField]
Item item = null;
Item item;
int maxItems = 1;
[SerializeField]
int count = 0;
public Item ItemType { get => item; set => item = value; }
public int Count { get => count; set => count = value; }
public int MaxItems { get => maxItems; set => maxItems = value; }
public Slot(int maxItems = 1)
public Item GetItem() => item;
public int GetCount() => count;
public void AddCount(int value) => count += value;
public void SetCount(int newCount)
{
item = null;
this.maxItems = maxItems;
count = newCount;
if (count <= 0)
Clear();
}
public void addItem(int count = 1)
public void RemoveCount(int value)
{
// adds any number of items to the slot will also go over the max items
this.count += count;
count -= value;
if (count <= 0)
Clear();
}
public void removeItem(int count = 1)
{
// removes any number of items from the slot will also go negative
this.count -= count;
}
public void clear()
public void ResetCount() => count = 0;
public void Clear()
{
item = null;
count = 0;
}
public Slot copy()
{
Slot slot = new Slot(maxItems);
slot.count = count;
slot.ItemType = ItemType;
return slot;
public void Set(Item _item, int _count = 1)
{
item = _item;
count = _count;
}
}

View File

@@ -0,0 +1,26 @@
using UnityEngine;
[CreateAssetMenu(fileName = "ToolItem", menuName = "Items/Tool", order = 2)]
public class ToolItem : Item
{
public ToolType toolType;
float durability = 1f;
SpriteRenderer playerHandSpriteRenderer;
public override void OnSelect()
{
playerHandSpriteRenderer = GameObject.Find("Player").transform.Find("Hand").gameObject.GetComponent<SpriteRenderer>();
if (isSelected)
{
playerHandSpriteRenderer.GetComponent<SpriteRenderer>().sprite = sprite;
}
else
{
playerHandSpriteRenderer.GetComponent<SpriteRenderer>().sprite = null;
}
}
}

View File

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

View File

@@ -0,0 +1,10 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum ToolType
{
AXE,
PICKAXE,
HOE,
}

View File

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

View File

@@ -6,7 +6,7 @@ using UnityEngine.UI;
public class UI_Inventory : MonoBehaviour
{
Inventory playerInventory;
/* Inventory playerInventory;
public Transform inventoryContainer;
public Transform itemSlotTemplate;
private RectTransform[] UIItemSlots;
@@ -132,5 +132,5 @@ public class UI_Inventory : MonoBehaviour
UIItemSlots[index] = itemSlotRectTransform;
index++;
}
}
}*/
}