Merge branch 'Inventory' into merge-inventory

This commit is contained in:
j.mei7
2022-02-21 19:12:08 +01:00
49 changed files with 2683 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ButtonEventHandler : MonoBehaviour
{
[SerializeField] UI_Inventory inventory;
}

View File

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

View File

@@ -0,0 +1,8 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Identity : MonoBehaviour
{
public int index;
}

View File

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

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 27c09b7e7d0e7824f882a062a77737c2
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,10 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "FoodItem", menuName = "Items/FoodItem", order = 2)]
public class FoodItem : Item
{
public int health;
public int water;
public int regeneration;
}

View File

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

View File

@@ -0,0 +1,145 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour
{
/// <summary>
/// -------------------------------------------------------------------------------///
///
/// Inventory holds a list of Slots where Items can be added.
///
///[IMPORTANT]
/// !!createEmptyInventory(int numberOfSlots, int maxSpaceOfSlots) : Has to be called after the Inventory has been initialized!!
///
///[Methods]
/// getInventory() : Gets the list of Slots.
/// addItemAt(int index, Item itemType,int count) : Adds a number (count) of items of type itemType (itemType) to an item slot(index).
/// removeItemAt(int index, int count) : Removes a number of items (count) from a specific slot (index).
/// addInventorySlots(int numberOfSlots, int maxSpaceOfSlots = 10)
///
/// -------------------------------------------------------------------------------///
/// </summary>
public delegate void OnItemChanged();
public OnItemChanged onItemChangedCallback; //
List<Slot> inventory = new List<Slot>();
public List<Slot> getInventory { get => inventory;}
public void createEmptyInventory(int numberOfSlots, int maxSpaceOfSlots = 10)
{
// 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++)
{
inventory.Add(new Slot(maxSpaceOfSlots));
}
if (onItemChangedCallback != null)
onItemChangedCallback.Invoke();
}
public void addInventorySlots(int numberOfSlots, int maxSpaceOfSlots = 10)
{
// 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++)
{
inventory.Add(new Slot(maxSpaceOfSlots));
}
if (onItemChangedCallback != null)
onItemChangedCallback.Invoke();
}
public int addItemAt(int index, Item itemType,int count)
{
// 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))
{
if (inventory[index].ItemType == null)
{
inventory[index].ItemType = Instantiate(itemType); // Set the itemType if the slot was empty.
}
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;
}
}
else
{
return -1;//Wrong item or index not in range.
}
}
public int removeItemAt(int index, int count)
{
// Removes a number of items (count = 5) from a specific slot (index = 4).
// Example inventory.removeItemAt(4,5)
if (indexIsInRange(index))
{
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).
}
}
else
{
return -1;//Index not in range.
}
}
private 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
if (index < inventory.Count && index >= 0)
{
return true;
}
else
{
return false;
}
}
}

View File

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

View File

@@ -0,0 +1,39 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InventoryController : MonoBehaviour
{
[SerializeField] Item item1;// not needed
[SerializeField] Item item2;// not needed
public Inventory inventory;
[SerializeField] UI_Inventory uiInventory;
private void Awake()
{
inventory = transform.GetComponent<Inventory>();
inventory.createEmptyInventory(8,10);
inventory.addItemAt(0, item1, 8);
inventory.addItemAt(0, item1, 1);
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()
{
}
// Update is called once per frame
void Update()
{
}
}

View File

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

View File

@@ -0,0 +1,12 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "Item", menuName = "Items/Item", order = 1)]
public class Item : ScriptableObject
{
public new string name;
public int id;
public bool isStackable;
public Sprite sprite;
}

View File

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

View File

@@ -0,0 +1,19 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemAtlas : MonoBehaviour
{
public static ItemAtlas Instance { get; private set; }
private void Awake()
{
Instance = this;
}
enum ItemTypes {
STONE,TOMATO
}
public Sprite stoneSprite;
public Sprite woodSprite;
}

