cirnogodot/Scripts/Components/Actors/KeyboardInputProvider.cs

193 lines
5.4 KiB
C#
Raw Normal View History

2025-03-22 00:33:24 +01:00
using System;
using System.Threading.Tasks;
using Cirno.Scripts.Utils;
2025-03-22 00:33:24 +01:00
using Godot;
2025-02-18 17:40:33 +01:00
namespace Cirno.Scripts.Components.Actors;
public partial class KeyboardInputProvider : InputProvider
{
2025-06-17 11:57:59 +02:00
[ExportCategory("Movement")] [Export] public StringName LeftAxisName { get; private set; } = "left";
[Export] public StringName RightAxisName { get; private set; } = "right";
[Export] public StringName UpAxisName { get; private set; } = "up";
[Export] public StringName DownAxisName { get; private set; } = "down";
[ExportCategory("Aiming")] [Export] public StringName LeftAimName { get; private set; } = "aim_left";
[Export] public StringName RightAimName { get; private set; } = "aim_right";
[Export] public StringName UpAimName { get; private set; } = "aim_up";
[Export] public StringName DownAimName { get; private set; } = "aim_down";
[ExportCategory("Action Names")] [Export]
private StringName _shootActionName = "shoot";
2025-03-02 11:58:30 +01:00
[Export] private StringName _useActionName = "Use";
[Export] private StringName _scanActionName = "scan";
[Export] private StringName _strafeActionName = "strafe";
[Export] private StringName _nextWeaponActionName = "next_weapon";
[Export] private StringName _previousWeaponActionName = "previous_weapon";
[Export] private StringName _inventoryActionName = "inventory";
2025-04-26 11:24:20 +02:00
[Export] private StringName _freezeActionName = "Freeze";
2025-05-07 21:50:00 +02:00
[Export] private StringName _reloadActionName = "Reload";
2025-02-28 22:49:55 +01:00
2025-06-17 11:57:59 +02:00
private IMouseAimProvider _mouseAImProvider;
private enum AimInputMethod
{
RightStick,
Mouse
}
2025-03-13 15:01:21 +01:00
private AimInputMethod _lastUsedInput = AimInputMethod.RightStick;
2025-03-22 00:33:24 +01:00
public override void _Ready()
{
2025-06-11 15:28:26 +02:00
2025-03-22 00:33:24 +01:00
CallDeferred(MethodName.DelayedRegisterGameManager);
}
private void DelayedRegisterGameManager()
{
2025-06-18 18:09:30 +02:00
_mouseAImProvider = this.GetParent().GetNodeOrNull<IMouseAimProvider>("MouseAimProvider");
2025-06-17 11:57:59 +02:00
if (_mouseAImProvider is null)
{
GD.Print("Mouse aim provider is null");
}
GameStateManager.Instance.GameStateChange += InstanceOnGameStateChange;
2025-06-18 18:09:30 +02:00
2025-03-22 00:33:24 +01:00
_enabled = true;
}
private bool _enabled = false;
private void InstanceOnGameStateChange(GameState state)
{
if (state is not GameState.Playing) return;
2025-06-17 11:57:59 +02:00
2025-03-22 00:33:24 +01:00
_enabled = false;
_ = DelayResume();
}
private async Task DelayResume()
{
//await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
await Task.Delay(200);
_enabled = true;
}
2025-06-17 11:57:59 +02:00
2025-02-18 17:40:33 +01:00
public override Vector2 GetMovementInput()
{
2025-02-28 22:49:55 +01:00
return Input.GetVector(LeftAxisName, RightAxisName, UpAxisName, DownAxisName);
2025-02-18 17:40:33 +01:00
}
2025-02-18 18:18:13 +01:00
public override Vector2 GetAimInput()
{
2025-06-26 17:00:49 +02:00
// TODO: Make a solution that does not rotate in 2D
Vector2 rightStickInput = GetRightStickInput().Rotated(Mathf.DegToRad(-45f));
2025-03-13 15:01:21 +01:00
Vector2 mouseInput = GetMouseAimInput();
// Determine which input method to use
if (rightStickInput != Vector2.Zero)
{
_lastUsedInput = AimInputMethod.RightStick;
}
else if (mouseInput != Vector2.Zero)
{
_lastUsedInput = AimInputMethod.Mouse;
}
return _lastUsedInput == AimInputMethod.RightStick ? rightStickInput : mouseInput;
2025-02-18 18:18:13 +01:00
}
2025-06-17 11:57:59 +02:00
2025-02-18 18:18:13 +01:00
private Vector2 GetRightStickInput()
{
2025-06-26 17:00:49 +02:00
return Input.GetVector(LeftAimName, RightAimName, UpAimName, DownAimName);
// return new Vector2(
// Input.GetAxis(LeftAimName, RightAimName),
// Input.GetAxis(UpAimName, DownAimName)
// );
2025-02-18 18:18:13 +01:00
}
2025-06-17 11:57:59 +02:00
2025-03-13 15:01:21 +01:00
private Vector2 GetMouseAimInput()
{
2025-06-17 11:57:59 +02:00
return _mouseAImProvider?.GetMouseAimInput() ?? Vector2.Zero;
2025-03-13 15:01:21 +01:00
}
2025-02-18 18:18:13 +01:00
public override bool GetActionJustPressed(string action)
{
return Input.IsActionJustPressed(action);
}
public override bool GetActionPressed(string action)
2025-02-18 18:18:13 +01:00
{
return Input.IsActionPressed(action);
2025-02-18 18:18:13 +01:00
}
2025-02-28 22:49:55 +01:00
2025-03-01 20:50:47 +01:00
public override bool GetInventoryJustPressed()
{
return GetActionJustPressed(_inventoryActionName);
}
2025-06-17 11:57:59 +02:00
2025-02-28 22:49:55 +01:00
public override bool GetShootPressed()
{
2025-03-22 00:33:24 +01:00
return _enabled && GetActionPressed(_shootActionName);
2025-02-28 22:49:55 +01:00
}
2025-06-17 11:57:59 +02:00
2025-03-01 20:50:47 +01:00
public override bool GetShootJustPressed()
{
return GetActionJustPressed(_shootActionName);
}
2025-02-28 22:49:55 +01:00
public override bool GetUseJustPressed()
{
return GetActionJustPressed(_useActionName);
}
2025-06-17 11:57:59 +02:00
2025-03-01 18:02:11 +01:00
public override bool GetScanJustPressed()
{
return GetActionJustPressed(_scanActionName);
}
2025-02-28 22:49:55 +01:00
public override bool GetStrafePressed()
{
return GetActionPressed(_strafeActionName);
}
public override bool GetWeaponNextJustPressed()
{
return GetActionJustPressed(_nextWeaponActionName);
}
public override bool GetWeaponPreviousJustPressed()
{
return GetActionJustPressed(_previousWeaponActionName);
}
2025-03-01 20:50:47 +01:00
public override bool GetPauseJustPressed()
{
return GlobalInputManager.IsPauseJustPressed();
2025-03-01 20:50:47 +01:00
}
2025-06-17 11:57:59 +02:00
2025-04-26 11:24:20 +02:00
public override bool GetFreezeJustPressed()
{
return GetActionJustPressed(_freezeActionName);
}
2025-06-17 11:57:59 +02:00
2025-04-26 11:24:20 +02:00
public override bool GetFreezePressed()
{
return GetActionPressed(_freezeActionName);
}
2025-06-17 11:57:59 +02:00
2025-05-07 21:50:00 +02:00
public override bool GetReloadJustPressed()
{
return GetActionJustPressed(_reloadActionName);
}
2025-06-17 11:57:59 +02:00
2025-05-07 21:50:00 +02:00
public override bool GetReloadPressed()
{
return GetActionPressed(_reloadActionName);
}
2025-02-18 17:40:33 +01:00
}