A 2D Farming RPG
This is just a simple 2D farming RPG game
Loading...
Searching...
No Matches
GridCursor.cs
Go to the documentation of this file.
1using UnityEngine;
2using UnityEngine.UI;
3
4public class GridCursor : MonoBehaviour
5{
6 private Canvas m_Canvas;
7 private Grid m_Grid;
8 private Camera m_MainCamera;
9
10 [SerializeField] private Image m_CursorImage = null;
11 [SerializeField] private RectTransform m_CursorRectTransform = null;
12 [SerializeField] private Sprite m_GreenCursorSprite = null;
13 [SerializeField] private Sprite m_RedCursorSprite = null;
14
15 private bool m_CursorPositionIsValid = false;
16 private int m_ItemUseGridRadius = 0;
18 private bool m_CursorIsEnabled = false;
19
21 public int ItemUseGridRadius { get => m_ItemUseGridRadius; set => m_ItemUseGridRadius = value; }
23 public bool CursorIsEnabled { get => m_CursorIsEnabled; set => m_CursorIsEnabled = value; }
24
28 private void OnDisable()
29 {
30 EventHandler.AfterSceneLoadEvent -= SceneLoaded;
31 }
32
36 private void OnEnable()
37 {
38 EventHandler.AfterSceneLoadEvent += SceneLoaded;
39 }
40
44 private void Start()
45 {
46 m_MainCamera = Camera.main;
47 m_Canvas = GetComponentInParent<Canvas>();
48 }
49
53 private void Update()
54 {
55 if( CursorIsEnabled )
56 {
58 }
59 }
60
65 private Vector3Int DisplayCursor()
66 {
67 if( m_Grid != null )
68 {
69 // Get grid position for the cursor.
70 Vector3Int gridPosition = GetGridPositionForCursor();
71
72 // Get grid position for player.
73 Vector3Int playerGridPosition = GetGridPositionForPlayer();
74
75 // Set cursor sprite.
76 SetCursorValidity( gridPosition, playerGridPosition );
77
78 // Get rect transform position for cursor.
79 m_CursorRectTransform.position = GetRectTransformPositionForCursor( gridPosition );
80
81 return gridPosition;
82 }
83 else
84 {
85 return Vector3Int.zero;
86 }
87 }
88
92 private void SceneLoaded()
93 {
94 m_Grid = GameObject.FindObjectOfType<Grid>();
95 }
96
103 private void SetCursorValidity( Vector3Int cursorGridPosition, Vector3Int playerGridPosition )
104 {
106
107 // Check item user radius is valid.
108 if( ( Mathf.Abs( cursorGridPosition.x - playerGridPosition.x ) > ItemUseGridRadius ) ||
109 ( Mathf.Abs( cursorGridPosition.y - playerGridPosition.y ) > ItemUseGridRadius ) )
110 {
112 return;
113 }
114
115 // Get selected item details.
116 ItemDetails itemDetails = InventoryManager.Instance.GetSelectedInventoryItemDetails( InventoryLocation.Player );
117
118 if( itemDetails == null )
119 {
121 return;
122 }
123
124 // Get grid property details at cursor position.
125 GridPropertyDetails gridPropertyDetails = GridPropertiesManager.Instance.GetGridPropertyDetails( cursorGridPosition.x, cursorGridPosition.y );
126
127 if( gridPropertyDetails != null )
128 {
129 // Determine cursor validity based on inventory item selected and grid property details.
130 switch( itemDetails.m_ItemType )
131 {
132 case ItemType.Seed:
133 if( !IsCursorValidForSeed( gridPropertyDetails ) )
134 {
136 return;
137 }
138 break;
139
140 case ItemType.Commodity:
141 if( !IsCursorValidForCommodity( gridPropertyDetails ) )
142 {
144 return;
145 }
146 break;
147
148 case ItemType.None:
149 break;
150
151 case ItemType.Count:
152 break;
153
154 default:
155 break;
156 }
157 }
158 else
159 {
161 return;
162 }
163 }
164
168 private void SetCursorToValid()
169 {
170 m_CursorImage.sprite = m_GreenCursorSprite;
172 }
173
177 private void SetCursorToInvalid()
178 {
179 m_CursorImage.sprite = m_RedCursorSprite;
180 CursorPositionIsValid = false;
181 }
182
188 private bool IsCursorValidForCommodity( GridPropertyDetails gridPropertyDetails )
189 {
190 return gridPropertyDetails.m_CanDropItem;
191 }
192
197 private bool IsCursorValidForSeed( GridPropertyDetails gridPropertyDetails )
198 {
199 return gridPropertyDetails.m_CanDropItem;
200 }
201
205 public void DisableCursor()
206 {
207 m_CursorImage.color = Color.clear;
208 CursorIsEnabled = false;
209 }
210
214 public void EnableCursor()
215 {
216 m_CursorImage.color = new Color( 1f, 1f, 1f, 1f );
217 CursorIsEnabled = true;
218 }
219
224 public Vector3Int GetGridPositionForCursor()
225 {
226 // Z is how far the objects infront of the camera.
227 Vector3 worldPosition = m_MainCamera.ScreenToWorldPoint( new Vector3( Input.mousePosition.x, Input.mousePosition.y, -m_MainCamera.transform.position.z ) );
228 return m_Grid.WorldToCell( worldPosition );
229 }
230
235 public Vector3Int GetGridPositionForPlayer()
236 {
237 return m_Grid.WorldToCell( Player.Instance.transform.position );
238 }
239
244 public Vector2 GetRectTransformPositionForCursor( Vector3Int gridPosition )
245 {
246 Vector3 gridWorldPosition = m_Grid.CellToWorld( gridPosition );
247 Vector2 gridScreenPosition = m_MainCamera.WorldToScreenPoint( gridWorldPosition );
248 return RectTransformUtility.PixelAdjustPoint( gridScreenPosition, m_CursorRectTransform, m_Canvas );
249 }
250
251}
InventoryLocation
Describes the type of inventory and count Ex: player inventory or chest inventory
Definition: Enums.cs:108
ItemType
Describe the item type
Definition: Enums.cs:162
Camera m_MainCamera
Definition: GridCursor.cs:8
void OnDisable()
Unsubscribe sceneload
Definition: GridCursor.cs:28
Sprite m_GreenCursorSprite
Definition: GridCursor.cs:12
int ItemUseGridRadius
Definition: GridCursor.cs:21
void Start()
Initialize camera and canvas
Definition: GridCursor.cs:44
bool IsCursorValidForSeed(GridPropertyDetails gridPropertyDetails)
Test cursor validity for a seed for the target gridPropertyDetails.
Definition: GridCursor.cs:197
bool CursorIsEnabled
Definition: GridCursor.cs:23
ItemType m_SelectedItemType
Definition: GridCursor.cs:17
ItemType SelectedItemType
Definition: GridCursor.cs:22
void SetCursorValidity(Vector3Int cursorGridPosition, Vector3Int playerGridPosition)
Check series of checks to determine validity of the cursor location. Based on the validity set the cu...
Definition: GridCursor.cs:103
bool m_CursorIsEnabled
Definition: GridCursor.cs:18
Vector2 GetRectTransformPositionForCursor(Vector3Int gridPosition)
Transfor the UI Items on canvas to pixel positions.
Definition: GridCursor.cs:244
Image m_CursorImage
Definition: GridCursor.cs:10
Vector3Int GetGridPositionForCursor()
Return the position of the cursor.
Definition: GridCursor.cs:224
Vector3Int DisplayCursor()
Display the cursor which marked valid and invalid drop locations.
Definition: GridCursor.cs:65
void SetCursorToValid()
Set the cursor to valid.
Definition: GridCursor.cs:168
Grid m_Grid
Definition: GridCursor.cs:7
void Update()
Call display cursor function at every frame.
Definition: GridCursor.cs:53
void SceneLoaded()
Initialize the Grid
Definition: GridCursor.cs:92
bool m_CursorPositionIsValid
Definition: GridCursor.cs:15
void DisableCursor()
Disable the cursor.
Definition: GridCursor.cs:205
Sprite m_RedCursorSprite
Definition: GridCursor.cs:13
bool IsCursorValidForCommodity(GridPropertyDetails gridPropertyDetails)
Test cursor validity for a commodity for the target gridPropertyDetails.
Definition: GridCursor.cs:188
void SetCursorToInvalid()
Set the cursor to invalid.
Definition: GridCursor.cs:177
void OnEnable()
Subscribe scene load.
Definition: GridCursor.cs:36
int m_ItemUseGridRadius
Definition: GridCursor.cs:16
void EnableCursor()
Enable the cursor.
Definition: GridCursor.cs:214
RectTransform m_CursorRectTransform
Definition: GridCursor.cs:11
Vector3Int GetGridPositionForPlayer()
Return the grid position of the player.
Definition: GridCursor.cs:235
bool CursorPositionIsValid
Definition: GridCursor.cs:20
Canvas m_Canvas
Definition: GridCursor.cs:6
This will get the information from serialiable object and populate these class Basically this will st...
This will get the information from serialiable object and populate these class Basically this will st...
This class will manage all the inventory related operations.
This class holds information of items.
Definition: ItemDetails.cs:8
ItemType m_ItemType
Definition: ItemDetails.cs:17
This class has all the things related to player
Definition: Player.cs:8