Skip to content

Commit

Permalink
query
Browse files Browse the repository at this point in the history
  • Loading branch information
andreakarasho committed Mar 11, 2024
1 parent 7cd7bd9 commit a83c526
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 171 deletions.
40 changes: 20 additions & 20 deletions plugins/TinyEcs.Plugins/Systems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ namespace TinyEcs;
public abstract class EcsSystem
{
#nullable disable
[NotNull] public World Ecs { get; internal set; }
[NotNull] internal World Ecs { get; set; }
[NotNull] public string Name { get; internal set; }
#nullable restore

public bool IsEnabled { get; private set; } = true;
internal bool IsDestroyed { get; set; }


public virtual void OnCreate() { }
public virtual void OnCreate(World ecs) { }

public virtual void OnStart() { }
public virtual void OnStart(World ecs) { }

public virtual void OnStop() { }
public virtual void OnStop(World ecs) { }

public virtual void OnBeforeUpdate() { }
public virtual void OnBeforeUpdate(World ecs) { }

public virtual void OnUpdate() { }
public virtual void OnUpdate(World ecs) { }

public virtual void OnAfterUpdate() { }
public virtual void OnAfterUpdate(World ecs) { }

public virtual void OnCleanup() { }
public virtual void OnCleanup(World ecs) { }

public virtual void OnDestroy() { }
public virtual void OnDestroy(World ecs) { }


public void Enable() => Enable(true);
Expand All @@ -43,9 +43,9 @@ private void Enable(bool enable)
IsEnabled = enable;

if (enable)
OnStart();
OnStart(Ecs);
else
OnStop();
OnStop(Ecs);
}
}

Expand Down Expand Up @@ -107,10 +107,10 @@ public void Update()
sys.Disable();

foreach (var sys in _toDelete)
sys.OnCleanup();
sys.OnCleanup(_ecs);

foreach (var sys in _toDelete)
sys.OnDestroy();
sys.OnDestroy(_ecs);

foreach (var sys in _toDelete)
_systems.Remove(sys);
Expand All @@ -122,9 +122,9 @@ public void Update()
{
foreach (var sys in _toCreate)
{
sys.OnCreate();
sys.OnCreate(_ecs);
if (sys.IsEnabled)
sys.OnStart();
sys.OnStart(_ecs);
}

_toCreate.Clear();
Expand All @@ -134,17 +134,17 @@ public void Update()
{
if (!sys.IsEnabled) continue;

sys.OnBeforeUpdate();
sys.OnUpdate();
sys.OnAfterUpdate();
sys.OnBeforeUpdate(_ecs);
sys.OnUpdate(_ecs);
sys.OnAfterUpdate(_ecs);
}
}