View File

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

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: db274161008cc6a4a8c92d42b6a81832
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,237 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &1031884711933943707
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 2917703646420147887}
- component: {fileID: 9007524712572810876}
- component: {fileID: 5255690316907466923}
- component: {fileID: 2310262034725730047}
m_Layer: 0
m_Name: Button
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &2917703646420147887
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1031884711933943707}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 4215392545672552190}
m_Father: {fileID: 4215392545969358082}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: -0, y: 0}
m_SizeDelta: {x: 85, y: 85}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &9007524712572810876
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1031884711933943707}
m_CullTransparentMesh: 1
--- !u!114 &5255690316907466923
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1031884711933943707}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_Sprite: {fileID: 21300000, guid: 0543fd6b654bedd4fa82b563887c0609, type: 3}
m_Type: 1
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 1
--- !u!114 &2310262034725730047
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1031884711933943707}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4e29b1a8efbd4b44bb3f3716e73f07ff, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Navigation:
m_Mode: 0
m_WrapAround: 0
m_SelectOnUp: {fileID: 0}
m_SelectOnDown: {fileID: 0}
m_SelectOnLeft: {fileID: 0}
m_SelectOnRight: {fileID: 0}
m_Transition: 1
m_Colors:
m_NormalColor: {r: 1, g: 1, b: 1, a: 1}
m_HighlightedColor: {r: 0.4811321, g: 0.4811321, b: 0.4811321, a: 1}
m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1}
m_SelectedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608}
m_ColorMultiplier: 1
m_FadeDuration: 0.1
m_SpriteState:
m_HighlightedSprite: {fileID: 0}
m_PressedSprite: {fileID: 0}
m_SelectedSprite: {fileID: 0}
m_DisabledSprite: {fileID: 0}
m_AnimationTriggers:
m_NormalTrigger: Normal
m_HighlightedTrigger: Highlighted
m_PressedTrigger: Pressed
m_SelectedTrigger: Selected
m_DisabledTrigger: Disabled
m_Interactable: 1
m_TargetGraphic: {fileID: 5255690316907466923}
m_OnClick:
m_PersistentCalls:
m_Calls: []
--- !u!1 &4215392545672552185
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 4215392545672552190}
- component: {fileID: 4215392545672552188}
- component: {fileID: 4215392545672552191}
m_Layer: 0
m_Name: Icon
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &4215392545672552190
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4215392545672552185}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 2917703646420147887}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 70, y: 70}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &4215392545672552188
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4215392545672552185}
m_CullTransparentMesh: 1
--- !u!114 &4215392545672552191
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4215392545672552185}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_Sprite: {fileID: 21300000, guid: 475f050511847c0499c1ca4f47fcaefa, type: 3}
m_Type: 0
m_PreserveAspect: 1
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 1
--- !u!1 &4215392545969358109
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 4215392545969358082}
m_Layer: 0
m_Name: ContainerTemplate
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &4215392545969358082
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4215392545969358109}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 2917703646420147887}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: -443, y: 131.1}
m_SizeDelta: {x: 100, y: 100}
m_Pivot: {x: 0.5, y: 0.5}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: ac29b266608f92b419b28a16d8ee7c1b
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,18 @@
%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

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

View File

@@ -0,0 +1,18 @@
%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

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

View File

@@ -0,0 +1,21 @@
%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: 0f6fecdcda7efd84baa350f4c41432a6, type: 3}
m_Name: Tomato
m_EditorClassIdentifier:
name: Tomato
id: 1
isStackable: 1
sprite: {fileID: 21300000, guid: 5ee3152fa9663e94092921b0bd6b4821, type: 3}
health: 15
water: 10
regeneration: 2

View File

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

View File

@@ -0,0 +1,18 @@
%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

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

View File

@@ -0,0 +1,54 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
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>
Item item;
int maxItems = 1;
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)
{
item = null;
this.maxItems = maxItems;
}
public void addItem(int count = 1)
{
// adds any number of items to the slot will also go over the max items
this.count += count;
}
public void removeItem(int count = 1)
{
// removes any number of items from the slot will also go negative
this.count -= count;
}
public void clear()
{
item = null;
count = 0;
}
public Slot copy()
{
Slot slot = new Slot(maxItems);
slot.count = count;
slot.ItemType = ItemType;
return slot;
}
}

View File

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

View File

