_playerinput refactor wip

This commit is contained in:
2025-03-19 23:00:03 +02:00
parent e564cf36a8
commit 294138eac4
6 changed files with 251 additions and 114 deletions

View 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() { }
}