Skip to content

Commit

Permalink
Entity.Set, EntitySetSameAs and EntitySetSameAsWorld will now reenabl…
Browse files Browse the repository at this point in the history
…e the component if it was disabled
  • Loading branch information
Doraku committed Nov 18, 2022
1 parent 2eb8c8a commit 0ab6dec
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
6 changes: 5 additions & 1 deletion documentation/NEXT_RELEASENOTES.txt
Original file line number Diff line number Diff line change
@@ -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)
- fixed TextSerializer not using invariant culture during serialization (#170 thanks to @Helco)
15 changes: 15 additions & 0 deletions source/DefaultEcs.Test/EntityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>();

entity.Set(true);

Check.That(entity.IsEnabled<bool>()).IsTrue();
}

#endregion
}
}
7 changes: 6 additions & 1 deletion source/DefaultEcs/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,15 @@ private void InnerSet<T>(bool isNew)
components[ComponentManager<T>.Flag] = true;
Publisher.Publish(WorldId, new ComponentAddedMessage<T>(EntityId, components));
}
else
else if (components[ComponentManager<T>.Flag])
{
Publisher.Publish(WorldId, new ComponentChangedMessage<T>(EntityId, components));
}
else
{
components[ComponentManager<T>.Flag] = true;
Publisher.Publish(WorldId, new ComponentEnabledMessage<T>(EntityId, components));
}

previousPool?.Set(EntityId, component);
}
Expand Down
6 changes: 4 additions & 2 deletions source/DefaultEcs/Internal/ComponentPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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();
Expand All @@ -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)]
Expand Down

0 comments on commit 0ab6dec

Please sign in to comment.