Skip to content
This repository was archived by the owner on Sep 15, 2024. It is now read-only.

Commit 93101a3

Browse files
Remove all server-side player position sync code
1 parent c192f4a commit 93101a3

6 files changed

Lines changed: 16 additions & 154 deletions

File tree

Scripts/Netcode/Common/CPacketPlayerDirectionPressed.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.

Scripts/Netcode/Common/SPacketLobbyGameStart.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ public override void Handle()
1212

1313
if (GameClient.IsHost)
1414
{
15-
GameServer.TimerGameLoop.Enabled = true;
16-
GameServer.TimerNotifyClients.Enabled = true;
15+
GameServer.EmitClientPositions.Enabled = true;
1716
}
1817

1918
SceneManager.ChangeScene("Game");

Scripts/Netcode/Common/_Opcodes.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ public enum ClientPacketOpcode
88
LobbyChatMessage,
99
LobbyReady,
1010
LobbyCountdownChange,
11-
LobbyGameStart,
12-
PlayerDirectionPressed
11+
LobbyGameStart
1312
}
1413

1514
// Sent to Game Client

Scripts/Netcode/Server/GameServer.cs

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,58 +12,25 @@ namespace GodotModules.Netcode.Server
1212
public class GameServer : ENetServer
1313
{
1414
public static Dictionary<uint, DataPlayer> Players { get; set; }
15-
public static Dictionary<uint, Vector2> LastSentPlayerPositions { get; set; }
16-
public static Timer TimerGameLoop { get; set; }
17-
public static Timer TimerNotifyClients { get; set; }
15+
public static Timer EmitClientPositions { get; set; }
1816
public static float Delta { get; set; }
1917

2018
public GameServer()
2119
{
2220
Players = new Dictionary<uint, DataPlayer>();
23-
LastSentPlayerPositions = new Dictionary<uint, Vector2>();
24-
TimerGameLoop = new Timer(16.67);
25-
TimerGameLoop.Elapsed += TimerGameLoopCallback;
26-
TimerGameLoop.AutoReset = true;
27-
28-
TimerNotifyClients = new Timer(200);
29-
TimerNotifyClients.Elapsed += TimerNotifyClientsCallback;
30-
TimerNotifyClients.AutoReset = true;
21+
22+
EmitClientPositions = new Timer(200);
23+
EmitClientPositions.Elapsed += EmitClientPositionsCallback;
24+
EmitClientPositions.AutoReset = true;
3125
}
3226

33-
public void TimerNotifyClientsCallback(System.Object source, ElapsedEventArgs args)
27+
public void EmitClientPositionsCallback(System.Object source, ElapsedEventArgs args)
3428
{
3529
var playerPositions = Players.ToDictionary(x => x.Key, x => x.Value.Position);
3630

3731
SendToAllPlayers(ServerPacketOpcode.PlayerPositions, new SPacketPlayerPositions {
3832
PlayerPositions = playerPositions
3933
}, PacketFlags.Reliable);
40-
41-
LastSentPlayerPositions = playerPositions;
42-
43-
//System.Console.WriteLine("SERVER: " + playerPositions[0]);
44-
}
45-
46-
public void TimerGameLoopCallback(System.Object source, ElapsedEventArgs args)
47-
{
48-
foreach (var pair in Players)
49-
{
50-
var player = pair.Value;
51-
52-
var dir = new Vector2();
53-
54-
if (player.DirectionHorizontal == Direction.West)
55-
dir.x -= 1;
56-
if (player.DirectionHorizontal == Direction.East)
57-
dir.x += 1;
58-
if (player.DirectionVertical == Direction.North)
59-
dir.y -= 1;
60-
if (player.DirectionVertical == Direction.South)
61-
dir.y += 1;
62-
63-
var delta = 0.016667f;
64-
65-
player.Position += dir * 250 * delta;
66-
}
6734
}
6835

6936
protected override void Connect(Event netEvent)
@@ -136,18 +103,5 @@ public static void SendToOtherPlayers(uint id, ServerPacketOpcode opcode, APacke
136103
else
137104
Send(opcode, data, flags, otherPlayers);
138105
}
139-
140-
public static void UpdatePressed(uint id, Direction directionHorizontal, Direction directionVertical, bool pressed)
141-
{
142-
var player = Players[id];
143-
player.DirectionHorizontal = directionHorizontal;
144-
player.DirectionVertical = directionVertical;
145-
}
146-
147-
public static void CheckPosition(uint id, Vector2 position)
148-
{
149-
var player = Players[id];
150-
Log("Distance: " + position.DistanceSquaredTo(player.Position));
151-
}
152106
}
153107
}

Scripts/Scenes/Game/ClientPlayer.cs

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ public class ClientPlayer : OtherPlayer
1616
private Label LabelPosition { get; set; }
1717

