_playerinput refactor wip
This commit is contained in:
@@ -104,7 +104,8 @@ public class PlayerActor : RigidBody//, INetworkSerializable
|
||||
|
||||
PlayerId = playerId;
|
||||
playerMovement.SetInput(playerId);
|
||||
if (playerId == NetworkManager.LocalPlayerClientId)
|
||||
bool isServerScene = Scene.Name == "ServerScene";
|
||||
if (playerId == NetworkManager.LocalPlayerClientId && !isServerScene)
|
||||
{
|
||||
FindActor("CameraHolder").IsActive = true;
|
||||
//FindActor("ViewModelHolder").IsActive = true;
|
||||
@@ -120,12 +121,12 @@ public class PlayerActor : RigidBody//, INetworkSerializable
|
||||
//[NetworkRpc(server: true)]
|
||||
public void UpdateNetworkInput(PlayerInputState inputState/*, Float4 viewDeltaXYMoveForwardRight, bool attacking, bool jumping*/)
|
||||
{
|
||||
if (playerMovement.input is not PlayerInputNetwork playerInputNetwork)
|
||||
if (playerMovement.Input is not PlayerInputNetwork2 playerInputNetwork)
|
||||
return;
|
||||
|
||||
//PlayerInputState inputState = new PlayerInputState(frame, viewDeltaXYMoveForwardRight.X, viewDeltaXYMoveForwardRight.Y, viewDeltaXYMoveForwardRight.Z, viewDeltaXYMoveForwardRight.W, attacking, jumping);
|
||||
//playerInputNetwork.currentState.input = inputState;
|
||||
playerInputNetwork.SetState(inputState.frame, inputState);
|
||||
//playerInputNetwork.SetState(inputState.frame, inputState);
|
||||
}
|
||||
|
||||
public void SetPosition(Float3 newPosition)
|
||||
|
||||
87
Source/Game/Player/PlayerInput2.cs
Normal file
87
Source/Game/Player/PlayerInput2.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using FlaxEngine;
|
||||
|
||||
namespace Game;
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct PlayerInputState2
|
||||
{
|
||||
//public ulong frame;
|
||||
public Float2 ViewDelta;
|
||||
public float MoveForward;
|
||||
public float MoveRight;
|
||||
public bool Attack;
|
||||
public bool Jump;
|
||||
}
|
||||
|
||||
public interface IPlayerInput
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets the frame number for current frame.
|
||||
/// </summary>
|
||||
/// <param name="frame">The frame number</param>
|
||||
void SetFrame(ulong frame);
|
||||
|
||||
/// <summary>
|
||||
/// Updates the current state, gathering the latest input.
|
||||
/// </summary>
|
||||
void UpdateState();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current input state of the current frame.
|
||||
/// </summary>
|
||||
PlayerInputState2 GetState();
|
||||
|
||||
/// <summary>
|
||||
/// Resets the input state for a new frame.
|
||||
/// </summary>
|
||||
void ResetState();
|
||||
}
|
||||
|
||||
public class PlayerInput2 : IPlayerInput
|
||||
{
|
||||
/// <summary>
|
||||
/// The state currently being recorded, may get modified during multiple in-flight frames.
|
||||
/// </summary>
|
||||
private PlayerInputState2 _recordState;
|
||||
|
||||
public void SetFrame(ulong frame)
|
||||
{
|
||||
}
|
||||
|
||||
public void UpdateState()
|
||||
{
|
||||
// Axis values should be either accumulated (view delta)
|
||||
// or taken with the peak values (axis direction),
|
||||
// binary values should be OR'ed so triggered action is not lost if button debounces mid-frame.
|
||||
|
||||
float sensitivity = 1.0f / 8.0f;
|
||||
sensitivity = 1.0f;
|
||||
float turnSpeed = 100.0f;
|
||||
|
||||
_recordState.ViewDelta += new Float2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")) * sensitivity;
|
||||
_recordState.ViewDelta += new Float2(Input.GetAxisRaw("LookRight"), Input.GetAxisRaw("LookUp")) * Time.DeltaTime * turnSpeed;
|
||||
|
||||
_recordState.MoveForward = Math.Max(_recordState.MoveForward, Input.GetAxis("Vertical"));
|
||||
_recordState.MoveRight = Math.Max(_recordState.MoveRight, Input.GetAxis("Horizontal"));
|
||||
_recordState.Attack |= Input.GetAction("Attack");
|
||||
_recordState.Jump |= Input.GetAction("Jump");
|
||||
}
|
||||
|
||||
public PlayerInputState2 GetState() => _recordState;
|
||||
|
||||
public void ResetState() => _recordState = new PlayerInputState2();
|
||||
}
|
||||
|
||||
public class PlayerInputNetwork2 : IPlayerInput
|
||||
{
|
||||
public void SetFrame(ulong frame) { }
|
||||
public void UpdateState() { }
|
||||
public PlayerInputState2 GetState() { return default; }
|
||||
public void ResetState() { }
|
||||
}
|
||||
@@ -10,6 +10,7 @@ using Console = Game.Console;
|
||||
|
||||
namespace Game;
|
||||
|
||||
#if false
|
||||
public class PlayerInputDemo : PlayerInput
|
||||
{
|
||||
protected List<PlayerInputState> buffer = new List<PlayerInputState>();
|
||||
@@ -98,4 +99,5 @@ public class PlayerInputDemo : PlayerInput
|
||||
//frame++;
|
||||
//currentState.actor = actorState;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,5 +1,6 @@
|
||||
namespace Game;
|
||||
|
||||
#if false
|
||||
public class PlayerInputNetwork : PlayerInput
|
||||
{
|
||||
public override bool Predict => true;
|
||||
@@ -9,4 +10,5 @@ public class PlayerInputNetwork : PlayerInput
|
||||
currentState.input.frame = frame;
|
||||
base.OnEndFrame();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -116,7 +116,7 @@ public class PlayerMovement : Script
|
||||
//private int currentInputFrame2;
|
||||
|
||||
|
||||
public PlayerInput input;
|
||||
public IPlayerInput Input;
|
||||
|
||||
//private bool physicsInteractions = false;
|
||||
|
||||
@@ -183,36 +183,39 @@ public class PlayerMovement : Script
|
||||
Assert.IsTrue(playerId != uint.MaxValue);
|
||||
|
||||
PlayerId = playerId;
|
||||
if (PlayerId == NetworkManager.LocalPlayerClientId)//if (NetworkReplicator.GetObjectRole(this.Parent) == NetworkObjectRole.OwnedAuthoritative)// if (playerId == NetworkManager.LocalPlayerClientId)
|
||||
bool isServerScene = Scene.Name == "ServerScene";
|
||||
if (PlayerId == NetworkManager.LocalPlayerClientId && !isServerScene)//if (NetworkReplicator.GetObjectRole(this.Parent) == NetworkObjectRole.OwnedAuthoritative)// if (playerId == NetworkManager.LocalPlayerClientId)
|
||||
{
|
||||
Console.Print("local player?: " + playerId.ToString());
|
||||
//string demoPath = System.IO.Path.Combine(AssetManager.DemoPath, $"{DateTimeOffset.Now.UtcTicks}.gdem");
|
||||
//input = new PlayerInputLocal(playerActor, demoPath); // TODO: support recording
|
||||
input = new PlayerInputLocal(playerActor);
|
||||
Input = new PlayerInput2();//new PlayerInputLocal(playerActor);
|
||||
worldStateManager = NetworkManager.serverWorldStateManager;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.Print("network player: " + playerId.ToString());
|
||||
input = new PlayerInputNetwork();
|
||||
Input = new PlayerInputNetwork2();
|
||||
worldStateManager = NetworkManager.clientWorldStateManager;
|
||||
}
|
||||
Assert.IsTrue(worldStateManager != null);
|
||||
}
|
||||
|
||||
#if false
|
||||
public void SetInput(string demoFile)
|
||||
{
|
||||
input = new PlayerInputDemo(demoFile);
|
||||
Input = new PlayerInputDemo(demoFile);
|
||||
|
||||
/*movementState.position = input.GetCurrentActorState().position;
|
||||
currentVelocity = input.GetCurrentActorState().velocity; f
|
||||
SetCameraEulerAngles(input.GetCurrentActorState().viewAngles);*/
|
||||
movementState.position = input.GetCurrentInputState().verificationPosition;
|
||||
movementState.position = Input.GetCurrentInputState().verificationPosition;
|
||||
Actor.Position = movementState.position;
|
||||
//rootActor.Orientation = input.GetCurrentInputState().verificationOrientation;
|
||||
movementState.currentVelocity = input.GetCurrentInputState().verificationVelocity;
|
||||
SetCameraEulerAngles(input.GetCurrentInputState().verificationViewAngles);
|
||||
movementState.currentVelocity = Input.GetCurrentInputState().verificationVelocity;
|
||||
SetCameraEulerAngles(Input.GetCurrentInputState().verificationViewAngles);
|
||||
}
|
||||
#endif
|
||||
|
||||
public override void OnEnable()
|
||||
{
|
||||
@@ -225,8 +228,8 @@ public class PlayerMovement : Script
|
||||
{
|
||||
base.OnDisable();
|
||||
|
||||
if (input != null && input is PlayerInputLocal) // FIXME
|
||||
(input as PlayerInputLocal).StopRecording();
|
||||
if (Input != null && Input is PlayerInputLocal) // FIXME
|
||||
(Input as PlayerInputLocal).StopRecording();
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(PhysicsColliderActor colliderActor)
|
||||
@@ -275,10 +278,10 @@ public class PlayerMovement : Script
|
||||
//input.OnUpdate();
|
||||
|
||||
|
||||
if (input is PlayerInputDemo /*&& currentInputFrame2 >= currentInputFrame*/)
|
||||
return;
|
||||
//if (Input is PlayerInputDemo /*&& currentInputFrame2 >= currentInputFrame*/)
|
||||
// return;
|
||||
|
||||
input.OnUpdate();
|
||||
Input.UpdateState();
|
||||
|
||||
/*if (input.frame > 0)
|
||||
{
|
||||
@@ -292,10 +295,10 @@ public class PlayerMovement : Script
|
||||
|
||||
viewAngles = viewAnglesLastFrame;
|
||||
|
||||
if (input is not PlayerInputNetwork)
|
||||
//if (Input is not PlayerInputNetwork2)
|
||||
{
|
||||
|
||||
PlayerInputState inputState = input.GetCurrentInputState();
|
||||
PlayerInputState2 inputState = Input.GetState();
|
||||
|
||||
//if (inputState.viewDeltaX != 0 || inputState.viewDeltaY != 0)
|
||||
// inputState = inputState;
|
||||
@@ -303,7 +306,7 @@ public class PlayerMovement : Script
|
||||
|
||||
ApplyInputToCamera(inputState);
|
||||
|
||||
input.RecordCurrentActorState(new PlayerActorState
|
||||
/*Input.RecordCurrentActorState(new PlayerActorState
|
||||
{
|
||||
position = movementState.position,
|
||||
velocity = movementState.currentVelocity,
|
||||
@@ -313,7 +316,7 @@ public class PlayerMovement : Script
|
||||
numJumps = movementState.numJumps,
|
||||
jumped = movementState.jumped,
|
||||
//viewAngles = new Float3(viewAngles.Y, viewAngles.X, viewAngles.Z)
|
||||
});
|
||||
});*/
|
||||
}
|
||||
|
||||
/*input.RecordCurrentActorState(new PlayerActorState()
|
||||
@@ -334,10 +337,10 @@ public class PlayerMovement : Script
|
||||
if (Time.PhysicsFPS > 0 && Math.Abs(timeDeltaDiff) > 0.0001f)
|
||||
Console.Print("Time.DeltaTime is not stable: " + timeDeltaDiff);
|
||||
|
||||
input.OnFixedUpdate();
|
||||
PlayerInputState inputState = input.GetCurrentInputState();
|
||||
//Input.OnFixedUpdate();
|
||||
PlayerInputState2 inputState = Input.GetState();
|
||||
|
||||
if (input is PlayerInputNetwork)
|
||||
if (Input is PlayerInputNetwork2)
|
||||
{
|
||||
#if false
|
||||
bool canpredict = true;
|
||||
@@ -447,12 +450,13 @@ public class PlayerMovement : Script
|
||||
|
||||
//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++)
|
||||
{
|
||||
if (!input.GetState(currentFrame, out var pastInputState, out var pastActorState))
|
||||
if (!worldStateManager.HasPlayerFrame(PlayerId, currentFrame))
|
||||
//if (!Input.GetState(currentFrame, out var pastInputState, out var pastActorState))
|
||||
{
|
||||
//Console.Print($"not predicting");
|
||||
canpredict = false;
|
||||
@@ -475,24 +479,26 @@ public class PlayerMovement : Script
|
||||
currentFrame = worldStateManager.ServerFrame;
|
||||
for (; currentFrame < lastFrame; currentFrame++)
|
||||
{
|
||||
if (!input.GetState(currentFrame, out var pastInputState, out var pastActorState))
|
||||
//if (!Input.GetState(currentFrame, out var pastInputState, out var pastActorState))
|
||||
var frameInfo = worldStateManager.GetPlayerFrame(PlayerId, currentFrame);
|
||||
if (frameInfo == null)
|
||||
{
|
||||
Console.Print($"unexpected predict failure: {currentFrame}");
|
||||
break;
|
||||
}
|
||||
|
||||
if (currentFrame == inputState.frame)
|
||||
movementState.jumped = movementState.jumped;
|
||||
//if (currentFrame == inputState.frame)
|
||||
// movementState.jumped = movementState.jumped;
|
||||
|
||||
if (currentFrame == worldStateManager.ServerFrame)
|
||||
{
|
||||
movementState.position = pastActorState.position;
|
||||
movementState.currentVelocity = pastActorState.velocity;
|
||||
movementState.lastJumped = pastActorState.lastJumpTime;
|
||||
movementState.numJumps = pastActorState.numJumps;
|
||||
movementState.jumped = pastActorState.jumped;
|
||||
movementState.position = frameInfo.movementState.position;
|
||||
movementState.currentVelocity = frameInfo.movementState.currentVelocity;
|
||||
movementState.lastJumped = frameInfo.movementState.lastJumped;
|
||||
movementState.numJumps = frameInfo.movementState.numJumps;
|
||||
movementState.jumped = frameInfo.movementState.jumped;
|
||||
Actor.Position = movementState.position;
|
||||
SetCameraEulerAngles(pastActorState.viewAngles, true);
|
||||
SetCameraEulerAngles(frameInfo.movementState.viewAngles, true);
|
||||
//cameraHolder.Orientation = Quaternion.Euler(pastActorState.viewAngles.Y, pastActorState.viewAngles.X, pastActorState.viewAngles.Z);
|
||||
//ApplyInputToCamera(pastInputState, true);
|
||||
|
||||
@@ -508,7 +514,7 @@ public class PlayerMovement : Script
|
||||
//viewAngles = new Float3(pastActorState.viewAngles.Y, pastActorState.viewAngles.X, pastActorState.viewAngles.Z);
|
||||
}
|
||||
else
|
||||
ApplyInputToCamera(pastInputState, true);
|
||||
ApplyInputToCamera(frameInfo.inputState, true);
|
||||
//SetCameraEulerAngles(pastActorState.viewAngles, true);
|
||||
|
||||
//if (currentVelocity.Length > 0)
|
||||
@@ -516,7 +522,7 @@ public class PlayerMovement : Script
|
||||
|
||||
//else
|
||||
// ApplyInputToCamera(pastInputState, true);
|
||||
SimulatePlayerMovement(pastInputState);
|
||||
SimulatePlayerMovement(frameInfo.inputState, frameInfo.frame);
|
||||
|
||||
/*if ((movementState.position - pastActorState.position).Length > 0.001)
|
||||
Console.Print($"mispredicted position");
|
||||
@@ -551,7 +557,7 @@ public class PlayerMovement : Script
|
||||
//rootActor.Orientation = Quaternion.Euler(0, viewAngles.X, 0);
|
||||
cameraHolder.Orientation = Quaternion.Euler(viewAngles.Y, viewAngles.X, viewAngles.Z);
|
||||
|
||||
SimulatePlayerMovement(inputState);
|
||||
SimulatePlayerMovement(inputState, worldStateManager.Frame); // MAYBE?
|
||||
}
|
||||
|
||||
var viewDelta = (viewAngles - oldAngles);
|
||||
@@ -573,7 +579,7 @@ public class PlayerMovement : Script
|
||||
{
|
||||
SetCameraEulerAngles(viewAnglesLastFrame, true);
|
||||
ApplyInputToCamera(inputState, true);
|
||||
SimulatePlayerMovement(inputState);
|
||||
SimulatePlayerMovement(inputState, worldStateManager.Frame); // MAYBE?
|
||||
}
|
||||
|
||||
if (movementState.position.Length < 0.1)
|
||||
@@ -600,7 +606,7 @@ public class PlayerMovement : Script
|
||||
//SetCameraEulerAngles(new Float3(viewAnglesLastFrame.Y, viewAnglesLastFrame.X, viewAnglesLastFrame.Z), true);
|
||||
//SimulatePlayerMovement(inputState);
|
||||
|
||||
input.RecordCurrentActorState(new PlayerActorState
|
||||
/*Input.RecordCurrentActorState(new PlayerActorState
|
||||
{
|
||||
position = movementState.position,
|
||||
velocity = movementState.currentVelocity,
|
||||
@@ -610,16 +616,17 @@ public class PlayerMovement : Script
|
||||
numJumps = movementState.numJumps,
|
||||
jumped = movementState.jumped,
|
||||
//viewAngles = new Float3(viewAngles.Y, viewAngles.X, viewAngles.Z)
|
||||
});
|
||||
});*/
|
||||
|
||||
//Console.Print($"recording frame {input.frame}, client: {GameModeManager.ClientFrame}, server: {GameModeManager.ServerFrame}");
|
||||
input.OnEndFrame();
|
||||
worldStateManager.RecordPlayerInput(PlayerId, worldStateManager.Frame, inputState, movementState); // MAYBE?
|
||||
//Input.OnEndFrame();
|
||||
}
|
||||
|
||||
if (movementState.position.Length < 0.1)
|
||||
movementState.jumped = movementState.jumped;
|
||||
|
||||
if (input is PlayerInputNetwork)
|
||||
if (Input is PlayerInputNetwork2)
|
||||
movementState.jumped = movementState.jumped;
|
||||
|
||||
|
||||
@@ -638,13 +645,13 @@ public class PlayerMovement : Script
|
||||
}*/
|
||||
}
|
||||
|
||||
public void ApplyInputToCamera(PlayerInputState inputState, bool wrapAround = true)
|
||||
public void ApplyInputToCamera(PlayerInputState2 inputState, bool wrapAround = true)
|
||||
{
|
||||
if (inputState.viewDeltaX == 0.0f && inputState.viewDeltaY == 0.0f)
|
||||
if (inputState.ViewDelta == Float2.Zero)
|
||||
return;
|
||||
|
||||
float viewPitch = Mathf.Clamp(viewAngles.Y + inputState.viewDeltaY, -90.0f, 90.0f);
|
||||
float viewYaw = viewAngles.X + inputState.viewDeltaX;
|
||||
float viewPitch = Mathf.Clamp(viewAngles.Y + inputState.ViewDelta.Y, -90.0f, 90.0f);
|
||||
float viewYaw = viewAngles.X + inputState.ViewDelta.X;
|
||||
SetCameraEulerAngles(new Float3(viewYaw, viewPitch, viewAngles.Z), wrapAround);
|
||||
}
|
||||
|
||||
@@ -894,10 +901,11 @@ public class PlayerMovement : Script
|
||||
|
||||
if (false)
|
||||
{
|
||||
#if false
|
||||
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))
|
||||
if (!Input.GetState(frame, out var pastInputState, out var pastActorState))
|
||||
continue;
|
||||
|
||||
var bbox = clientBbox;
|
||||
@@ -909,19 +917,21 @@ public class PlayerMovement : Script
|
||||
Color color = new Color(color3.X, color3.Y, color3.Z, color3.W);
|
||||
DebugDraw.DrawBox(bbox, color * 1f);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
else
|
||||
else if (worldStateManager.IsClient)
|
||||
{
|
||||
var serverBbox = boxCollider.OrientedBox.GetBoundingBox();
|
||||
if (input.GetState(worldStateManager.ServerFrame, out var serverInputState, out var serverActorState))
|
||||
serverBbox.Center = serverActorState.position;
|
||||
var frameInfo = worldStateManager.GetPlayerFrame(PlayerId, worldStateManager.ServerFrame);
|
||||
if (frameInfo != null)
|
||||
serverBbox.Center = frameInfo.movementState.position;
|
||||
|
||||
if (serverBbox.Center == clientBbox.Center)
|
||||
DebugDraw.DrawBox(clientBbox, Color.Magenta * 0.6f);
|
||||
else
|
||||
{
|
||||
DebugDraw.DrawBox(serverBbox, Color.Red * 0.6f);
|
||||
DebugDraw.DrawBox(clientBbox, Color.Blue * 0.6f);
|
||||
//DebugDraw.DrawBox(serverBbox, Color.Red * 0.6f);
|
||||
//DebugDraw.DrawBox(clientBbox, Color.Blue * 0.6f);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1160,12 +1170,12 @@ public class PlayerMovement : Script
|
||||
return slideMoveHit;
|
||||
}
|
||||
|
||||
public void SimulatePlayerMovement(PlayerInputState inputState)
|
||||
public void SimulatePlayerMovement(PlayerInputState2 inputState, ulong frame)
|
||||
{
|
||||
simulationTime = worldStateManager.ClientTime + (inputState.frame * (1.0f / Time.PhysicsFPS));
|
||||
simulationTime = worldStateManager.ClientTime + (frame * (1.0f / Time.PhysicsFPS));
|
||||
|
||||
Vector3 inputDirection =
|
||||
new Float3(inputState.moveRight, 0.0f, inputState.moveForward);
|
||||
new Float3(inputState.MoveRight, 0.0f, inputState.MoveForward);
|
||||
Vector3 moveDirection = rootActor.Transform.TransformDirection(inputDirection);
|
||||
Vector3 position = movementState.position;
|
||||
Vector3 velocity = movementState.currentVelocity; //rigidBody.LinearVelocity;
|
||||
@@ -1180,7 +1190,7 @@ public class PlayerMovement : Script
|
||||
|
||||
TraceInfo traceGround = CategorizePosition(position, ref velocity);
|
||||
|
||||
bool jumpAction = inputState.jumping;
|
||||
bool jumpAction = inputState.Jump;
|
||||
|
||||
if (movementState.jumped && !jumpAction)
|
||||
movementState.jumped = false; // jump released
|
||||
|
||||
Reference in New Issue
Block a user