Skip to content

Commit

Permalink
Simplified API by removing certain methods from Arch.
Browse files Browse the repository at this point in the history
  • Loading branch information
genaray committed Jul 24, 2024
1 parent abf9b85 commit 66a46cb
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 279 deletions.
12 changes: 6 additions & 6 deletions src/Arch.Tests/EntityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ public void AddAndRemove_NonGeneric()
using var world = World.Create();
var entity = world.Create();

entity.AddRange(new List<ComponentType>{typeof(Transform), typeof(Rotation)});
That(entity.HasRange(typeof(Transform), typeof(Rotation)));
entity.AddRange(new ComponentType[]{typeof(Transform), typeof(Rotation)});
That(entity.HasRange(new ComponentType[]{typeof(Transform), typeof(Rotation)}));

entity.RemoveRange(typeof(Transform), typeof(Rotation));
That(!entity.HasRange(typeof(Transform), typeof(Rotation)));
entity.RemoveRange(new ComponentType[]{typeof(Transform), typeof(Rotation)});
That(!entity.HasRange(new ComponentType[]{ typeof(Transform), typeof(Rotation)}));

entity.AddRange(new Transform(), new Rotation());
That(entity.HasRange(typeof(Transform), typeof(Rotation)));
entity.AddRange(new object[]{ new Transform(), new Rotation()});
That(entity.HasRange(new ComponentType[]{ typeof(Transform), typeof(Rotation)}));
}
}

Expand Down
23 changes: 12 additions & 11 deletions src/Arch.Tests/WorldTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Arch.Core.Extensions;
using Arch.Core.Extensions.Dangerous;
using Arch.Core.Utils;
using CommunityToolkit.HighPerformance;
using static NUnit.Framework.Assert;

