Skip to content

Commit

Permalink
fixed Entity.Set overriding shared component value (fix #92)
Browse files Browse the repository at this point in the history
  • Loading branch information
Doraku committed Apr 24, 2020
1 parent bb9cc5f commit 6a94399
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
22 changes: 19 additions & 3 deletions source/DefaultEcs.Test/EntityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,22 @@ public void Set_Should_only_produce_one_component_for_flag_type()
Check.That(world.Get<FlagType>().Length).IsEqualTo(1);
}

[Fact]
public void Set_Should_override_SetSameAs()
{
using World world = new World();

Entity reference = world.CreateEntity();
Entity entity = world.CreateEntity();

reference.Set(true);
entity.SetSameAs<bool>(reference);
entity.Set(false);

Check.That(reference.Get<bool>()).IsTrue();
Check.That(entity.Get<bool>()).IsFalse();
}

[Fact]
public void SetSameAs_Should_throw_When_Entity_not_created_from_World()
{
Expand Down Expand Up @@ -365,11 +381,11 @@ public void SetSameAs_Should_set_component_to_reference()

Check.That(entity.Get<bool>()).IsEqualTo(reference.Get<bool>());

reference.Set(true);
reference.Get<bool>() = true;

Check.That(entity.Get<bool>()).IsEqualTo(reference.Get<bool>());

entity.Set(false);
entity.Get<bool>() = false;

Check.That(reference.Get<bool>()).IsEqualTo(entity.Get<bool>());
}
Expand Down Expand Up @@ -530,7 +546,7 @@ public void Get_Should_get_component_of_reference_When_is_same_as()

Check.That(entity.Get<bool>()).IsTrue();

reference.Set(false);
reference.Get<bool>() = false;

Check.That(entity.Get<bool>()).IsFalse();
}
Expand Down
2 changes: 2 additions & 0 deletions source/DefaultEcs/DefaultEcs.Release.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
removed restriction on ManagedResource TResource to be IDisposable

added AResourceManager.Unload to give the possibility to override the unload action for a resource

fixed Entity.Set overriding shared component value
</PackageReleaseNotes>
</PropertyGroup>
</Project>
9 changes: 7 additions & 2 deletions source/DefaultEcs/Technical/ComponentPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,14 @@ public bool Set(int entityId, in T component)
ref int componentIndex = ref _mapping[entityId];
if (componentIndex != -1)
{
_components[componentIndex] = component;
if (_links[componentIndex].ReferenceCount == 1)
{
_components[componentIndex] = component;

return false;
return false;
}

Remove(entityId);
}

if (_lastComponentIndex == MaxCapacity - 1)
Expand Down

0 comments on commit 6a94399

Please sign in to comment.