summaryrefslogtreecommitdiff
path: root/client/World.cs
diff options
context:
space:
mode:
authorJoel Stålnacke <joel@saker.fi>2025-07-26 21:09:26 +0300
committerJoel Stålnacke <joel@saker.fi>2025-07-26 21:09:26 +0300
commit63506e59366acddf4a9e017ad8aebeadcf58c164 (patch)
tree2fa106661b85497fc1d63b7743a78e523ab48fba /client/World.cs
parent53f68bb7b0dce309723675c4b97f726a469031c0 (diff)
Old changesHEADmaster
Diffstat (limited to 'client/World.cs')
-rw-r--r--client/World.cs75
1 files changed, 75 insertions, 0 deletions
diff --git a/client/World.cs b/client/World.cs
new file mode 100644
index 0000000..8f5dfb6
--- /dev/null
+++ b/client/World.cs
@@ -0,0 +1,75 @@
+using Godot;
+using System;
+using Djup.Native;
+
+public partial class World : Node2D
+{
+ private IntPtr context;
+ private IntPtr currentSnapshot;
+ public const double Tps = 1d/60d;
+
+ public unsafe override void _Ready()
+ {
+ context = LibDjup.physics_context_create(2);
+ currentSnapshot = LibDjup.physics_context_get_snapshot(context);
+ }
+
+ public override void _ExitTree()
+ {
+ LibDjup.physics_context_free(context);
+ }
+
+ public unsafe void PutPlayer(uint id, Djup.Native.Player player)
+ {
+ if (LibDjup.snapshot_put_player(currentSnapshot, id, &player) != 0)
+ {
+ throw new Exception($"Failed to put player {id}");
+ }
+ }
+
+ public void SetPlayerInput(uint playerId, byte input)
+ {
+ int ret = LibDjup.snapshot_set_player_input(currentSnapshot, playerId, input);
+ if (ret != 0)
+ {
+ throw new Exception($"Failed to set input of player {playerId}: {ret}");
+ }
+ }
+
+ private unsafe void DrawPlayer(Djup.Native.Player player)
+ {
+ float radius = 50f;
+ this.DrawCircle(new Vector2(player.Position.X, player.Position.Y), radius, Colors.DarkBlue, antialiased: true);
+ this.DrawCircle(new Vector2(player.Position.X, player.Position.Y), radius * 0.90f, Colors.Blue, antialiased: true);
+ }
+
+ public unsafe override void _Draw()
+ {
+ Snapshot *s = (Snapshot *)currentSnapshot;
+
+ for (int i = 0; i < Constants.WorldMaxPlayers; i++)
+ {
+ var p = s->Players[i];
+ if (p.State == EntityState.Active)
+ {
+ DrawPlayer(p);
+ }
+ }
+ }
+
+ public override void _Process(double delta)
+ {
+ this.QueueRedraw();
+ }
+
+ public override void _PhysicsProcess(double delta)
+ {
+ IntPtr next = LibDjup.physics_context_get_snapshot(context);
+ if (LibDjup.physics_tick(currentSnapshot, Tps, next) != 0)
+ {
+ throw new Exception("World tick failed");
+ }
+ LibDjup.physics_context_return_snapshot(context, currentSnapshot);
+ currentSnapshot = next;
+ }
+}