From 78a0252006bd43f5a35da6d0f7f17587095effa5 Mon Sep 17 00:00:00 2001 From: Laszlo Paillat Date: Sat, 5 Jun 2021 18:33:54 +0200 Subject: [PATCH] added EntityRecord.SetSameAsWorld method --- documentation/NEXT_RELEASENOTES.txt | 3 ++- .../Command/EntityCommandRecorderTest.cs | 16 ++++++++++++++++ source/DefaultEcs/Command/EntityRecord.cs | 8 ++++++++ .../DefaultEcs/Technical/Command/CommandType.cs | 9 +++++---- .../Technical/Command/ComponentCommands.cs | 2 ++ source/DefaultEcs/Technical/Command/Executer.cs | 6 ++++++ .../Technical/Command/IComponentCommand.cs | 1 + 7 files changed, 40 insertions(+), 5 deletions(-) diff --git a/documentation/NEXT_RELEASENOTES.txt b/documentation/NEXT_RELEASENOTES.txt index 86fb091b..4d68ab75 100644 --- a/documentation/NEXT_RELEASENOTES.txt +++ b/documentation/NEXT_RELEASENOTES.txt @@ -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 diff --git a/source/DefaultEcs.Test/Command/EntityCommandRecorderTest.cs b/source/DefaultEcs.Test/Command/EntityCommandRecorderTest.cs index ca29a805..068fb046 100644 --- a/source/DefaultEcs.Test/Command/EntityCommandRecorderTest.cs +++ b/source/DefaultEcs.Test/Command/EntityCommandRecorderTest.cs @@ -408,6 +408,22 @@ public void SetSameAs_Should_set_same_as_on_created_entity() Check.That(world.Single().Get()).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(); + + recorder.Execute(); + + Check.That(world.Single().Get()).IsTrue(); + } + [Fact] public void Should_work_in_multithread() { diff --git a/source/DefaultEcs/Command/EntityRecord.cs b/source/DefaultEcs/Command/EntityRecord.cs index a96ca5e3..cf6945cb 100644 --- a/source/DefaultEcs/Command/EntityRecord.cs +++ b/source/DefaultEcs/Command/EntityRecord.cs @@ -86,6 +86,14 @@ internal EntityRecord(EntityCommandRecorder recorder, int offset) /// Command buffer is full. public void SetSameAs(in EntityRecord reference) => _recorder.WriteCommand(new EntityReferenceOffsetComponentCommand(CommandType.SetSameAs, ComponentCommands.ComponentCommand.Index, _offset, reference._offset)); + /// + /// Sets the value of the component of type on the corresponding to the same instance of its . + /// This command takes 9 bytes. + /// + /// The type of the component. + /// Command buffer is full. + public void SetSameAsWorld() => _recorder.WriteCommand(new EntityOffsetComponentCommand(CommandType.SetSameAsWorld, ComponentCommands.ComponentCommand.Index, _offset)); + /// /// Removes the component of type on the corresponding . /// This command takes 9 bytes. diff --git a/source/DefaultEcs/Technical/Command/CommandType.cs b/source/DefaultEcs/Technical/Command/CommandType.cs index 21445c97..7c393f30 100644 --- a/source/DefaultEcs/Technical/Command/CommandType.cs +++ b/source/DefaultEcs/Technical/Command/CommandType.cs @@ -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 } } diff --git a/source/DefaultEcs/Technical/Command/ComponentCommands.cs b/source/DefaultEcs/Technical/Command/ComponentCommands.cs index 754d5db0..11de273b 100644 --- a/source/DefaultEcs/Technical/Command/ComponentCommands.cs +++ b/source/DefaultEcs/Technical/Command/ComponentCommands.cs @@ -77,6 +77,8 @@ static ComponentCommand() public void SetSameAs(in Entity entity, in Entity reference) => entity.SetSameAs(reference); + public void SetSameAsWorld(in Entity entity) => entity.SetSameAsWorld(); + public void Remove(in Entity entity) => entity.Remove(); public void NotifyChanged(in Entity entity) => entity.NotifyChanged(); diff --git a/source/DefaultEcs/Technical/Command/Executer.cs b/source/DefaultEcs/Technical/Command/Executer.cs index 49f091ee..cbb48a2d 100644 --- a/source/DefaultEcs/Technical/Command/Executer.cs +++ b/source/DefaultEcs/Technical/Command/Executer.cs @@ -61,6 +61,12 @@ public static unsafe void Execute(byte[] memory, int commandLength, List 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)); diff --git a/source/DefaultEcs/Technical/Command/IComponentCommand.cs b/source/DefaultEcs/Technical/Command/IComponentCommand.cs index d786e034..b195c559 100644 --- a/source/DefaultEcs/Technical/Command/IComponentCommand.cs +++ b/source/DefaultEcs/Technical/Command/IComponentCommand.cs @@ -8,6 +8,7 @@ internal unsafe interface IComponentCommand void Disable(in Entity entity); int Set(in Entity entity, List 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); }