NxrInteractive.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3. namespace Nxr.Internal
  4. {
  5. [RequireComponent(typeof(Collider))]
  6. public class NxrInteractive : MonoBehaviour
  7. {
  8. public UnityEvent OnLookExitInvoke;
  9. public UnityEvent OnLookEnterInvoke;
  10. public UnityEvent OnTriggerInvoke;
  11. protected virtual void OnLooked()
  12. {
  13. this.OnLookEnterInvoke.Invoke();
  14. }
  15. protected virtual void OnUnLooked()
  16. {
  17. this.OnLookExitInvoke.Invoke();
  18. }
  19. protected virtual void OnTriggered()
  20. {
  21. this.OnTriggerInvoke.Invoke();
  22. }
  23. public enum GameObjectSightState
  24. {
  25. NotLooked,
  26. Looked,
  27. Triggered
  28. }
  29. public GameObjectSightState _currentState;
  30. private void Awake()
  31. {
  32. this._currentState = GameObjectSightState.NotLooked;
  33. }
  34. private void ChangeState(GameObjectSightState stat)
  35. {
  36. this._currentState = stat;
  37. }
  38. internal void HandleIsLookedAt()
  39. {
  40. if (this._currentState == GameObjectSightState.NotLooked)
  41. {
  42. this.ChangeState(GameObjectSightState.Looked);
  43. this.OnLooked();
  44. }
  45. }
  46. internal void OtherIsLooked()
  47. {
  48. if (this._currentState != GameObjectSightState.NotLooked)
  49. {
  50. this.ChangeState(GameObjectSightState.NotLooked);
  51. this.OnUnLooked();
  52. }
  53. }
  54. private void OnDisable()
  55. {
  56. NxrLineOfSight.NonLook -= new NxrLineOfSight.NonLookAction(this.OtherIsLooked);
  57. }
  58. private void OnEnable()
  59. {
  60. ChangeState(GameObjectSightState.NotLooked);
  61. NxrLineOfSight.NonLook += new NxrLineOfSight.NonLookAction(this.OtherIsLooked);
  62. }
  63. public void OnPointerClicked()
  64. {
  65. if (this._currentState == GameObjectSightState.Looked)
  66. {
  67. ChangeState(GameObjectSightState.Triggered);
  68. this.OnTriggered();
  69. }
  70. }
  71. }
  72. }