Skip to content

Commit

Permalink
added EntityRecord.SetSameAsWorld method
Browse files Browse the repository at this point in the history
  • Loading branch information
Doraku committed Jun 5, 2021
1 parent 0a9bc0e commit 78a0252
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 5 deletions.
3 changes: 2 additions & 1 deletion documentation/NEXT_RELEASENOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ renamed World.Get to World.GetAll
added ComponentCloner type to clone components between entities
added Entity.CopyTo method overload taking a ComponentCloner
added Entity.SetSameAsWorld method
added EntityRecorder.CopyTo methods
added EntityRecord.CopyTo methods
added EntityRecord.SetSameAsWorld method
added World.Set method to set a world component
added World.Get method to get a world component
added World.Remove method to remove a world component
Expand Down
16 changes: 16 additions & 0 deletions source/DefaultEcs.Test/Command/EntityCommandRecorderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,22 @@ public void SetSameAs_Should_set_same_as_on_created_entity()
Check.That(world.Single().Get<bool>()).IsTrue();
}

[Fact]
public void SetSameAsWorld_Should_set_same_as_on_created_entity()
{
using EntityCommandRecorder recorder = new(1024);
using World world = new();

world.Set(true);

EntityRecord record = recorder.CreateEntity(world);
record.SetSameAsWorld<bool>();

recorder.Execute();

Check.That(world.Single().Get<bool>()).IsTrue();
}

[Fact]
public void Should_work_in_multithread()
{
Expand Down
8 changes: 8 additions & 0 deletions source/DefaultEcs/Command/EntityRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ internal EntityRecord(EntityCommandRecorder recorder, int offset)
/// <exception cref="InvalidOperationException">Command buffer is full.</exception>
public void SetSameAs<T>(in EntityRecord reference) => _recorder.WriteCommand(new EntityReferenceOffsetComponentCommand(CommandType.SetSameAs, ComponentCommands.ComponentCommand<T>.Index, _offset, reference._offset));

/// <summary>
/// Sets the value of the component of type <typeparamref name="T"/> on the corresponding <see cref="Entity"/> to the same instance of its <see cref="World"/>.
/// This command takes 9 bytes.
/// </summary>
/// <typeparam name="T">The type of the component.</typeparam>
/// <exception cref="InvalidOperationException">Command buffer is full.</exception>
public void SetSameAsWorld<T>() => _recorder.WriteCommand(new EntityOffsetComponentCommand(CommandType.SetSameAsWorld, ComponentCommands.ComponentCommand<T>.Index, _offset));

/// <summary>
/// Removes the component of type <typeparamref name="T"/> on the corresponding <see cref="Entity"/>.
/// This command takes 9 bytes.
Expand Down
9 changes: 5 additions & 4 deletions source/DefaultEcs/Technical/Command/CommandType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ internal enum CommandType : byte
DisableT = 5,
Set = 6,
SetSameAs = 7,
Remove = 8,
NotifyChanged = 9,
Clone = 10,
Dispose = 11
SetSameAsWorld = 8,
Remove = 9,
NotifyChanged = 10,
Clone = 11,
Dispose = 12
}
}
2 changes: 2 additions & 0 deletions source/DefaultEcs/Technical/Command/ComponentCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ static ComponentCommand()

public void SetSameAs(in Entity entity, in Entity reference) => entity.SetSameAs<T>(reference);

public void SetSameAsWorld(in Entity entity) => entity.SetSameAsWorld<T>();

public void Remove(in Entity entity) => entity.Remove<T>();

public void NotifyChanged(in Entity entity) => entity.NotifyChanged<T>();
Expand Down
6 changes: 6 additions & 0 deletions source/DefaultEcs/Technical/Command/Executer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public static unsafe void Execute(byte[] memory, int commandLength, List<object>
commandSize = sizeof(EntityReferenceOffsetComponentCommand);
break;

case CommandType.SetSameAsWorld:
componentCommand = (EntityOffsetComponentCommand*)commands;
ComponentCommands.GetCommand(componentCommand->ComponentIndex).SetSameAsWorld(*(Entity*)(memoryP + componentCommand->EntityOffset));
commandSize = sizeof(EntityOffsetComponentCommand);
break;

case CommandType.Remove:
componentCommand = (EntityOffsetComponentCommand*)commands;
ComponentCommands.GetCommand(componentCommand->ComponentIndex).Remove(*(Entity*)(memoryP + componentCommand->EntityOffset));
Expand Down
1 change: 1 addition & 0 deletions source/DefaultEcs/Technical/Command/IComponentCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ internal unsafe interface IComponentCommand
void Disable(in Entity entity);
int Set(in Entity entity, List<object> objects, byte* memory);
void SetSameAs(in Entity entity, in Entity reference);
void SetSameAsWorld(in Entity entity);
void Remove(in Entity entity);
void NotifyChanged(in Entity entity);
}
Expand Down

0 comments on commit 78a0252

Please sign in to comment.