Swapping input systems and using a class called InputData to store player's input
This commit is contained in:
25
Assets/Scripts/Game/Defines.cs
Normal file
25
Assets/Scripts/Game/Defines.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Defines
|
||||
{
|
||||
public class Input
|
||||
{
|
||||
public const int kLeftMouseButton = 0;
|
||||
public const int kRightMouseButton = 1;
|
||||
public const int kMiddleMouseButton = 2;
|
||||
|
||||
public const float kShortClickDelay = 0.25f;
|
||||
public const float kShortClickCancelMinMouseMovement = 10.0f;
|
||||
|
||||
public const float kHeldClickDelay = 0.3f;
|
||||
|
||||
public const KeyCode rotateLeftKey = KeyCode.R;
|
||||
public const KeyCode rotateRightKey = KeyCode.T;
|
||||
public const KeyCode kCancelKeyCode = KeyCode.Escape;
|
||||
|
||||
public const CursorLockMode kDefaultCursorLockMode = CursorLockMode.Confined;
|
||||
|
||||
public const float kMouseRotationDistance = 100.0f;
|
||||
public const float kDoubleClickThreshold = 0.5f;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Game/Defines.cs.meta
Normal file
2
Assets/Scripts/Game/Defines.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 87ada8c9eefa71f47aefa0442b6e990f
|
||||
8
Assets/Scripts/Game/Input.meta
Normal file
8
Assets/Scripts/Game/Input.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d14cfa81116a2f342bb961c77495567a
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
474
Assets/Scripts/Game/Input/InputData.cs
Normal file
474
Assets/Scripts/Game/Input/InputData.cs
Normal file
@@ -0,0 +1,474 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
public static class InputData
|
||||
{
|
||||
public const float kDoubleClickThreshold = 0.5f;
|
||||
|
||||
public static Vector2 mousePosition;
|
||||
public static Vector2 mouseDelta;
|
||||
public static float smoothScrollDelta;
|
||||
public static float scrollDelta;
|
||||
|
||||
public static bool mouseOverUI
|
||||
{
|
||||
get; private set;
|
||||
}
|
||||
|
||||
public static MouseButton leftMouseButton;
|
||||
public static MouseButton middleMouseButton;
|
||||
public static MouseButton rightMouseButton;
|
||||
|
||||
public static Button up;
|
||||
public static Button down;
|
||||
public static Button debugButton;
|
||||
public static Button agentDebugButton;
|
||||
|
||||
public static Button rotateLeft;
|
||||
public static Button rotateRight;
|
||||
|
||||
public static Button cancel;
|
||||
|
||||
public static Button floorUp;
|
||||
public static Button floorDown;
|
||||
|
||||
public static Button cameraLeft;
|
||||
public static Button cameraRight;
|
||||
public static Button cameraUp;
|
||||
public static Button cameraDown;
|
||||
public static Button cameraRotateLeft;
|
||||
public static Button cameraRotateRight;
|
||||
public static Button cameraSpeedBoost;
|
||||
|
||||
public static Button pauseTime;
|
||||
|
||||
public static Button pauseGame;
|
||||
|
||||
public static Axis cameraHorizontal;
|
||||
public static Axis cameraVertical;
|
||||
|
||||
#if MARKETING_BUILD
|
||||
|
||||
public static Axis fovAxis;
|
||||
|
||||
public static Axis cameraUpDownAxis;
|
||||
public static Axis marketingCameraX;
|
||||
public static Axis marketingCameraY;
|
||||
|
||||
#endif
|
||||
|
||||
private static List<Button> allButtons = new List<Button>();
|
||||
private static List<Axis> allAxis = new List<Axis>();
|
||||
|
||||
private static float storedScrollDelta;
|
||||
|
||||
private static InputConfig inputConfig = new InputConfig();
|
||||
|
||||
static InputData()
|
||||
{
|
||||
leftMouseButton = AddButton( new MouseButton( Defines.Input.kLeftMouseButton ) );
|
||||
middleMouseButton = AddButton( new MouseButton( Defines.Input.kMiddleMouseButton ) );
|
||||
rightMouseButton = AddButton( new MouseButton( Defines.Input.kRightMouseButton ) );
|
||||
|
||||
up = AddButton( new KeyboardButton( KeyCode.UpArrow ) );
|
||||
down = AddButton( new KeyboardButton( KeyCode.DownArrow ) );
|
||||
|
||||
rotateLeft = AddButton( new KeyboardButton( Defines.Input.rotateLeftKey ) );
|
||||
rotateRight = AddButton( new KeyboardButton( Defines.Input.rotateRightKey ) );
|
||||
|
||||
cancel = AddButton( new KeyboardButton( Defines.Input.kCancelKeyCode ) );
|
||||
pauseGame = AddButton( new KeyboardButton( KeyCode.Escape ) );
|
||||
|
||||
debugButton = AddButton( new KeyboardButton( KeyCode.Space ) );
|
||||
agentDebugButton = AddButton( new KeyboardButton( KeyCode.Z ) );
|
||||
|
||||
floorUp = AddButton( new MultiButton(
|
||||
new Button[]
|
||||
{
|
||||
new KeyboardButton( KeyCode.Alpha2 ),
|
||||
new KeyboardButton( KeyCode.G )
|
||||
} ) );
|
||||
floorDown = AddButton( new MultiButton(
|
||||
new Button[]
|
||||
{
|
||||
new KeyboardButton( KeyCode.Alpha1 ),
|
||||
new KeyboardButton( KeyCode.F )
|
||||
} ) );
|
||||
|
||||
cameraLeft = AddButton( new MultiButton(
|
||||
new Button[]
|
||||
{
|
||||
new KeyboardButton( KeyCode.A ),
|
||||
new KeyboardButton( KeyCode.LeftArrow )
|
||||
} ) );
|
||||
cameraRight = AddButton( new MultiButton(
|
||||
new Button[]
|
||||
{
|
||||
new KeyboardButton( KeyCode.D ),
|
||||
new KeyboardButton( KeyCode.RightArrow )
|
||||
} ) );
|
||||
cameraUp = AddButton( new MultiButton(
|
||||
new Button[]
|
||||
{
|
||||
new KeyboardButton( KeyCode.W ),
|
||||
new KeyboardButton( KeyCode.UpArrow )
|
||||
} ) );
|
||||
cameraDown = AddButton( new MultiButton(
|
||||
new Button[]
|
||||
{
|
||||
new KeyboardButton( KeyCode.S ),
|
||||
new KeyboardButton( KeyCode.DownArrow )
|
||||
} ) );
|
||||
cameraRotateLeft = AddButton( new KeyboardButton( KeyCode.Q ) );
|
||||
cameraRotateRight = AddButton( new KeyboardButton( KeyCode.E ) );
|
||||
cameraSpeedBoost = AddButton( new KeyboardButton( KeyCode.LeftShift ) );
|
||||
|
||||
pauseTime = AddButton( new KeyboardButton( KeyCode.Space ) );
|
||||
|
||||
inputConfig.Default.Enable();
|
||||
|
||||
cameraHorizontal = AddAxis( new MultiAxis(
|
||||
new Axis[]
|
||||
{
|
||||
new InputActionAxis( inputConfig.Default.CameraHorizontal )
|
||||
} ) );
|
||||
cameraVertical = AddAxis( new MultiAxis(
|
||||
new Axis[]
|
||||
{
|
||||
new InputActionAxis( inputConfig.Default.CameraVertical )
|
||||
} ) );
|
||||
|
||||
|
||||
#if MARKETING_BUILD
|
||||
|
||||
inputConfig.Marketing.Enable();
|
||||
fovAxis = AddAxis( new InputActionAxis( inputConfig.Marketing.CameraFOV ) );
|
||||
|
||||
cameraUpDownAxis = AddAxis( new InputActionAxis( inputConfig.Marketing.CameraUpDown ) );
|
||||
|
||||
marketingCameraX = AddAxis( new InputActionAxis( inputConfig.Marketing.CameraRotationX ) );
|
||||
marketingCameraY = AddAxis( new InputActionAxis( inputConfig.Marketing.CameraRotationY ) );
|
||||
#endif
|
||||
}
|
||||
|
||||
public static T AddButton<T>( T value ) where T : Button
|
||||
{
|
||||
allButtons.Add( value );
|
||||
return value;
|
||||
}
|
||||
|
||||
public static T AddAxis<T>( T value ) where T : Axis
|
||||
{
|
||||
allAxis.Add( value );
|
||||
return value;
|
||||
}
|
||||
|
||||
public class MouseButton : Button
|
||||
{
|
||||
public Vector2 downPosition;
|
||||
public int mouseButton;
|
||||
|
||||
public MouseButton( int button )
|
||||
{
|
||||
mouseButton = button;
|
||||
}
|
||||
|
||||
public override bool GetButton()
|
||||
{
|
||||
return Input.GetMouseButton( mouseButton );
|
||||
}
|
||||
|
||||
protected override bool HeldCancelCheck()
|
||||
{
|
||||
return ( mousePosition - downPosition ).magnitude <= Defines.Input.kShortClickCancelMinMouseMovement;
|
||||
}
|
||||
|
||||
public override void Update( float unscaledTime )
|
||||
{
|
||||
base.Update( unscaledTime );
|
||||
if( pressed )
|
||||
{
|
||||
downPosition = mousePosition;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class KeyboardButton : Button
|
||||
{
|
||||
private KeyCode key;
|
||||
|
||||
public KeyboardButton( KeyCode keycode )
|
||||
{
|
||||
key = keycode;
|
||||
}
|
||||
|
||||
public override bool GetButton()
|
||||
{
|
||||
return Input.GetKey( key );
|
||||
}
|
||||
}
|
||||
|
||||
public class MultiButton : Button
|
||||
{
|
||||
private Button[] buttons;
|
||||
|
||||
public MultiButton( Button[] value )
|
||||
{
|
||||
buttons = value;
|
||||
}
|
||||
|
||||
public override bool GetButton()
|
||||
{
|
||||
bool down = false;
|
||||
for( int i = buttons.Length - 1; i >= 0; --i )
|
||||
{
|
||||
down |= buttons[i].GetButton();
|
||||
}
|
||||
return down;
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class Button
|
||||
{
|
||||
/// <summary>
|
||||
/// true when the mouse button is down.
|
||||
/// </summary>
|
||||
public bool down;
|
||||
/// <summary>
|
||||
/// true for the frame that the mouse button pressed (On Click).
|
||||
/// </summary>
|
||||
public bool pressed;
|
||||
/// <summary>
|
||||
/// true on the frame that the mouse button has been down
|
||||
/// for X number of frames without being moved.
|
||||
/// </summary>
|
||||
public bool held;
|
||||
/// <summary>
|
||||
/// true on the frame the mouse button is released.
|
||||
/// </summary>
|
||||
public bool released;
|
||||
/// <summary>
|
||||
/// true if the mouse is clicked quickly and not moved much.
|
||||
/// example: right clicking to cancel placement of item.
|
||||
/// </summary>
|
||||
public bool shortClick;
|
||||
|
||||
/// <summary>
|
||||
/// The time that the button was presesd.
|
||||
/// </summary>
|
||||
public float downTime;
|
||||
|
||||
/// <summary>
|
||||
/// True when the user has double clicked this button.
|
||||
/// </summary>
|
||||
public bool doubleClick;
|
||||
|
||||
/// <summary>
|
||||
/// How long this button has been down for. Only valid when
|
||||
/// down == true
|
||||
/// </summary>
|
||||
public float downLength;
|
||||
|
||||
private bool isHeld;
|
||||
private float lastClickTime;
|
||||
private Vector3 lastClickPosition;
|
||||
|
||||
public abstract bool GetButton();
|
||||
/// <summary>
|
||||
/// Overload this funtion in order to have
|
||||
/// the mouse be able to cancel the held functionality
|
||||
/// if the user moves the mouse.
|
||||
/// </summary>
|
||||
/// <returns>If the held or shortclick should be cancelled</returns>
|
||||
protected virtual bool HeldCancelCheck()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual void Update( float unscaledTime )
|
||||
{
|
||||
bool value = GetButton();
|
||||
|
||||
pressed = value && down == false;
|
||||
released = value == false && down == true;
|
||||
shortClick = false;
|
||||
down = value;
|
||||
|
||||
bool isHeldValue = pressed == false && down && downTime + Defines.Input.kHeldClickDelay < unscaledTime && HeldCancelCheck();
|
||||
held = isHeld == false && isHeldValue == true; // Only show this as true for a single frame.
|
||||
isHeld = isHeldValue;
|
||||
|
||||
if( pressed )
|
||||
{
|
||||
downTime = unscaledTime;
|
||||
downLength = 0.0f;
|
||||
doubleClick = lastClickTime + kDoubleClickThreshold >= unscaledTime && Vector2.Distance( mousePosition, lastClickPosition ) < 5.0f;
|
||||
}
|
||||
|
||||
if( released )
|
||||
{
|
||||
if( unscaledTime - downTime <= Defines.Input.kShortClickDelay
|
||||
&& HeldCancelCheck() )
|
||||
{
|
||||
shortClick = true;
|
||||
}
|
||||
lastClickTime = unscaledTime;
|
||||
doubleClick = false;
|
||||
}
|
||||
|
||||
if( down )
|
||||
{
|
||||
downLength = unscaledTime - downTime;
|
||||
lastClickPosition = mousePosition;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class Axis
|
||||
{
|
||||
public float value;
|
||||
|
||||
public abstract float GetAxisValue();
|
||||
|
||||
public virtual void Update()
|
||||
{
|
||||
value = GetAxisValue();
|
||||
}
|
||||
}
|
||||
|
||||
public class InputAxis : Axis
|
||||
{
|
||||
public string axisName;
|
||||
|
||||
public InputAxis( string name )
|
||||
{
|
||||
axisName = name;
|
||||
}
|
||||
|
||||
public override float GetAxisValue()
|
||||
{
|
||||
return Input.GetAxis( axisName );
|
||||
}
|
||||
}
|
||||
|
||||
public class TriggerNormaliser : Axis
|
||||
{
|
||||
private Axis trigger;
|
||||
bool invert = false;
|
||||
|
||||
public TriggerNormaliser( Axis value, bool doInvert )
|
||||
{
|
||||
trigger = value;
|
||||
invert = doInvert;
|
||||
}
|
||||
|
||||
public override float GetAxisValue()
|
||||
{
|
||||
float value = trigger.GetAxisValue();
|
||||
if( invert == false )
|
||||
{
|
||||
value += 1.0f;
|
||||
value /= 2.0f;
|
||||
}
|
||||
value *= invert ? -1.0f : 1.0f;
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
public class InvertAxis : Axis
|
||||
{
|
||||
private Axis targetAxis;
|
||||
|
||||
public InvertAxis( Axis target )
|
||||
{
|
||||
targetAxis = target;
|
||||
}
|
||||
|
||||
public override float GetAxisValue()
|
||||
{
|
||||
return -1 * targetAxis.GetAxisValue();
|
||||
}
|
||||
}
|
||||
|
||||
public class ButtonAxis : Axis
|
||||
{
|
||||
private Button positiveButton;
|
||||
private Button negativeButton;
|
||||
|
||||
public ButtonAxis( Button negativeButton, Button positiveButton )
|
||||
{
|
||||
this.positiveButton = positiveButton;
|
||||
this.negativeButton = negativeButton;
|
||||
}
|
||||
|
||||
public override float GetAxisValue()
|
||||
{
|
||||
return ( positiveButton.down ? 1.0f : 0.0f ) + ( negativeButton.down ? -1.0f : 0.0f );
|
||||
}
|
||||
}
|
||||
|
||||
public class InputActionAxis : Axis
|
||||
{
|
||||
private InputAction action;
|
||||
|
||||
public InputActionAxis( InputAction inputAction )
|
||||
{
|
||||
action = inputAction;
|
||||
}
|
||||
|
||||
public override float GetAxisValue()
|
||||
{
|
||||
return action.ReadValue<float>();
|
||||
}
|
||||
}
|
||||
|
||||
public class MultiAxis : Axis
|
||||
{
|
||||
protected Axis[] axis;
|
||||
|
||||
public MultiAxis( Axis[] axis )
|
||||
{
|
||||
this.axis = axis;
|
||||
}
|
||||
|
||||
public override float GetAxisValue()
|
||||
{
|
||||
float value = 0.0f;
|
||||
|
||||
for( int i = 0; i < axis.Length; i++ )
|
||||
{
|
||||
value += axis[i].GetAxisValue();
|
||||
}
|
||||
|
||||
value = Mathf.Clamp( value, -1.0f, 1.0f );
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Update( float unscaledTime )
|
||||
{
|
||||
mouseDelta = mousePosition - (Vector2)Input.mousePosition;
|
||||
mousePosition = Input.mousePosition;
|
||||
//mouseOverUI = UIUtils.MouseIsOverUI();
|
||||
foreach( Button i in allButtons )
|
||||
{
|
||||
i.Update( unscaledTime );
|
||||
}
|
||||
|
||||
foreach( Axis i in allAxis )
|
||||
{
|
||||
i.Update();
|
||||
}
|
||||
|
||||
#if UNITY_STANDALONE_WIN
|
||||
scrollDelta = Input.mouseScrollDelta.y;
|
||||
#else
|
||||
scrollDelta = ( Input.GetKey( KeyCode.LeftShift ) || Input.GetKey( KeyCode.RightShift ) ) ? Input.mouseScrollDelta.x : Input.mouseScrollDelta.y;
|
||||
#endif
|
||||
storedScrollDelta += scrollDelta;
|
||||
float smoothAmount = Mathf.Lerp( 0.0f, storedScrollDelta, Time.unscaledDeltaTime * 5.0f );
|
||||
storedScrollDelta -= smoothAmount;
|
||||
smoothScrollDelta = smoothAmount;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Game/Input/InputData.cs.meta
Normal file
2
Assets/Scripts/Game/Input/InputData.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b47ddc9765face745a9c52aaf22038bc
|
||||
Reference in New Issue
Block a user