summaryrefslogtreecommitdiff
path: root/client/Root.cs
diff options
context:
space:
mode:
authorJoel Stålnacke <joel@saker.fi>2024-10-11 13:31:44 +0300
committerJoel Stålnacke <joel@saker.fi>2024-10-13 13:34:46 +0300
commit53a1cdf5bee2955995dfbf441f5354d1dcfc1e0c (patch)
treebe8c2894226a2b7e1a47f7583f2041df75f795b3 /client/Root.cs
parent4bac6ae2e725a1997674fd3369bf4ea032235d8b (diff)
Add Godot client
Diffstat (limited to 'client/Root.cs')
-rw-r--r--client/Root.cs74
1 files changed, 74 insertions, 0 deletions
diff --git a/client/Root.cs b/client/Root.cs
new file mode 100644
index 0000000..e39a485
--- /dev/null
+++ b/client/Root.cs
@@ -0,0 +1,74 @@
+using Godot;
+using System;
+using Djup.Native;
+
+public partial class Root : Node2D
+{
+ private IntPtr native_world;
+ private int[] balls = new int[100];
+ private Random rng = new();
+ private const double Tps = 1d/60d;
+
+ // Called when the node enters the scene tree for the first time.
+ public unsafe override void _Ready()
+ {
+ RenderingServer.SetDefaultClearColor(Colors.LightGray);
+
+ native_world = LibDjup.world_create();
+ for (int i = 0; i < balls.Length; i++)
+ {
+ int id = LibDjup.world_create_entity(native_world, EntityKind.Ball);
+ if (id < 0)
+ {
+ throw new Exception("Failed to create entity");
+ }
+ balls[i] = id;
+ Entity *ent;
+ var pos = new Vec2(5f * rng.Next(10), 5f * rng.Next(20));
+ var vel = new Vec2(50f + 5f * rng.Next(10), 50f);
+ ent = LibDjup.world_find_entity(native_world, balls[i]);
+ ent->ball.Position = pos;
+ ent->ball.Velocity = vel;
+ }
+ }
+
+ public override void _ExitTree()
+ {
+ LibDjup.world_free(native_world);
+ }
+
+ private void DrawBall(Ball ball)
+ {
+ //Color color = rng.Next(5) switch
+ //{
+ //0 => Colors.Black,
+ //1 => Colors.Blue,
+ //2 => Colors.Red,
+ //3 => Colors.Green,
+ //4 => Colors.Pink
+ //};
+ Color color = Colors.Blue;
+ this.DrawCircle(new Vector2(ball.Position.X, ball.Position.Y), 5f, color, antialiased: true);
+ }
+
+ public unsafe override void _Draw()
+ {
+ for (int i = 0; i < balls.Length; i++)
+ {
+ DrawBall(LibDjup.world_find_entity(native_world, balls[i])->ball);
+ }
+ }
+
+ public override void _Process(double delta)
+ {
+ this.QueueRedraw();
+ }
+
+ public override void _PhysicsProcess(double delta)
+ {
+ if (LibDjup.world_tick(native_world, Tps) != 0)
+ {
+ GD.Print("world_tick failed");
+ }
+ }
+}