RenderableTeleport.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //***********************************************************
  2. // Filename: RenderableTeleport.cs
  3. // Author: Moritz Kolvenbach, Marco Fendrich
  4. // Last changes: Wednesday, 8th of August 2018
  5. // Content: General base class for teleport displaying a parabolic or similar line to indicate where the player is aiming at
  6. //***********************************************************
  7. using System.Collections.Generic;
  8. using UnityEngine;
  9. /// <summary>
  10. /// Abstract base class for teleports already including interface to be used drag and drop style with vive controllers
  11. /// </summary>
  12. public abstract class RenderableTeleport : MonoBehaviour, IButton
  13. {
  14. // general reference classes for access
  15. protected GameObject gameManager;
  16. protected Visualiser renderScript;
  17. protected Teleport teleportScript;
  18. protected ViveNavMesh navMesh;
  19. protected GameManager gameManagerScript;
  20. // list of points within parabola or line being shown to player as indicator for his teleportation
  21. protected List<Vector3> projectionPoints;
  22. // distance in meters between the single points of the optical representation
  23. protected float pointSpacing;
  24. // maximum number of points being calculated until parabola is being stopped
  25. protected int pointCount;
  26. // checks whether the player is trying to aim a teleport function and the parabola has to be calculated
  27. protected bool isActive;
  28. // indicator of whether last point of current projectionPoints is on navMesh
  29. protected bool pointOnNavMesh;
  30. // forward vector of controller object
  31. protected Vector3 normalisedVelocity = Vector3.forward;
  32. // upward vector of hit point
  33. protected Vector3 normalisedHitPoint = Vector3.up;
  34. // rotation being passed through from specific child teleportation to teleportation class
  35. protected float rotation = float.NegativeInfinity;
  36. // timestamp for tracked event
  37. protected float timeInitialized;
  38. void Start()
  39. {
  40. // get references for variables declared above
  41. gameManager = GameObject.FindWithTag("GameController");
  42. gameManagerScript = (GameManager)gameManager.GetComponent(typeof(GameManager));
  43. teleportScript = (Teleport)gameManager.GetComponent(typeof(Teleport));
  44. renderScript = (Visualiser)gameManager.GetComponent(typeof(Visualiser));
  45. navMesh = (ViveNavMesh)gameManager.GetComponent(typeof(ViveNavMesh));
  46. projectionPoints = new List<Vector3>(pointCount);
  47. pointSpacing = renderScript.PointSpacing;
  48. pointCount = renderScript.PointCount;
  49. }
  50. void Update()
  51. {
  52. if (!isActive)
  53. return;
  54. UpdateProjectionPoints();
  55. renderScript.updateRendering(pointOnNavMesh, projectionPoints, normalisedHitPoint, normalisedVelocity, rotation);
  56. }
  57. /// <summary>
  58. /// Function being called on press by player; sets rendering to active and starts calculating parabola
  59. /// </summary>
  60. /// <param name="controllerObject">gameObject of controller</param>
  61. /// <param name="controllerIdentificator">ID of controller currently using this function</param>
  62. public void OnButtonDown(GameObject controllerObject, int controllerIdentificator)
  63. {
  64. gameObject.transform.parent = controllerObject.transform;
  65. gameObject.transform.localPosition = Vector3.zero;
  66. gameObject.transform.localRotation = Quaternion.identity;
  67. renderScript.enabled = true;
  68. isActive = true;
  69. timeInitialized = Time.time;
  70. }
  71. /// <summary>
  72. /// Function being called on release of button by player; starts teleportation if valid target is selected
  73. /// </summary>
  74. public void OnButtonUp()
  75. {
  76. // only teleport if valid target is selected
  77. if (pointOnNavMesh)
  78. {
  79. // call to teleport function
  80. teleportScript.CallTeleport(projectionPoints[projectionPoints.Count - 1], rotation, timeInitialized);
  81. // set rotation back to none
  82. rotation = float.NegativeInfinity;
  83. }
  84. isActive = false; // deactivate calculations
  85. renderScript.enabled = false; // disable render script
  86. }
  87. /// <summary>
  88. /// Child classes have to calculate the new points of the parabola as those are specific to the given teleportation
  89. /// </summary>
  90. protected abstract void UpdateProjectionPoints();
  91. }