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
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;
}
}
|