EntityModule

EntityModule
This commit is contained in:
ALEXTANG
2022-08-08 16:32:55 +08:00
parent 1fd7e21981
commit 5d7b4e20e9
5 changed files with 56 additions and 23 deletions

View File

@@ -1,4 +1,4 @@
using TEngine;
using TEngine.EntityModule;
using UnityEngine;
public class EcsDemoApp : MonoBehaviour

View File

@@ -65,6 +65,14 @@ namespace TEngine.EntityModule
{
entityComponent.Entity.Updates.Remove(update);
}
if (entityComponent is IFixedUpdate fixedupdate)
{
entityComponent.Entity.FixedUpdates.Remove(fixedupdate);
}
if (entityComponent is ILateUpdate lateupdate)
{
entityComponent.Entity.LateUpdates.Remove(lateupdate);
}
if (reuse)
{
entityComponent.Entity.System.Push(entityComponent);
@@ -82,11 +90,10 @@ namespace TEngine.EntityModule
entityComponentTemp.OnDestroy();
if (reuse)
{
entity.System.Push(entityComponentTemp);
Destroy(entityComponentTemp);
}
}
entity.Updates.Clear();
entity.CanUpdate = false;
entity.IsDispose = true;
if (reuse)
{
entity.System.Push(entity);

View File

@@ -23,7 +23,7 @@ namespace TEngine.EntityModule
#region Status
[IgnoreDataMember]
private bool IsFromPool
internal bool IsFromPool
{
get => (this.status & EntityStatus.IsFromPool) == EntityStatus.IsFromPool;
set
@@ -40,7 +40,27 @@ namespace TEngine.EntityModule
}
[IgnoreDataMember]
private bool IsDispose => (this.status & EntityStatus.IsDispose) == EntityStatus.IsDispose;
internal bool IsDispose
{
get => (this.status & EntityStatus.IsDispose) == EntityStatus.IsDispose;
set
{
if (value)
{
this.status |= EntityStatus.IsDispose;
CanUpdate = false;
CanFixedUpdate = false;
CanLateUpdates = false;
Updates.Clear();
FixedUpdates.Clear();
LateUpdates.Clear();
}
else
{
this.status &= ~EntityStatus.IsDispose;
}
}
}
#endregion
@@ -50,19 +70,19 @@ namespace TEngine.EntityModule
internal List<IUpdate> Updates = new List<IUpdate>();
internal List<IFixedUpdate> FixedUpdates = new List<IFixedUpdate>();
internal List<ILateUpdate> LateUpdates = new List<ILateUpdate>();
internal bool InActive;
internal bool CanUpdate;
internal bool CanFixedUpdate;
internal bool CanLateUpdates;
public int Index { get; set; } = -1;
public Entity()
{
InActive = true;
System = EntitySystem.Instance;
}
~Entity()
{
InActive = false;
}
internal void Update()

View File

@@ -7,18 +7,9 @@ namespace TEngine.EntityModule
{
public void RmvComponent<T>(T component) where T :EntityComponent, new()
{
if (component is IUpdate update)
{
Updates.Remove(update);
}
else if (component is IFixedUpdate fixedUpdate)
{
FixedUpdates.Remove(fixedUpdate);
}
CanUpdate = Updates.Count > 0;
Destroy(component);
CheckUpdate();
}
public T AddComponent<T>() where T :EntityComponent, new()
@@ -36,8 +27,11 @@ namespace TEngine.EntityModule
{
FixedUpdates.Add(fixedUpdate);
}
CanUpdate = Updates.Count > 0;
else if (component is ILateUpdate lateUpdate)
{
LateUpdates.Add(lateUpdate);
}
CheckUpdate();
return component;
}
@@ -55,7 +49,11 @@ namespace TEngine.EntityModule
{
FixedUpdates.Add(fixedUpdate);
}
CanUpdate = Updates.Count > 0;
else if (component is ILateUpdate lateUpdate)
{
LateUpdates.Add(lateUpdate);
}
CheckUpdate();
return component;
}
@@ -125,5 +123,12 @@ namespace TEngine.EntityModule
}
return elements.ToArray();
}
private void CheckUpdate()
{
CanUpdate = Updates.Count > 0;
CanFixedUpdate = FixedUpdates.Count > 0;
CanLateUpdates = LateUpdates.Count > 0;
}
}
}

View File

@@ -81,6 +81,7 @@ namespace TEngine.EntityModule
internal T Create<T>() where T : Entity, new()
{
T entity = Get<T>();
entity.IsDispose = false;
AddEntity(entity);
return entity;
}