69 lines
1.7 KiB
C#
69 lines
1.7 KiB
C#
using System;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.InputSystem.Users;
|
|
using static InputActionMaps;
|
|
|
|
namespace ShipHandling
|
|
{
|
|
public class ShipInputHandler : IDisposable, IPlayerActions
|
|
{
|
|
private ShipInput input;
|
|
private readonly InputActionMaps inputActions = new();
|
|
private readonly string controlScheme;
|
|
private readonly InputDevice device;
|
|
public InputUser user;
|
|
public ShipInputHandler()
|
|
{
|
|
|
|
}
|
|
public ShipInputHandler(ShipInput si, InputDevice device, string controlScheme)
|
|
{
|
|
input = si;
|
|
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);
|
|
input = ship.Input;
|
|
inputActions.Player.SetCallbacks(this);
|
|
inputActions.Player.Enable();
|
|
}
|
|
|
|
public void OnThrust(InputAction.CallbackContext context)
|
|
{
|
|
input.thrustInput = context.ReadValue<float>();
|
|
}
|
|
|
|
public void OnSteer(InputAction.CallbackContext context)
|
|
{
|
|
input.steerInput = context.ReadValue<float>();
|
|
}
|
|
|
|
public void OnBoost(InputAction.CallbackContext context)
|
|
{
|
|
input.boostInput = context.ReadValue<float>();
|
|
}
|
|
public void OnShoot(InputAction.CallbackContext context)
|
|
{
|
|
input.shootInput = context.ReadValue<float>();
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
inputActions.Player.Disable();
|
|
inputActions.Player.RemoveCallbacks(this);
|
|
user.UnpairDevicesAndRemoveUser();
|
|
}
|
|
|
|
}
|
|
}
|