/// <summary> /// Returns true if system with TSystem type exists /// </summary> /// <typeparam name="TSystem"></typeparam> /// <returns></returns> public bool HasSystem <TSystem>() where TSystem : class, ISystemBase, new() { if (this.world == null) { SystemGroupRegistryException.Throw(); } for (int i = 0, count = this.systems.Length; i < count; ++i) { if (this.systems.arr[i] is TSystem) { return(true); } } return(false); }
/// <summary> /// Get first system by type /// </summary> /// <typeparam name="TSystem"></typeparam> /// <returns></returns> public TSystem GetSystem <TSystem>() where TSystem : class, ISystemBase { if (this.world == null) { SystemGroupRegistryException.Throw(); } for (int i = 0, count = this.systems.Count; i < count; ++i) { var system = this.systems.arr[i]; if (system is TSystem tSystem) { return(tSystem); } } return(default);
/// <summary> /// Add system by type /// Retrieve system from pool, OnConstruct() call /// </summary> /// <typeparam name="TSystem"></typeparam> public bool AddSystem <TSystem>() where TSystem : class, ISystemBase, new() { if (this.world == null) { SystemGroupRegistryException.Throw(); } var instance = PoolSystems.Spawn <TSystem>(); if (this.AddSystem(instance) == false) { instance.world = null; PoolSystems.Recycle(ref instance); return(false); } return(true); }
/// <summary> /// Add system manually /// Pool will not be used, OnConstruct() call /// </summary> /// <param name="instance"></param> public bool AddSystem(ISystemBase instance) { if (this.world == null) { SystemGroupRegistryException.Throw(); } WorldUtilities.SetWorld(this.world); instance.world = this.world; if (instance is ISystemValidation instanceValidate) { if (instanceValidate.CouldBeAdded() == false) { instance.world = null; return(false); } } var k = this.length; ArrayUtils.Resize(in k, ref this.systems, resizeWithOffset: false); ArrayUtils.Resize(in k, ref this.statesSystems, resizeWithOffset: false); ++this.length; this.systems.arr[k] = instance; this.statesSystems.arr[k] = ModuleState.AllActive; instance.OnConstruct(); if (instance is ISystemFilter systemFilter) { systemFilter.filter = systemFilter.CreateFilter(); } this.world.UpdateGroup(this); return(true); }
/// <summary> /// Remove system manually /// Pool will not be used, OnDeconstruct() call /// </summary> /// <param name="instance"></param> public void RemoveSystem(ISystemBase instance) { if (this.world == null) { SystemGroupRegistryException.Throw(); } var idx = this.systems.IndexOf(instance); if (idx >= 0) { if (instance is ISystemFilter systemFilter) { systemFilter.filter = null; } instance.world = null; this.systems = this.systems.RemoveAt(idx); this.statesSystems = this.statesSystems.RemoveAt(idx); instance.OnDeconstruct(); this.world.UpdateGroup(this); } }