Assignment for RMIT Mixed Reality in 2020
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.

110 lines
2.5 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using UnityEngine;
  5. using VRTK;
  6. [RequireComponent(typeof(VRTK_SnapDropZone))]
  7. public class GravitySelector : MonoBehaviour
  8. {
  9. [Header("Settings")]
  10. private float m_radius = 11.5f;
  11. [Header("References")]
  12. [SerializeField]
  13. private RotationController m_ship;
  14. [Header("Canvas")]
  15. [SerializeField]
  16. private TextMeshProUGUI m_txtName;
  17. [SerializeField]
  18. private TextMeshProUGUI m_txtRevolution;
  19. [SerializeField]
  20. private TextMeshProUGUI m_txtAcceleration;
  21. private VRTK_SnapDropZone m_snapZone;
  22. [SerializeField]
  23. private GravityData m_currentPlanet;
  24. private void Awake()
  25. {
  26. m_snapZone = GetComponent<VRTK_SnapDropZone>();
  27. }
  28. private void OnEnable()
  29. {
  30. RegisterEvents(true);
  31. }
  32. private void OnDisable()
  33. {
  34. RegisterEvents(false);
  35. }
  36. private void AddObject(object sender, SnapDropZoneEventArgs e)
  37. {
  38. Debug.Log("Object Snapped");
  39. GravityData newObject = e.snappedObject.GetComponent<GravityData>();
  40. if (newObject != null)
  41. {
  42. SetGravity(newObject);
  43. }
  44. }
  45. private void RemoveObject(object sender, SnapDropZoneEventArgs e)
  46. {
  47. GravityData newObject = e.snappedObject.GetComponent<GravityData>();
  48. if (newObject == m_currentPlanet)
  49. {
  50. SetGravity(null);
  51. }
  52. }
  53. private void SetGravity(GravityData data)
  54. {
  55. if (data == null)
  56. {
  57. m_ship.SetGravityAtRadius(0.0f, m_radius);
  58. m_txtName.text = "Zero Gravity";
  59. m_txtRevolution.text = "n/a";
  60. m_txtAcceleration.text = "0.00 m/s²";
  61. }
  62. else
  63. {
  64. m_ship.SetGravityAtRadius(data.Gravity, m_radius);
  65. m_txtName.text = $"{data.name} Gravity";
  66. m_txtRevolution.text = $"{m_ship.RotationPeriod.ToString("0.00")} seconds";
  67. m_txtAcceleration.text = $"{data.Gravity.ToString("0.00")} m/s²";
  68. }
  69. m_currentPlanet = data;
  70. }
  71. private void RegisterEvents(bool value)
  72. {
  73. if (value)
  74. {
  75. m_snapZone.ObjectSnappedToDropZone += AddObject;
  76. m_snapZone.ObjectUnsnappedFromDropZone += RemoveObject;
  77. }else
  78. {
  79. m_snapZone.ObjectSnappedToDropZone -= AddObject;
  80. m_snapZone.ObjectUnsnappedFromDropZone -= RemoveObject;
  81. }
  82. }
  83. public void PrintLog(string log)
  84. {
  85. Debug.Log(log);
  86. m_txtName.text = log;
  87. }
  88. }