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

Commit 5569cc1

Browse files
Remove static from Players
1 parent b34e511 commit 5569cc1

7 files changed

Lines changed: 28 additions & 15 deletions

File tree

Scenes/Main.tscn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ script = ExtResource( 3 )
2121

2222
[node name="NetworkManager" type="Node" parent="."]
2323
script = ExtResource( 4 )
24+
NodePathServerSimulation = NodePath("../CanvasLayer/Server Simulation/ViewportContainer/Viewport/Server Simulation")
2425

2526
[node name="SceneManager" type="Node" parent="."]
2627
script = ExtResource( 5 )

Scripts/Netcode/Common/SPacketGame.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public override async Task Handle()
6565
{
6666
var enemy = Prefabs.Enemy.Instance<GodotModules.Netcode.Server.Enemy>();
6767
enemy.Position = pair.Value.Position;
68+
enemy.SetPlayers(sceneGameScript.Players);
6869
sceneGameScript.AddChild(enemy);
6970
sceneGameScript.Enemies[pair.Key] = enemy;
7071
}

Scripts/Netcode/Common/Server/ServerSimulation.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class ServerSimulation : Node
88
private static ConcurrentQueue<ThreadCmd<SimulationOpcode>> ServerSimulationQueue = new ConcurrentQueue<ThreadCmd<SimulationOpcode>>();
99

1010
public static Dictionary<ushort, Enemy> Enemies = new Dictionary<ushort, Enemy>();
11-
public static Dictionary<byte, Game.OtherPlayer> Players = new Dictionary<byte, Game.OtherPlayer>();
11+
private Dictionary<byte, Game.OtherPlayer> Players;
1212

1313
private static ServerSimulation Instance { get; set; }
1414
private static GTimer Timer { get; set; }
@@ -17,6 +17,7 @@ public class ServerSimulation : Node
1717
public override void _Ready()
1818
{
1919
Instance = this;
20+
Players = new Dictionary<byte, Game.OtherPlayer>();
2021
Timer = new GTimer(ServerIntervals.PlayerTransforms, true, false);
2122
Timer.Connect(this, nameof(EmitSimulationData));
2223
}
@@ -36,7 +37,7 @@ public override void _PhysicsProcess(float delta)
3637

3738
public static void Enqueue(ThreadCmd<SimulationOpcode> cmd) => ServerSimulationQueue.Enqueue(cmd);
3839

39-
public static void Dequeue()
40+
public void Dequeue()
4041
{
4142
if (ServerSimulationQueue.TryDequeue(out ThreadCmd<SimulationOpcode> cmd))
4243
{
@@ -77,7 +78,7 @@ public static void Dequeue()
7778
}
7879
}
7980

80-
private static void CreatePlayer(byte id)
81+
private void CreatePlayer(byte id)
8182
{
8283
var otherPlayer = Prefabs.OtherPlayer.Instance<Game.OtherPlayer>();
8384
otherPlayer.AddToGroup("Player");
@@ -86,10 +87,11 @@ private static void CreatePlayer(byte id)
8687
Instance.AddChild(otherPlayer);
8788
}
8889

89-
private static void CreateEnemy(SimulationEnemy simEnemy)
90+
private void CreateEnemy(SimulationEnemy simEnemy)
9091
{
9192
var enemy = Prefabs.Enemy.Instance<Enemy>();
9293
enemy.AddToGroup("Enemy");
94+
enemy.SetPlayers(Players);
9395
enemy.Position = simEnemy.SpawnForce;
9496
Enemies.Add(simEnemy.Id, enemy);
9597
Instance.AddChild(enemy);
@@ -105,7 +107,7 @@ private void EmitSimulationData()
105107
NetworkManager.GameServer.ENetCmds.Enqueue(new ThreadCmd<ENetOpcode>(ENetOpcode.EnemyTransforms, enemyData));
106108
}
107109

