From 0ab6deca985c6733646dd8f3310a9a9195450632 Mon Sep 17 00:00:00 2001 From: Laszlo Paillat Date: Fri, 18 Nov 2022 23:44:28 +0100 Subject: [PATCH] Entity.Set, EntitySetSameAs and EntitySetSameAsWorld will now reenable the component if it was disabled --- documentation/NEXT_RELEASENOTES.txt | 6 +++++- source/DefaultEcs.Test/EntityTest.cs | 15 +++++++++++++++ source/DefaultEcs/Entity.cs | 7 ++++++- source/DefaultEcs/Internal/ComponentPool.cs | 6 ++++-- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/documentation/NEXT_RELEASENOTES.txt b/documentation/NEXT_RELEASENOTES.txt index 3ad99253..5821c62d 100644 --- a/documentation/NEXT_RELEASENOTES.txt +++ b/documentation/NEXT_RELEASENOTES.txt @@ -1,7 +1,11 @@ +## Breaking changes + +- Entity.Set, EntitySetSameAs and EntitySetSameAsWorld will now reenable the component if it was disabled + ## New features - World.SetMaxCapacity can now be called multiple times for the same component type (#149) ## Bug fixes -- fixed TextSerializer not using invariant cultur during serialization (#170 thanks to @Helco) \ No newline at end of file +- fixed TextSerializer not using invariant culture during serialization (#170 thanks to @Helco) \ No newline at end of file diff --git a/source/DefaultEcs.Test/EntityTest.cs b/source/DefaultEcs.Test/EntityTest.cs index aba3f46a..805c2e5f 100644 --- a/source/DefaultEcs.Test/EntityTest.cs +++ b/source/DefaultEcs.Test/EntityTest.cs @@ -873,6 +873,21 @@ public void ToString_Should_not_throw() Check.That(Regex.IsMatch(entity.ToString(), "^Entity \\d*:\\d*.\\d*$")).IsTrue(); } + [Fact] + public void Set_Should_reenable_disabled_component() + { + using World world = new(); + + Entity entity = world.CreateEntity(); + + entity.Set(true); + entity.Disable(); + + entity.Set(true); + + Check.That(entity.IsEnabled()).IsTrue(); + } + #endregion } } diff --git a/source/DefaultEcs/Entity.cs b/source/DefaultEcs/Entity.cs index 533c56a4..a5307823 100644 --- a/source/DefaultEcs/Entity.cs +++ b/source/DefaultEcs/Entity.cs @@ -94,10 +94,15 @@ private void InnerSet(bool isNew) components[ComponentManager.Flag] = true; Publisher.Publish(WorldId, new ComponentAddedMessage(EntityId, components)); } - else + else if (components[ComponentManager.Flag]) { Publisher.Publish(WorldId, new ComponentChangedMessage(EntityId, components)); } + else + { + components[ComponentManager.Flag] = true; + Publisher.Publish(WorldId, new ComponentEnabledMessage(EntityId, components)); + } previousPool?.Set(EntityId, component); } diff --git a/source/DefaultEcs/Internal/ComponentPool.cs b/source/DefaultEcs/Internal/ComponentPool.cs index 79811b47..1c734036 100644 --- a/source/DefaultEcs/Internal/ComponentPool.cs +++ b/source/DefaultEcs/Internal/ComponentPool.cs @@ -170,6 +170,7 @@ public bool Set(int entityId, in T component) { ArrayExtension.EnsureLength(ref _mapping, entityId, _worldMaxCapacity, -1); + bool isNew = true; ref int componentIndex = ref _mapping[entityId]; if (componentIndex != -1) { @@ -181,13 +182,14 @@ public bool Set(int entityId, in T component) } Remove(entityId); + isNew = false; } if (_lastComponentIndex == MaxCapacity - 1) { if (_isFlagType) { - return SetSameAs(entityId, _links[0].EntityId); + return SetSameAs(entityId, _links[0].EntityId) && isNew; } ThrowMaxNumberOfComponentReached(); @@ -206,7 +208,7 @@ public bool Set(int entityId, in T component) _components[_lastComponentIndex] = component; _links[_lastComponentIndex] = new ComponentLink(entityId); - return true; + return isNew; } [MethodImpl(MethodImplOptions.AggressiveInlining)]