major rework
This commit is contained in:
83
Car simulation/Inputs/CarInputHandler.cs
Normal file
83
Car simulation/Inputs/CarInputHandler.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using SFML.Window;
|
||||
|
||||
namespace Car_simulation.Input
|
||||
{
|
||||
public class CarInputHandler
|
||||
{
|
||||
private Dictionary<Keyboard.Key, bool> _previousKeyStates = new();
|
||||
private Dictionary<Keyboard.Key, bool> _currentKeyStates = new();
|
||||
|
||||
public CarInputState InputState { get; private set; } = new CarInputState();
|
||||
|
||||
public CarInputHandler()
|
||||
{
|
||||
InitializeTrackedKeys();
|
||||
}
|
||||
|
||||
private void InitializeTrackedKeys()
|
||||
{
|
||||
var keys = new[] {
|
||||
Keyboard.Key.W, Keyboard.Key.S,
|
||||
Keyboard.Key.Down, Keyboard.Key.Up, Keyboard.Key.Space,
|
||||
Keyboard.Key.Escape,
|
||||
Keyboard.Key.Right, Keyboard.Key.Left
|
||||
};
|
||||
|
||||
foreach (var key in keys)
|
||||
{
|
||||
_previousKeyStates[key] = false;
|
||||
_currentKeyStates[key] = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnKeyPressed(KeyEventArgs e)
|
||||
{
|
||||
if (_currentKeyStates.ContainsKey(e.Code))
|
||||
_currentKeyStates[e.Code] = true;
|
||||
}
|
||||
|
||||
public void OnKeyReleased(KeyEventArgs e)
|
||||
{
|
||||
if (_currentKeyStates.ContainsKey(e.Code))
|
||||
_currentKeyStates[e.Code] = false;
|
||||
}
|
||||
|
||||
public void ProcessInput(float deltaTime)
|
||||
{
|
||||
// Update input state based on key states
|
||||
InputState.ThrottleChange = _currentKeyStates[Keyboard.Key.W] ? deltaTime * 4f : -deltaTime * 8f;
|
||||
InputState.BrakeChange = _currentKeyStates[Keyboard.Key.S] ? deltaTime * 4f : -deltaTime * 8f;
|
||||
|
||||
InputState.ClutchUp = _currentKeyStates[Keyboard.Key.Up];
|
||||
InputState.ClutchDown = _currentKeyStates[Keyboard.Key.Down];
|
||||
|
||||
InputState.GearUp = _currentKeyStates[Keyboard.Key.Right] && !_previousKeyStates[Keyboard.Key.Right];
|
||||
InputState.GearDown = _currentKeyStates[Keyboard.Key.Left] && !_previousKeyStates[Keyboard.Key.Left];
|
||||
|
||||
InputState.ToggleForceClutch = _currentKeyStates[Keyboard.Key.Space] && !_previousKeyStates[Keyboard.Key.Space];
|
||||
InputState.Quit = _currentKeyStates[Keyboard.Key.Escape];
|
||||
|
||||
UpdatePreviousKeyStates();
|
||||
}
|
||||
|
||||
private void UpdatePreviousKeyStates()
|
||||
{
|
||||
foreach (var key in _currentKeyStates.Keys.ToList())
|
||||
{
|
||||
_previousKeyStates[key] = _currentKeyStates[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class CarInputState
|
||||
{
|
||||
public float ThrottleChange { get; set; }
|
||||
public float BrakeChange { get; set; }
|
||||
public bool ClutchUp { get; set; }
|
||||
public bool ClutchDown { get; set; }
|
||||
public bool GearUp { get; set; }
|
||||
public bool GearDown { get; set; }
|
||||
public bool ToggleForceClutch { get; set; }
|
||||
public bool Quit { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user