summaryrefslogtreecommitdiff
path: root/Djup.Native/src/Types.cs
diff options
context:
space:
mode:
authorJoel Stålnacke <joel@saker.fi>2026-03-23 19:50:18 +0200
committerJoel Stålnacke <joel@saker.fi>2026-03-23 19:50:18 +0200
commit3a1f9fe6800ef5fe7bbf1c0a5976720383ba84fe (patch)
treef1218eb4085a654f6bb70671b41b4a74e646c052 /Djup.Native/src/Types.cs
parent3870982f640260822bf605e693782ce9f5d79cf6 (diff)
Rewrite native code in Odin and use ECSodin
Diffstat (limited to 'Djup.Native/src/Types.cs')
-rw-r--r--Djup.Native/src/Types.cs126
1 files changed, 87 insertions, 39 deletions
diff --git a/Djup.Native/src/Types.cs b/Djup.Native/src/Types.cs
index 3afce83..e9eb824 100644
--- a/Djup.Native/src/Types.cs
+++ b/Djup.Native/src/Types.cs
@@ -4,10 +4,84 @@ using System.Runtime.InteropServices;
namespace Djup.Native;
[StructLayout(LayoutKind.Sequential)]
+public struct EntityId
+{
+ public int Id { get; }
+
+ public static readonly EntityId Invalid = new(-1);
+
+ public EntityId(int id)
+ {
+ Id = id;
+ }
+
+ public bool IsInvalid => Id < 0;
+
+ public override string ToString() => $"#{Id}";
+
+ public override bool Equals(object? other)
+ {
+ if (other is EntityId id)
+ {
+ return this == id;
+ }
+ return false;
+ }
+
+ public override int GetHashCode() => Id.GetHashCode();
+
+ public static bool operator ==(EntityId a, EntityId b)
+ {
+ if (a < 0)
+ return b < 0;
+ if (b < 0)
+ return false;
+ return a.Id == b.Id;
+ }
+ public static bool operator !=(EntityId a, EntityId b) => !(a == b);
+ public static implicit operator int(EntityId entityId) => entityId.Id;
+ public static implicit operator EntityId(int id) => new(id);
+}
+
+public enum Component : int
+{
+ Exists,
+ Ball,
+ PlayerInput
+}
+
+[Flags]
+public enum Components : int
+{
+ Exists = 1 << Component.Exists,
+ Ball = 1 << Component.Ball,
+ PlayerInput = 1 << Component.PlayerInput
+}
+
+[StructLayout(LayoutKind.Sequential)]
+public unsafe struct OdinSlice<T>
+{
+ public void* Data { get; }
+ public nint Len { get; }
+
+ public Span<T> AsSpan() => new Span<T>(Data, Len.ToInt32());
+ public ReadOnlySpan<T> AsReadOnlySpan() => new ReadOnlySpan<T>(Data, Len.ToInt32());
+}
+
+[StructLayout(LayoutKind.Sequential)]
+public unsafe struct OdinString
+{
+ public sbyte* Data { get; }
+ public nint Len { get; }
+
+ public override string ToString() => new string(Data, 0, Len.ToInt32());
+}
+
+[StructLayout(LayoutKind.Sequential)]
public struct Vec2
{
- public float X { get; }
- public float Y { get; }
+ public float X { get; set; }
+ public float Y { get; set; }
public Vec2(float x, float y)
{
@@ -21,59 +95,32 @@ public struct Vec2
public override string ToString() => $"({X}, {Y})";
}
-public enum EntityState : byte
-{
- Inactive = 0,
- Active,
- Removed,
- Dead
-}
-
-public enum PlayerInput : byte
-{
- Up = 1 << 0,
- Down = 1 << 1,
- Left = 1 << 2,
- Right = 1 << 3
-}
-
[StructLayout(LayoutKind.Sequential)]
-public struct Player
+public struct Ball
{
- public EntityState State { get; }
- private byte input;
public Vec2 Position { get; set; }
public Vec2 Velocity { get; set; }
}
[StructLayout(LayoutKind.Sequential)]
-public struct Bullet
+public struct PlayerInput
{
- public EntityState State { get; set; }
- public Vec2 Position { get; set; }
- public Vec2 Velocity { get; set; }
+ public Vec2 Direction { get; set; }
}
[StructLayout(LayoutKind.Sequential)]
-public unsafe struct Snapshot
+public struct Snapshot
{
- private byte active;
+ private readonly byte active;
public uint Tick { get; }
- private int playersLength;
- private Player* players;
- private int bulletsLength;
- private Bullet* bullets;
-
- public ReadOnlySpan<Player> Players =>
- new ReadOnlySpan<Player>(players, playersLength);
-
- public ReadOnlySpan<Bullet> Bullets =>
- new ReadOnlySpan<Bullet>(bullets, bulletsLength);
+ public OdinSlice<Components> Metadata { get; }
+ public OdinSlice<Ball> Balls { get; }
+ public OdinSlice<PlayerInput> PlayerInputs { get; }
+ private IntPtr ctx;
}
public enum LogLevel
{
- Trace,
Debug,
Info,
Warn,
@@ -81,4 +128,5 @@ public enum LogLevel
Fatal
}
-public delegate void LoggerDelegate(LogLevel level, string source, int line, string message);
+public delegate void LoggerDelegate(LogLevel level, OdinString message, OdinString procedure, OdinString file, int line);
+public delegate void AssertDelegate(OdinString prefix, OdinString message, OdinString procedure, OdinString file, int line);