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); LibDjup.log_set_output((level, source, line, msg) => { string level_name = level switch { LogLevel.Trace => "trace", LogLevel.Debug => "debug", LogLevel.Info => "info", LogLevel.Warn => "warn", LogLevel.Error => "error", LogLevel.Fatal => "fatal" }; GD.Print($"libdjup {source}:{line} {level_name.PadLeft(5, ' ')}: {msg}"); }); 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"); } } }