Skip to content

Commit

Permalink
Update CommandBuffer.cs (#208)
Browse files Browse the repository at this point in the history
Co-authored-by: Skillitronic <[email protected]>
  • Loading branch information
genaray and skelitheprogrammer committed Mar 13, 2024
1 parent b4823e7 commit 4294cce
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions src/Arch/CommandBuffer/CommandBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,9 @@ public sealed class CommandBuffer : IDisposable
/// Initializes a new instance of the <see cref="CommandBuffer"/> class
/// with the specified <see cref="Core.World"/> and an optional <paramref name="initialCapacity"/> (default: 128).
/// </summary>
/// <param name="world">The <see cref="World"/>.</param>
/// <param name="initialCapacity">The initial capacity.</param>
public CommandBuffer(World world, int initialCapacity = 128)
public CommandBuffer(int initialCapacity = 128)
{
World = world;
Entities = new PooledList<Entity>(initialCapacity);
BufferedEntityInfo = new PooledDictionary<int, BufferedEntityInfo>(initialCapacity);
Creates = new PooledList<CreateCommand>(initialCapacity);
Expand All @@ -86,11 +84,6 @@ public CommandBuffer(World world, int initialCapacity = 128)
_removeTypes = new PooledList<ComponentType>(16);
}

/// <summary>
/// Gets the <see cref="Core.World"/>.
/// </summary>
public World World { get; }

/// <summary>
/// Gets the amount of <see cref="Entity"/> instances targeted by this <see cref="CommandBuffer"/>.
/// </summary>
Expand Down Expand Up @@ -170,14 +163,15 @@ internal Entity Resolve(Entity entity)
/// Records a Create operation for an <see cref="Entity"/> based on its component structure.
/// Will be created during <see cref="Playback"/>.
/// </summary>
/// <param name="world"></param>
/// <param name="types">The <see cref="Entity"/>'s component structure/<see cref="Archetype"/>.</param>
/// <returns>The buffered <see cref="Entity"/> with an index of <c>-1</c>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Entity Create(ComponentType[] types)
public Entity Create(World world, ComponentType[] types)
{
lock (this)
{
var entity = new Entity(-(Size + 1), World.Id);
var entity = new Entity(-(Size + 1), world.Id);
Register(entity, out _);

var command = new CreateCommand(Size - 1, types);
Expand Down Expand Up @@ -306,20 +300,20 @@ internal static void AddRange(World world, Entity entity, IList<ComponentType> c

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

/// <summary>
/// Plays back all recorded commands, modifying the world.
/// </summary>
/// <remarks>
/// This operation should only happen on the main thread.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Playback()
public void Playback(World world)
{
// Create recorded entities.
foreach (var cmd in Creates)
{
var entity = World.Create(cmd.Types);
var entity = world.Create(cmd.Types);
Entities[cmd.Index] = entity;
}

Expand All @@ -344,12 +338,12 @@ public void Playback()
{
continue;
}

// Resolves the entity to get the real one (e.g. for newly created negative entities and stuff).
var entity = Resolve(wrappedEntity.Entity);
Debug.Assert(World.IsAlive(entity), $"CommandBuffer can not to add components to the dead {wrappedEntity.Entity}");
Debug.Assert(world.IsAlive(entity), $"CommandBuffer can not to add components to the dead {wrappedEntity.Entity}");

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

Expand All @@ -360,11 +354,11 @@ public void Playback()
var wrappedEntity = Sets.Entities[index];
var entity = Resolve(wrappedEntity.Entity);
var id = wrappedEntity.Index;
Debug.Assert(World.IsAlive(entity), $"CommandBuffer can not to set components to the dead {wrappedEntity.Entity}");

Debug.Assert(world.IsAlive(entity), $"CommandBuffer can not to set components to the dead {wrappedEntity.Entity}");

// Get entity chunk
var entityInfo = World.EntityInfo[entity.Id];
var entityInfo = world.EntityInfo[entity.Id];
var archetype = entityInfo.Archetype;
ref readonly var chunk = ref archetype.GetChunk(entityInfo.Slot.ChunkIndex);
var chunkIndex = entityInfo.Slot.Index;
Expand Down Expand Up @@ -417,18 +411,18 @@ public void Playback()
{
continue;
}

var entity = Resolve(wrappedEntity.Entity);
Debug.Assert(World.IsAlive(entity), $"CommandBuffer can not to remove components from the dead {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);
_removeTypes.Clear();
}

// Play back destructions.
foreach (var cmd in Destroys)
{
World.Destroy(Entities[cmd]);
world.Destroy(Entities[cmd]);
}

// Reset values.
Expand Down

0 comments on commit 4294cce

Please sign in to comment.