using System.Collections; using System.Collections.Generic; using UnityEngine; using RPGCreationKit; using RPGCreationKit.CellsSystem; namespace RPGCreationKit.PersistentReferences { [System.Serializable] public class PersistentReferencesDictionary : SerializableDictionary { } [System.Serializable] public class CellsDoorsDictionary : SerializableDictionary { } /// /// Manages each and every persistent reference in the world, making all of them accessible anytime. /// public class PersistentReferenceManager : MonoBehaviour { public static PersistentReferenceManager instance; private void Awake() { if (instance == null) instance = this; } private void Start() { LoadAllMutables(); } [SerializeField] Transform tAI; [SerializeField] Transform tLootingPoint; [SerializeField] Transform tDoor; [SerializeField] Transform tOther; [SerializeField] Transform tUnregistered; public PersistentReferencesDictionary refs; public CellsDoorsDictionary cellsDoorsDictionary; public MutablesInWorldDictionary persistentMutableDictionary; /// /// Register a PersistentReference to the Dictionary and trasfers its GameObject to the _PersistentReferences_ scene. /// /// The reference to register. /// True if successfully registered. public bool RegisterPersistentReference(PersistentReference _ref) { // Conditions of error if (_ref == null) return false; if(string.IsNullOrEmpty(_ref.refID)) { Debug.LogWarning("Tried to register a reference of type: " + _ref.type.ToString() + " from the GameObject " + _ref.gameObject.name + ". But no RefID was provided."); return false; } else // Reference is set, proceed to register { // Checks if the reference is already registered if(refs.ContainsKey(_ref.refID)) { Debug.LogWarning("Tried to register the RefID: " + _ref.refID + " but it was already registered. Aborting..."); return false; } // Adds it to the dictionary refs.Add(_ref.refID, _ref); // Moves to its scene //SceneManager.MoveGameObjectToScene(_ref.gameObject, this.gameObject.scene); // Sets the correct parent switch(_ref.type) { case PersistentReferenceType.AI: _ref.gameObject.transform.SetParent(tAI); break; case PersistentReferenceType.Door: _ref.gameObject.transform.SetParent(tDoor); break; case PersistentReferenceType.LootingPoint: _ref.gameObject.transform.SetParent(tLootingPoint); break; case PersistentReferenceType.Other: _ref.gameObject.transform.SetParent(tOther); break; default: _ref.gameObject.transform.SetParent(tOther); break; } DisablePersistentReference(_ref.refID); return true; } } /// /// Unregisters a PersistentReference from the Dictionary and trasfers its GameObject under the Unregistered GameObjects. /// /// The reference to unregister. /// True if successfully unregistered. public bool UnregisterPersistentReference(PersistentReference _ref, bool destroyGameObject = false) { // Conditions of error if (_ref == null) return false; if (string.IsNullOrEmpty(_ref.refID)) { Debug.LogWarning("Tried to unregister a persistent reference of type: " + _ref.type.ToString() + " from the GameObject " + _ref.gameObject.name + ". But no RefID was provided."); return false; } // Try to get the reference to see if this PersistentReference was registered. PersistentReference pTest = GetPersistentReference(_ref.refID); if (pTest == null) { Debug.LogWarning("Tried to unregister the persistent reference with ID " + _ref.refID + " but it wasn't registered in first place."); return false; } refs.Remove(_ref.refID); if (!destroyGameObject) _ref.gameObject.transform.SetParent(tUnregistered); else DestroyImmediate(_ref.gameObject); Debug.Log("Persistent Reference with ID: " + _ref.refID + " was successfully unregistered. (Destroyed: " + destroyGameObject + ")"); return true; } /// /// Gets a PersistentReference from the Dictionary. /// /// /// public PersistentReference GetPersistentReference(string _refID) { PersistentReference reference; refs.TryGetValue(_refID, out reference); if (reference != null) return reference; else { Debug.Log("Tried to Get the Persistent Reference with ID: " + _refID + ", but it isn't registered."); return null; } } /// /// Activates the GameObject of a Persistent Reference, providing its refID. /// /// public void ActivatePersistentReference(string _refID) { PersistentReference reference = GetPersistentReference(_refID); if (reference != null) { reference.gameObject.SetActive(true); // If the AI was using offline mode if (reference.type == PersistentReferenceType.AI) { AI.AIPerception ai = reference.GetComponent(); if (ai != null && ai.usesOfflineMode) ai.onlineComponents.OnGoingOnline(); else reference.gameObject.SetActive(true); } else reference.gameObject.SetActive(true); reference.isLoaded = true; } } /// /// Activates the GameObject of a Persistent Reference and returns its GameObject, providing its refID. /// /// public GameObject ActivatePersistentReferenceAndGetGameObject(string _refID) { PersistentReference reference = GetPersistentReference(_refID); if (reference != null) { reference.gameObject.SetActive(true); return reference.gameObject; } else { Debug.Log("Tried to Activate the Persistent Reference with ID: " + _refID + ", but it isn't registered."); return null; } } /// /// Disables the GameObject of a Persistent Reference, providing its refID. /// public void DisablePersistentReference(string _refID) { PersistentReference reference = GetPersistentReference(_refID); if (reference != null) { // If the AI was using offline mode if (reference.type == PersistentReferenceType.AI && reference.isLoaded) { AI.AIPerception ai = reference.GetComponent(); if (ai != null && ai.usesOfflineMode) ai.onlineComponents.OnGoingOffline(); else reference.gameObject.SetActive(false); } else reference.gameObject.SetActive(false); reference.isLoaded = false; } } /// /// Gets the Type of a Persistent Reference, providing its refID /// /// /// public PersistentReferenceType GetPRefType(string _refID) { PersistentReference pRef = GetPersistentReference(_refID); if (pRef != null) return pRef.type; else return default; } private void LoadAllMutables() { for (int i = 0; i < persistentMutableDictionary.Count; i++) foreach (KeyValuePair item in persistentMutableDictionary) if (item.Value != null) item.Value.LoadFromSavefile(); } } }