using System; using System.Collections.Generic; using UnityEngine; namespace Core { public class PermanentController : BaseMonoBehaviour { private static PermanentController _instance; private Dictionary _systems; private HashSet _tickSystems; private bool _isInitialized; protected override void ReleaseReferences() { _systems = null; _tickSystems = null; } private void Awake() { if (_instance != null) { Destroy(gameObject); return; } _instance = this; DontDestroyOnLoad(gameObject); } public static void Initialize() { if (_instance._isInitialized) return; _instance._isInitialized = true; _instance._systems = new Dictionary(); _instance._tickSystems = new HashSet(); foreach (Transform trSystem in _instance.transform) { var system = trSystem.gameObject.GetComponent(); if (system == null) { Debug.LogWarning($"{trSystem.gameObject.name} must inherit from SystemBase"); continue; } _instance._systems.Add(system.GetType(), system); system.Initialize(); if (system is ITick tick) _instance._tickSystems.Add(tick); } } public static void Tick() { foreach (var system in _instance._tickSystems) system?.Tick(); } public static void StartControllers() { foreach (var system in _instance._systems.Values) if (system.AutoStart) system.StartSystem(); } public static T GetSystem() where T : SystemBase { if (!_instance._systems.ContainsKey(typeof(T))) return null; return (T) _instance._systems[typeof(T)]; } // public static bool HasSystem() where T : SystemBase => _instance._systems.ContainsKey(typeof(T)); } }