Global Game Jam 2023
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
4.0 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem;
  5. using NaughtyAttributes;
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. public class HandController : MonoBehaviour
  10. {
  11. #region Inspector Fields
  12. [SerializeField, BoxGroup("References")]
  13. private Transform m_handTransform;
  14. [SerializeField, BoxGroup("References")]
  15. private OarController m_oar;
  16. [SerializeField,BoxGroup("settings")]
  17. private Transform m_otherHand;
  18. [SerializeField, BoxGroup("Settings")]
  19. private Vector3 m_axisNormal = Vector3.forward;
  20. [SerializeField, BoxGroup("Settings")]
  21. private Vector3 m_axisForward = Vector3.up;
  22. [SerializeField, BoxGroup("Settings")]
  23. private float m_armRange = 1;
  24. [SerializeField, BoxGroup("Settings"), Range(0.0f, 1.0f)]
  25. private float m_armSpeed = 0.5f;
  26. [SerializeField, BoxGroup("Settings")]
  27. private Vector2 m_startInput = new Vector2(0, 1);
  28. [SerializeField, BoxGroup("Input")]
  29. private InputActionProperty m_inputAxis;
  30. [SerializeField, BoxGroup("Input")]
  31. private InputActionProperty m_row;
  32. #endregion Inspector Fields
  33. #region Private Fields
  34. //Last input of player
  35. private Vector2 m_desiredInput;
  36. private Vector3 m_startPosition;
  37. private Vector3 m_lastPosition;
  38. private bool m_doUndo;
  39. #endregion Private Fields
  40. #region Getters
  41. public new Transform transform => m_handTransform;
  42. #endregion Getters
  43. #region MonoBehaviour Functions
  44. private void Start()
  45. {
  46. m_startPosition = transform.localPosition;
  47. UpdateHand(m_startInput);
  48. }
  49. /// <summary>
  50. /// OnEnable is called when the object becomes enabled and active.
  51. /// </summary>
  52. private void OnEnable()
  53. {
  54. m_inputAxis.action.performed += OnInputRecieved;
  55. m_inputAxis.action.canceled += OnInputRecieved;
  56. m_inputAxis.action.Enable();
  57. m_row.action?.Enable();
  58. }
  59. /// <summary>
  60. /// OnDisable is called when the behaviour becomes disabled.
  61. /// </summary>
  62. private void OnDisable()
  63. {
  64. m_inputAxis.action.performed -= OnInputRecieved;
  65. m_inputAxis.action.canceled -= OnInputRecieved;
  66. m_inputAxis.action.Disable();
  67. m_row.action?.Disable();
  68. }
  69. /// <summary>
  70. /// Update is called once per frame
  71. /// </summary>
  72. private void FixedUpdate()
  73. {
  74. UpdateHand(m_desiredInput);
  75. }
  76. #endregion MonoBehaviour Functions
  77. #region Class Functionality
  78. /// <summary>
  79. /// Called every fixed update to move the arms
  80. /// </summary>
  81. /// <param name="input"></param>
  82. private void UpdateHand(Vector2 input, bool registerUndo = true)
  83. {
  84. m_lastPosition = transform.localPosition;
  85. Quaternion rotation = Quaternion.LookRotation(m_axisForward.normalized, m_axisNormal.normalized);
  86. Vector3 axisInput = transform.rotation * rotation * new Vector3(input.x, 0, input.y);
  87. Vector3 desiredPosition = m_startPosition + axisInput * m_armRange;
  88. transform.localPosition = Vector3.Lerp(transform.localPosition, desiredPosition, m_armSpeed);
  89. if (m_row.action != null)
  90. {
  91. if (m_row.action.IsPressed())
  92. {
  93. transform.localPosition = Vector3.Lerp(transform.localPosition, m_otherHand.localPosition, 0.5f);
  94. }
  95. }
  96. }
  97. public void UndoLastMovement()
  98. {
  99. Vector3 direction = m_lastPosition - transform.localPosition;
  100. transform.localPosition += direction;
  101. }
  102. /// <summary>
  103. /// Called by the input system when the input is changed
  104. /// </summary>
  105. /// <param name="args"></param>
  106. private void OnInputRecieved(InputAction.CallbackContext args)
  107. {
  108. m_desiredInput = args.ReadValue<Vector2>();
  109. }
  110. #endregion Class Functionality
  111. #region Editor Functions
  112. /// <summary>
  113. /// Called when the Component is created or Reset from the Inspector
  114. /// </summary>
  115. private void Reset()
  116. {
  117. m_handTransform = base.transform;
  118. }
  119. #endregion Editor Functions
  120. }