SayWordsOnTouch.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. namespace ZenFulcrum.EmbeddedBrowser {
  5. /** Says something(s) in the HUD when the user touches our collider (one-shot). */
  6. public class SayWordsOnTouch : MonoBehaviour {
  7. public static int ActiveSpeakers { get; private set; }
  8. [Serializable]
  9. public class Verse {
  10. /** How long since the touch/last saying should we wait? */
  11. public float delay;
  12. /** What should we say? Full HTML support (so mind the security implications). */
  13. [Multiline]
  14. public string textHTML;
  15. public float dwellTime = 5;
  16. }
  17. public Verse[] thingsToSay;
  18. private bool triggered, stillTriggered;
  19. /** Make our box collider this much bigger when we enter so it's harder to leave. */
  20. public float extraLeaveRange = 0;
  21. public void OnTriggerEnter(Collider other) {
  22. if (triggered) return;
  23. var inventory = other.GetComponent<PlayerInventory>();
  24. if (!inventory) return;
  25. triggered = true;
  26. stillTriggered = true;
  27. ++ActiveSpeakers;
  28. StartCoroutine(SayStuff());
  29. var bc = GetComponent<BoxCollider>();
  30. if (bc) {
  31. var size = bc.size;
  32. size.x += extraLeaveRange * 2;
  33. size.y += extraLeaveRange * 2;
  34. size.z += extraLeaveRange * 2;
  35. bc.size = size;
  36. }
  37. }
  38. private IEnumerator SayStuff() {
  39. for (int idx = 0; idx < thingsToSay.Length && stillTriggered; ++idx) {
  40. yield return new WaitForSeconds(thingsToSay[idx].delay);
  41. if (!stillTriggered) break;
  42. HUDManager.Instance.Say(thingsToSay[idx].textHTML, thingsToSay[idx].dwellTime);
  43. }
  44. --ActiveSpeakers;
  45. Destroy(gameObject);
  46. }
  47. public void OnTriggerExit(Collider other) {
  48. var inventory = other.GetComponent<PlayerInventory>();
  49. if (!inventory) return;
  50. stillTriggered = false;
  51. }
  52. }
  53. }