summaryrefslogtreecommitdiff
path: root/client/Root.cs
blob: e39a48520a96e04bd1bda56c8d23b9196f4daf0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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");
		}
	}
}