A 2D Farming RPG
This is just a simple 2D farming RPG game
Loading...
Searching...
No Matches
SceneItemsManager.cs
Go to the documentation of this file.
1using System.Collections.Generic;
2using UnityEngine;
3
7
8[RequireComponent(typeof(GenerateGUID))]
9public class SceneItemsManager : SingletonMonobehaviour<SceneItemsManager>, ISaveable
10{
11 private Transform m_ParentItem;
12
13 [SerializeField] private GameObject m_ItemPrefab = null;
14
15 private string iSaveableUniqueID;
16
18
19 public string ISaveableUniqueID { get => iSaveableUniqueID; set => iSaveableUniqueID = value; }
21
25 private void AfterSceneLoad()
26 {
27 m_ParentItem = GameObject.FindGameObjectWithTag( Tags.ItemsParentTransform ).transform;
28 }
29
34 protected override void Awake()
35 {
36 base.Awake();
37
38 ISaveableUniqueID = GetComponent<GenerateGUID>().GUID;
40 }
41
45 private void DestroySceneItems()
46 {
47 // Get all items in the scene
48 Item[] itemsInScene = GameObject.FindObjectsOfType<Item>();
49
50 // Loop through all scene items and destroy them
51 for( int i = itemsInScene.Length - 1; i > -1; i--)
52 {
53 Destroy( itemsInScene[i].gameObject );
54 }
55 }
56
61 public void InstantiateSceneItem( int itemCode, Vector3 itemPosition )
62 {
63 GameObject itemGameObject = Instantiate( m_ItemPrefab, itemPosition, Quaternion.identity, m_ParentItem );
64 Item item = itemGameObject.GetComponent<Item>();
65 item.Init( itemCode );
66 }
67
72 private void InstantiateSceneItems( List<SceneItem> sceneItemList )
73 {
74 GameObject itemGameObject;
75
76 foreach( SceneItem sceneItem in sceneItemList )
77 {
78 itemGameObject = Instantiate( m_ItemPrefab, new Vector3( sceneItem.m_Position.m_X, sceneItem.m_Position.m_Y, sceneItem.m_Position.m_Z ), Quaternion.identity, m_ParentItem );
79
80 Item item = itemGameObject.GetComponent<Item>();
81 item.ItemCode = sceneItem.m_ItemCode;
82 item.name = sceneItem.m_ItemName;
83 }
84 }
85
90 private void OnDisable()
91 {
93 EventHandler.AfterSceneLoadEvent -= AfterSceneLoad;
94 }
95
100 private void OnEnable()
101 {
103 EventHandler.AfterSceneLoadEvent += AfterSceneLoad;
104 }
105
110 {
111 SaveLoadManager.Instance.iSaveableObjectList.Remove( this );
112 }
113
117 public void ISaveableRegister()
118 {
119 SaveLoadManager.Instance.iSaveableObjectList.Add( this );
120 }
121
126 public void ISaveableRestoreScene( string sceneName )
127 {
128 if( GameObjectSave.m_SceneData.TryGetValue( sceneName, out SceneSave sceneSave ) )
129 {
130 if( sceneSave.m_ListSceneItem != null )
131 {
132 // Scene list item found - destroy existing items in scene
134
135 // Now instantiate the list of scene items
136 InstantiateSceneItems( sceneSave.m_ListSceneItem );
137 }
138 }
139 }
140
147 public void ISaveableStoreScene( string sceneName )
148 {
149 // Remove old scene save for gameObject if exists
150 GameObjectSave.m_SceneData.Remove( sceneName );
151
152 // Get all items in the scene
153 List<SceneItem> sceneItemList = new List<SceneItem>();
154 Item[] itemsInScene = FindObjectsOfType<Item>();
155
156 // Loop through all scene items
157 foreach( Item item in itemsInScene )
158 {
159 SceneItem sceneItem = new SceneItem();
160 sceneItem.m_ItemCode = item.ItemCode;
161 sceneItem.m_Position = new Vector3Serializable( item.transform.position.x, item.transform.position.y, item.transform.position.z );
162 sceneItem.m_ItemName = item.name;
163
164 // Add scene item to list
165 sceneItemList.Add( sceneItem );
166 }
167
168 // Create list scene items in scene save and set to scene item list.
169 SceneSave sceneSave = new SceneSave();
170 sceneSave.m_ListSceneItem = sceneItemList;
171
172 // Add scene save to game object
173 GameObjectSave.m_SceneData.Add( sceneName, sceneSave );
174 }
175
176}
177
178
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
Definition: Item.cs:4
void Init(int itemCode)
At the start if we have a reapable item, we add the item nudge class.
Definition: Item.cs:31
int ItemCode
Definition: Item.cs:11
Manage saving and loading scenes. This class is a singleton instance.
We have scene item instances for every item in the scene that we want to store in a save file.
Definition: SceneItem.cs:7
string m_ItemName
Definition: SceneItem.cs:10
int m_ItemCode
Definition: SceneItem.cs:8
Vector3Serializable m_Position
Definition: SceneItem.cs:9
Control store, restore, save and load functionality in the scene.
void ISaveableRestoreScene(string sceneName)
Restore the scene
void InstantiateSceneItems(List< SceneItem > sceneItemList)
Loop through all the scene items Then instantiate a game object for each SceneItem
void ISaveableStoreScene(string sceneName)
Remove old scene data Save all the information on new scene Add that back to the GameObjectSave
void ISaveableRegister()
Adding the current game object to iSaveableObjectList instance in SaveLoadManager
void DestroySceneItems()
Destroy Items currently in the scene
GameObjectSave m_GameObjectSave
void AfterSceneLoad()
Find the game object of the parent
override void Awake()
Call the base of singleton class Initialize variables.
void InstantiateSceneItem(int itemCode, Vector3 itemPosition)
For future. Use to instantiate a single SceneItem
GameObjectSave GameObjectSave
void OnDisable()
Calls ISaveableDeregister Unsubscribe from AfterSceneLoad
void ISaveableDeregister()
Removing the current game object to iSaveableObjectList instance in SaveLoadManager
void OnEnable()
Calls ISaveableRegister Subscribe to AfterSceneLoad
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...
This class will have Tags values.
Definition: Tags.cs:5
const string ItemsParentTransform
Definition: Tags.cs:7
Unity Vector3 data type is NOT serializable. Therefore, it can't be searilize and saved to a datafile...
Interface for unity ISaveable
Definition: ISaveable.cs:5