diff --git a/Install/1 Plug Console.unitypackage b/Install/1 Plug Console.unitypackage index 90342178..9b175ea5 100644 Binary files a/Install/1 Plug Console.unitypackage and b/Install/1 Plug Console.unitypackage differ diff --git a/Runtime/LibEcs/Entity.cs b/Runtime/LibEcs/Entity.cs index 1086a7f9..f042ded1 100644 --- a/Runtime/LibEcs/Entity.cs +++ b/Runtime/LibEcs/Entity.cs @@ -27,13 +27,14 @@ public EntityOperation(in ent entity, int arg, Entity.Delayed.Action action) } } - + [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)] public struct Utils { + public int id; public bool isAlive; public bool isPooled; - public byte ageCache; // caching age of entity for retrivieng it in future. ( ParseBy method ) + public byte age; // caching age of entity for retrivieng it in future. ( ParseBy method ) } @@ -63,7 +64,7 @@ static public implicit operator bitBool(bool value) public static unsafe class Entity { - public static int entitiesDebugCount; + public static int Count; public static Transform[] transforms = new Transform[SettingsEngine.SizeEntities]; @@ -77,7 +78,7 @@ public static unsafe class Entity internal static BufferComponents[] components; internal static BufferTags* tags; - internal static Utils* utils; + internal static Utils* cache; // internal static BufferComponents* components; //===============================// // Initialize @@ -88,12 +89,12 @@ internal static void Start() { components = new BufferComponents[SettingsEngine.SizeEntities]; tags = (BufferTags*) UnmanagedMemory.Alloc(sizeBufferTags * SettingsEngine.SizeEntities); - utils = (Utils*) UnmanagedMemory.Alloc(sizeUtils * SettingsEngine.SizeEntities); + cache = (Utils*) UnmanagedMemory.Alloc(sizeUtils * SettingsEngine.SizeEntities); for (int i = 0; i < SettingsEngine.SizeEntities; i++) { tags[i] = new BufferTags(); - utils[i] = new Utils(); + cache[i] = new Utils(); components[i] = new BufferComponents(1); } @@ -113,12 +114,12 @@ internal static ent Setup(int id, byte age) Array.Resize(ref transforms, l); Array.Resize(ref components, l); tags = (BufferTags*) UnmanagedMemory.ReAlloc(tags, sizeBufferTags * l); - utils = (Utils*) UnmanagedMemory.ReAlloc(utils, sizeUtils * l); + cache = (Utils*) UnmanagedMemory.ReAlloc(cache, sizeUtils * l); for (int i = counter; i < l; i++) { tags[i] = new BufferTags(); - utils[i] = new Utils(); + cache[i] = new Utils(); components[i] = new BufferComponents(1); } @@ -127,11 +128,13 @@ internal static ent Setup(int id, byte age) components[id].length = 0; - utils[id].ageCache = age; - utils[id].isAlive = true; - utils[id].isPooled = false; + var ptrCache = &cache[id]; + ptrCache->id = id; + ptrCache->age = age; + ptrCache->isAlive = true; + ptrCache->isPooled = true; - entitiesDebugCount++; + Count++; return new ent(id, age); } @@ -148,12 +151,12 @@ internal static void SetupWithTransform(int id, bool pooled, byte age) Array.Resize(ref components, l); tags = (BufferTags*) UnmanagedMemory.ReAlloc(tags, sizeBufferTags * l); - utils = (Utils*) UnmanagedMemory.ReAlloc(utils, sizeUtils * l); + cache = (Utils*) UnmanagedMemory.ReAlloc(cache, sizeUtils * l); for (int i = counter; i < l; i++) { tags[i] = new BufferTags(); - utils[i] = new Utils(); + cache[i] = new Utils(); components[i] = new BufferComponents(1); } @@ -162,11 +165,13 @@ internal static void SetupWithTransform(int id, bool pooled, byte age) components[id].length = 0; - utils[id].ageCache = age; - utils[id].isAlive = true; - utils[id].isPooled = pooled; + var ptrCache = &cache[id]; + ptrCache->id = id; + ptrCache->age = age; + ptrCache->isAlive = true; + ptrCache->isPooled = pooled; - entitiesDebugCount++; + Count++; } public static ent Create() @@ -192,15 +197,14 @@ public static ent Create() return Setup(id, age); } - #if ODIN_INSPECTOR - public static ent Create(BlueprintEntity bpAsset) + public static ent Create(ModelComposer model) { - int id; + int id; byte age = 0; if (ent.entityStackLength > 0) { - var pop = ent.entityStack.Dequeue(); + var pop = ent.entityStack.Dequeue(); byte ageOld = pop.age; id = pop.id; unchecked @@ -214,12 +218,14 @@ public static ent Create(BlueprintEntity bpAsset) id = ent.lastID++; var entity = Setup(id, age); - bpAsset.Execute(entity); + + model(entity, null); + Delayed.Set(entity, 0, Delayed.Action.Activate); + return entity; } - #endif - public static ent Create(ModelComposer model) + public static ent Bind(GameObject prefab, ModelComposer model, bool pooled = false) { int id; byte age = 0; @@ -239,7 +245,10 @@ public static ent Create(ModelComposer model) else id = ent.lastID++; - var entity = Setup(id, age); + SetupWithTransform(id, pooled, age); + transforms[id] = prefab.transform; + + var entity = new ent(id, age); model(entity, null); Delayed.Set(entity, 0, Delayed.Action.Activate); @@ -247,7 +256,7 @@ public static ent Create(ModelComposer model) return entity; } - public static ent Bind(GameObject prefab, ModelComposer model, bool pooled = false) + public static ent Create(string prefabID, Vector3 position, ModelComposer model, bool pooled = false) { int id; byte age = 0; @@ -266,9 +275,8 @@ public static ent Bind(GameObject prefab, ModelComposer model, bool pooled = fal } else id = ent.lastID++; - SetupWithTransform(id, pooled, age); - transforms[id] = prefab.transform; + transforms[id] = pooled ? HelperFramework.SpawnInternal(Pool.Entities, prefabID, position) : HelperFramework.SpawnInternal(prefabID, position); var entity = new ent(id, age); @@ -278,7 +286,7 @@ public static ent Bind(GameObject prefab, ModelComposer model, bool pooled = fal return entity; } - public static ent Create(string prefabID, Vector3 position, ModelComposer model, bool pooled = false) + public static ent Create(string prefabID, ModelComposer model, bool pooled = false) { int id; byte age = 0; @@ -297,9 +305,9 @@ public static ent Create(string prefabID, Vector3 position, ModelComposer model, } else id = ent.lastID++; - SetupWithTransform(id, pooled, age); - transforms[id] = pooled ? HelperFramework.SpawnInternal(Pool.Entities, prefabID, position) : HelperFramework.SpawnInternal(prefabID, position); + SetupWithTransform(id, pooled, age); + transforms[id] = pooled ? HelperFramework.SpawnInternal(Pool.Entities, prefabID) : HelperFramework.SpawnInternal(prefabID); var entity = new ent(id, age); model(entity, null); @@ -308,7 +316,7 @@ public static ent Create(string prefabID, Vector3 position, ModelComposer model, return entity; } - public static ent Create(string prefabID, ModelComposer model, bool pooled = false) + public static ent Create(GameObject prefab, ModelComposer model, bool pooled = false) { int id; byte age = 0; @@ -329,7 +337,8 @@ public static ent Create(string prefabID, ModelComposer model, bool pooled = fal id = ent.lastID++; SetupWithTransform(id, pooled, age); - transforms[id] = pooled ? HelperFramework.SpawnInternal(Pool.Entities, prefabID) : HelperFramework.SpawnInternal(prefabID); + transforms[id] = pooled ? HelperFramework.SpawnInternal(Pool.Entities, prefab) : HelperFramework.SpawnInternal(prefab); + var entity = new ent(id, age); model(entity, null); @@ -338,7 +347,7 @@ public static ent Create(string prefabID, ModelComposer model, bool pooled = fal return entity; } - public static ent Create(GameObject prefab, ModelComposer model, bool pooled = false) + public static ent Create(string prefabID, Vector3 position, bool pooled = false) { int id; byte age = 0; @@ -359,16 +368,10 @@ public static ent Create(GameObject prefab, ModelComposer model, bool pooled = f id = ent.lastID++; SetupWithTransform(id, pooled, age); - transforms[id] = pooled ? HelperFramework.SpawnInternal(Pool.Entities, prefab) : HelperFramework.SpawnInternal(prefab); - - var entity = new ent(id, age); - - model(entity, null); - Delayed.Set(entity, 0, Delayed.Action.Activate); - - return entity; + transforms[id] = pooled ? HelperFramework.SpawnInternal(Pool.Entities, prefabID, position) : HelperFramework.SpawnInternal(prefabID, position); + return new ent(id, age); } - + public static ent Create(string prefabID, bool pooled = false) { int id; @@ -421,6 +424,31 @@ public static ent Create(GameObject prefab, bool pooled = false) } #if ODIN_INSPECTOR + public static ent Create(BlueprintEntity bpAsset) + { + int id; + byte age = 0; + + if (ent.entityStackLength > 0) + { + var pop = ent.entityStack.Dequeue(); + byte ageOld = pop.age; + id = pop.id; + unchecked + { + age = (byte) (ageOld + 1); + } + + ent.entityStackLength--; + } + else + id = ent.lastID++; + + var entity = Setup(id, age); + bpAsset.Execute(entity); + return entity; + } + public static ent Create(string prefabID, BlueprintEntity bpAsset, bool pooled = false) { int id; @@ -500,7 +528,7 @@ public static ent ParseBy(string name) index = index * 10 + (name[i] - '0'); #endif - return new ent(index, utils[index].ageCache); + return new ent(index, cache[index].age); } #endif @@ -558,7 +586,7 @@ public static T Add(in this ent entity) var entityID = entity.id; #if UNITY_EDITOR - if (!utils[entity.id].isAlive) + if (!cache[entity.id].isAlive) { Debug.LogError($"-> Entity with id: [{entityID}] is not active. You should not add components to inactive entity. "); return default; diff --git a/Runtime/LibEcs/GroupCore.cs b/Runtime/LibEcs/GroupCore.cs index 745f6f97..3b774561 100644 --- a/Runtime/LibEcs/GroupCore.cs +++ b/Runtime/LibEcs/GroupCore.cs @@ -5,9 +5,8 @@ using System.Collections; using System.Collections.Generic; using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; using Unity.IL2CPP.CompilerServices; -using UnityEngine; + namespace Pixeye.Framework { @@ -232,7 +231,6 @@ internal Enumerator(GroupCore g, int length) { position = -1; this.g = g; - // this.entities = entities; this.length = length; } diff --git a/Runtime/LibEcs/ent.cs b/Runtime/LibEcs/ent.cs index 512fffe7..cf84cb75 100644 --- a/Runtime/LibEcs/ent.cs +++ b/Runtime/LibEcs/ent.cs @@ -109,31 +109,31 @@ public bool Has() [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Unbind() { - Entity.utils[id].isAlive = false; + Entity.cache[id].isAlive = false; Entity.Delayed.Set(this, 0, Entity.Delayed.Action.Unbind); - Entity.entitiesDebugCount--; + Entity.Count--; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Release() { #if UNITY_EDITOR - if (!Entity.utils[id].isAlive) + if (!Entity.cache[id].isAlive) { Debug.LogError($"Entity with id [{id}] already destroyed."); return; } #endif - Entity.utils[id].isAlive = false; + Entity.cache[id].isAlive = false; Entity.Delayed.Set(this, 0, Entity.Delayed.Action.Kill); - Entity.entitiesDebugCount--; + Entity.Count--; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool EqualsAndExist(ent other) { - return id > -1 && Entity.utils[id].isAlive && this.id == other.id && this.age == other.age; + return id > -1 && Entity.cache[id].isAlive && this.id == other.id && this.age == other.age; } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -147,7 +147,7 @@ public bool Exist [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - return id > -1 && Entity.utils[id].isAlive && Entity.utils[id].ageCache == age; + return id > -1 && Entity.cache[id].isAlive && Entity.cache[id].age == age; } } diff --git a/Runtime/LibMisc/SignalDebug.cs b/Runtime/LibMisc/SignalDebug.cs new file mode 100644 index 00000000..cb6e19e5 --- /dev/null +++ b/Runtime/LibMisc/SignalDebug.cs @@ -0,0 +1,10 @@ +namespace Pixeye.Framework +{ + public struct SignalDebug + { + + public static SignalDebug New=> new SignalDebug(); + + + } +} \ No newline at end of file diff --git a/Runtime/LibMisc/SignalDebug.cs.meta b/Runtime/LibMisc/SignalDebug.cs.meta new file mode 100644 index 00000000..eb7ec9a0 --- /dev/null +++ b/Runtime/LibMisc/SignalDebug.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 378c27652c0943c58799d384b694ff34 +timeCreated: 1559373339 \ No newline at end of file diff --git a/Runtime/LibMono/Actor.cs b/Runtime/LibMono/Actor.cs index b4dda2f6..c55cb092 100644 --- a/Runtime/LibMono/Actor.cs +++ b/Runtime/LibMono/Actor.cs @@ -52,7 +52,7 @@ void OnEnable() { if (!manualRemoved) return; manualRemoved = false; - Entity.utils[entity.id].isAlive = true; + Entity.cache[entity.id].isAlive = true; Entity.Delayed.Set(entity, 0, Entity.Delayed.Action.Activate); } } @@ -61,9 +61,9 @@ void OnDisable() { unsafe { - if (Toolbox.applicationIsQuitting || !Entity.utils[entity.id].isAlive) return; + if (Toolbox.applicationIsQuitting || !Entity.cache[entity.id].isAlive) return; manualRemoved = true; - Entity.utils[entity.id].isAlive = false; + Entity.cache[entity.id].isAlive = false; Entity.Delayed.Set(entity, 0, Entity.Delayed.Action.Deactivate); } } diff --git a/Runtime/LibProcessors/ProcessorDebug.cs b/Runtime/LibProcessors/ProcessorDebug.cs new file mode 100644 index 00000000..76755f86 --- /dev/null +++ b/Runtime/LibProcessors/ProcessorDebug.cs @@ -0,0 +1,87 @@ +// Project : ecs.unity.structs +// Contacts : Pix - ask@pixeye.games + +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; + +namespace Pixeye.Framework +{ + sealed unsafe class ProcessorDebug : Processor, ITick, IReceive + { + + static GameObject prefabEntityLabel = Box.Get("UI Debug Entity ID"); + + public List objs = new List(); + public RectTransform rectTransform; + + public void Tick(float delta) + { + } + + public void HandleSignal(in SignalDebug arg) + { + if (SettingsEngine.DebugMode) + { + if (rectTransform == null) + { + var go = new GameObject("Canvas Debug"); + var canvas = go.AddComponent(); + var scaler = go.AddComponent(); + scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; + scaler.screenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight; + + scaler.matchWidthOrHeight = 0.6118785f; + scaler.referenceResolution = new Vector2(12, 12); + canvas.worldCamera = Camera.main; + canvas.renderMode = RenderMode.ScreenSpaceCamera; + canvas.planeDistance = 60; + canvas.transform.SetParent(Camera.main.transform); + + rectTransform = go.GetComponent(); + } + rectTransform.gameObject.SetActive(true); + + for (int i = 0; i < Entity.Count; i++) + { + var tr = Entity.transforms[i]; + var t = Obj.Spawn(prefabEntityLabel, rectTransform.transform, tr.position, Quaternion.identity); + t.text = $"Entity ID: {Entity.cache[i].id}"; + Vector2 screenP = RectTransformUtility.WorldToScreenPoint(null, tr.position + new Vector3(0, 0.6f, 0)); + var source = t.GetComponent(); + source.anchorMin = new Vector2(0.5f, 0.5f); + source.anchorMax = new Vector2(0.5f, 0.5f); + source.anchoredPosition = screenP; + source.sizeDelta = new Vector2(2.25f, 0.3f); + objs.Add(t.gameObject); + } + } + else + { + for (int i = 0; i < objs.Count; i++) + { + GameObject.Destroy(objs[i]); + } + objs.Clear(); + rectTransform.gameObject.SetActive(false); + } + + Time.Default.timeScale = SettingsEngine.DebugMode ? 0 : 1f; + } + + protected override void OnDispose() + { + if (Toolbox.applicationIsQuitting) return; + + for (int i = 0; i < objs.Count; i++) + { + GameObject.Destroy(objs[i]); + } + objs.Clear(); + + rectTransform.Destroy(); + rectTransform = null; + } + + } +} \ No newline at end of file diff --git a/Runtime/LibProcessors/ProcessorDebug.cs.meta b/Runtime/LibProcessors/ProcessorDebug.cs.meta new file mode 100644 index 00000000..6af4ea4a --- /dev/null +++ b/Runtime/LibProcessors/ProcessorDebug.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: e6e1138412454fbda1f08c186c117a0b +timeCreated: 1559366844 \ No newline at end of file diff --git a/Runtime/LibProcessors/ProcessorEntities.cs b/Runtime/LibProcessors/ProcessorEntities.cs index a092a5c3..2e73d49c 100644 --- a/Runtime/LibProcessors/ProcessorEntities.cs +++ b/Runtime/LibProcessors/ProcessorEntities.cs @@ -106,7 +106,7 @@ public void Tick(float delta) components.length = 0; if (Entity.transforms.Length > entityID && Entity.transforms[entityID] != null) - Entity.transforms[entityID].gameObject.Release(Entity.utils[entityID].isPooled ? Pool.Entities : 0); + Entity.transforms[entityID].gameObject.Release(Entity.cache[entityID].isPooled ? Pool.Entities : 0); Entity.tags[entityID].Clear(); @@ -200,7 +200,7 @@ public void Tick(float delta) } case Entity.Delayed.Action.ChangeTag: { - if (!Entity.utils[entityID].isAlive) continue; + if (!Entity.cache[entityID].isAlive) continue; var index = operation.arg; var groups = HelperTags.inUseGroups.groupStorage[index]; diff --git a/Runtime/LibProcessors/ProcessorUpdate.cs b/Runtime/LibProcessors/ProcessorUpdate.cs index e383aca6..d368ad1e 100644 --- a/Runtime/LibProcessors/ProcessorUpdate.cs +++ b/Runtime/LibProcessors/ProcessorUpdate.cs @@ -80,7 +80,7 @@ void Update() { if (Toolbox.changingScene) return; - var delta = Time.delta; + var delta = Time.delta * Time.Default.timeScale; for (int i = 0; i < timesLen; i++) { diff --git a/Runtime/LibStarter/Starter.cs b/Runtime/LibStarter/Starter.cs index 95faad39..5b9282cd 100644 --- a/Runtime/LibStarter/Starter.cs +++ b/Runtime/LibStarter/Starter.cs @@ -72,10 +72,10 @@ public void ClearNodes() public void AddToNode(GameObject prefab, GameObject instance, int pool) { - var id = prefab.GetInstanceID(); - var nodesValid = nodes.FindValidNodes(id); - var conditionNodeCreate = true; - List nodesToKill = new List(); + var id = prefab.GetInstanceID(); + var nodesValid = nodes.FindValidNodes(id); + var conditionNodeCreate = true; + List nodesToKill = new List(); for (int i = 0; i < nodesValid.Count; i++) { @@ -131,7 +131,7 @@ public void RemoveFromNode(GameObject instance, int pool) #endif if (prefab == null) return; - var id = prefab.GetInstanceID(); + var id = prefab.GetInstanceID(); var index = nodes.FindValidNode(id, pool); if (index != -1) { @@ -160,6 +160,9 @@ public void BindScene() Toolbox.Add(factory); } + if (SettingsEngine.DefineDebugMode) + Add(); + Add(); Add(); @@ -187,9 +190,13 @@ public void BindScene() return Toolbox.Add(); } - protected virtual void Setup() { } + protected virtual void Setup() + { + } - protected virtual void PostSetup() { } + protected virtual void PostSetup() + { + } protected virtual void OnDestroy() { diff --git a/Runtime/Settings/SettingsEngine.cs b/Runtime/Settings/SettingsEngine.cs index a7a57bc6..df54a96e 100644 --- a/Runtime/Settings/SettingsEngine.cs +++ b/Runtime/Settings/SettingsEngine.cs @@ -5,7 +5,11 @@ namespace Pixeye.Framework { public static class SettingsEngine { + + public static bool DefineDebugMode = false; + public static bool DebugMode = false; + public static int SizeEntities = 1024; public static int SizeComponents = 256; public static int SizeBlueprinths = 24;