using System; using System.Collections; using System.Collections.Generic; using MoreMountains.Feedbacks; using UnityEngine; namespace MoreMountains.Feel { public class Wheel : MonoBehaviour { [Header("Input")] /// a key to use to jump [Tooltip("a key to use to jump")] public KeyCode ActionKey = KeyCode.Space; /// a secondary key to use to jump [Tooltip("a secondary key to use to jump")] public KeyCode ActionKeyAlt = KeyCode.Joystick1Button0; [Header("Binding")] /// the part of the wheel that rotates [Tooltip("the part of the wheel that rotates")] public Transform RotatingPart; [Header("Settings")] /// the speed at which the wheel should rotate [Tooltip("the speed at which the wheel should rotate")] public float RotationSpeed = 20f; [Header("Feedbacks")] /// a feedback to call when the wheel starts turning [Tooltip("a feedback to call when the wheel starts turning")] public MMFeedbacks TurnFeedback; /// a feedback to call when the wheel stops turning [Tooltip("a feedback to call when the wheel stops turning")] public MMFeedbacks TurnStopFeedback; protected bool _turning; /// /// On Update we look for input /// protected virtual void Update() { HandleInput(); HandleWheel(); } /// /// Detects input /// protected virtual void HandleInput() { if (Input.GetKey(ActionKey) || Input.GetKey(ActionKeyAlt) || Input.GetMouseButton(0)) { Turn(); } if (Input.GetKeyUp(ActionKey) || Input.GetKeyUp(ActionKeyAlt) || Input.GetMouseButtonUp(0)) { TurnStop(); } } /// /// Every frame, rotates the wheel if needed /// protected virtual void HandleWheel() { if (_turning) { RotatingPart.transform.Rotate(this.transform.right, RotationSpeed * Time.deltaTime); } } /// /// Makes the wheel turn, plays a feedback if it's just starting to turn this frame /// protected virtual void Turn() { if (!_turning) { TurnFeedback?.PlayFeedbacks(); } _turning = true; } /// /// Stops the wheel from turning /// protected virtual void TurnStop() { TurnFeedback?.StopFeedbacks(); TurnStopFeedback?.PlayFeedbacks(); _turning = false; } } }