public void Clear()
{
_systems.ForEach(s => s.Disable());
_systems.ForEach(s => s.OnCleanup());
_systems.ForEach(s => s.OnDestroy());
_systems.ForEach(s => s.OnCleanup(_ecs));
_systems.ForEach(s => s.OnDestroy(_ecs));
_systems.Clear();
_toDelete.Clear();
_hashes.Clear();
Expand Down
2 changes: 1 addition & 1 deletion plugins/TinyEcs.Plugins/UniqueEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static EntityView Entity(this World world, string name)
return world.Entity();

var found = EntityView.Invalid;
world.Query((EntityView entity, ref Identity identity) => {
world.Each((EntityView entity, ref Identity identity) => {
if (identity.Value?.Equals(name) ?? false)
found = entity;
});
Expand Down
94 changes: 36 additions & 58 deletions samples/MyBattleground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,22 @@

Console.WriteLine("system name : {0}", ff.Name);

ecs.Entity<PlayerTag>();
ecs.Entity<Likes>();

var e = ecs.Entity("Main")
.Set<Position>(new Position() {X = 2})
.Set<Velocity>(new Velocity());



var query = ecs.Query<(Position, Velocity), (Not<PlayerTag>, Not<Likes>)>();
query.Each((ref Position pos, ref Velocity vel) => {

});



systems.Update();

var rabbit = ecs.Entity();
Expand All @@ -31,14 +43,14 @@
e.Enable();
enabled = e.IsEnabled();

ecs.Filter<(Position, Velocity, Not<Disabled>)>()
.Query((EntityView entity) => {
Console.WriteLine(entity.Name());
});
// ecs.Filter<(Position, Velocity, Not<Disabled>)>()
// .Query((EntityView entity) => {
// Console.WriteLine(entity.Name());
// });

ecs.Query((EntityView entity, ref ComponentInfo cmp) => {
Console.WriteLine("cmp {0} size {1}", cmp.ID, cmp.Size);
});
// ecs.Query((EntityView entity, ref ComponentInfo cmp) => {
// Console.WriteLine("cmp {0} size {1}", cmp.ID, cmp.Size);
// });

// var e2 = ecs.Entity("Main");
// ref var pp = ref e2.Get<Position>();
Expand All @@ -58,46 +70,6 @@

//child2.Delete();

foreach (var childId in e.Children<Hierarchy>())
{

}

foreach (var childId in e.Children<Chunk>())
{

}

ecs.Filter<(With<Child<Hierarchy>>, With<Parent<Hierarchy>>)>().Query((EntityView entity, ref Relationship<Hierarchy> relation) => {
Console.WriteLine("im [{0}] a child of {1}, but also having {2} children", entity.Name(), ecs.Entity(relation.Parent).Name(), relation.Count);
});

Console.WriteLine();

ecs.Filter<With<Parent<Hierarchy>>>().Query((EntityView entity, ref Relationship<Hierarchy> relation) => {
Console.WriteLine("parent {0} has {1} children", entity.Name(), relation.Count);

foreach (var id in entity.Children<Hierarchy>())
{
Console.WriteLine("\tChild: {0}", ecs.Entity(id).Name());
}
});

Console.WriteLine();

e.RemoveChild<Hierarchy>(child);
ecs.Filter<With<Parent<Hierarchy>>>().Query((EntityView entity, ref Relationship<Hierarchy> relation) => {
Console.WriteLine("parent {0} has {1} children", entity.Name(), relation.Count);
});

Console.WriteLine();

child2.RemoveChild<Hierarchy>(child3);
ecs.Filter<With<Parent<Hierarchy>>>().Query((EntityView entity, ref Relationship<Hierarchy> relation) => {
Console.WriteLine("parent {0} has {1} children", entity.Name(), relation.Count);
});

Console.WriteLine();

// e.ClearChildren();
// ecs.Filter<With<Parent>>().Query((EntityView entity, ref Relationship relation) => {
Expand Down Expand Up @@ -145,13 +117,16 @@
//var cur = (start - last) / 1000f;
for (int i = 0; i < 3600; ++i)
{
ecs
//.Filter<With<PlayerTag>>()
.Query((ref Position pos, ref Velocity vel) =>
{
pos.X *= vel.X;
pos.Y *= vel.Y;
});
// ecs.Query<(Position, Velocity)>()
// .Each((ref Position pos, ref Velocity vel) => {
// pos.X *= vel.X;
// pos.Y *= vel.Y;
// });

ecs.Each((ref Position pos, ref Velocity vel) => {
pos.X *= vel.X;
pos.Y *= vel.Y;
});
}

last = start;
Expand Down Expand Up @@ -194,19 +169,22 @@ struct ChunkTile;

sealed class MoveSystem : EcsSystem
{
public override void OnCreate()
private Query? _query;

public override void OnCreate(World ecs)
{
_query = ecs.Query<(Position, Velocity)>();
Console.WriteLine("system {0} created", Name);
}

public override void OnStart()
public override void OnStart(World ecs)
{
Console.WriteLine("system {0} started", Name);
}

public override void OnUpdate()
public override void OnUpdate(World ecs)
{
Ecs.Query((ref Position pos, ref Velocity vel) => {
_query!.Each((ref Position pos, ref Velocity vel) => {
Console.WriteLine("aa");
});
}
Expand Down
24 changes: 12 additions & 12 deletions samples/TinyEcsGame/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ struct Rotation

sealed class MoveSystem : TinyEcs.EcsSystem
{
public override void OnUpdate()
public override void OnUpdate(World ecs)
{
var deltaTime = Raylib.GetFrameTime();

Ecs.Query((ref Position pos, ref Velocity vel, ref Rotation rot) =>
ecs.Each((ref Position pos, ref Velocity vel, ref Rotation rot) =>
{
pos.Value += vel.Value * deltaTime;
rot.Value += rot.Acceleration * deltaTime * Raylib.RAD2DEG;
Expand All @@ -82,9 +82,9 @@ sealed class CheckBorderSystem : TinyEcs.EcsSystem
{
public Vector2 WindowSize { get; set; }

public override void OnUpdate()
public override void OnUpdate(World ecs)
{
Ecs.Query((ref Position pos, ref Velocity vel) =>
ecs.Each((ref Position pos, ref Velocity vel) =>
{
if (pos.Value.X < 0.0f)
{
Expand Down Expand Up @@ -113,7 +113,7 @@ public override void OnUpdate()

sealed class BeginRenderSystem : TinyEcs.EcsSystem
{
public override void OnUpdate()
public override void OnUpdate(World ecs)
{
Raylib.BeginDrawing();
Raylib.ClearBackground(Color.Black);
Expand All @@ -122,17 +122,17 @@ public override void OnUpdate()

sealed class EndRenderSystem : TinyEcs.EcsSystem
{
public override void OnUpdate()
public override void OnUpdate(World ecs)
{
Raylib.EndDrawing();
}
}

sealed class RenderEntities : TinyEcs.EcsSystem
{
public override void OnUpdate()
public override void OnUpdate(World ecs)
{
Ecs.Query((ref Sprite sprite, ref Position pos, ref Rotation rotation) =>
ecs.Each((ref Sprite sprite, ref Position pos, ref Rotation rotation) =>
{
Raylib.DrawTextureEx(sprite.Texture, pos.Value, rotation.Value, sprite.Scale, sprite.Color);
});
Expand All @@ -141,15 +141,15 @@ public override void OnUpdate()

sealed class RenderText : TinyEcs.EcsSystem
{
public override void OnUpdate()
public override void OnUpdate(World ecs)
{
var deltaTime = Raylib.GetFrameTime();

var dbgText =
$"""
[Debug]
FPS: {Raylib.GetFPS()}
Entities: {Ecs.EntityCount}
Entities: {ecs.EntityCount}
DeltaTime: {deltaTime}
""".Replace("\r", "\n");
var textSize = 24;
Expand All @@ -163,7 +163,7 @@ sealed class SpawnEntities : TinyEcs.EcsSystem
public Vector2 WindowSize { get; set; }
public int Velocity { get; set; }

public override void OnCreate()
public override void OnCreate(World ecs)
{
// This system is just one shot
Disable();
Expand All @@ -173,7 +173,7 @@ public override void OnCreate()

for (var i = 0; i < EntitiesToSpawn; ++i)
{
Ecs!
ecs!
.Entity()
.Set(
new Position()
Expand Down
3 changes: 3 additions & 0 deletions src/Archetype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,14 @@ ComponentComparer comparer
_lookup.GetOrAddValueRef(components[i].ID, out _) = i;

_chunks = new ArchetypeChunk[ARCHETYPE_INITIAL_CAPACITY];

Id = Hashing.Calculate(components);
}

public World World => _world;
public int Count => _count;
public readonly ImmutableArray<ComponentInfo> Components;
public ulong Id { get; }
internal Span<ArchetypeChunk> Chunks => _chunks.AsSpan(0, (_count + CHUNK_THRESHOLD - 1) / CHUNK_THRESHOLD);
internal int EmptyChunks => _chunks.Length - Chunks.Length;

Expand Down
Loading

0 comments on commit a83c526

Please sign in to comment.