A 2D Farming RPG
This is just a simple 2D farming RPG game
Loading...
Searching...
No Matches
GridPropertiesManager.cs
Go to the documentation of this file.
1using System.Collections.Generic;
2using UnityEngine;
3
8[RequireComponent(typeof(GenerateGUID))]
9public class GridPropertiesManager : SingletonMonobehaviour<GridPropertiesManager>, ISaveable
10{
11 public Grid m_Grid;
12 private Dictionary<string, GridPropertyDetails> m_GridPropertyDictionary;
13 [SerializeField] private SO_GridProperties[] m_SOGridPropertiesArray = null;
14
15 private string m_ISaveableUniqueID;
16
17 public string ISaveableUniqueID { get { return m_ISaveableUniqueID; } set { m_ISaveableUniqueID = value; } }
18
20
21 public GameObjectSave GameObjectSave { get { return m_GameObjectSave; } set { m_GameObjectSave = value; } }
22
26 protected override void Awake()
27 {
28 base.Awake();
29 ISaveableUniqueID = GetComponent<GenerateGUID>().GUID;
31 }
32
37 private void OnEnable()
38 {
40 EventHandler.AfterSceneLoadEvent += AfterSceneLoaded;
41 }
42
47 private void OnDisable()
48 {
50 EventHandler.AfterSceneLoadEvent -= AfterSceneLoaded;
51 }
52
56 private void Start()
57 {
59 }
60
66 {
67 foreach( SO_GridProperties soGridProperties in m_SOGridPropertiesArray )
68 {
69 // Create dictionary of grid property details
70 Dictionary<string, GridPropertyDetails> gridPropertyDictionary = new Dictionary<string, GridPropertyDetails>();
71
72 // Populate grid property dictionaary - Iterate through all the grid properties in the so gridproperties list
73 foreach( GridProperty gridProperty in soGridProperties.m_GridPropertyList )
74 {
75 GridPropertyDetails gridPropertyDetails;
76 gridPropertyDetails = GetGridPropertyDetails( gridProperty.m_GridCoordinate.x, gridProperty.m_GridCoordinate.y, gridPropertyDictionary );
77
78 if( gridPropertyDetails == null )
79 {
80 gridPropertyDetails = new GridPropertyDetails();
81 }
82
83 switch( gridProperty.m_GridBoolProperty )
84 {
85 case GridBoolProperty.Diggable:
86 gridPropertyDetails.m_IsDiggable = gridProperty.m_GridBoolValue;
87 break;
88
89 case GridBoolProperty.CanDropItem:
90 gridPropertyDetails.m_CanDropItem = gridProperty.m_GridBoolValue;
91 break;
92
93 case GridBoolProperty.CanPlaceFurniture:
94 gridPropertyDetails.m_CanPlaceFurniture = gridProperty.m_GridBoolValue;
95 break;
96
97 case GridBoolProperty.IsPath:
98 gridPropertyDetails.m_IsPath = gridProperty.m_GridBoolValue;
99 break;
100
101 case GridBoolProperty.IsNPCObstacle:
102 gridPropertyDetails.m_IsNPCObstacle = gridProperty.m_GridBoolValue;
103 break;
104
105 default:
106 break;
107
108 }
109
110 SetGridPropertyDetails( gridProperty.m_GridCoordinate.x, gridProperty.m_GridCoordinate.y, gridPropertyDetails, gridPropertyDictionary );
111
112 }
113
114 // Create scene save for this gameobject
115 SceneSave sceneSave = new SceneSave();
116
117 // Add grid property dictionary to scene save data.
118 sceneSave.m_GridPropertyDetailsDictionary = gridPropertyDictionary;
119
120 // If staring scene set the gridpropertyDictionary member variable to the current iteration
121 if( soGridProperties.m_SceneName.ToString() == SceneControllerManager.Instance.m_StartingSceneName.ToString() )
122 {
123 this.m_GridPropertyDictionary = gridPropertyDictionary;
124 }
125
126 // Add scene save to game object scene data
127 GameObjectSave.m_SceneData.Add( soGridProperties.m_SceneName.ToString(), sceneSave );
128 }
129 }
130
134 private void AfterSceneLoaded()
135 {
136 m_Grid = GameObject.FindObjectOfType<Grid>();
137 }
138
146 public GridPropertyDetails GetGridPropertyDetails( int gridX, int gridY, Dictionary<string, GridPropertyDetails> gridPropertyDictionary )
147 {
148 // Construct key from coordinate
149 string key = "x" + gridX + "y" + gridY;
150
151 GridPropertyDetails gridPropertyDetails;
152
153 // Check if grid property details exist for coordinate and retrieve
154 if( !gridPropertyDictionary.TryGetValue( key, out gridPropertyDetails ) )
155 {
156 // if not found
157 return null;
158 }
159 else
160 {
161 return gridPropertyDetails;
162 }
163 }
164
171 public GridPropertyDetails GetGridPropertyDetails( int gridX, int gridY )
172 {
173 return GetGridPropertyDetails( gridX, gridY, m_GridPropertyDictionary );
174 }
175
180 {
181 SaveLoadManager.Instance.iSaveableObjectList.Remove( this );
182 }
183
187 public void ISaveableRegister()
188 {
189 SaveLoadManager.Instance.iSaveableObjectList.Add (this );
190 }
191
197 public void ISaveableRestoreScene( string sceneName )
198 {
199 // Get sceneSave for scene - it exists since we created it in initialise.
200 if( GameObjectSave.m_SceneData.TryGetValue( sceneName, out SceneSave sceneSave ) )
201 {
202 // get grid property details dictionary - it exists since we created it in initialise
203 if( sceneSave.m_GridPropertyDetailsDictionary != null )
204 {
205 m_GridPropertyDictionary = sceneSave.m_GridPropertyDetailsDictionary;
206 }
207 }
208 }
209
216 public void ISaveableStoreScene( string sceneName )
217 {
218 // Remove sceneSave for scene
219 GameObjectSave.m_SceneData.Remove( sceneName );
220
221 // Create sceneSave for scene
222 SceneSave sceneSave = new SceneSave();
223
224 // Create and add dictionary grid property details.
225 sceneSave.m_GridPropertyDetailsDictionary = m_GridPropertyDictionary;
226
227 // Add scene save to game object scene data
228 GameObjectSave.m_SceneData.Add( sceneName, sceneSave );
229 }
230
234 public void SetGridPropertyDetails( int gridX, int gridY, GridPropertyDetails gridPropertyDetails )
235 {
236 SetGridPropertyDetails( gridX, gridY, gridPropertyDetails, m_GridPropertyDictionary );
237 }
238
246 public void SetGridPropertyDetails(int gridX, int gridY, GridPropertyDetails gridPropertyDetails, Dictionary<string, GridPropertyDetails> gridPropertyDictionary )
247 {
248 // Construct key from coordinate
249 string key = "x" + gridX + "y" + gridY;
250
251 gridPropertyDetails.m_gridX = gridX;
252 gridPropertyDetails.m_gridY = gridY;
253
254 // Set value
255 gridPropertyDictionary[key] = gridPropertyDetails;
256 }
257
258}
GridBoolProperty
Describes the Grid bool property. Each grid holds following information.
Definition: Enums.cs:95
Store all scenes in here. If we have 3 scenes, we have 3 entries.
Dictionary< string, SceneSave > m_SceneData
Assign a unique GUID for the objects. These ids are used to retrive the gameobjects when restoring sc...
Definition: GenerateGUID.cs:9
int x
Define x Axis
int y
Define y Axis
This will get the information from serialiable object and populate these class Basically this will st...
void Start()
Call InitialiseGridProperties()
SO_GridProperties[] m_SOGridPropertiesArray
void SetGridPropertyDetails(int gridX, int gridY, GridPropertyDetails gridPropertyDetails)
Set the grid property details to gridPropertyDetails for the tile at (gridX, gridY) for current scene...
void ISaveableRestoreScene(string sceneName)
Restore the previous scene when we revist to that scene. Scenario: We moved scene A to scene B....
void ISaveableRegister()
Add the iSaveableObjectList
Dictionary< string, GridPropertyDetails > m_GridPropertyDictionary
void OnDisable()
Call ISaveableDeregister to remove the iSaveableObjectList Unsubscribe from after scene loaded event.
GridPropertyDetails GetGridPropertyDetails(int gridX, int gridY)
Get the grid property details for the tile at (gridX, gridY). If no grid property details exist null ...
void InitialiseGridProperties()
This initialises the grid property dictionary with the values from the SO_GridProperties assets and s...
void SetGridPropertyDetails(int gridX, int gridY, GridPropertyDetails gridPropertyDetails, Dictionary< string, GridPropertyDetails > gridPropertyDictionary)
Set the grid property details to gridPropertyDetails for the tile at (gridX, gridY) for the gridPrope...
void AfterSceneLoaded()
Get the Grid.
void ISaveableDeregister()
Remove the iSaveableObjectList
GridPropertyDetails GetGridPropertyDetails(int gridX, int gridY, Dictionary< string, GridPropertyDetails > gridPropertyDictionary)
Return thr grid property details at the gridLocation for the supplied dictionary, or null if not foun...
void OnEnable()
Call ISaveableRegister to add the iSaveableObjectList Subscribe to after scene loaded event.
void ISaveableStoreScene(string sceneName)
Saves the grid property dictionary for the current scene. This gets called when we moving scene A to ...
This will get the information from serialiable object and populate these class Basically this will st...
Holds information about grid of every scene.
Definition: GridProperty.cs:6
bool m_GridBoolValue
Definition: GridProperty.cs:15
GridBoolProperty m_GridBoolProperty
For example, is this grid diaggable? Is this grid an obstacle? etc...
Definition: GridProperty.cs:13
GridCoordinate m_GridCoordinate
X and Y Axis information.
Definition: GridProperty.cs:9
This ScriptableObject object asset will contain all the information of each scene.
List< GridProperty > m_GridPropertyList
For every grid, we read our tilemap and add to this list.
SceneName m_SceneName
Scene name.
Manage saving and loading scenes. This class is a singleton instance.
Manage the transition between scenes
Store all of the scene items in this list.
Definition: SceneSave.cs:8
Abstract class that inherited by all the game managers. All the game managers use singleton since gam...
Interface for unity ISaveable
Definition: ISaveable.cs:5