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

Commit f63efe0

Browse files
Enforce server-side positions
1 parent 34acb43 commit f63efe0

4 files changed

Lines changed: 83 additions & 1 deletion

File tree

Scripts/Netcode/Common/SPacketLobbyGameStart.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ public class SPacketLobbyGameStart : APacketServer
77
{
88
public override void Handle()
99
{
10-
if (GameClient.IsHost)
10+
if (GameClient.IsHost)
11+
{
1112
GameServer.TimerGameLoop.Enabled = true;
13+
GameServer.TimerNotifyClients.Enabled = true;
14+
}
1215

1316
SceneManager.ChangeScene("Game");
1417
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using GodotModules.Netcode.Client;
2+
using System;
3+
using System.Collections.Generic;
4+
using Game;
5+
using Godot;
6+
7+
namespace GodotModules.Netcode
8+
{
9+
public class SPacketPlayerPositions : APacketServer
10+
{
11+
public Dictionary<uint, Vector2> PlayerPositions { get; set; }
12+
13+
public override void Write(PacketWriter writer)
14+
{
15+
writer.Write((ushort)PlayerPositions.Count);
16+
foreach (var pair in PlayerPositions)
17+
{
18+
var id = pair.Key;
19+
var pos = pair.Value;
20+
21+
var x = (float)Math.Round(pos.x, 1);
22+
var y = (float)Math.Round(pos.y, 1);
23+
24+
writer.Write(pair.Key);
25+
writer.Write(x);
26+
writer.Write(y);
27+
}
28+
}
29+
30+
public override void Read(PacketReader reader)
31+
{
32+
PlayerPositions = new Dictionary<uint, Vector2>();
33+
var count = reader.ReadUInt16();
34+
for (int i = 0; i < count; i++)
35+
{
36+
var id = reader.ReadUInt32();
37+
var x = reader.ReadFloat();
38+
var y = reader.ReadFloat();
39+
40+
PlayerPositions.Add(id, new Vector2(x, y));
41+
}
42+
}
43+
44+
public override void Handle()
45+
{
46+
SceneGame.UpdatePlayerPositions(PlayerPositions);
47+
}
48+
}
49+
}

Scripts/Netcode/Server/GameServer.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class GameServer : ENetServer
1313
{
1414
public static Dictionary<uint, DataPlayer> Players { get; set; }
1515
public static Timer TimerGameLoop { get; set; }
16+
public static Timer TimerNotifyClients { get; set; }
1617
public static float Delta { get; set; }
1718

1819
public GameServer()
@@ -21,6 +22,17 @@ public GameServer()
2122
TimerGameLoop = new Timer(16.67);
2223
TimerGameLoop.Elapsed += TimerGameLoopCallback;
2324
TimerGameLoop.AutoReset = true;
25+
26+
TimerNotifyClients = new Timer(1000);
27+
TimerNotifyClients.Elapsed += TimerNotifyClientsCallback;
28+
TimerNotifyClients.AutoReset = true;
29+
}
30+
31+
public void TimerNotifyClientsCallback(System.Object source, ElapsedEventArgs args)
32+
{
33+
SendToAllPlayers(ServerPacketOpcode.PlayerPositions, new SPacketPlayerPositions {
34+
PlayerPositions = Players.ToDictionary(x => x.Key, x => x.Value.Position)
35+
});
2436
}
2537

2638
public void TimerGameLoopCallback(System.Object source, ElapsedEventArgs args)

Scripts/Scenes/Game/SceneGame.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using GodotModules.ModLoader;
44
using GodotModules.Netcode.Client;
55
using System;
6+
using System.Collections.Generic;
67

78
namespace Game
89
{
@@ -14,12 +15,16 @@ public class SceneGame : Node
1415
[Export] public readonly NodePath NodePathLabelPlayerHealth;
1516
public Label LabelPlayerHealth;
1617

18+
private Dictionary<uint, OtherPlayer> Players;
19+
1720
public override void _Ready()
1821
{
22+
Players = new Dictionary<uint, OtherPlayer>();
1923
Instance = this;
2024
LabelPlayerHealth = GetNode<Label>(NodePathLabelPlayerHealth);
2125
Player = Prefabs.ClientPlayer.Instance<ClientPlayer>();
2226
Player.Position = Vector2.Zero;
27+
Players.Add(GameClient.PeerId, Player);
2328
AddChild(Player);
2429

2530
// set game definitions
@@ -38,12 +43,25 @@ public override void _Ready()
3843

3944
var otherPlayer = Prefabs.OtherPlayer.Instance<OtherPlayer>();
4045
otherPlayer.Position = Vector2.Zero;
46+
Players.Add(pair.Key, otherPlayer);
4147
AddChild(otherPlayer);
4248
otherPlayer.SetUsername(pair.Value);
4349
}
4450
}
4551
}
4652

53+
public static void UpdatePlayerPositions(Dictionary<uint, Vector2> playerPositions)
54+
{
55+
if (SceneManager.ActiveScene != "Game")
56+
return;
57+
58+
foreach (var pair in playerPositions)
59+
{
60+
var player = Instance.Players[pair.Key];
61+
player.Position = pair.Value;
62+
}
63+
}
64+
4765
public override void _Process(float delta)
4866
{
4967
ModLoader.Call("OnGameUpdate", delta);

0 commit comments

Comments
 (0)