| | | 1 | | using BNG; |
| | | 2 | | using System; |
| | | 3 | | using UnityEngine; |
| | | 4 | | using System.Diagnostics.CodeAnalysis; |
| | | 5 | | using UnityEngine.Events; |
| | | 6 | | using HenryLab; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// When an object is tilted, run some functionality |
| | | 10 | | /// Used with a grabable object |
| | | 11 | | /// </summary> |
| | | 12 | | public class OnTilt : MonoBehaviour, IGrabbableEntity |
| | | 13 | | { |
| | | 14 | | [ExcludeFromCodeCoverage] |
| | | 15 | | public Grabbable Grabbable |
| | | 16 | | { |
| | | 17 | | get |
| | | 18 | | { |
| | | 19 | | var g = GetComponent<Grabbable>(); |
| | | 20 | | if(g) return g; |
| | | 21 | | return gameObject.AddComponent<Grabbable>(); |
| | | 22 | | } |
| | | 23 | | } |
| | | 24 | | [ExcludeFromCodeCoverage] public string Name => Str.Grabbable; |
| | | 25 | | [ExcludeFromCodeCoverage] public Transform Destination => null; |
| | | 26 | | |
| | | 27 | | [ExcludeFromCodeCoverage] |
| | | 28 | | public void OnGrabbed() |
| | | 29 | | { |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | [Tooltip("Tilt range, 0 - 180 degrees")] |
| | 2 | 33 | | [Range(0, 1)] public float threshold = 0.1f; |
| | | 34 | | |
| | | 35 | | [Serializable] public class TiltEvent : UnityEvent<MonoBehaviour> { } |
| | | 36 | | |
| | | 37 | | // Threshold has been broken |
| | 2 | 38 | | public TiltEvent OnBegin = new TiltEvent(); |
| | | 39 | | |
| | | 40 | | // Threshold is no longer broken |
| | 2 | 41 | | public TiltEvent OnEnd = new TiltEvent(); |
| | | 42 | | |
| | 2 | 43 | | private bool withinThreshold = false; |
| | | 44 | | |
| | | 45 | | private void Update() |
| | 1196 | 46 | | { |
| | 1196 | 47 | | CheckOrientation(); |
| | 1196 | 48 | | } |
| | | 49 | | |
| | | 50 | | private void CheckOrientation() |
| | 1196 | 51 | | { |
| | 1196 | 52 | | float similarity = Vector3.Dot(-transform.up, Vector3.up); |
| | 1196 | 53 | | similarity = Mathf.InverseLerp(0, 1, similarity); |
| | | 54 | | |
| | 1196 | 55 | | bool thresholdCheck = similarity >= threshold; |
| | | 56 | | |
| | 1196 | 57 | | if(withinThreshold != thresholdCheck) |
| | 0 | 58 | | { |
| | 0 | 59 | | withinThreshold = thresholdCheck; |
| | | 60 | | |
| | 0 | 61 | | if(withinThreshold) |
| | 0 | 62 | | { |
| | 0 | 63 | | OnBegin.Invoke(this); |
| | 0 | 64 | | } |
| | | 65 | | else |
| | 0 | 66 | | { |
| | 0 | 67 | | OnEnd.Invoke(this); |
| | 0 | 68 | | } |
| | 0 | 69 | | } |
| | 1196 | 70 | | } |
| | | 71 | | |
| | | 72 | | public void OnReleased() |
| | 0 | 73 | | { |
| | 0 | 74 | | throw new NotImplementedException(); |
| | | 75 | | } |
| | | 76 | | } |