Skip to content

Commit

Permalink
Merge pull request #23 from andreakarasho/each-iter
Browse files Browse the repository at this point in the history
Each iter
  • Loading branch information
andreakarasho authored Jun 5, 2024
2 parents f628972 + 81a3791 commit 156ed7b
Show file tree
Hide file tree
Showing 5 changed files with 689 additions and 176 deletions.
76 changes: 63 additions & 13 deletions samples/MyBattleground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@
using System.Diagnostics;
using TinyEcs;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;


const int ENTITIES_COUNT = (524_288 * 2 * 1);

var tt = typeof(AtLeast<(PlayerTag, Networked)>);
var inter = tt.GetInterfaces();

var f0 = typeof(IFilter).IsAssignableFrom(tt);
var f1 = tt.GetInterfaces().Any(k => typeof(IFilter) == k);

using var ecs = new World();


ecs.Entity<PlayerTag>().Set<Networked>();
ecs.Entity<Likes>();
ecs.Entity<Dogs>();
Expand All @@ -24,7 +18,7 @@


ecs.Entity().Set(new Velocity());
ecs.Entity().Set(new Position() { X = 9 }).Set(new Velocity());
ecs.Entity().Set(new Position() { X = 9 }).Set(new Velocity()).Set(new ManagedData());
ecs.Entity().Set(new Position() { X = 99 }).Set(new Velocity());
ecs.Entity().Set(new Position() { X = 999 }).Set(new Velocity());
ecs.Entity().Set(new Position() { X = 9999 }).Set(new Velocity()).Set<Networked>();
Expand All @@ -34,6 +28,33 @@
//ecs.Entity().Set(new Position() {X = 2}).Set<Likes>();
ecs.Entity().Set(new Position() {X = 3});


var query = ecs.Query<(Position, Velocity)>();

// foreach (var (pos, vel) in query.Iter<Position, Velocity>())
// {
// var count = pos.Length;

// for (var i = 0; i < count; ++i)
// {
// ref var p = ref pos[i];
// ref var v = ref vel[i];

// p.X += 1;
// }
// }

// foreach (var (pos, vel) in query.IterTest<Position, Velocity>())
// {
// var count = pos.Length;

// for (var i = 0; i < count; ++i)
// {
// ref var p = ref pos[i];
// ref var v = ref vel[i];
// }
// }

ecs.Query<
(Position, ManagedData),
Or<(With<Position>, With<Networked>, Without<Velocity>,
Expand Down Expand Up @@ -347,6 +368,9 @@
var start = 0f;
var last = 0f;

var q = ecs.Query<(Position, Velocity, Optional<ManagedData>)>();


while (true)
{
for (int i = 0; i < 3600; ++i)
Expand All @@ -356,11 +380,37 @@
// pos.Y *= vel.Y;
// });

ecs.Query<(Position, Velocity)>()
.Each((ref Position pos, ref Velocity vel) => {
pos.X *= vel.X;
pos.Y *= vel.Y;
});
// ecs.Query<(Position, Velocity)>()
q.Each2((ref Position pos, ref Velocity vel, ref ManagedData t) => {
pos.X *= vel.X;
pos.Y *= vel.Y;
});

// unsafe
// {
// foreach (var (pos, vel) in q.IterTest2<Position, Velocity>())
// {
// pos->X *= vel->X;
// pos->Y *= vel->Y;
// }
// }


// foreach ((var entiies, var pos, var vel) in q.Iter<Position, Velocity>())
// {
// ref var p = ref MemoryMarshal.GetReference(pos);
// ref var v = ref MemoryMarshal.GetReference(vel);
// ref var lastA = ref Unsafe.Add(ref p, pos.Length);
//
// while (Unsafe.IsAddressLessThan(ref p, ref lastA))
// {
// p.X *= v.X;
// p.Y *= v.Y;
//
// p = ref Unsafe.Add(ref p, 1);
// v = ref Unsafe.Add(ref v, 1);
// }
// }

//scheduler.Run();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Archetype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public readonly ref T GetReference<T>(int column) where T : struct
public readonly Span<T> GetSpan<T>(int column) where T : struct
{
if (column < 0 || column >= Components!.Length)
return MemoryMarshal.CreateSpan(ref Unsafe.NullRef<T>(), 1);
return Span<T>.Empty;

ref var array = ref Unsafe.As<Array, T[]>(ref Components![column]);
return array.AsSpan(0, Count);
Expand Down
Loading

0 comments on commit 156ed7b

Please sign in to comment.