Skip to content

Commit

Permalink
2nd entity identity refactor (#1)
Browse files Browse the repository at this point in the history
* Title page iteration, more coming

* Title page iteration, more coming

* Title page iteration, more coming

* Entitybuilder tests

* Static Match class to clarify which entity types are Match expressions.

* TypeExpression comments and slight reformatting

* Refactored Wildcard tests into match tests.

* Spelling unification and terminology iteration

* Spelling unification and terminology iteration

* Large refactor changing how Entity and Identity relate.

* Renamed Meta.

* Refactor cleanup

* Convenience method added.

* More code cleanup

* Entity operators and IComparable

* Entity operators and IComparable

* Entity comparable and Iterators fixed.

* Entity comparable and Iterators fixed.

* Entity comparable coverage

* Entity tests.

* Entity tests.
  • Loading branch information
thygrrr authored Feb 24, 2024
1 parent 3273bc8 commit 8511977
Show file tree
Hide file tree
Showing 46 changed files with 1,455 additions and 1,212 deletions.
25 changes: 14 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
![fennecs logo](./docs/logos/fennecs-logo-darkmode.svg#gh-dark-mode-only) ![fennecs logo](./docs/logos/fennecs-logo-lightmode.svg#gh-light-mode-only)

<table style="width: 90%">
<th colspan="10">
<h3>... the tiny, tiny, high-energy Entity Component System!</h3>
<th colspan="9">
<h2><em>... the tiny, tiny, high-energy Entity Component System!</em></h2>
</th>
<tr>
<td colspan="3" style="width: fit-content">
<td colspan="4" style="width: fit-content">
<img src="docs/logos/fennecs.png" alt="a box of fennecs, 8-color pixel art" style="min-width: 320px"/>
</td>
<td colspan="7">
<h1>What the fox!? Another ECS?</h1>
<td colspan="5">
<h1>What the fox, another ECS?!</h1>
<p>We know... oh, <em>we know.</em> 😩</p>
<p>But in a nutshell, <a href="https://fennecs.tech"><span style="font-size: larger"><em><b>fenn</b>ecs</em></span></a> is...</p>
<p>
Expand All @@ -22,16 +22,19 @@
<p><span style="font-size: larger"><em><b>fenn</b>ecs</em></span> is a re-imagining of <a href="https://github.com/Byteron/HypEcs">RelEcs/HypEcs</a>
which <em>feels just right<a href="#quickstart-lets-go">*</a></em> for high performance game development in any modern C# engine. Including, of course, the fantastic <a href="https://godotengine.org">Godot</a>.
</p>
<p></p>
</td>
</tr>
<th colspan="9">
<a href="https://www.nuget.org/packages/fennecs/"><img alt="Nuget" src="https://img.shields.io/nuget/v/fennecs?color=blue"/></a>
<a href="https://github.com/thygrrr/fennECS/actions"><img alt="GitHub Actions Workflow Status" src="https://img.shields.io/github/actions/workflow/status/thygrrr/fennECS/xUnit.yml"/></a>
<a href="https://github.com/thygrrr/fennECS/issues"><img alt="Open issues" src="https://img.shields.io/github/issues-raw/thygrrr/fennECS?color=green"/></a>
<img alt="GitHub top language" src="https://img.shields.io/github/languages/top/thygrrr/fennECS"/>
<a href="https://github.com/thygrrr/fennECS?tab=MIT-1-ov-file#readme"><img alt="License: MIT" src="https://img.shields.io/github/license/thygrrr/fennECS?color=blue"/></a>
</th>
<tr>
<td colspan="3"><h3>Code Samples</h3></td>
<td colspan="7">
<a href="https://www.nuget.org/packages/fennecs/"><img alt="Nuget" src="https://img.shields.io/nuget/v/fennecs?color=blue"/></a>
<a href="https://github.com/thygrrr/fennECS/actions"><img alt="GitHub Actions Workflow Status" src="https://img.shields.io/github/actions/workflow/status/thygrrr/fennECS/xUnit.yml"/></a>
<a href="https://github.com/thygrrr/fennECS/issues"><img alt="Open issues" src="https://img.shields.io/github/issues-raw/thygrrr/fennECS?color=green"/></a>
<img alt="GitHub top language" src="https://img.shields.io/github/languages/top/thygrrr/fennECS"/>
<a href="https://github.com/thygrrr/fennECS?tab=MIT-1-ov-file#readme"><img alt="License: MIT" src="https://img.shields.io/github/license/thygrrr/fennECS?color=blue"/></a>
</td>
</tr>
<tr>
Expand Down Expand Up @@ -60,7 +63,7 @@ using Position = System.Numerics.Vector3;
var world = new fennecs.World();

// Spawn an entity into the world with a choice of components. (or add/remove them later)
var entity = world.Spawn().Add<Position>().Id();
var entity = world.Spawn().Add<Position>();

// Queries are cached, just build them right where you want to use them.
var query = world.Query<Position>().Build();
Expand Down
5 changes: 2 additions & 3 deletions examples/example-godot/BasicCubes/MultiMeshExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ private void SpawnWave(int spawnCount)
{
_world.Spawn()
.Add(i + MeshInstance.Multimesh.InstanceCount)
.Add<Matrix4X3>()
.Id();
.Add<Matrix4X3>();
}

MeshInstance.Multimesh.InstanceCount += spawnCount;
Expand Down Expand Up @@ -70,7 +69,7 @@ public override void _Process(double delta)
floatSpan.CopyTo(uniform.submission);
RenderingServer.MultimeshSetBuffer(uniform.mesh, uniform.submission);

// Ideal way - raw query to pass Memory<T>, Godot Memory<TY overload not yet available.
// Ideal way - raw Query to pass Memory<T>, Godot Memory<TY overload not yet available.
//_query.Raw((_, transforms) => RenderingServer.MultimeshSetBuffer(MeshInstance.Multimesh.GetRid(), transforms));

// This variant is also fast, but it doesn't work with the Godot API as that expects an array.
Expand Down
2 changes: 1 addition & 1 deletion examples/example-godot/SpaceBattle/EntityNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace examples.godot.SpaceBattle;

public interface IEntityNode
{
public Entity entity { get; set; }
public Identity identity { get; set; }

}
4 changes: 2 additions & 2 deletions examples/example-godot/SpaceBattle/Fighter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ namespace examples.godot.SpaceBattle;
public partial class Fighter : Node3D, IEntityNode
{
private Color _color = new(1, 1, 1);
public Entity entity { get; set; }
public Identity identity { get; set; }

public override void _Ready()
{
_color = Color.FromOkHsl(Random.Shared.NextSingle(), 0.7f, 0.5f, 1);
GetNode<MeshInstance3D>("MeshInstance3D").SetInstanceShaderParameter("Albedo", _color);

// TODO: find out if can be set on Instantiate.
Console.WriteLine($"Fighter._Ready(): {entity}");
Console.WriteLine($"Fighter._Ready(): {identity}");
}

}
10 changes: 5 additions & 5 deletions fennecs.benchmarks/ECS/ChunkingBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@ public void Setup()
{
_vectorsRaw[i] = new Vector3(random.NextSingle(), random.NextSingle(), random.NextSingle());

//Multiple unused components added to create fennecs archetype fragmentation, which is used as basis for many parallel processing partitions.
//Multiple unused Components added to create fennecs Archetype fragmentation, which is used as basis for many parallel processing partitions.
switch (i % 4)
{
case 0:
_world.Spawn().Add(_vectorsRaw[i]).Id();
_world.Spawn().Add(_vectorsRaw[i]);
break;
case 1:
_world.Spawn().Add(_vectorsRaw[i]).Add<int>().Id();
_world.Spawn().Add(_vectorsRaw[i]).Add<int>();
break;
case 2:
_world.Spawn().Add(_vectorsRaw[i]).Add<double>().Id();
_world.Spawn().Add(_vectorsRaw[i]).Add<double>();
break;
case 3:
_world.Spawn().Add(_vectorsRaw[i]).Add<float>().Id();
_world.Spawn().Add(_vectorsRaw[i]).Add<float>();
break;
}
}
Expand Down
10 changes: 5 additions & 5 deletions fennecs.benchmarks/ECS/SimpleEntityBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ public void Setup()
{
_vectorsRaw[i] = new Vector3(random.NextSingle(), random.NextSingle(), random.NextSingle());

//Multiple unused components added to create fennecs archetype fragmentation, which is used as basis for many parallel processing partitions.
//Multiple unused Components added to create fennecs Archetype fragmentation, which is used as basis for many parallel processing partitions.
switch (i % 4)
{
case 0:
_world.Spawn().Add(_vectorsRaw[i]).Id();
_world.Spawn().Add(_vectorsRaw[i]);
break;
case 1:
_world.Spawn().Add(_vectorsRaw[i]).Add<int>().Id();
_world.Spawn().Add(_vectorsRaw[i]).Add<int>();
break;
case 2:
_world.Spawn().Add(_vectorsRaw[i]).Add<double>().Id();
_world.Spawn().Add(_vectorsRaw[i]).Add<double>();
break;
case 3:
_world.Spawn().Add(_vectorsRaw[i]).Add<float>().Id();
_world.Spawn().Add(_vectorsRaw[i]).Add<float>();
break;
}
}
Expand Down
41 changes: 30 additions & 11 deletions fennecs.tests/ArchetypeTests.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
namespace fennecs.tests;
using System.Collections;

namespace fennecs.tests;

public class ArchetypeTests(ITestOutputHelper output)
{
[Fact]
public void Table_String_Contains_Types()
{
var world = new World();
var identity = world.Spawn().Add("foo").Add(123).Add(17.0f).Id();
var identity = world.Spawn().Add("foo").Add(123).Add(17.0f);

var table = world.GetEntityMeta(identity).Archetype;

output.WriteLine(table.ToString());
Assert.Contains(typeof(Entity).ToString(), table.ToString());
Assert.Contains(typeof(Identity).ToString(), table.ToString());
Assert.Contains(typeof(string).ToString(), table.ToString());
Assert.Contains(typeof(int).ToString(), table.ToString());
Assert.Contains(typeof(float).ToString(), table.ToString());
Expand All @@ -21,7 +23,7 @@ public void Table_String_Contains_Types()
public void Table_Resizing_Fails_On_Wrong_Size()
{
var world = new World();
var identity = world.Spawn().Add("foo").Add(123).Add(17.0f).Id();
var identity = world.Spawn().Add("foo").Add(123).Add(17.0f);

var table = world.GetEntityMeta(identity).Archetype;

Expand All @@ -33,7 +35,7 @@ public void Table_Resizing_Fails_On_Wrong_Size()
public void Table_Resizing_Matches_Length()
{
var world = new World();
var identity = world.Spawn().Add("foo").Add(123).Add(17.0f).Id();
var identity = world.Spawn().Add("foo").Add(123).Add(17.0f);

var table = world.GetEntityMeta(identity).Archetype;

Expand All @@ -49,26 +51,43 @@ public void Table_Resizing_Matches_Length()
public void Table_GetStorage_Returns_System_Array()
{
var world = new World();
var identity = world.Spawn().Add("foo").Add(123).Add(17.0f).Id();
var identity = world.Spawn().Add("foo").Add(123).Add(17.0f);
var table = world.GetEntityMeta(identity).Archetype;
var storage = table.GetStorage(TypeExpression.Create<string>(Entity.None));
var storage = table.GetStorage(TypeExpression.Create<string>(Match.Plain));
Assert.IsAssignableFrom<Array>(storage);
}

[Fact]
public void Table_Matches_TypeExpression()
{
var world = new World();
var identity = world.Spawn().Add("foo").Add(123).Add(17.0f).Id();
var identity = world.Spawn().Add("foo").Add(123).Add(17.0f).Id;
var table = world.GetEntityMeta(identity).Archetype;

var typeExpression = TypeExpression.Create<string>(Entity.None);
var typeExpression = TypeExpression.Create<string>(Match.Plain);
Assert.True(table.Matches(typeExpression));

var typeExpressionAny = TypeExpression.Create<string>(Entity.Any);
var typeExpressionAny = TypeExpression.Create<string>(Match.Any);
Assert.True(table.Matches(typeExpressionAny));

var typeExpressionTarget = TypeExpression.Create<string>(new Entity(99999));
var typeExpressionTarget = TypeExpression.Create<string>(new Identity(99999));
Assert.False(table.Matches(typeExpressionTarget));
}

[Fact]
public void Table_Can_be_Generically_Enumerated()
{
var world = new World();
var other = world.Spawn().Add("foo").Add(123).Add(17.0f).Id;
var table = world.GetEntityMeta(other).Archetype;

var count = 0;
foreach (var entity in (IEnumerable)table)
{
count++;
Assert.Equal(entity, entity);
}

Assert.Equal(1, count);
}
}
22 changes: 0 additions & 22 deletions fennecs.tests/EntityBuilderTests.cs

This file was deleted.

Loading

0 comments on commit 8511977

Please sign in to comment.