public void Playback() { DebugHelper.AssertThrow <ThreadAccessException>(ECSWorld.CheckThreadIsMainThread()); if (!IsEmpty()) { Jobs.CompleteAllJobs(); int i = 0; while (i < modList.Length) { IModification?mod = modList[i++]; if (mod == null) { break; } try { mod.Execute(this); } catch (InvalidEntityException _) { } catch (ComponentNotFoundException _) { } } pool.Return(modList); modList = Array.Empty <IModification?>(); entityTarget = default; hasEntityTarget = false; nextIndex = 0; } }
public EntityManager(ECSWorld world, ComponentManager cm) { this.world = world; componentManager = cm; freeEntities = new Stack <Entity>(); nextIdx = 1; }
public sealed override void Update(float deltaTime, ECSWorld world) { if (!initialized) { afterUpdateCommands = new ConcurrentEntityCommandBuffer(world); query = GetQuery(); filter = GetComponentFilter(); initialized = true; } BeforeUpdate(deltaTime, world); afterUpdateCommands.PlaybackAfterUpdate(); var blocks = world.ComponentManager.GetBlocksNoSync(query, filter); var group = Jobs.StartNewGroup(query); foreach (BlockAccessor block in blocks) { ComponentProcessJob processJob = new ComponentProcessJob() { block = block, deltaTime = deltaTime, instance = this }; processJob.Schedule(group); } }
public Entity[] CreateEntities(int numEntities, EntityArchetype archetype = null) { DebugHelper.AssertThrow <ThreadAccessException>(ECSWorld.CheckThreadIsMainThread()); world.SyncPoint(); if (archetype == null) { archetype = EntityArchetype.Empty; } Entity[] arr = new Entity[numEntities]; for (int i = 0; i < arr.Length; i++) { if (freeEntities.TryPop(out Entity result)) { result = new Entity(result.id, result.version + 1); componentManager.AddEntity(result, archetype); arr[i] = result; new EntityCreatedEvent(result).Fire(world); } else { Entity e = new Entity(NextIndex(), 0); componentManager.AddEntity(e, archetype); arr[i] = e; new EntityCreatedEvent(e).Fire(world); } } return(arr); }
public SystemManager(ECSWorld world) { this.world = world; world.EarlyUpdate += EarlyUpdate; world.Update += OnUpdate; world.LateUpdate += OnLateUpdate; world.FixedUpdate += OnFixedUpdate; world.BeforeRender += OnBeforeRender; world.Render += OnRender; world.AfterRender += OnAfterRender; }
public void Playback() { DebugHelper.AssertThrow <ThreadAccessException>(ECSWorld.CheckThreadIsMainThread()); if (threadBuffersCache == null) { threadBuffersCache = threadBuffers.Values as List <EntityCommandBuffer>; } world.SyncPoint(); foreach (EntityCommandBuffer buffer in threadBuffersCache) { buffer.Playback(); } }
public bool TryGetComponentData <T>(out Span <T> data) where T : unmanaged, IComponent { DebugHelper.AssertThrow <ThreadAccessException>(ECSWorld.CheckThreadIsMainThread()); Jobs.CompleteAllJobs(); if (block.archetype.Has <T>()) { block.IncrementComponentVersion(TypeHelper <T> .hashCode); data = block.GetComponentData <T>().Slice(0, block.Size); return(true); } else { data = Span <T> .Empty; return(false); } }
public bool IsEmpty() { DebugHelper.AssertThrow <ThreadAccessException>(ECSWorld.CheckThreadIsMainThread()); Jobs.CompleteAllJobs(); if (threadBuffersCache == null) { threadBuffersCache = threadBuffers.Values as List <EntityCommandBuffer>; } foreach (EntityCommandBuffer buffer in threadBuffersCache) { if (!buffer.IsEmpty()) { return(false); } } return(true); }
public sealed override void Update(float deltaTime, ECSWorld world) { if (!initialized) { afterUpdateCommands = new EntityCommandBuffer(world); query = GetQuery(); filter = GetComponentFilter(); initialized = true; } BeforeUpdate(deltaTime, world); var blocks = world.ComponentManager.GetBlocks(query, filter); foreach (BlockAccessor block in blocks) { ProcessBlock(deltaTime, block); } afterUpdateCommands.Playback(); AfterUpdate(deltaTime, world); }
internal void OnAfterRender(float deltaTime, ECSWorld world) { foreach (var system in afterRenderSystems) { if (system.system.Enabled) { Profiler.StartMethod(system.systemType.Name); try { system.system.Update(deltaTime, world); } catch (Exception ex) { Console.WriteLine(ex); } finally { Profiler.EndMethod(); } } } }
public Entity CreateEntity(EntityArchetype archetype = null) { DebugHelper.AssertThrow <ThreadAccessException>(ECSWorld.CheckThreadIsMainThread()); world.SyncPoint(); if (archetype == null) { archetype = EntityArchetype.Empty; } if (freeEntities.TryPop(out Entity result)) { result = new Entity(result.id, result.version + 1); componentManager.AddEntity(result, archetype); new EntityCreatedEvent(result).Fire(world); return(result); } else { Entity e = new Entity(NextIndex(), 0); componentManager.AddEntity(e, archetype); new EntityCreatedEvent(e).Fire(world); return(e); } }
public virtual void AfterUpdate(float deltaTime, ECSWorld world) { }
public EntityCommandBuffer(ECSWorld world) { this.world = world; }
internal static ECSWorld CreateMain() { ECSWorld world = new ECSWorld(true); return(world); }
public static bool IsValid(this Entity entity, ECSWorld world) { return(world.ComponentManager.IsEntityValid(entity)); }
public static void AddComponent <T>(this Entity entity, ECSWorld world, in T component) where T : unmanaged, IComponent
public static void Destroy(this Entity entity, ECSWorld world) { world.EntityManager.DestroyEntity(entity); }
public virtual void BeforeUpdate(float deltaTime, ECSWorld world) { }
public ConcurrentEntityCommandBuffer(ECSWorld world) { this.world = world; }
public abstract void Update(float deltaTime, ECSWorld world);
public virtual void OnDestroySystem(ECSWorld world) { }
public abstract void ProcessBlock(float deltaTime, BlockAccessor block, ECSWorld world);
public virtual void OnCreateSystem(ECSWorld world) { }