From 6a6f0591f1cfb415296a9755ecf8f200f71c126b Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Mon, 17 Jul 2023 01:00:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84Entity=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 完善Entity状态 --- .../GameScripts/DotNet/Core/Entitas/Entity.cs | 118 ++++++++++++++++-- .../DotNet/Core/IdFactory/RuntimeIdStruct.cs | 5 + 2 files changed, 113 insertions(+), 10 deletions(-) diff --git a/Assets/GameScripts/DotNet/Core/Entitas/Entity.cs b/Assets/GameScripts/DotNet/Core/Entitas/Entity.cs index 22f22bd9..6c183179 100644 --- a/Assets/GameScripts/DotNet/Core/Entitas/Entity.cs +++ b/Assets/GameScripts/DotNet/Core/Entitas/Entity.cs @@ -16,8 +16,107 @@ using Newtonsoft.Json; namespace TEngine { + [Flags] + public enum EntityStatus: byte + { + None = 0, + IsFromPool = 1, + IsComponent = 1 << 1, + IsCreated = 1 << 2, + IsNew = 1 << 3, + } + public abstract class Entity : IDisposable { + #region Status + [BsonIgnore] + [IgnoreDataMember] + private EntityStatus status = EntityStatus.None; + + [BsonIgnore] + [IgnoreDataMember] + public bool IsFromPool + { + get => (this.status & EntityStatus.IsFromPool) == EntityStatus.IsFromPool; + set + { + if (value) + { + this.status |= EntityStatus.IsFromPool; + } + else + { + this.status &= ~EntityStatus.IsFromPool; + } + } + } + + [BsonIgnore] + [IgnoreDataMember] + protected bool IsComponent + { + get => (this.status & EntityStatus.IsComponent) == EntityStatus.IsComponent; + set + { + if (value) + { + this.status |= EntityStatus.IsComponent; + } + else + { + this.status &= ~EntityStatus.IsComponent; + } + } + } + + [BsonIgnore] + [IgnoreDataMember] + protected bool IsCreated + { + get => (this.status & EntityStatus.IsCreated) == EntityStatus.IsCreated; + set + { + if (value) + { + this.status |= EntityStatus.IsCreated; + } + else + { + this.status &= ~EntityStatus.IsCreated; + } + } + } + + [BsonIgnore] + [IgnoreDataMember] + protected bool IsNew + { + get => (this.status & EntityStatus.IsNew) == EntityStatus.IsNew; + set + { + if (value) + { + this.status |= EntityStatus.IsNew; + } + else + { + this.status &= ~EntityStatus.IsNew; + } + } + } + + [BsonIgnore] + [IgnoreDataMember] + protected virtual string ViewName + { + get + { + return this.GetType().FullName; + } + } + + #endregion + #region Entities private static readonly Dictionary Entities = new Dictionary(); @@ -73,7 +172,7 @@ namespace TEngine entity = Activator.CreateInstance(); } - entity._isFromPool = true; + entity.IsFromPool = true; return entity; } @@ -81,12 +180,12 @@ namespace TEngine { entity.Id = 0; - if (!entity._isFromPool) + if (!entity.IsFromPool) { return; } - entity._isFromPool = false; + entity.IsFromPool = false; Pool.Enqueue(entity.GetType(), entity); } @@ -123,7 +222,8 @@ namespace TEngine EntitiesSystem.Instance.Awake(entity); EntitiesSystem.Instance.StartUpdate(entity); } - + entity.IsCreated = true; + entity.IsNew = true; return entity; } @@ -144,7 +244,8 @@ namespace TEngine EntitiesSystem.Instance.Awake(entity); EntitiesSystem.Instance.StartUpdate(entity); } - + entity.IsCreated = true; + entity.IsNew = true; return entity; } @@ -199,11 +300,6 @@ namespace TEngine [BsonIgnore] [IgnoreDataMember] private DictionaryPool _multi; - - [BsonIgnore] - [IgnoreDataMember] - private bool _isFromPool; - #endregion #region AddComponent @@ -275,8 +371,10 @@ namespace TEngine } } + this.IsComponent = false; component.Parent = this; component.Scene = Scene; + component.IsComponent = true; } #endregion diff --git a/Assets/GameScripts/DotNet/Core/IdFactory/RuntimeIdStruct.cs b/Assets/GameScripts/DotNet/Core/IdFactory/RuntimeIdStruct.cs index 24676fa0..18ab3f71 100644 --- a/Assets/GameScripts/DotNet/Core/IdFactory/RuntimeIdStruct.cs +++ b/Assets/GameScripts/DotNet/Core/IdFactory/RuntimeIdStruct.cs @@ -36,5 +36,10 @@ namespace TEngine.Core }; return idStruct; } + + public override string ToString() + { + return $"time: {this.Time}, value: {this.Sequence}"; + } } } \ No newline at end of file