namespace Arch.Tests;
Expand Down Expand Up @@ -91,10 +92,10 @@ public void DestroyAll()
{
var query = new QueryDescription { All = new ComponentType[] { typeof(Transform) } };

var entities = new List<Entity>();
_world.GetEntities(query, entities);
var entities = new Entity[_world.CountEntities(query)];
_world.GetEntities(query, entities.AsSpan());

for (var i = 0; i < entities.Count; i++)
for (var i = 0; i < entities.Length; i++)
{
var entity = entities[i];
_world.Destroy(entity);
Expand Down Expand Up @@ -363,15 +364,15 @@ public void GetEntitesTest()
var entity = world.Create(archTypes);

// Get entities
var entites = new List<Entity>();
world.GetEntities(query, entites);
var entites = new Entity[world.CountEntities(query)];
world.GetEntities(query, entites.AsSpan());

That(entites.Count, Is.EqualTo(1));

// Destroy the one entity
entites.Clear();
world.Destroy(entity);
world.GetEntities(query, entites);
entites = new Entity[world.CountEntities(query)];
world.GetEntities(query, entites.AsSpan());

That(entites.Count, Is.EqualTo(0));
}
Expand Down Expand Up @@ -634,8 +635,8 @@ public void Remove_NonGeneric()
{
var entity = _world.Create(_entityGroup);
var entity2 = _world.Create(_entityGroup);
_world.RemoveRange(entity, typeof(Transform));
_world.RemoveRange(entity2, typeof(Transform));
_world.RemoveRange(entity, new ComponentType[]{typeof(Transform)});
_world.RemoveRange(entity2, new ComponentType[]{typeof(Transform)});

That(_world.GetArchetype(entity2), Is.EqualTo(_world.GetArchetype(entity)));
That(_world.GetArchetype(entity).ChunkCount, Is.EqualTo(1));
Expand All @@ -650,8 +651,8 @@ public void Add_NonGeneric()
{
var entity = _world.Create(_entityGroup);
var entity2 = _world.Create(_entityGroup);
_world.AddRange(entity, new Ai());
_world.AddRange(entity2, new Ai());
_world.AddRange(entity, new object[]{new Ai()});
_world.AddRange(entity2, new object[]{new Ai()});

_world.TryGetArchetype(_entityAiGroup, out var arch);
That(_world.GetArchetype(entity2), Is.EqualTo(_world.GetArchetype(entity)));
Expand Down
2 changes: 1 addition & 1 deletion src/Arch/Buffer/CommandBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public void Playback(World world, bool dispose = true)
var entity = Resolve(wrappedEntity.Entity);
Debug.Assert(world.IsAlive(entity), $"CommandBuffer can not to remove components from the dead {wrappedEntity.Entity}");

world.RemoveRange(entity, _removeTypes);
world.RemoveRange(entity, _removeTypes.Span);
_removeTypes.Clear();
}

Expand Down
29 changes: 8 additions & 21 deletions src/Arch/Core/Extensions/EntityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public static void Set(this in Entity entity, object cmp)
/// <param name="entity">The <see cref="Entity"/>.</param>
/// <param name="components">The components <see cref="IList{T}"/>.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetRange(this in Entity entity, params object[] components)
public static void SetRange(this in Entity entity, Span<object> components)
{
var world = World.Worlds[entity.WorldId];
world.SetRange(entity, components);
Expand Down Expand Up @@ -270,7 +270,7 @@ public static bool Has(this in Entity entity, ComponentType type)
/// <returns>True if it has the desired component, otherwhise false.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Pure]
public static bool HasRange(this in Entity entity, params ComponentType[] types)
public static bool HasRange(this in Entity entity, Span<ComponentType> types)
{
var world = World.Worlds[entity.WorldId];
return world.HasRange(entity, types);
Expand Down Expand Up @@ -298,7 +298,7 @@ public static bool HasRange(this in Entity entity, params ComponentType[] types)
/// <returns>A reference to the component.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Pure]
public static object?[] GetRange(this in Entity entity, params ComponentType[] types)
public static object?[] GetRange(this in Entity entity, Span<ComponentType> types)
{
var world = World.Worlds[entity.WorldId];
return world.GetRange(entity, types);
Expand All @@ -314,7 +314,7 @@ public static bool HasRange(this in Entity entity, params ComponentType[] types)
/// <returns>A reference to the component.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Pure]
public static void GetRange(this in Entity entity, ComponentType[] types, IList<object?> components)
public static void GetRange(this in Entity entity, Span<ComponentType> types, Span<object?> components)
{
var world = World.Worlds[entity.WorldId];
world.GetRange(entity, types, components);
Expand Down Expand Up @@ -356,7 +356,7 @@ public static void Add(this in Entity entity, in object cmp)
/// <param name="components">The component <see cref="IList{T}"/>.</param>
[SkipLocalsInit]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void AddRange(this in Entity entity, params object[] components)
public static void AddRange(this in Entity entity, Span<object> components)
{
var world = World.Worlds[entity.WorldId];
world.AddRange(entity, components);
Expand All @@ -366,10 +366,10 @@ public static void AddRange(this in Entity entity, params object[] components)
/// Adds an list of new components to the <see cref="Entity"/> and moves it to the new <see cref="Archetype"/>.
/// </summary>
/// <param name="entity">The <see cref="Entity"/>.</param>
/// <param name="components">A <see cref="IList{T}"/> of <see cref="ComponentType"/>'s, those are added to the <see cref="Entity"/>.</param>
/// <param name="components">A <see cref="Span{T}"/> of <see cref="ComponentType"/>'s, those are added to the <see cref="Entity"/>.</param>
[SkipLocalsInit]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void AddRange(this in Entity entity, IList<ComponentType> components)
public static void AddRange(this in Entity entity, Span<ComponentType> components)
{
var world = World.Worlds[entity.WorldId];
world.AddRange(entity, components);
Expand All @@ -382,20 +382,7 @@ public static void AddRange(this in Entity entity, IList<ComponentType> componen
/// <param name="types">A <see cref="IList{T}"/> of <see cref="ComponentType"/>'s, those are removed from the <see cref="Entity"/>.</param>
[SkipLocalsInit]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void RemoveRange(this in Entity entity, params ComponentType[] types)
{
var world = World.Worlds[entity.WorldId];
world.RemoveRange(entity, types);
}

/// <summary>
/// Removes a list of <see cref="ComponentType"/>'s from the <see cref="Entity"/> and moves it to a different <see cref="Archetype"/>.
/// </summary>
/// <param name="entity">The <see cref="Entity"/>.</param>
/// <param name="types">A <see cref="IList{T}"/> of <see cref="ComponentType"/>'s, those are removed from the <see cref="Entity"/>.</param>
[SkipLocalsInit]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void RemoveRange(this in Entity entity, IList<ComponentType> types)
public static void RemoveRange(this in Entity entity, Span<ComponentType> types)
{
var world = World.Worlds[entity.WorldId];
world.RemoveRange(entity, types);
Expand Down
Loading

0 comments on commit 66a46cb

Please sign in to comment.