1818
private float Speed = 250f;
19-
private Timer NotifyServerPlayerDirection { get; set; }
20-
private float Delta { get; set; }
21-
private Direction DirectionHorizontal { get; set; }
22-
private Direction DirectionVertical { get; set; }
19+
private Timer EmitPosition { get; set; }
2320

2421
public override void _Ready()
2522
{
@@ -29,28 +26,20 @@ public override void _Ready()
2926

3027
if (GameClient.Running)
3128
{
32-
NotifyServerPlayerDirection = new Timer(50);
33-
NotifyServerPlayerDirection.Elapsed += NotifyServerPlayerDirectionCallback;
34-
NotifyServerPlayerDirection.AutoReset = true;
35-
NotifyServerPlayerDirection.Enabled = true;
29+
EmitPosition = new Timer(200);
30+
EmitPosition.Elapsed += EmitPositionCallback;
31+
EmitPosition.AutoReset = true;
32+
EmitPosition.Enabled = true;
3633
}
3734
}
3835

39-
public async void NotifyServerPlayerDirectionCallback(System.Object source, ElapsedEventArgs args)
36+
public async void EmitPositionCallback(System.Object source, ElapsedEventArgs args)
4037
{
41-
//if (DirectionHorizontal == Direction.None && DirectionVertical == Direction.None)
42-
//return;
43-
44-
await GameClient.Send(ClientPacketOpcode.PlayerDirectionPressed, new CPacketPlayerDirectionPressed
45-
{
46-
DirectionHorizontal = DirectionHorizontal,
47-
DirectionVertical = DirectionVertical
48-
});
38+
4939
}
5040

5141
public override void _PhysicsProcess(float delta)
5242
{
53-
Delta = delta;
5443
LabelPosition.Text = $"X: {Mathf.RoundToInt(Position.x)} Y: {Mathf.RoundToInt(Position.y)}";
5544
HandleMovement(delta);
5645
}
@@ -69,41 +58,6 @@ private void HandleMovement(float delta)
6958
dir.y += 1;
7059

7160
Position += dir * Speed * delta;
72-
73-
if (DirectionHorizontal == Direction.None && DirectionVertical == Direction.None)
74-
Position = Utils.Lerp(Position, SceneGame.ServerPlayerPositions[GameClient.PeerId], 0.01f);
75-
76-
77-
//else
78-
//Position = Utils.Lerp(Position, SceneGame.ServerPlayerPosition, 0.05f);
79-
80-
UpdateDirectionPressed();
81-
}
82-
83-
private void UpdateDirectionPressed()
84-
{
85-
if (!GameClient.Running)
86-
return;
87-
88-
// PRESSED
89-
if (Input.IsActionJustPressed("ui_left"))
90-
DirectionHorizontal = Direction.West;
91-
if (Input.IsActionJustPressed("ui_right"))
92-
DirectionHorizontal = Direction.East;
93-
if (Input.IsActionJustPressed("ui_up"))
94-
DirectionVertical = Direction.North;
95-
if (Input.IsActionJustPressed("ui_down"))
96-
DirectionVertical = Direction.South;
97-
98-
// RELEASED
99-
if (Input.IsActionJustReleased("ui_left"))
100-
DirectionHorizontal = Direction.None;
101-
if (Input.IsActionJustReleased("ui_right"))
102-
DirectionHorizontal = Direction.None;
103-
if (Input.IsActionJustReleased("ui_up"))
104-
DirectionVertical = Direction.None;
105-
if (Input.IsActionJustReleased("ui_down"))
106-
DirectionVertical = Direction.None;
10761
}
10862

10963
public void SetHealth(int v)

Scripts/Scenes/Game/SceneGame.cs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,6 @@ public static void UpdatePlayerPositions(Dictionary<uint, Vector2> playerPositio
6060
return;
6161

6262
ServerPlayerPositions = playerPositions;
63-
64-
/*foreach (var pair in playerPositions)
65-
{
66-
var player = Instance.Players[pair.Key];
67-
68-
if (pair.Key == GameClient.PeerId)
69-
{
70-
//GD.Print("CLIENT: " + player.Position);
71-
ServerPlayerPosition = pair.Value;
72-
//if (player.Position.DistanceTo(pair.Value) > Test)
73-
//player.Position = Utils.Lerp(player.Position, pair.Value, 0.1f);
74-
}
75-
else
76-
player.Position = pair.Value;
77-
}*/
7863
}
7964

8065
public static int Test = 100;
@@ -86,9 +71,7 @@ public override void _Process(float delta)
8671
foreach (var pair in ServerPlayerPositions)
8772
{
8873
var player = Players[pair.Key];
89-
90-
if (pair.Key != GameClient.PeerId)
91-
player.Position = Utils.Lerp(player.Position, pair.Value, 0.1f);
74+
player.Position = Utils.Lerp(player.Position, pair.Value, 0.1f);
9275
}
9376
}
9477
}

0 commit comments

Comments
 (0)