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

Commit b39be05

Browse files
Fix #41
1 parent be7e8c9 commit b39be05

12 files changed

Lines changed: 41 additions & 2 deletions

Scripts/Netcode/Common/SPacketLobbyChatMessage.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public override void Read(PacketReader reader)
1818

1919
public override void Handle()
2020
{
21+
if (!SceneManager.InLobby())
22+
return;
23+
2124
SceneLobby.Log(Id, Message);
2225
}
2326
}

Scripts/Netcode/Common/SPacketLobbyCountdownChange.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public override void Read(PacketReader reader)
2020

2121
public override void Handle()
2222
{
23+
if (!SceneManager.InLobby())
24+
return;
25+
2326
if (CountdownRunning)
2427
SceneLobby.StartGameCountdown();
2528
else

Scripts/Netcode/Common/SPacketLobbyGameStart.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ public class SPacketLobbyGameStart : APacketServer
77
{
88
public override void Handle()
99
{
10+
if (!SceneManager.InLobby())
11+
return;
12+
1013
if (GameClient.IsHost)
1114
{
1215
GameServer.TimerGameLoop.Enabled = true;

Scripts/Netcode/Common/SPacketLobbyInfo.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public override void Read(PacketReader reader)
4242

4343
public override void Handle()
4444
{
45+
if (!SceneManager.InGameServers())
46+
return;
47+
4548
ENetClient.PeerId = Id;
4649
ENetClient.IsHost = IsHost;
4750
ENetClient.Log($"{GameManager.Options.OnlineUsername} joined lobby with id {Id} also other players in lobby are {Players.Print()}");

Scripts/Netcode/Common/SPacketLobbyJoin.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public override void Read(PacketReader reader)
2020

2121
public override void Handle()
2222
{
23+
if (!SceneManager.InLobby())
24+
return;
25+
2326
SceneLobby.AddPlayer(Id, Username);
2427

2528
ENetClient.Log($"Player with username {Username} id: {Id} joined the lobby");

Scripts/Netcode/Common/SPacketLobbyLeave.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public override void Read(PacketReader reader)
1616

1717
public override void Handle()
1818
{
19+
if (!SceneManager.InLobby())
20+
return;
21+
1922
if (!GameClient.Players.ContainsKey(Id))
2023
{
2124
ENetClient.Log($"Received LobbyLeave packet from server for id {Id}. Tried to remove from Players but does not exist in Players to begin with");

Scripts/Netcode/Common/SPacketLobbyReady.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public override void Read(PacketReader reader)
2020

2121
public override void Handle()
2222
{
23+
if (!SceneManager.InLobby())
24+
return;
25+
2326
SceneLobby.SetReady(Id, Ready);
2427
}
2528
}

Scripts/Netcode/Common/SPacketPlayerPositions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public override void Read(PacketReader reader)
4343

4444
public override void Handle()
4545
{
46+
if (!SceneManager.InGame())
47+
return;
48+
4649
SceneGame.UpdatePlayerPositions(PlayerPositions);
4750
}
4851
}

Scripts/Netcode/Server/GameServer.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,34 @@ 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; }
1516
public static Timer TimerGameLoop { get; set; }
1617
public static Timer TimerNotifyClients { get; set; }
1718
public static float Delta { get; set; }
1819

1920
public GameServer()
2021
{
2122
Players = new Dictionary<uint, DataPlayer>();
23+
LastSentPlayerPositions = new Dictionary<uint, Vector2>();
2224
TimerGameLoop = new Timer(16.67);
2325
TimerGameLoop.Elapsed += TimerGameLoopCallback;
2426
TimerGameLoop.AutoReset = true;
2527

26-
TimerNotifyClients = new Timer(1000);
28+
TimerNotifyClients = new Timer(100);
2729
TimerNotifyClients.Elapsed += TimerNotifyClientsCallback;
2830
TimerNotifyClients.AutoReset = true;
2931
}
3032

3133
public void TimerNotifyClientsCallback(System.Object source, ElapsedEventArgs args)
3234
{
35+
var playerPositions = Players.ToDictionary(x => x.Key, x => x.Value.Position);
36+
System.Console.WriteLine("DEBUG: " + playerPositions.Count);
37+
3338
SendToAllPlayers(ServerPacketOpcode.PlayerPositions, new SPacketPlayerPositions {
34-
PlayerPositions = Players.ToDictionary(x => x.Key, x => x.Value.Position)
39+
PlayerPositions = playerPositions
3540
}, PacketFlags.Reliable);
41+
42+
LastSentPlayerPositions = playerPositions;
3643
}
3744

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

Scripts/Scenes/Game/SceneGame.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public override void _Ready()
3030
// set game definitions
3131
ModLoader.Script.Globals["Player", "setHealth"] = (Action<int>)SceneGame.Player.SetHealth;
3232

33+
Utils.Log("SceneGame.cs _Ready()");
3334
ModLoader.Call("OnGameInit");
3435

3536
if (GameClient.Running)

0 commit comments

Comments
 (0)