namespacify everything

This commit is contained in:
2024-04-06 14:42:10 +03:00
parent 68b735b59c
commit 6430cc9b4d
42 changed files with 10066 additions and 10107 deletions

View File

@@ -1,28 +1,27 @@
using FlaxEngine;
namespace Game
namespace Game;
public static class InputManager
{
public static class InputManager
public static bool GetAction(string name)
{
public static bool GetAction(string name)
{
if (Console.IsOpen)
return false;
return Input.GetAction(name);
}
if (Console.IsOpen)
return false;
return Input.GetAction(name);
}
public static float GetAxis(string name)
{
if (Console.IsOpen)
return 0.0f;
return Input.GetAxis(name);
}
public static float GetAxis(string name)
{
if (Console.IsOpen)
return 0.0f;
return Input.GetAxis(name);
}
public static float GetAxisRaw(string name)
{
if (Console.IsOpen)
return 0.0f;
return Input.GetAxisRaw(name);
}
public static float GetAxisRaw(string name)
{
if (Console.IsOpen)
return 0.0f;
return Input.GetAxisRaw(name);
}
}

View File

@@ -2,173 +2,172 @@
using FlaxEngine;
using FlaxEngine.Networking;
namespace Game
namespace Game;
[StructLayout(LayoutKind.Sequential)]
public struct PlayerInputState
{
[StructLayout(LayoutKind.Sequential)]
public struct PlayerInputState
/*static PlayerInputState()
{
/*static PlayerInputState()
{
NetworkReplicator.AddSerializer(typeof(PlayerInputState),
(ptr, streamPtr) =>
{
},
(ptr, streamPtr) =>
{
});
}*/
#if false
public PlayerInputState(ulong frame, float viewDeltaX, float viewDeltaY, float moveForward, float moveRight,
bool attacking, bool jumping)
{
this.frame = frame;
this.viewDeltaX = viewDeltaX;
this.viewDeltaY = viewDeltaY;
this.moveForward = moveForward;
this.moveRight = moveRight;
this.attacking = attacking;
this.jumping = jumping;
/*this.verificationPosition = verificationPosition;
this.verificationVelocity = verificationVelocity;
this.verificationViewAngles = verificationViewAngles;
this.verificationOrientation = verificationOrientation;*/
}
public PlayerInputState(ulong frame, float viewDeltaX, float viewDeltaY, float moveForward, float moveRight,
bool attacking, bool jumping, Float3 verificationPosition, Float3 verificationVelocity,
Float3 verificationViewAngles, Quaternion verificationOrientation)
{
this.frame = frame;
this.viewDeltaX = viewDeltaX;
this.viewDeltaY = viewDeltaY;
this.moveForward = moveForward;
this.moveRight = moveRight;
this.attacking = attacking;
this.jumping = jumping;
this.verificationPosition = verificationPosition;
this.verificationVelocity = verificationVelocity;
this.verificationViewAngles = verificationViewAngles;
this.verificationOrientation = verificationOrientation;
}
#endif
public ulong frame;
public float viewDeltaX, viewDeltaY;
public float moveForward;
public float moveRight;
public bool attacking;
public bool jumping;
public Float3 verificationPosition;
public Float3 verificationVelocity;
public Float3 verificationViewAngles;
public Quaternion verificationOrientation;
}
[StructLayout(LayoutKind.Sequential)]
public struct PlayerActorState
{
public Float3 position;
public Float3 velocity;
public Quaternion orientation;
public Float3 viewAngles; // yaw, pitch, roll
public float lastJumpTime;
public int numJumps;
public bool jumped;
}
[StructLayout(LayoutKind.Sequential)]
public struct PlayerState
{
public PlayerInputState input;
public PlayerActorState actor;
}
public class PlayerInput
{
public const byte DemoVer = 1;
public const int MaxPlayerStates = 120;
public PlayerState currentState;
public ulong frame;
//public ulong oldestFrame;
private PlayerState[] states = new PlayerState[MaxPlayerStates];
public virtual bool Predict => false;
public virtual void OnUpdate()
{
}
public virtual void OnFixedUpdate()
{
}
public virtual void OnEndFrame()
{
//Console.Print("recorded frame " + frame);
states[frame % MaxPlayerStates] = currentState;
/*ulong oldest = ulong.MaxValue;
for (int i = 0; i < 120; i++)
oldest = states[i].input.frame < oldest ? states[i].input.frame : oldest;
oldestFrame = oldest;*/
frame++;
currentState.input.frame = frame;
}
public virtual void RecordCurrentActorState(PlayerActorState actorState)
{
if (actorState.position.Length <= 0.01)
Console.Print("wrong recorded position?");
currentState.actor = actorState;
}
public bool GetState(ulong frame, out PlayerInputState inputState, out PlayerActorState actorState)
{
int frameIndex = (int)frame % MaxPlayerStates;
if (states[frameIndex].input.frame != frame)
NetworkReplicator.AddSerializer(typeof(PlayerInputState),
(ptr, streamPtr) =>
{
inputState = default;
actorState = default;
return false;
}
inputState = states[frameIndex].input;
actorState = states[frameIndex].actor;
return true;
}
},
(ptr, streamPtr) =>
{
public void SetState(ulong frame, PlayerInputState inputState)
});
}*/
#if false
public PlayerInputState(ulong frame, float viewDeltaX, float viewDeltaY, float moveForward, float moveRight,
bool attacking, bool jumping)
{
this.frame = frame;
this.viewDeltaX = viewDeltaX;
this.viewDeltaY = viewDeltaY;
this.moveForward = moveForward;
this.moveRight = moveRight;
this.attacking = attacking;
this.jumping = jumping;
/*this.verificationPosition = verificationPosition;
this.verificationVelocity = verificationVelocity;
this.verificationViewAngles = verificationViewAngles;
this.verificationOrientation = verificationOrientation;*/
}
public PlayerInputState(ulong frame, float viewDeltaX, float viewDeltaY, float moveForward, float moveRight,
bool attacking, bool jumping, Float3 verificationPosition, Float3 verificationVelocity,
Float3 verificationViewAngles, Quaternion verificationOrientation)
{
this.frame = frame;
this.viewDeltaX = viewDeltaX;
this.viewDeltaY = viewDeltaY;
this.moveForward = moveForward;
this.moveRight = moveRight;
this.attacking = attacking;
this.jumping = jumping;
this.verificationPosition = verificationPosition;
this.verificationVelocity = verificationVelocity;
this.verificationViewAngles = verificationViewAngles;
this.verificationOrientation = verificationOrientation;
}
#endif
public ulong frame;
public float viewDeltaX, viewDeltaY;
public float moveForward;
public float moveRight;
public bool attacking;
public bool jumping;
public Float3 verificationPosition;
public Float3 verificationVelocity;
public Float3 verificationViewAngles;
public Quaternion verificationOrientation;
}
[StructLayout(LayoutKind.Sequential)]
public struct PlayerActorState
{
public Float3 position;
public Float3 velocity;
public Quaternion orientation;
public Float3 viewAngles; // yaw, pitch, roll
public float lastJumpTime;
public int numJumps;
public bool jumped;
}
[StructLayout(LayoutKind.Sequential)]
public struct PlayerState
{
public PlayerInputState input;
public PlayerActorState actor;
}
public class PlayerInput
{
public const byte DemoVer = 1;
public const int MaxPlayerStates = 120;
public PlayerState currentState;
public ulong frame;
//public ulong oldestFrame;
private PlayerState[] states = new PlayerState[MaxPlayerStates];
public virtual bool Predict => false;
public virtual void OnUpdate()
{
}
public virtual void OnFixedUpdate()
{
}
public virtual void OnEndFrame()
{
//Console.Print("recorded frame " + frame);
states[frame % MaxPlayerStates] = currentState;
/*ulong oldest = ulong.MaxValue;
for (int i = 0; i < 120; i++)
oldest = states[i].input.frame < oldest ? states[i].input.frame : oldest;
oldestFrame = oldest;*/
frame++;
currentState.input.frame = frame;
}
public virtual void RecordCurrentActorState(PlayerActorState actorState)
{
if (actorState.position.Length <= 0.01)
Console.Print("wrong recorded position?");
currentState.actor = actorState;
}
public bool GetState(ulong frame, out PlayerInputState inputState, out PlayerActorState actorState)
{
int frameIndex = (int)frame % MaxPlayerStates;
if (states[frameIndex].input.frame != frame)
{
int frameIndex = (int)frame % MaxPlayerStates;
states[frameIndex].input = inputState;
states[frameIndex].input.frame = frame;
states[frameIndex].actor = new PlayerActorState();
inputState = default;
actorState = default;
return false;
}
public void SetState(ulong frame, PlayerInputState inputState, PlayerActorState actorState)
{
int frameIndex = (int)frame % MaxPlayerStates;
states[frameIndex].input = inputState;
states[frameIndex].input.frame = frame;
states[frameIndex].actor = actorState;
}
inputState = states[frameIndex].input;
actorState = states[frameIndex].actor;
return true;
}
public PlayerInputState GetCurrentInputState()
{
return currentState.input;
}
public void SetState(ulong frame, PlayerInputState inputState)
{
int frameIndex = (int)frame % MaxPlayerStates;
states[frameIndex].input = inputState;
states[frameIndex].input.frame = frame;
states[frameIndex].actor = new PlayerActorState();
}
public PlayerActorState GetCurrentActorState()
{
return currentState.actor;
}
public void SetState(ulong frame, PlayerInputState inputState, PlayerActorState actorState)
{
int frameIndex = (int)frame % MaxPlayerStates;
states[frameIndex].input = inputState;
states[frameIndex].input.frame = frame;
states[frameIndex].actor = actorState;
}
public PlayerInputState GetCurrentInputState()
{
return currentState.input;
}
public PlayerActorState GetCurrentActorState()
{
return currentState.actor;
}
}

