Skip to content

Commit

Permalink
added World.Get method, obsolete GetAllComponents
Browse files Browse the repository at this point in the history
renamed some new api again...
- World.SetComponentMaxCapacity to SetMaxCapacity
- World.GetComponentMaxCapacity to GetMaxCapacity
  • Loading branch information
Doraku committed Jan 19, 2020
1 parent 68beab1 commit efc6868
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 64 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public struct Example
To reduce memory, it is possible to set a maximum count for a given component type. If nothing is set, then the maximum entity count of the world will be used.
```csharp
int componentMaxCapacity = 42;
world.SetComponentMaxCapacity<Example>(componentMaxCapacity);
world.SetMaxCapacity<Example>(componentMaxCapacity);
```

It is then possible to add the component to the entity
Expand Down Expand Up @@ -187,7 +187,7 @@ EntitySet set = world.GetEntities().With<Example>().With<int>().AsSet();
EntitySet set = world.GetEntities().WithEither<Example>().Or<int>().AsSet();

// this gives all the component of type Example currently used in the world
Span<Example> components = world.GetAllComponents<Example>();
Span<Example> components = world.Get<Example>();
```

There is also some special rules which will make the EntitySet react to some events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public void DefaultEcs_EntitySet()
[Benchmark]
public void DefaultEcs_Component()
{
foreach (ref DefaultComponent component in _defaultWorld.GetAllComponents<DefaultComponent>())
foreach (ref DefaultComponent component in _defaultWorld.Get<DefaultComponent>())
{
++component.Value;
}
Expand Down
30 changes: 15 additions & 15 deletions source/DefaultEcs.Test/EntityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void Has_Should_return_false_When_Entity_does_not_have_component()
{
using World world = new World(1);

world.SetComponentMaxCapacity<bool>(0);
world.SetMaxCapacity<bool>(0);

Entity entity = world.CreateEntity();

Expand All @@ -194,7 +194,7 @@ public void Has_Should_return_true_When_Entity_has_component()
{
using World world = new World(1);

world.SetComponentMaxCapacity<bool>(1);
world.SetMaxCapacity<bool>(1);

Entity entity = world.CreateEntity();
entity.Set(true);
Expand All @@ -215,7 +215,7 @@ public void Set_Should_throw_When_max_number_of_component_reached()
{
using World world = new World(1);

world.SetComponentMaxCapacity<bool>(0);
world.SetMaxCapacity<bool>(0);
Entity entity = world.CreateEntity();

Check.ThatCode(() => entity.Set(true)).Throws<InvalidOperationException>();
Expand All @@ -226,7 +226,7 @@ public void Set_Should_set_component_value()
{
using World world = new World(1);

world.SetComponentMaxCapacity<bool>(1);
world.SetMaxCapacity<bool>(1);
Entity entity = world.CreateEntity();

entity.Set(true);
Expand All @@ -239,7 +239,7 @@ public void Set_Should_not_thow_When_more_than_32_components()
{
using World world = new World(1);

world.SetComponentMaxCapacity<bool>(1);
world.SetMaxCapacity<bool>(1);
Entity entity = world.CreateEntity();

entity.Set<bool>();
Expand Down Expand Up @@ -290,7 +290,7 @@ public void Set_Should_only_produce_one_component_for_flag_type()
Check.That(entity.Has<FlagType>()).IsTrue();
Check.That(entity2.Has<FlagType>()).IsTrue();

Check.That(world.GetAllComponents<FlagType>().Length).IsEqualTo(1);
Check.That(world.Get<FlagType>().Length).IsEqualTo(1);
}

[Fact]
Expand Down Expand Up @@ -339,7 +339,7 @@ public void SetSameAs_Should_throw_When_reference_does_not_have_a_component()
{
using World world = new World(2);

world.SetComponentMaxCapacity<bool>(1);
world.SetMaxCapacity<bool>(1);

Entity entity = world.CreateEntity();
Entity reference = world.CreateEntity();
Expand All @@ -352,7 +352,7 @@ public void SetSameAs_Should_set_component_to_reference()
{
using World world = new World(2);

world.SetComponentMaxCapacity<bool>(1);
world.SetMaxCapacity<bool>(1);

Entity entity = world.CreateEntity();
Entity reference = world.CreateEntity();
Expand Down Expand Up @@ -386,7 +386,7 @@ public void Remove_Should_remove_component()
{
using World world = new World(1);

world.SetComponentMaxCapacity<bool>(1);
world.SetMaxCapacity<bool>(1);

Entity entity = world.CreateEntity();

Expand All @@ -404,7 +404,7 @@ public void Remove_Should_remove_component_without_removing_reference()
{
using World world = new World(2);

world.SetComponentMaxCapacity<bool>(1);
world.SetMaxCapacity<bool>(1);

Entity entity = world.CreateEntity();
Entity reference = world.CreateEntity();
Expand All @@ -423,7 +423,7 @@ public void Remove_Should_remove_component_and_pass_reference()
{
using World world = new World(3);

world.SetComponentMaxCapacity<bool>(2);
world.SetMaxCapacity<bool>(2);

Entity entity = world.CreateEntity();
Entity reference = world.CreateEntity();
Expand All @@ -446,7 +446,7 @@ public void Remove_Should_not_remove_component_of_all_referenced_Entity()
{
using World world = new World(3);

world.SetComponentMaxCapacity<bool>(1);
world.SetMaxCapacity<bool>(1);

Entity entity = world.CreateEntity();
world.CreateEntity();
Expand Down Expand Up @@ -537,7 +537,7 @@ public void Dispose_Should_remove_all_component()
{
using World world = new World(2);

world.SetComponentMaxCapacity<bool>(1);
world.SetMaxCapacity<bool>(1);
world.CreateEntity();
Entity deletedEntity = world.CreateEntity();
deletedEntity.Set(true);
Expand Down Expand Up @@ -732,10 +732,10 @@ public void CopyTo_Should_left_no_trace_When_there_is_an_exception()
main.Set(42);
main.Set("kikoo");

world2.SetComponentMaxCapacity<string>(0);
world2.SetMaxCapacity<string>(0);

Check.ThatCode(() => main.CopyTo(world2)).ThrowsAny();
Check.That(world2.GetAllComponents<int>().Length).IsEqualTo(0);
Check.That(world2.Get<int>().Length).IsEqualTo(0);
Check.That(world2.GetEntities().AsSet().Count).IsEqualTo(0);
}

Expand Down
8 changes: 4 additions & 4 deletions source/DefaultEcs.Test/Serialization/ISerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ public void Serialize_Should_serialize_World(Type serializerType)
{
using World world = new World(42);

world.SetComponentMaxCapacity<int>(13);
world.SetComponentMaxCapacity<float>(60);
world.SetMaxCapacity<int>(13);
world.SetMaxCapacity<float>(60);

Entity[] entities = new[]
{
Expand Down Expand Up @@ -309,8 +309,8 @@ public void Serialize_Should_serialize_Entities(Type serializerType)
{
using World world = new World(42);

world.SetComponentMaxCapacity<int>(13);
world.SetComponentMaxCapacity<float>(60);
world.SetMaxCapacity<int>(13);
world.SetMaxCapacity<float>(60);

Entity[] entities = new[]
{
Expand Down
50 changes: 25 additions & 25 deletions source/DefaultEcs.Test/WorldTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,86 +86,86 @@ public void CreateEntity_Should_throw_When_max_number_of_Entity_is_reached()
}

[Fact]
public void SetComponentMaxCapacity_Should_return_true_When_maxCapacity_is_setted()
public void SetMaxCapacity_Should_return_true_When_maxCapacity_is_setted()
{
using World world = new World(0);

Check.That(world.SetComponentMaxCapacity<bool>(0)).IsTrue();
Check.That(world.SetMaxCapacity<bool>(0)).IsTrue();
}

[Fact]
public void SetComponentMaxCapacity_Should_return_false_When_maxCapacity_is_already_setted()
public void SetMaxCapacity_Should_return_false_When_maxCapacity_is_already_setted()
{
using World world = new World(42);

Check.That(world.SetComponentMaxCapacity<bool>(0)).IsTrue();
Check.That(world.SetComponentMaxCapacity<bool>(42)).IsFalse();
Check.That(world.SetMaxCapacity<bool>(0)).IsTrue();
Check.That(world.SetMaxCapacity<bool>(42)).IsFalse();
}

[Fact]
public void GetComponentMaxCapacity_Should_return_component_max_capacity()
public void GetMaxCapacity_Should_return_component_max_capacity()
{
using World world = new World(100);

world.SetComponentMaxCapacity<bool>(42);
Check.That(world.GetComponentMaxCapacity<bool>()).IsEqualTo(42);
world.SetMaxCapacity<bool>(42);
Check.That(world.GetMaxCapacity<bool>()).IsEqualTo(42);
}

[Fact]
public void GetComponentMaxCapacity_Should_return_minus_one_when_not_handled()
public void GetMaxCapacity_Should_return_minus_one_when_not_handled()
{
using World world = new World(100);

Check.That(world.GetComponentMaxCapacity<bool>()).IsEqualTo(-1);
Check.That(world.GetMaxCapacity<bool>()).IsEqualTo(-1);
}

[Fact]
public void GetComponentMaxCapacity_Should_return_one_for_flag_type()
public void GetMaxCapacity_Should_return_one_for_flag_type()
{
using World world = new World(100);

world.SetComponentMaxCapacity<FlagType>(42);
Check.That(world.GetComponentMaxCapacity<FlagType>()).IsEqualTo(1);
world.SetMaxCapacity<FlagType>(42);
Check.That(world.GetMaxCapacity<FlagType>()).IsEqualTo(1);
}

[Fact]
public void SetComponentMaxCapacity_Should_throw_When_maxCapacity_is_negative()
public void SetMaxCapacity_Should_throw_When_maxCapacity_is_negative()
{
using World world = new World(0);

Check.ThatCode(() => world.SetComponentMaxCapacity<bool>(-1)).Throws<ArgumentException>();
Check.ThatCode(() => world.SetMaxCapacity<bool>(-1)).Throws<ArgumentException>();
}

[Fact]
public void SetComponentMaxCapacity_Should_not_throw_When_already_added()
public void SetMaxCapacity_Should_not_throw_When_already_added()
{
using World world = new World(0);

world.SetComponentMaxCapacity<bool>(0);
Check.ThatCode(() => world.SetComponentMaxCapacity<bool>(0)).DoesNotThrow();
world.SetMaxCapacity<bool>(0);
Check.ThatCode(() => world.SetMaxCapacity<bool>(0)).DoesNotThrow();
}

[Fact]
public void GetAllComponents_Should_not_throw_When_not_added()
public void Get_Should_not_throw_When_not_added()
{
using World world = new World(0);

Check.ThatCode(() => world.GetAllComponents<bool>()).DoesNotThrow();
Check.ThatCode(() => world.Get<bool>()).DoesNotThrow();
}

[Fact]
public void GetAllComponents_Should_return_component()
public void Get_Should_return_component()
{
using World world = new World(2);

world.SetComponentMaxCapacity<int>(2);
world.SetMaxCapacity<int>(2);
Entity entity = world.CreateEntity();
Entity entity2 = world.CreateEntity();

entity.Set(1);
entity2.Set(2);

Span<int> components = world.GetAllComponents<int>();
Span<int> components = world.Get<int>();

Check.That(components[0]).IsEqualTo(entity.Get<int>());
Check.That(components[1]).IsEqualTo(entity2.Get<int>());
Expand Down Expand Up @@ -237,8 +237,8 @@ public void ReadAllComponentTypes_Should_callback_reader()
bool longIsOk = false;
bool floatIsOk = true;

world.SetComponentMaxCapacity<int>(1);
world.GetAllComponents<long>();
world.SetMaxCapacity<int>(1);
world.Get<long>();

IComponentTypeReader reader = Substitute.For<IComponentTypeReader>();
reader.When(m => m.OnRead<int>(1)).Do(_ => intIsOk = true);
Expand Down
5 changes: 3 additions & 2 deletions source/DefaultEcs/DefaultEcs.Release.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ added minEntityCountByRunnerIndex parameter to AEntitySystem constructors
added minComponentCountByRunnerIndex parameter to AComponentSystem constructor
added AEntityBufferedSystem type to make structural modification on entities in a safe way
added World.MaxCapacity property, obsolete MaxEntityCount
added World.SetComponentMaxCapacity method, obsolete SetMaximumComponentCount
added World.GetComponentMaxCapacity method, obsolete GetMaximumComponentCount
added World.SetMaxCapacity method, obsolete SetMaximumComponentCount
added World.GetMaxCapacity method, obsolete GetMaximumComponentCount
added World.Get method, obsolete GetAllComponents
added World.GetEnumerator method, obsolete GetAllEntities
added EntitySet.World property
added Components type for fast access to entities component
Expand Down
2 changes: 1 addition & 1 deletion source/DefaultEcs/Serialization/BinarySerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private sealed class ComponentOperation<T> : IComponentOperation
{
#region IOperation

public void SetMaxCapacity(World world, int maxCapacity) => world.SetComponentMaxCapacity<T>(maxCapacity);
public void SetMaxCapacity(World world, int maxCapacity) => world.SetMaxCapacity<T>(maxCapacity);

public void Set(in Entity entity, in StreamReaderWrapper reader) => entity.Set(Converter<T>.Read(reader));

Expand Down
2 changes: 1 addition & 1 deletion source/DefaultEcs/Serialization/TextSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private sealed class ComponentOperation<T> : IComponentOperation
{
#region IOperation

public void SetMaxCapacity(World world, int maxCapacity) => world.SetComponentMaxCapacity<T>(maxCapacity);
public void SetMaxCapacity(World world, int maxCapacity) => world.SetMaxCapacity<T>(maxCapacity);

public void Set(in Entity entity, string line, StreamReader reader)
{
Expand Down
2 changes: 1 addition & 1 deletion source/DefaultEcs/Technical/Debug/WorldDebugView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ private sealed class WorldComponents<T> : IComponent

public int MaxCapacity { get; }

public T[] Components => _world.GetAllComponents<T>().ToArray();
public T[] Components => _world.Get<T>().ToArray();

public WorldComponents(World world, int maxCapacity)
{
Expand Down
23 changes: 16 additions & 7 deletions source/DefaultEcs/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public Entity CreateEntity()
/// <param name="maxCapacity">The maximum number of component of type <typeparamref name="T"/> that can exist in this <see cref="World"/>.</param>
/// <returns>Whether the maximum count has been setted or not.</returns>
/// <exception cref="ArgumentException"><paramref name="maxCapacity"/> cannot be negative.</exception>
public bool SetComponentMaxCapacity<T>(int maxCapacity)
public bool SetMaxCapacity<T>(int maxCapacity)
{
if (maxCapacity < 0)
{
Expand All @@ -212,31 +212,40 @@ public bool SetComponentMaxCapacity<T>(int maxCapacity)
/// <param name="maxCapacity">The maximum number of component of type <typeparamref name="T"/> that can exist in this <see cref="World"/>.</param>
/// <returns>Whether the maximum count has been setted or not.</returns>
/// <exception cref="ArgumentException"><paramref name="maxCapacity"/> cannot be negative.</exception>
[Obsolete("Use SetComponentMaxCapacity instead, will be removed next version")]
public bool SetMaximumComponentCount<T>(int maxCapacity) => SetComponentMaxCapacity<T>(maxCapacity);
[Obsolete("Use SetMaxCapacity instead, will be removed next version")]
public bool SetMaximumComponentCount<T>(int maxCapacity) => SetMaxCapacity<T>(maxCapacity);

/// <summary>
/// Gets the maximum number of <typeparamref name="T"/> components this <see cref="World"/> can create.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns>The maximum number of <typeparamref name="T"/> components this <see cref="World"/> can create, or -1 if it is currently not handled.</returns>
public int GetComponentMaxCapacity<T>() => ComponentManager<T>.Get(WorldId)?.MaxCapacity ?? -1;
public int GetMaxCapacity<T>() => ComponentManager<T>.Get(WorldId)?.MaxCapacity ?? -1;

/// <summary>
/// Gets the maximum number of <typeparamref name="T"/> components this <see cref="World"/> can create.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns>The maximum number of <typeparamref name="T"/> components this <see cref="World"/> can create, or -1 if it is currently not handled.</returns>
[Obsolete("Use GetComponentMaxCapacity instead, will be removed next version")]
public int GetMaximumComponentCount<T>() => GetComponentMaxCapacity<T>();
[Obsolete("Use GetMaxCapacity instead, will be removed next version")]
public int GetMaximumComponentCount<T>() => GetMaxCapacity<T>();

/// <summary>
/// Gets all the component of a given type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type of component.</typeparam>
/// <returns>A <see cref="Span{T}"/> pointing directly to the component values to edit them.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Span<T> GetAllComponents<T>() => ComponentManager<T>.GetOrCreate(WorldId).AsSpan();
public Span<T> Get<T>() => ComponentManager<T>.GetOrCreate(WorldId).AsSpan();

/// <summary>
/// Gets all the component of a given type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type of component.</typeparam>
/// <returns>A <see cref="Span{T}"/> pointing directly to the component values to edit them.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Obsolete("Use Get<T> instead, will be removed next version")]
public Span<T> GetAllComponents<T>() => Get<T>();

/// <summary>
/// Gets an <see cref="Components{T}"/> to get a fast access to the component of type <typeparamref name="T"/> of this <see cref="World"/> instance <see cref="Entity"/>.
Expand Down
Loading

0 comments on commit efc6868

Please sign in to comment.