108-
public static void Cleanup()
110+
public void Cleanup()
109111
{
110112
Timer.Stop();
111113
while (ServerSimulationQueue.TryDequeue(out _));

Scripts/Netcode/Common/Utils/NetworkManager.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace GodotModules.Netcode
1111
{
1212
public class NetworkManager : Node
1313
{
14+
[Export] public readonly NodePath NodePathServerSimulation;
15+
1416
private static SceneTree Tree { get; set; }
1517
private static bool ENetInitialized { get; set; }
1618
public static WebClient WebClient = new WebClient();
@@ -20,6 +22,7 @@ public override void _Ready()
2022
{
2123
Tree = GetTree();
2224
ServerAuthoritativeMovement = false;
25+
ServerSimulation = GetNode<ServerSimulation>(NodePathServerSimulation);
2326

2427
ENetInitialized = ENet.Library.Initialize();
2528
if (!ENetInitialized)
@@ -30,6 +33,7 @@ public override void _Ready()
3033

3134
// SERVER
3235
public static GameServer GameServer { get; set; }
36+
public static ServerSimulation ServerSimulation { get; set; }
3337
public static bool ServerAuthoritativeMovement { get; set; }
3438
private static int GameServerStillRunning { get; set; }
3539
public static bool IsServerRunning() => GameServer == null ? false : GameServer.IsRunning;

Scripts/Scenes/Game/Enemy.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,34 @@ public override void _PhysicsProcess(float delta)
2323
{
2424
Delta = delta;
2525

26-
var players = ServerSimulation.Players;
27-
if (players.Count == 0)
26+
if (Players.Count == 0)
2827
return;
2928

30-
Sprite.LookAt(players.First().Value.Position);
29+
Sprite.LookAt(Players.First().Value.Position);
3130
}
3231

3332
public override void _IntegrateForces(Physics2DDirectBodyState state)
3433
{
3534
state.LinearVelocity = Velocity;
3635
}
3736

37+
public Dictionary<byte, Game.OtherPlayer> Players { get; set; }
38+
39+
public void SetPlayers(Dictionary<byte, Game.OtherPlayer> players)
40+
{
41+
Players = players;
42+
}
43+
3844
private void CalcVelocity()
3945
{
40-
var players = ServerSimulation.Players;
41-
if (players.Count == 0)
46+
if (Players.Count == 0)
4247
{
4348
Velocity = Vector2.Zero;
4449
return;
4550
}
4651

4752
// TODO: Find nearest player instead of first player in array
48-
var dir = (players.First().Value.Position - Position).Normalized();
53+
var dir = (Players.First().Value.Position - Position).Normalized();
4954
Velocity = (dir * Delta * 10000f);
5055
}
5156

Scripts/Scenes/Game/SceneGame.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class SceneGame : AScene
1212
[Export] public readonly NodePath NodePathLabelPlayerHealth;
1313
public Label LabelPlayerHealth;
1414

15-
private Dictionary<uint, OtherPlayer> Players;
15+
public Dictionary<byte, OtherPlayer> Players;
1616
public Dictionary<ushort, Enemy> Enemies;
1717
public ClientPlayer Player { get; set; }
1818
private PrevCurQueue<Dictionary<byte, DataEntityTransform>> PlayerTransformQueue { get; set; }
@@ -108,7 +108,7 @@ public void RemovePlayer(byte id)
108108

109109
private void InitMultiplayerStuff()
110110
{
111-
Players[NetworkManager.PeerId] = Player;
111+
Players[(byte)NetworkManager.PeerId] = Player;
112112
Player.SetUsername(GameManager.Options.OnlineUsername);
113113

114114
bool IsNotClient(uint id) => id != NetworkManager.PeerId;
@@ -119,7 +119,7 @@ private void InitMultiplayerStuff()
119119
{
120120
var otherPlayer = Prefabs.OtherPlayer.Instance<OtherPlayer>();
121121
otherPlayer.Position = Vector2.Zero;
122-
Players.Add(pair.Key, otherPlayer);
122+
Players.Add((byte)pair.Key, otherPlayer);
123123
AddChild(otherPlayer);
124124
otherPlayer.SetUsername(pair.Value);
125125
});

Scripts/Scenes/Main/GameManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public override void _Ready()
1919
public override async void _Process(float delta)
2020
{
2121
Logger.Dequeue();
22-
ServerSimulation.Dequeue();
22+
NetworkManager.ServerSimulation.Dequeue();
2323
await GodotCommands.Dequeue();
2424
}
2525

0 commit comments

Comments
 (0)