@@ -0,0 +1,136 @@
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class UI_Inventory : MonoBehaviour
{
Inventory playerInventory;
public Transform inventoryContainer;
public Transform itemSlotTemplate;
private RectTransform[] UIItemSlots;
private Button[] buttons;
private Slot pickedItem = new Slot();
private RectTransform pickedItemSlot;
private void Awake()
{
//itemSlotTemplate = inventoryContainer.Find("containerTemplate");
pickedItemSlot = Instantiate(itemSlotTemplate, this.transform).GetComponent<RectTransform>();
}
void pickItem(int index)
{
pickedItem = playerInventory.getInventory[index].copy();
playerInventory.getInventory[index].clear();
UIItemSlots[index].GetChild(0).Find("Icon").gameObject.SetActive(false);
}
void placeItem()
{
}
public void buttonEvent(int index)
{
if (pickedItem.ItemType == null && playerInventory.getInventory[index].ItemType != null)
{
pickItem(index);
}
else
{
if (pickedItem.ItemType != null)
{
int rest = playerInventory.addItemAt(index, pickedItem.ItemType, pickedItem.Count);
if (rest == 0)
{
pickedItem.clear();
}
else
{
pickedItem.Count = rest;
}
//placeItem(index);
}
}
//Debug.Log("button " + index + " has been pressed!");
}
public void setInventory(Inventory inventory)
{
playerInventory = inventory;
inventory.onItemChangedCallback += updateInventory;
updateInventory();
}
private void Update()
{
//buttons[index] = itemSlotRectTransform.Find("Button").GetComponent<Button>();
//itemSlotRectTransform.Find("Button").GetComponent<Identity>().index = index;
//buttons[index].onClick.AddListener(() => buttonEvent(itemSlotRectTransform.Find("Button").GetComponent<Identity>().index));
pickedItemSlot.anchoredPosition = new Vector2(Input.mousePosition.x, 0);
pickedItemSlot.gameObject.SetActive(true);
pickedItemSlot.gameObject.name = "pickedItem";
if (pickedItem.ItemType != null)
{
Transform item = pickedItemSlot.GetChild(0).Find("Icon");
item.gameObject.SetActive(true);
item.GetComponent<Image>().sprite = pickedItem.ItemType.sprite;
}
else
{
Transform item = pickedItemSlot.GetChild(0).Find("Icon");
item.gameObject.SetActive(false);
}
}
private void updateInventory()
{
int rows = 2;
int columns = 4;
float slotSize = 95;
int x = 0;
int y = 0;
int index = 0;
if (UIItemSlots != null) {
foreach (RectTransform ItemSLot in UIItemSlots)
{
Destroy(ItemSLot.gameObject);
}
}
UIItemSlots = new RectTransform[playerInventory.getInventory.Count];
buttons = new Button[playerInventory.getInventory.Count];
//inventoryContainer.GetComponent<RectTransform>().
foreach (Slot slot in playerInventory.getInventory)
{
RectTransform itemSlotRectTransform = Instantiate(itemSlotTemplate, inventoryContainer).GetComponent<RectTransform>();
buttons[index] = itemSlotRectTransform.Find("Button").GetComponent<Button>();
itemSlotRectTransform.Find("Button").GetComponent<Identity>().index = index;
buttons[index].onClick.AddListener(() => buttonEvent(itemSlotRectTransform.Find("Button").GetComponent<Identity>().index));
itemSlotRectTransform.anchoredPosition = new Vector2(x* slotSize + slotSize * 0.5f,y * slotSize - slotSize * 0.5f );
itemSlotRectTransform.gameObject.SetActive(true);
itemSlotRectTransform.gameObject.name = "ItemSlot" + index;
if (slot.ItemType != null)
{
Transform item = itemSlotRectTransform.GetChild(0).Find("Icon");
Transform itemCount = itemSlotRectTransform.GetChild(0).Find("Count");
item.gameObject.SetActive(true);
item.GetComponent<Image>().sprite = slot.ItemType.sprite;
if (slot.Count > 1)
{
itemCount.gameObject.SetActive(true);
itemCount.GetComponent<TextMeshProUGUI>().text = slot.Count.ToString();
}
}
x++;
if (x > 3)
{
x = 0;
y--;
}
UIItemSlots[index] = itemSlotRectTransform;
index++;
}
}
}

View File

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