Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Pixeye committed Jun 1, 2019
1 parent fe5e5fd commit 29deda5
Show file tree
Hide file tree
Showing 13 changed files with 209 additions and 69 deletions.
Binary file modified Install/1 Plug Console.unitypackage
Binary file not shown.
120 changes: 74 additions & 46 deletions Runtime/LibEcs/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 )

}

Expand Down Expand Up @@ -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];

Expand All @@ -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
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -239,15 +245,18 @@ 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);

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;
Expand All @@ -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);

Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -558,7 +586,7 @@ public static T Add<T>(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;
Expand Down
4 changes: 1 addition & 3 deletions Runtime/LibEcs/GroupCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -232,7 +231,6 @@ internal Enumerator(GroupCore g, int length)
{
position = -1;
this.g = g;
// this.entities = entities;
this.length = length;
}

Expand Down
14 changes: 7 additions & 7 deletions Runtime/LibEcs/ent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,31 +109,31 @@ public bool Has<T>()
[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)]
Expand All @@ -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;
}
}

Expand Down
10 changes: 10 additions & 0 deletions Runtime/LibMisc/SignalDebug.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Pixeye.Framework
{
public struct SignalDebug
{

public static SignalDebug New=> new SignalDebug();


}
}
3 changes: 3 additions & 0 deletions Runtime/LibMisc/SignalDebug.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 29deda5

Please sign in to comment.