A 2D Farming RPG
This is just a simple 2D farming RPG game
Loading...
Searching...
No Matches
UIInventoryBar.cs
Go to the documentation of this file.
1using System.Collections;
2using System.Collections.Generic;
3using UnityEngine;
4
5public class UIInventoryBar : MonoBehaviour
6{
7
8 [SerializeField]
9 private Sprite m_Blank16x16Sprite = null;
10
11 [SerializeField]
13
14 public GameObject m_InventoryBarDraggedItem;
15
16 [HideInInspector]
18
19 private RectTransform m_RectTransform;
20
21 private bool m_IsInventoryBarPositionBottom = true;
22
24
25 private void Awake()
26 {
27 m_RectTransform = GetComponent<RectTransform>();
28 }
29
33 private void OnEnable()
34 {
35 EventHandler.m_InventoryUpdatedEvent += InventoryUpdated;
36 }
37
41 private void OnDisable()
42 {
43 EventHandler.m_InventoryUpdatedEvent -= InventoryUpdated;
44 }
45
49 private void Update()
50 {
52 }
53
58 {
59 if( m_InventorySlot.Length > 0 )
60 {
61 // Loop through inventory slots and clear highlight sprites
62 for( int i = 0; i < m_InventorySlot.Length; i++ )
63 {
64 if( m_InventorySlot[i].m_IsSelected )
65 {
67 m_InventorySlot[i].m_InventorySlotHighlight.color = new Color( 0f, 0f, 0f, 0f );
68
69 // Update inventory to show item as not selected
70 InventoryManager.Instance.ClearSelectedInventoryItem( InventoryLocation.Player );
71 }
72 }
73 }
74 }
75
79 private void ClearInventorySlots()
80 {
81 if( m_InventorySlot.Length > 0 )
82 {
83 for( int i = 0; i < m_InventorySlot.Length; i++ )
84 {
89
91 }
92 }
93 }
94
100 private void InventoryUpdated( InventoryLocation inventoryLocation, List<InventoryItem> inventoryList )
101 {
102 if( inventoryLocation == InventoryLocation.Player )
103 {
105
106 if( m_InventorySlot.Length > 0 && inventoryList.Count > 0 )
107 {
108 // Loop through inventory slots and update with corresponding inventory list item.
109 for( int i = 0; i < m_InventorySlot.Length; i++ )
110 {
111 if( i < inventoryList.Count )
112 {
113 int itemCode = inventoryList[i]._ItemCode;
114
115 // ItemDetails itemDetails = InventoryManager.Instance.m_ItemList.itemDetails.Find( x=> x.itemCode == itemCode );
116 ItemDetails itemDetails = InventoryManager.Instance.GetItemDetails( itemCode );
117
118 if( itemDetails != null )
119 {
120 // Add images and details to inventory item slot
121 m_InventorySlot[i].m_InventorySlotImage.sprite = itemDetails.m_ItemSprite;
122 m_InventorySlot[i].m_TextMeshProUGUI.text = inventoryList[i]._ItemQuantity.ToString();
123 m_InventorySlot[i].m_ItemDetails = itemDetails;
124 m_InventorySlot[i].m_ItemQuantity = inventoryList[i]._ItemQuantity;
125
127 }
128 }
129 else
130 {
131 break;
132 }
133 }
134 }
135 }
136 }
137
142 {
143 if( m_InventorySlot.Length > 0 )
144 {
145 // Loop through inventory slots and clear highlight sprites
146 for( int i = 0; i < m_InventorySlot.Length; i++ )
147 {
149 }
150 }
151 }
152
156 public void SetHighlightedInventorySlots( int itemPosition )
157 {
158 if( m_InventorySlot.Length > 0 && m_InventorySlot[itemPosition].m_ItemDetails != null )
159 {
160 if( m_InventorySlot[itemPosition].m_IsSelected )
161 {
162 m_InventorySlot[itemPosition].m_InventorySlotHighlight.color = new Color( 1f, 1f, 1f, 1f );
163
164 // Update inventory to show item as selected
165 InventoryManager.Instance.SetSelectedInventoryItem( InventoryLocation.Player, m_InventorySlot[itemPosition].m_ItemDetails.m_ItemCode );
166 }
167 }
168 }
169
174 {
175 Vector3 playerViewportPosition = Player.Instance.GetPlayerViewportPosition();
176
177 if( playerViewportPosition.y > 0.3f && IsInventoryBarPositionBottom == false )
178 {
179 // tranform.position = new Vector3( transform.position.x, 7.5f, 0f ); // This was changed to control the rect transform see below
180 m_RectTransform.pivot = new Vector2( 0.5f, 0f );
181 m_RectTransform.anchorMin = new Vector2( 0.5f, 0f );
182 m_RectTransform.anchorMax = new Vector2( 0.5f, 0f );
183 m_RectTransform.anchoredPosition = new Vector2( 0f, 2.5f );
184
186 }
187 else if( playerViewportPosition.y <= 0.3f && IsInventoryBarPositionBottom == true )
188 {
189 // tranform.position = new Vector3( transform.position.x, m_MainCamera.pixelHeight - 120f, 0f ); // This was changed to control the rect transform see below
190 m_RectTransform.pivot = new Vector2(0.5f, 1f);
191 m_RectTransform.anchorMin = new Vector2(0.5f, 1f);
192 m_RectTransform.anchorMax = new Vector2(0.5f, 1f);
193 m_RectTransform.anchoredPosition = new Vector2(0f, -2.5f);
194
196
197 }
198
199 }
200}
InventoryLocation
Describes the type of inventory and count Ex: player inventory or chest inventory
Definition: Enums.cs:108
This class will manage all the inventory related operations.
This class holds information of items.
Definition: ItemDetails.cs:8
Sprite m_ItemSprite
Definition: ItemDetails.cs:12
int m_ItemCode
Definition: ItemDetails.cs:16
This class has all the things related to player
Definition: Player.cs:8
RectTransform m_RectTransform
void InventoryUpdated(InventoryLocation inventoryLocation, List< InventoryItem > inventoryList)
Draw the images of new items in the inventory.
UIInventorySlot[] m_InventorySlot
void ClearInventorySlots()
Clear the inventory slots to default values.
void OnDisable()
Unsubscribed to InventoryUpdatedEvent to trigger InventoryUpdated method.
void OnEnable()
Subscribed to InventoryUpdatedEvent to trigger InventoryUpdated method.
void ClearHighlightOnInventorySlots()
Clear all highlights from the inventory bar.
void SetHighlightedInventorySlots(int itemPosition)
Set the selected highlight if set on an inventory item for a given slot item position
bool IsInventoryBarPositionBottom
void SwitchInventoryBarPosition()
When player move to the bottom we switch our inventory to the top to avoid player been obscured.
Sprite m_Blank16x16Sprite
void Update()
Switch inventory bar position depending on player position
GameObject m_InventoryTextBoxGameObject
GameObject m_InventoryBarDraggedItem
bool m_IsInventoryBarPositionBottom
void SetHighlightedInventorySlots()
Set the selected highlight if set on all inventory item position
Image m_InventorySlotImage
ItemDetails m_ItemDetails
Image m_InventorySlotHighlight
TextMeshProUGUI m_TextMeshProUGUI