Space-Smash-Out/Assets/Scripts/Input/ShipInputHandler.cs

63 lines
1.6 KiB
C#

using System;
using Managers;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Users;
using static InputActionMaps;
namespace ShipHandling
{
public class ShipInputHandler : IDisposable, IPlayerActions
{
private ShipState state;
private readonly InputActionMaps inputActions = new();
private readonly string controlScheme;
private readonly InputDevice device;
public InputUser user;
public ShipInputHandler(ShipState shipState, InputDevice device, string controlScheme)
{
state = shipState;
this.controlScheme = controlScheme;
this.device = device;
user = InputUser.PerformPairingWithDevice(device);
user.AssociateActionsWithUser(inputActions);
user.ActivateControlScheme(controlScheme);
inputActions.Player.SetCallbacks(this);
inputActions.Player.Enable();
}
public void ReActivateInput(Ship ship)
{
// user = InputUser.PerformPairingWithDevice(device);
// user.AssociateActionsWithUser(inputActions);
// user.ActivateControlScheme(controlScheme);
this.state = ship.state;
inputActions.Player.SetCallbacks(this);
inputActions.Player.Enable();
}
public void OnThrust(InputAction.CallbackContext context)
{
state.thrustInput = context.ReadValue<float>();
}
public void OnSteer(InputAction.CallbackContext context)
{
state.steerInput = context.ReadValue<float>();
}
public void OnBoost(InputAction.CallbackContext context)
{
state.boostInput = context.ReadValue<float>();
}
public void Dispose()
{
inputActions.Player.Disable();
inputActions.Player.RemoveCallbacks(this);
user.UnpairDevicesAndRemoveUser();
}
}
}