unstaticify part1
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -27,3 +27,4 @@ Assets/Maps/autosave/
|
||||
Demos/
|
||||
omnisharp.json
|
||||
console.log
|
||||
console-1.log
|
||||
|
||||
@@ -137,7 +137,7 @@ namespace Game
|
||||
//FlaxEditor.Editor.Instance.PlayModeBegin -= Instance_PlayModeBegin;
|
||||
LoadConfig();
|
||||
//GameMode.Connect();
|
||||
WorldStateManager.Init();
|
||||
//WorldStateManager.Init();
|
||||
//NetworkManager.StartServer();
|
||||
|
||||
//GameMode.StartServer(true);
|
||||
|
||||
@@ -77,7 +77,7 @@ namespace Game
|
||||
Editor.Instance.PlayModeEnd += Cleanup;
|
||||
#endif
|
||||
|
||||
WorldStateManager.Init(); // FIXME
|
||||
//WorldStateManager.Init(); // FIXME
|
||||
}
|
||||
|
||||
public static void Cleanup()
|
||||
@@ -107,7 +107,7 @@ namespace Game
|
||||
Editor.Instance.PlayModeEnd -= Cleanup;
|
||||
//GameModeManager.Cleanup(); // FIXME
|
||||
#endif
|
||||
WorldStateManager.Cleanup(); // FIXME
|
||||
//WorldStateManager.Cleanup(); // FIXME
|
||||
|
||||
StopRecording();
|
||||
|
||||
|
||||
@@ -13,12 +13,19 @@ namespace Game
|
||||
|
||||
public static INetworkDriver ClientNetworkDriver { get; set; }
|
||||
|
||||
public static WorldStateManager clientWorldStateManager = null;
|
||||
|
||||
public static bool ConnectServer(string serverAddress = "localhost", bool listenServer = false)
|
||||
{
|
||||
if (!listenServer)
|
||||
{
|
||||
Cleanup();
|
||||
WorldStateManager.Init();
|
||||
//WorldStateManager.Init();
|
||||
clientWorldStateManager = new WorldStateManager(isClient: true);
|
||||
}
|
||||
else
|
||||
{
|
||||
clientWorldStateManager = new WorldStateManager(isLocalClient: true);
|
||||
}
|
||||
ServerAddress = serverAddress;
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ namespace Game
|
||||
{
|
||||
{
|
||||
Cleanup();
|
||||
WorldStateManager.Init();
|
||||
clientWorldStateManager = new WorldStateManager(isServer: true);
|
||||
}
|
||||
|
||||
if (!ReadDemo(demoName))
|
||||
|
||||
@@ -17,6 +17,8 @@ namespace Game
|
||||
|
||||
public static INetworkDriver ServerNetworkDriver { get; set; }
|
||||
|
||||
public static WorldStateManager serverWorldStateManager = null;
|
||||
|
||||
public static bool StartServer(bool listenServer = true)
|
||||
{
|
||||
Cleanup();
|
||||
@@ -79,7 +81,8 @@ namespace Game
|
||||
foreach (Type type in NetworkedTypes)
|
||||
Console.Print("tracking networked type: " + type.Name);
|
||||
#endif
|
||||
WorldStateManager.Init();
|
||||
serverWorldStateManager = new WorldStateManager(isServer: true);
|
||||
//WorldStateManager.Init();
|
||||
|
||||
if (listenServer)
|
||||
return ConnectServer(listenServer: true);
|
||||
@@ -135,13 +138,13 @@ namespace Game
|
||||
try
|
||||
{
|
||||
IsServer = true;
|
||||
if (WorldStateManager.OnClientConnecting(networkEvent.Sender))
|
||||
if (serverWorldStateManager.OnClientConnecting(networkEvent.Sender))
|
||||
{
|
||||
ConnectedClients.Add(networkEvent.Sender);
|
||||
Console.Print(
|
||||
$"Client({networkEvent.Sender.ConnectionId}) connected. Total clients: {ConnectedClients.Count}");
|
||||
|
||||
WorldStateManager.OnClientConnected(networkEvent.Sender);
|
||||
serverWorldStateManager.OnClientConnected(networkEvent.Sender);
|
||||
}
|
||||
else
|
||||
Console.Print($"Client({networkEvent.Sender.ConnectionId}) connection refused");
|
||||
|
||||
@@ -20,8 +20,8 @@ namespace Game
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static class WorldStateManager //FIXME: remove static for better init/cleanup
|
||||
// Manages world simulation and handles network messages
|
||||
public class WorldStateManager
|
||||
{
|
||||
private class WorldState
|
||||
{
|
||||
@@ -43,28 +43,42 @@ namespace Game
|
||||
public Float3 position;
|
||||
}
|
||||
|
||||
private static Dictionary<uint, PlayerActor> players;
|
||||
private static Dictionary<uint, NetworkConnection> playerConnections;
|
||||
public static Dictionary<uint, ulong> playerLastReceivedFrames;
|
||||
private static Dictionary<uint, ulong> playerLastFrame;
|
||||
private static bool welcomed = false;
|
||||
private Dictionary<uint, PlayerActor> players;
|
||||
private Dictionary<uint, NetworkConnection> playerConnections;
|
||||
public Dictionary<uint, ulong> playerLastReceivedFrames;
|
||||
private Dictionary<uint, ulong> playerLastFrame;
|
||||
private bool welcomed = false;
|
||||
|
||||
private static WorldState serverWorldState;
|
||||
private static WorldState clientWorldState;
|
||||
private static GameMode gameMode;
|
||||
private WorldState serverWorldState;
|
||||
private WorldState clientWorldState;
|
||||
private GameMode gameMode;
|
||||
|
||||
private static ulong lastReceivedServerFrame = 0;
|
||||
public static ulong ServerFrame => /*NetworkManager.server != null ? serverWorldState.frame :*/ lastReceivedServerFrame;
|
||||
public static ulong ClientFrame => clientWorldState.frame;
|
||||
public static float ServerTime = 0f;
|
||||
public static float ClientTime = 0f;
|
||||
private ulong lastReceivedServerFrame = 0;
|
||||
public ulong ServerFrame => /*NetworkManager.server != null ? serverWorldState.frame :*/ lastReceivedServerFrame;
|
||||
public ulong ClientFrame => clientWorldState.frame;
|
||||
public float ServerTime = 0f;
|
||||
public float ClientTime = 0f;
|
||||
|
||||
public static void Init()
|
||||
public bool IsServer = false;
|
||||
public bool IsClient = false;
|
||||
public bool IsLocalClient = false; // Context dependant, true when message is handled by local client
|
||||
|
||||
public WorldStateManager(bool isServer = false, bool isClient = false, bool isLocalClient = false)
|
||||
{
|
||||
welcomed = false;
|
||||
lastReceivedServerFrame = 0;
|
||||
IsServer = isServer;
|
||||
IsClient = isClient;
|
||||
IsLocalClient = isLocalClient;
|
||||
Assert.IsTrue(isServer || isClient || isLocalClient);
|
||||
|
||||
Init();
|
||||
}
|
||||
|
||||
public void Init()
|
||||
{
|
||||
//welcomed = false;
|
||||
//lastReceivedServerFrame = 0;
|
||||
ServerTime = Time.GameTime;
|
||||
ClientTime = 0f;
|
||||
//ClientTime = 0f;
|
||||
|
||||
players = new Dictionary<uint, PlayerActor>();
|
||||
playerConnections = new Dictionary<uint, NetworkConnection>();
|
||||
@@ -80,7 +94,7 @@ namespace Game
|
||||
Scripting.LateFixedUpdate += OnLateUpdatePre;
|
||||
}
|
||||
|
||||
public static void Cleanup()
|
||||
public void Cleanup()
|
||||
{
|
||||
NetworkManager.OnMessage -= OnMessage;
|
||||
Level.SceneLoaded -= OnLevelLoaded;
|
||||
@@ -93,7 +107,7 @@ namespace Game
|
||||
}
|
||||
}
|
||||
|
||||
private static PlayerFrame GetPlayerFrame(uint playerIndex, ulong playerFrameIndex)
|
||||
private PlayerFrame GetPlayerFrame(uint playerIndex, ulong playerFrameIndex)
|
||||
{
|
||||
WorldState worldState = NetworkManager.server != null ? serverWorldState : clientWorldState;
|
||||
PlayerFrame[] playerFrames = worldState.playerFrameHistory[playerIndex];
|
||||
@@ -105,13 +119,13 @@ namespace Game
|
||||
return playerFrame;
|
||||
}
|
||||
|
||||
public static void OnLevelLoaded(Scene scene, Guid assetGuid)
|
||||
public void OnLevelLoaded(Scene scene, Guid assetGuid)
|
||||
{
|
||||
serverWorldState.frame = 0;
|
||||
Console.Print("level loaded");
|
||||
}
|
||||
|
||||
public static void OnLateUpdatePre()
|
||||
public void OnLateUpdatePre()
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -128,7 +142,7 @@ namespace Game
|
||||
}
|
||||
}
|
||||
|
||||
public static void OnLateUpdate()
|
||||
public void OnLateUpdate()
|
||||
{
|
||||
if (NetworkManager.IsServer)
|
||||
{
|
||||
@@ -278,7 +292,17 @@ namespace Game
|
||||
}*/
|
||||
}
|
||||
|
||||
public static bool OnMessage(ref NetworkEvent networkEvent)
|
||||
public bool OnMessageServer(ref NetworkEvent networkEvent)
|
||||
{
|
||||
return OnMessage(ref networkEvent);
|
||||
}
|
||||
|
||||
public bool OnMessageClient(ref NetworkEvent networkEvent)
|
||||
{
|
||||
return OnMessage(ref networkEvent /*NetworkMessage message, NetworkConnection sender*/);
|
||||
}
|
||||
|
||||
public bool OnMessage(ref NetworkEvent networkEvent)
|
||||
{
|
||||
byte messageTypeByte = networkEvent.Message.ReadByte();
|
||||
if (!Enum.IsDefined(typeof(GameModeMessageType), messageTypeByte))
|
||||
@@ -404,7 +428,7 @@ namespace Game
|
||||
//Console.Print($"packet wrong order, last received: {lastReceivedServerFrame}, new: {reportedFrame}");
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
if (NetworkManager.IsClient)
|
||||
{
|
||||
if (reportedPlayerId == NetworkManager.LocalPlayerClientId)
|
||||
@@ -447,7 +471,7 @@ namespace Game
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool OnClientConnecting(NetworkConnection connection)
|
||||
public bool OnClientConnecting(NetworkConnection connection)
|
||||
{
|
||||
//if (connection.ConnectionId != NetworkManager.LocalPlayerClientId)
|
||||
{
|
||||
@@ -483,7 +507,7 @@ namespace Game
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool OnClientConnected(NetworkConnection connection)
|
||||
public bool OnClientConnected(NetworkConnection connection)
|
||||
{
|
||||
uint playerId = connection.ConnectionId;
|
||||
if (NetworkManager.LocalPlayerClientId == 0)
|
||||
@@ -494,7 +518,7 @@ namespace Game
|
||||
|
||||
var randomSpawn = spawns.First();
|
||||
|
||||
|
||||
|
||||
Float3 position = randomSpawn.Position + new Float3(0f, 4.1f, 0f);
|
||||
Float3 eulerAngles = randomSpawn.Orientation.EulerAngles;
|
||||
|
||||
@@ -524,7 +548,7 @@ namespace Game
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void SpawnPlayer(uint playerId, Float3 position, Vector3 eulerAngles)
|
||||
private void SpawnPlayer(uint playerId, Float3 position, Vector3 eulerAngles)
|
||||
{
|
||||
if (NetworkManager.IsServer && !playerLastFrame.ContainsKey(playerId))
|
||||
playerLastFrame.Add(playerId, serverWorldState.frame);
|
||||
@@ -556,7 +580,7 @@ namespace Game
|
||||
}
|
||||
}
|
||||
|
||||
private static void UpdatePlayerInput(uint playerId, PlayerInputState inputState)
|
||||
private void UpdatePlayerInput(uint playerId, PlayerInputState inputState)
|
||||
{
|
||||
if (playerId == NetworkManager.LocalPlayerClientId)
|
||||
{
|
||||
@@ -590,7 +614,7 @@ namespace Game
|
||||
|
||||
playerMovement.ApplyInputToCamera(lastInputState, true);
|
||||
playerMovement.SimulatePlayerMovement(lastInputState);
|
||||
|
||||
|
||||
playerMovement.input.SetState(frame, lastInputState, new PlayerActorState()
|
||||
{
|
||||
position = playerActor.Position,
|
||||
|
||||
@@ -106,7 +106,8 @@ namespace Game
|
||||
private readonly bool demoDeltasCorrect = true;
|
||||
private readonly bool demoDeltasVerify = true;
|
||||
|
||||
|
||||
|
||||
private WorldStateManager worldStateManager;
|
||||
|
||||
private bool predicting = false;
|
||||
|
||||
@@ -188,13 +189,15 @@ namespace Game
|
||||
//string demoPath = System.IO.Path.Combine(AssetManager.DemoPath, $"{DateTimeOffset.Now.UtcTicks}.gdem");
|
||||
//input = new PlayerInputLocal(playerActor, demoPath); // TODO: support recording
|
||||
input = new PlayerInputLocal(playerActor);
|
||||
|
||||
worldStateManager = NetworkManager.serverWorldStateManager;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.Print("network player: " + playerId.ToString());
|
||||
input = new PlayerInputNetwork();
|
||||
worldStateManager = NetworkManager.clientWorldStateManager;
|
||||
}
|
||||
Assert.IsTrue(worldStateManager != null);
|
||||
}
|
||||
|
||||
public void SetInput(string demoFile)
|
||||
@@ -444,10 +447,10 @@ namespace Game
|
||||
|
||||
//viewAngles = viewAnglesLastFrame;
|
||||
bool canpredict = true;
|
||||
if (true && input.Predict /*&& !NetworkManager.IsDemoPlaying*/ && WorldStateManager.ClientFrame > 0 && WorldStateManager.ServerFrame > 0)
|
||||
if (true && input.Predict /*&& !NetworkManager.IsDemoPlaying*/ && worldStateManager.ClientFrame > 0 && worldStateManager.ServerFrame > 0)
|
||||
{
|
||||
ulong currentFrame = WorldStateManager.ServerFrame;
|
||||
for (; currentFrame < WorldStateManager.ClientFrame; currentFrame++)
|
||||
ulong currentFrame = worldStateManager.ServerFrame;
|
||||
for (; currentFrame < worldStateManager.ClientFrame; currentFrame++)
|
||||
{
|
||||
if (!input.GetState(currentFrame, out var pastInputState, out var pastActorState))
|
||||
{
|
||||
@@ -469,7 +472,7 @@ namespace Game
|
||||
|
||||
|
||||
predicting = true;
|
||||
currentFrame = WorldStateManager.ServerFrame;
|
||||
currentFrame = worldStateManager.ServerFrame;
|
||||
for (; currentFrame < lastFrame; currentFrame++)
|
||||
{
|
||||
if (!input.GetState(currentFrame, out var pastInputState, out var pastActorState))
|
||||
@@ -481,7 +484,7 @@ namespace Game
|
||||
if (currentFrame == inputState.frame)
|
||||
movementState.jumped = movementState.jumped;
|
||||
|
||||
if (currentFrame == WorldStateManager.ServerFrame)
|
||||
if (currentFrame == worldStateManager.ServerFrame)
|
||||
{
|
||||
movementState.position = pastActorState.position;
|
||||
movementState.currentVelocity = pastActorState.velocity;
|
||||
@@ -891,8 +894,8 @@ namespace Game
|
||||
|
||||
if (false)
|
||||
{
|
||||
if (WorldStateManager.ServerFrame > 0 && WorldStateManager.ClientFrame > 0)
|
||||
for (ulong frame = WorldStateManager.ServerFrame; frame < WorldStateManager.ClientFrame; frame++)
|
||||
if (worldStateManager.ServerFrame > 0 && worldStateManager.ClientFrame > 0)
|
||||
for (ulong frame = worldStateManager.ServerFrame; frame < worldStateManager.ClientFrame; frame++)
|
||||
{
|
||||
if (!input.GetState(frame, out var pastInputState, out var pastActorState))
|
||||
continue;
|
||||
@@ -902,7 +905,7 @@ namespace Game
|
||||
|
||||
Float4 color1 = new Float4(Color.Red.R, Color.Red.G, Color.Red.B, Color.Red.A);
|
||||
Float4 color2 = new Float4(Color.Blue.R, Color.Blue.G, Color.Blue.B, Color.Blue.A);
|
||||
Float4 color3 = Float4.Lerp(color1, color2, (float)(frame - WorldStateManager.ServerFrame) / (float)(WorldStateManager.ClientFrame - WorldStateManager.ServerFrame));
|
||||
Float4 color3 = Float4.Lerp(color1, color2, (float)(frame - worldStateManager.ServerFrame) / (float)(worldStateManager.ClientFrame - worldStateManager.ServerFrame));
|
||||
Color color = new Color(color3.X, color3.Y, color3.Z, color3.W);
|
||||
DebugDraw.DrawBox(bbox, color * 1f);
|
||||
}
|
||||
@@ -910,7 +913,7 @@ namespace Game
|
||||
else
|
||||
{
|
||||
var serverBbox = boxCollider.OrientedBox.GetBoundingBox();
|
||||
if (input.GetState(WorldStateManager.ServerFrame, out var serverInputState, out var serverActorState))
|
||||
if (input.GetState(worldStateManager.ServerFrame, out var serverInputState, out var serverActorState))
|
||||
serverBbox.Center = serverActorState.position;
|
||||
|
||||
if (serverBbox.Center == clientBbox.Center)
|
||||
@@ -1159,7 +1162,7 @@ namespace Game
|
||||
|
||||
public void SimulatePlayerMovement(PlayerInputState inputState)
|
||||
{
|
||||
simulationTime = WorldStateManager.ClientTime + (inputState.frame * (1.0f / Time.PhysicsFPS));
|
||||
simulationTime = worldStateManager.ClientTime + (inputState.frame * (1.0f / Time.PhysicsFPS));
|
||||
|
||||
Vector3 inputDirection =
|
||||
new Float3(inputState.moveRight, 0.0f, inputState.moveForward);
|
||||
|
||||
@@ -6,6 +6,13 @@
|
||||
"executablePath": "C:/dev/Flax/FlaxEngine/Binaries/Editor/Win64/Development/FlaxEditor.exe",
|
||||
"commandLineArgs": "-project \"C:/dev/GoakeFlax\"",
|
||||
"nativeDebugging": false
|
||||
},
|
||||
"Game Debug": {
|
||||
"commandName": "Executable",
|
||||
"workingDirectory": "C:/dev/GoakeFlax",
|
||||
"executablePath": "C:/dev/Flax/FlaxEngine/Binaries/Editor/Win64/Debug/FlaxEditor.exe",
|
||||
"commandLineArgs": "-project \"C:/dev/GoakeFlax\"",
|
||||
"nativeDebugging": false
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user