View File

@@ -8,95 +8,94 @@ using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Console = Game.Console;
namespace Game
namespace Game;
public class PlayerInputDemo : PlayerInput
{
public class PlayerInputDemo : PlayerInput
protected List<PlayerInputState> buffer = new List<PlayerInputState>();
protected IEnumerator<PlayerInputState> bufferEnumerable;
public bool IsPlaying
{
protected List<PlayerInputState> buffer = new List<PlayerInputState>();
protected IEnumerator<PlayerInputState> bufferEnumerable;
public bool IsPlaying
get
{
get
{
return bufferEnumerable != null;
}
}
public PlayerInputDemo(string demoPath)
{
if (!File.Exists(demoPath))
return;
int expectedPlayerInputStateSize = Unsafe.SizeOf<PlayerInputState>();
Stopwatch sw = Stopwatch.StartNew();
using FileStream fileStream = File.OpenRead(demoPath);
using GZipStream stream = new GZipStream(fileStream, CompressionMode.Decompress);
int ver = stream.ReadByte();
int inputStateSize = stream.ReadByte();
if (ver != DemoVer || inputStateSize != expectedPlayerInputStateSize)
{
Console.Print("demover mismatch: version " + ver + " != " + DemoVer + ", inputStateSize " +
inputStateSize + " != " + Unsafe.SizeOf<PlayerInputState>());
stream.Close();
return;
}
Span<byte> b = stackalloc byte[expectedPlayerInputStateSize];
while (true)
{
int bytesLeftInBuffer = expectedPlayerInputStateSize;
do
{
int readBytes = stream.Read(b.Slice(expectedPlayerInputStateSize - bytesLeftInBuffer, bytesLeftInBuffer));
if (readBytes == 0)
break;
bytesLeftInBuffer -= readBytes;
} while (bytesLeftInBuffer > 0);
if (bytesLeftInBuffer > 0)
break; // EOF;
buffer.Add(MemoryMarshal.Read<PlayerInputState>(b));
}
sw.Stop();
bufferEnumerable = buffer.GetEnumerator();
Console.Print($"Demo parse time {sw.Elapsed.TotalMilliseconds}ms, frames: {buffer.Count} ");
OnEndFrame(); // advances to first frame
}
public override void OnEndFrame()
{
// TODO: check if the current state frame matches the current frame number before advancing
/*asdf++;
if (asdf < 8)
return;*/
if (bufferEnumerable == null || !bufferEnumerable.MoveNext())
{
if (buffer.Any())
{
bufferEnumerable.Dispose();
bufferEnumerable = null;
buffer.Clear();
Console.Print("Demo ended");
}
return;
}
//var actorState = currentState.actor;
currentState.input = bufferEnumerable.Current;
//frame++;
//currentState.actor = actorState;
return bufferEnumerable != null;
}
}
public PlayerInputDemo(string demoPath)
{
if (!File.Exists(demoPath))
return;
int expectedPlayerInputStateSize = Unsafe.SizeOf<PlayerInputState>();
Stopwatch sw = Stopwatch.StartNew();
using FileStream fileStream = File.OpenRead(demoPath);
using GZipStream stream = new GZipStream(fileStream, CompressionMode.Decompress);
int ver = stream.ReadByte();
int inputStateSize = stream.ReadByte();
if (ver != DemoVer || inputStateSize != expectedPlayerInputStateSize)
{
Console.Print("demover mismatch: version " + ver + " != " + DemoVer + ", inputStateSize " +
inputStateSize + " != " + Unsafe.SizeOf<PlayerInputState>());
stream.Close();
return;
}
Span<byte> b = stackalloc byte[expectedPlayerInputStateSize];
while (true)
{
int bytesLeftInBuffer = expectedPlayerInputStateSize;
do
{
int readBytes = stream.Read(b.Slice(expectedPlayerInputStateSize - bytesLeftInBuffer, bytesLeftInBuffer));
if (readBytes == 0)
break;
bytesLeftInBuffer -= readBytes;
} while (bytesLeftInBuffer > 0);
if (bytesLeftInBuffer > 0)
break; // EOF;
buffer.Add(MemoryMarshal.Read<PlayerInputState>(b));
}
sw.Stop();
bufferEnumerable = buffer.GetEnumerator();
Console.Print($"Demo parse time {sw.Elapsed.TotalMilliseconds}ms, frames: {buffer.Count} ");
OnEndFrame(); // advances to first frame
}
public override void OnEndFrame()
{
// TODO: check if the current state frame matches the current frame number before advancing
/*asdf++;
if (asdf < 8)
return;*/
if (bufferEnumerable == null || !bufferEnumerable.MoveNext())
{
if (buffer.Any())
{
bufferEnumerable.Dispose();
bufferEnumerable = null;
buffer.Clear();
Console.Print("Demo ended");
}
return;
}
//var actorState = currentState.actor;
currentState.input = bufferEnumerable.Current;
//frame++;
//currentState.actor = actorState;
}
}

