124 lines
4.0 KiB
C#
124 lines
4.0 KiB
C#
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace HeneGames.Airplane
|
|
{
|
|
/// <summary>
|
|
/// Reads flight controls via the Input System (works when the project uses the new Input System).
|
|
/// Falls back to legacy Input when the old backend is enabled.
|
|
/// </summary>
|
|
internal static class SimpleAirplaneInput
|
|
{
|
|
public static float Horizontal => ReadAxis("Horizontal", ReadHorizontalKeyboard);
|
|
|
|
public static float Vertical => ReadAxis("Vertical", ReadVerticalKeyboard);
|
|
|
|
public static bool YawLeft => IsPressed(Key.Q, KeyCode.Q);
|
|
|
|
public static bool YawRight => IsPressed(Key.E, KeyCode.E);
|
|
|
|
public static bool Turbo => IsPressed(Key.LeftShift, KeyCode.LeftShift);
|
|
|
|
public static bool GetKeyDown(KeyCode keyCode)
|
|
{
|
|
var keyboard = Keyboard.current;
|
|
if (keyboard != null && TryMapKey(keyCode, out Key key))
|
|
return keyboard[key].wasPressedThisFrame;
|
|
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
|
return Input.GetKeyDown(keyCode);
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
|
|
public static bool GetKey(KeyCode keyCode)
|
|
{
|
|
var keyboard = Keyboard.current;
|
|
if (keyboard != null && TryMapKey(keyCode, out Key key))
|
|
return keyboard[key].isPressed;
|
|
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
|
return Input.GetKey(keyCode);
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
|
|
private static float ReadAxis(string axisName, System.Func<float> readKeyboard)
|
|
{
|
|
var keyboard = Keyboard.current;
|
|
if (keyboard != null)
|
|
{
|
|
float value = readKeyboard();
|
|
if (value != 0f)
|
|
return value;
|
|
}
|
|
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
|
return Input.GetAxisRaw(axisName);
|
|
#else
|
|
return 0f;
|
|
#endif
|
|
}
|
|
|
|
private static float ReadHorizontalKeyboard()
|
|
{
|
|
var keyboard = Keyboard.current;
|
|
float value = 0f;
|
|
if (keyboard.aKey.isPressed || keyboard.leftArrowKey.isPressed)
|
|
value -= 1f;
|
|
if (keyboard.dKey.isPressed || keyboard.rightArrowKey.isPressed)
|
|
value += 1f;
|
|
return value;
|
|
}
|
|
|
|
private static float ReadVerticalKeyboard()
|
|
{
|
|
var keyboard = Keyboard.current;
|
|
float value = 0f;
|
|
if (keyboard.sKey.isPressed || keyboard.downArrowKey.isPressed)
|
|
value -= 1f;
|
|
if (keyboard.wKey.isPressed || keyboard.upArrowKey.isPressed)
|
|
value += 1f;
|
|
return value;
|
|
}
|
|
|
|
private static bool IsPressed(Key inputSystemKey, KeyCode legacyKey)
|
|
{
|
|
var keyboard = Keyboard.current;
|
|
if (keyboard != null && keyboard[inputSystemKey].isPressed)
|
|
return true;
|
|
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
|
return Input.GetKey(legacyKey);
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
|
|
private static bool TryMapKey(KeyCode keyCode, out Key key)
|
|
{
|
|
switch (keyCode)
|
|
{
|
|
case KeyCode.Space: key = Key.Space; return true;
|
|
case KeyCode.LeftShift: key = Key.LeftShift; return true;
|
|
case KeyCode.RightShift: key = Key.RightShift; return true;
|
|
case KeyCode.Q: key = Key.Q; return true;
|
|
case KeyCode.E: key = Key.E; return true;
|
|
case KeyCode.W: key = Key.W; return true;
|
|
case KeyCode.A: key = Key.A; return true;
|
|
case KeyCode.S: key = Key.S; return true;
|
|
case KeyCode.D: key = Key.D; return true;
|
|
case KeyCode.UpArrow: key = Key.UpArrow; return true;
|
|
case KeyCode.DownArrow: key = Key.DownArrow; return true;
|
|
case KeyCode.LeftArrow: key = Key.LeftArrow; return true;
|
|
case KeyCode.RightArrow: key = Key.RightArrow; return true;
|
|
default:
|
|
key = Key.None;
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|