|
|
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using VRTK;
-
- [RequireComponent(typeof(VRTK_SnapDropZone))]
- public class GravitySelector : MonoBehaviour
- {
-
- [Header("Settings")]
- private float m_radius = 11.5f;
-
- [Header("References")]
- [SerializeField]
- private RotationController m_ship;
-
- [Header("Canvas")]
- [SerializeField]
- private TextMeshProUGUI m_txtName;
- [SerializeField]
- private TextMeshProUGUI m_txtRevolution;
- [SerializeField]
- private TextMeshProUGUI m_txtAcceleration;
-
-
- private VRTK_SnapDropZone m_snapZone;
-
- [SerializeField]
- private GravityData m_currentPlanet;
-
- private void Awake()
- {
- m_snapZone = GetComponent<VRTK_SnapDropZone>();
- }
-
- private void OnEnable()
- {
- RegisterEvents(true);
- }
-
- private void OnDisable()
- {
- RegisterEvents(false);
- }
-
- private void AddObject(object sender, SnapDropZoneEventArgs e)
- {
- Debug.Log("Object Snapped");
- GravityData newObject = e.snappedObject.GetComponent<GravityData>();
-
- if (newObject != null)
- {
- SetGravity(newObject);
- }
- }
-
- private void RemoveObject(object sender, SnapDropZoneEventArgs e)
- {
- GravityData newObject = e.snappedObject.GetComponent<GravityData>();
-
- if (newObject == m_currentPlanet)
- {
- SetGravity(null);
- }
-
- }
-
-
- private void SetGravity(GravityData data)
- {
- if (data == null)
- {
- m_ship.SetGravityAtRadius(0.0f, m_radius);
- m_txtName.text = "Zero Gravity";
- m_txtRevolution.text = "n/a";
- m_txtAcceleration.text = "0.00 m/s²";
- }
- else
- {
- m_ship.SetGravityAtRadius(data.Gravity, m_radius);
- m_txtName.text = $"{data.name} Gravity";
- m_txtRevolution.text = $"{m_ship.RotationPeriod.ToString("0.00")} seconds";
- m_txtAcceleration.text = $"{data.Gravity.ToString("0.00")} m/s²";
- }
-
- m_currentPlanet = data;
- }
-
-
- private void RegisterEvents(bool value)
- {
- if (value)
- {
- m_snapZone.ObjectSnappedToDropZone += AddObject;
- m_snapZone.ObjectUnsnappedFromDropZone += RemoveObject;
- }else
- {
- m_snapZone.ObjectSnappedToDropZone -= AddObject;
- m_snapZone.ObjectUnsnappedFromDropZone -= RemoveObject;
- }
- }
-
- public void PrintLog(string log)
- {
- Debug.Log(log);
- m_txtName.text = log;
- }
-
- }
|