Skip to content

Commit

Permalink
Some cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
genaray committed Jul 10, 2023
1 parent 21c0de7 commit 0d7b542
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
35 changes: 34 additions & 1 deletion src/Arch/CommandBuffer/CommandBuffer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Arch.Core;
using Arch.Core.Extensions;
using Arch.Core.Extensions.Internal;
using Arch.Core.Utils;
using Collections.Pooled;

Expand Down Expand Up @@ -273,6 +274,38 @@ public void Remove<T>(in Entity entity)
Removes.Set<T>(info.RemoveIndex);
}

/// <summary>
/// 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>
[SkipLocalsInit]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void AddRange(World world, Entity entity, IList<ComponentType> components)
{
var oldArchetype = world.EntityInfo.GetArchetype(entity.Id);

// BitSet to stack/span bitset, size big enough to contain ALL registered components.
Span<uint> stack = stackalloc uint[BitSet.RequiredLength(ComponentRegistry.Size)];
oldArchetype.BitSet.AsSpan(stack);

// Create a span bitset, doing it local saves us headache and gargabe
var spanBitSet = new SpanBitSet(stack);

for (var index = 0; index < components.Count; index++)
{
var type = components[index];
spanBitSet.SetBit(type.Id);
}

if (!world.TryGetArchetype(spanBitSet.GetHashCode(), out var newArchetype))
{
newArchetype = world.GetOrCreate(oldArchetype.Types.Add(components));
}

world.Move(entity, oldArchetype, newArchetype, out _);
}

/// <summary>
/// Plays back all recorded commands, modifying the world.
/// </summary>
Expand Down Expand Up @@ -315,7 +348,7 @@ public void Playback()
var entity = Resolve(wrappedEntity.Entity);
Debug.Assert(World.IsAlive(entity), $"CommandBuffer can not to add components to the dead {wrappedEntity.Entity}");

World.AddRange_Internal(entity, _addTypes);
AddRange(World, entity, _addTypes);
_addTypes.Clear();
}

Expand Down
15 changes: 2 additions & 13 deletions src/Arch/Core/Extensions/WorldExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public static void AddRange(this World world, Entity entity, params object[] com
/// <param name="components">A <see cref="IList{T}"/> of <see cref="ComponentType"/>'s, those are added to the <see cref="Entity"/>.</param>
[SkipLocalsInit]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void AddRange_Internal(this World world, Entity entity, IList<ComponentType> components)
public static void AddRange(this World world, Entity entity, IList<ComponentType> components)
{
var oldArchetype = world.EntityInfo.GetArchetype(entity.Id);

Expand All @@ -168,18 +168,7 @@ internal static void AddRange_Internal(this World world, Entity entity, IList<Co
}

world.Move(entity, oldArchetype, newArchetype, out _);
}

/// <summary>
/// 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>
[SkipLocalsInit]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void AddRange(this World world, Entity entity, IList<ComponentType> components)
{
AddRange_Internal(world, entity, components);

#if EVENTS
for (var i = 0; i < components.Count; i++)
{
Expand Down

0 comments on commit 0d7b542

Please sign in to comment.