Files
GoakeFlax/Source/Game/Player/PlayerInput2.cs
2025-03-20 23:29:47 +02:00

110 lines
3.1 KiB
C#

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;
[ConsoleVariable("sensitivity")]
private static float _sensitivity { get; set; } = 1.0f;
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 = MathF.MaxMagnitude(_recordState.MoveForward, Input.GetAxis("Vertical"));
_recordState.MoveRight = MathF.MaxMagnitude(_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
{
private uint _playerId;
private WorldStateManager _worldStateManager;
private ulong _frame;
private PlayerInputState2 _state;
public PlayerInputNetwork2(uint playerId, WorldStateManager worldStateManager)
{
_playerId = playerId;
_worldStateManager = worldStateManager;
}
public void SetFrame(ulong frame) => _frame = frame;
public void UpdateState()
{
_worldStateManager.GetPlayerInputState(_playerId, _frame, out _state);
}
public PlayerInputState2 GetState() { return default; }
public void ResetState()
{
_state = new PlayerInputState2();
}
}