View File

@@ -9,142 +9,141 @@ using FlaxEngine;
using FlaxEngine.Networking;
using Console = Game.Console;
namespace Game
namespace Game;
public class PlayerInputLocal : PlayerInput
{
public class PlayerInputLocal : PlayerInput
protected List<PlayerInputState> buffer = new List<PlayerInputState>();
protected GZipStream demoStream;
protected FileStream demoFileStream;
private PlayerActor playerActor;
public bool IsNetworked => NetworkManager.client != null;
private long flushedFrames = 0;
/*public PlayerInputLocal()
{
protected List<PlayerInputState> buffer = new List<PlayerInputState>();
protected GZipStream demoStream;
protected FileStream demoFileStream;
}*/
private PlayerActor playerActor;
public bool IsNetworked => NetworkManager.client != null;
private long flushedFrames = 0;
public override bool Predict => true;
/*public PlayerInputLocal()
public PlayerInputLocal(PlayerActor playerActor, string demoPath = null)
{
this.playerActor = playerActor;
if (demoPath == null)
return;
var demoFolder = Directory.GetParent(demoPath);
if (!demoFolder.Exists)
Directory.CreateDirectory(demoFolder.FullName);
demoFileStream = File.Open(demoPath, FileMode.Create, FileAccess.Write);
demoStream = new GZipStream(demoFileStream, CompressionMode.Compress);
demoStream.WriteByte(DemoVer);
demoStream.WriteByte((byte)Unsafe.SizeOf<PlayerInputState>());
}
public bool IsRecording => demoStream != null;
public override void OnUpdate()
{
// Collect all input here
// All axis values here should be accumulated
float sensitivity = 1.0f / 8.0f;
sensitivity = 1.0f;
//var asf = InputManager.GetAxisRaw("Mouse X");
//if (asf != 0.0f)
// Console.Print(InputManager.GetAxisRaw("Mouse X").ToString("G9", System.Globalization.CultureInfo.InvariantCulture));
currentState.input.viewDeltaX += InputManager.GetAxisRaw("Mouse X") * sensitivity;
currentState.input.viewDeltaY += InputManager.GetAxisRaw("Mouse Y") * sensitivity;
currentState.input.viewDeltaX += InputManager.GetAxisRaw("LookRight") * Time.DeltaTime * 100;
currentState.input.viewDeltaY += -InputManager.GetAxisRaw("LookUp") * Time.DeltaTime * 100;
currentState.input.moveForward = InputManager.GetAxis("Vertical");
currentState.input.moveRight = InputManager.GetAxis("Horizontal");
currentState.input.attacking = InputManager.GetAction("Attack");
currentState.input.jumping = InputManager.GetAction("Jump");
}
public override void OnFixedUpdate()
{
}
public override void OnEndFrame()
{
currentState.input.frame = frame;
if (IsRecording)
{
}*/
currentState.input.verificationPosition = currentState.actor.position;
currentState.input.verificationVelocity = currentState.actor.velocity;
currentState.input.verificationViewAngles = currentState.actor.viewAngles;
currentState.input.verificationOrientation = currentState.actor.orientation;
public override bool Predict => true;
public PlayerInputLocal(PlayerActor playerActor, string demoPath = null)
{
this.playerActor = playerActor;
if (demoPath == null)
return;
var demoFolder = Directory.GetParent(demoPath);
if (!demoFolder.Exists)
Directory.CreateDirectory(demoFolder.FullName);
demoFileStream = File.Open(demoPath, FileMode.Create, FileAccess.Write);
demoStream = new GZipStream(demoFileStream, CompressionMode.Compress);
demoStream.WriteByte(DemoVer);
demoStream.WriteByte((byte)Unsafe.SizeOf<PlayerInputState>());
buffer.Add(currentState.input);
}
public bool IsRecording => demoStream != null;
public override void OnUpdate()
if (playerActor != null)
{
// Collect all input here
// All axis values here should be accumulated
float sensitivity = 1.0f / 8.0f;
sensitivity = 1.0f;
//var asf = InputManager.GetAxisRaw("Mouse X");
//if (asf != 0.0f)
// Console.Print(InputManager.GetAxisRaw("Mouse X").ToString("G9", System.Globalization.CultureInfo.InvariantCulture));
currentState.input.viewDeltaX += InputManager.GetAxisRaw("Mouse X") * sensitivity;
currentState.input.viewDeltaY += InputManager.GetAxisRaw("Mouse Y") * sensitivity;
currentState.input.viewDeltaX += InputManager.GetAxisRaw("LookRight") * Time.DeltaTime * 100;
currentState.input.viewDeltaY += -InputManager.GetAxisRaw("LookUp") * Time.DeltaTime * 100;
currentState.input.moveForward = InputManager.GetAxis("Vertical");
currentState.input.moveRight = InputManager.GetAxis("Horizontal");
currentState.input.attacking = InputManager.GetAction("Attack");
currentState.input.jumping = InputManager.GetAction("Jump");
}
public override void OnFixedUpdate()
{
}
public override void OnEndFrame()
{
currentState.input.frame = frame;
if (IsRecording)
{
currentState.input.verificationPosition = currentState.actor.position;
currentState.input.verificationVelocity = currentState.actor.velocity;
currentState.input.verificationViewAngles = currentState.actor.viewAngles;
currentState.input.verificationOrientation = currentState.actor.orientation;
buffer.Add(currentState.input);
}
if (playerActor != null)
{
//playerActor.UpdateNetworkInput(currentState.input.frame, new Float4(currentState.input.viewDeltaX, currentState.input.viewDeltaY, currentState.input.moveForward, currentState.input.moveRight), currentState.input.attacking, currentState.input.jumping);
}
//playerActor.UpdateNetworkInput(currentState.input.frame, currentState.input.viewDeltaX, currentState.input.viewDeltaY, currentState.input.moveForward, currentState.input.moveRight, currentState.input.attacking, currentState.input.jumping, currentState.input.verificationPosition, currentState.input.verificationVelocity, currentState.input.verificationViewAngles, currentState.input.verificationOrientation);
if (IsNetworked)
{
var message = NetworkManager.ClientBeginSendMessage();
message.WriteByte((byte)GameModeMessageType.PlayerInput);
message.WriteUInt64(currentState.input.frame);
message.WriteSingle(currentState.input.viewDeltaX);
message.WriteSingle(currentState.input.viewDeltaY);
message.WriteSingle(currentState.input.moveForward);
message.WriteSingle(currentState.input.moveRight);
message.WriteBoolean(currentState.input.attacking);
message.WriteBoolean(currentState.input.jumping);
NetworkManager.ClientEndSendMessage(ref message);
}
base.OnEndFrame();
// Reset anything accumulatable here
currentState.input.viewDeltaX = 0;
currentState.input.viewDeltaY = 0;
}
public void FlushDemo()
{
if (!IsRecording)
return;
Stopwatch sw = Stopwatch.StartNew();
Span<byte> bytes = stackalloc byte[Unsafe.SizeOf<PlayerInputState>()];
foreach (ref PlayerInputState state in CollectionsMarshal.AsSpan(buffer))
{
MemoryMarshal.Write(bytes, state);
demoStream.Write(bytes);
}
sw.Stop();
//playerActor.UpdateNetworkInput(currentState.input.frame, new Float4(currentState.input.viewDeltaX, currentState.input.viewDeltaY, currentState.input.moveForward, currentState.input.moveRight), currentState.input.attacking, currentState.input.jumping);
flushedFrames += buffer.Count;
buffer.Clear();
FlaxEngine.Debug.Write(LogType.Info, $"Wrote demo in {sw.Elapsed.TotalMilliseconds}ms, frames: {flushedFrames}");
}
//playerActor.UpdateNetworkInput(currentState.input.frame, currentState.input.viewDeltaX, currentState.input.viewDeltaY, currentState.input.moveForward, currentState.input.moveRight, currentState.input.attacking, currentState.input.jumping, currentState.input.verificationPosition, currentState.input.verificationVelocity, currentState.input.verificationViewAngles, currentState.input.verificationOrientation);
public void StopRecording()
if (IsNetworked)
{
if (!IsRecording)
return;
FlushDemo();
demoStream.Close();
demoStream = null;
demoFileStream.Close();
demoFileStream = null;
var message = NetworkManager.ClientBeginSendMessage();
message.WriteByte((byte)GameModeMessageType.PlayerInput);
message.WriteUInt64(currentState.input.frame);
message.WriteSingle(currentState.input.viewDeltaX);
message.WriteSingle(currentState.input.viewDeltaY);
message.WriteSingle(currentState.input.moveForward);
message.WriteSingle(currentState.input.moveRight);
message.WriteBoolean(currentState.input.attacking);
message.WriteBoolean(currentState.input.jumping);
NetworkManager.ClientEndSendMessage(ref message);
}
base.OnEndFrame();
// Reset anything accumulatable here
currentState.input.viewDeltaX = 0;
currentState.input.viewDeltaY = 0;
}
public void FlushDemo()
{
if (!IsRecording)
return;
Stopwatch sw = Stopwatch.StartNew();
Span<byte> bytes = stackalloc byte[Unsafe.SizeOf<PlayerInputState>()];
foreach (ref PlayerInputState state in CollectionsMarshal.AsSpan(buffer))
{
MemoryMarshal.Write(bytes, state);
demoStream.Write(bytes);
}
sw.Stop();
flushedFrames += buffer.Count;
buffer.Clear();
FlaxEngine.Debug.Write(LogType.Info, $"Wrote demo in {sw.Elapsed.TotalMilliseconds}ms, frames: {flushedFrames}");
}
public void StopRecording()
{
if (!IsRecording)
return;
FlushDemo();
demoStream.Close();
demoStream = null;
demoFileStream.Close();
demoFileStream = null;
}
}

View File

@@ -1,13 +1,12 @@
namespace Game
{
public class PlayerInputNetwork : PlayerInput
{
public override bool Predict => true;
namespace Game;
public override void OnEndFrame()
{
currentState.input.frame = frame;
base.OnEndFrame();
}
public class PlayerInputNetwork : PlayerInput
{
public override bool Predict => true;
public override void OnEndFrame()
{
currentState.input.frame = frame;
base.OnEndFrame();
}
}

File diff suppressed because it is too large Load Diff