153 lines
4.6 KiB
C#
153 lines
4.6 KiB
C#
using System.Runtime.InteropServices;
|
|
using FlaxEngine;
|
|
using FlaxEngine.Networking;
|
|
|
|
namespace Game
|
|
{
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public struct PlayerInputState
|
|
{
|
|
/*static PlayerInputState()
|
|
{
|
|
NetworkReplicator.AddSerializer(typeof(PlayerInputState),
|
|
(ptr, streamPtr) =>
|
|
{
|
|
|
|
},
|
|
(ptr, streamPtr) =>
|
|
{
|
|
|
|
});
|
|
}*/
|
|
|
|
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;
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public struct PlayerState
|
|
{
|
|
public PlayerInputState input;
|
|
public PlayerActorState actor;
|
|
}
|
|
|
|
public class PlayerInput
|
|
{
|
|
public const byte DemoVer = 1;
|
|
public PlayerState currentState;
|
|
public ulong frame;
|
|
//public ulong oldestFrame;
|
|
|
|
private PlayerState[] states = new PlayerState[120];
|
|
|
|
public virtual bool Predict => false;
|
|
|
|
public virtual void OnUpdate()
|
|
{
|
|
}
|
|
|
|
public virtual void OnFixedUpdate()
|
|
{
|
|
}
|
|
|
|
public virtual void OnEndFrame()
|
|
{
|
|
states[frame % 120] = 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++;
|
|
}
|
|
|
|
public virtual void RecordCurrentActorState(PlayerActorState actorState)
|
|
{
|
|
}
|
|
|
|
public bool GetState(ulong frame, out PlayerInputState inputState, out PlayerActorState actorState)
|
|
{
|
|
int frameIndex = (int)frame % 120;
|
|
if (states[frameIndex].input.frame != frame)
|
|
{
|
|
inputState = default;
|
|
actorState = default;
|
|
return false;
|
|
}
|
|
|
|
inputState = states[frameIndex].input;
|
|
actorState = states[frameIndex].actor;
|
|
return true;
|
|
}
|
|
|
|
public void SetState(ulong frame, ref PlayerInputState inputState, ref PlayerActorState actorState)
|
|
{
|
|
int frameIndex = (int)frame % 120;
|
|
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;
|
|
}
|
|
}
|
|
} |