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.

285 lines
12 KiB

  1. /************************************************************************************
  2. See SampleFramework license.txt for license terms. Unless required by applicable law
  3. or agreed to in writing, the sample code is provided AS IS WITHOUT WARRANTIES OR
  4. CONDITIONS OF ANY KIND, either express or implied. See the license for specific
  5. language governing permissions and limitations under the license.
  6. ************************************************************************************/
  7. #define DEBUG_LOCOMOTION_PANEL
  8. using UnityEngine;
  9. using System.Collections;
  10. using System.Diagnostics;
  11. using UnityEngine.UI;
  12. using Debug = UnityEngine.Debug;
  13. using UnityEngine.EventSystems;
  14. using UnityEngine.Events;
  15. public class LocomotionSampleSupport : MonoBehaviour
  16. {
  17. private LocomotionController lc;
  18. private bool inMenu = false;
  19. private LocomotionTeleport TeleportController
  20. {
  21. get
  22. {
  23. return lc.GetComponent<LocomotionTeleport>();
  24. }
  25. }
  26. public void Start()
  27. {
  28. lc = FindObjectOfType<LocomotionController>();
  29. DebugUIBuilder.instance.AddButton("Node Teleport w/ A", SetupNodeTeleport);
  30. DebugUIBuilder.instance.AddButton("Dual-stick teleport", SetupTwoStickTeleport);
  31. DebugUIBuilder.instance.AddButton("L Strafe R Teleport", SetupLeftStrafeRightTeleport);
  32. //DebugUIBuilder.instance.AddButton("R Turn L Teleport", SetupRightTurnLeftTeleport);
  33. DebugUIBuilder.instance.AddButton("Walk Only", SetupWalkOnly);
  34. // This is just a quick hack-in, need a prefab-based way of setting this up easily.
  35. EventSystem eventSystem = FindObjectOfType<EventSystem>();
  36. if (eventSystem == null)
  37. {
  38. Debug.LogError("Need EventSystem");
  39. }
  40. SetupTwoStickTeleport();
  41. // SAMPLE-ONLY HACK:
  42. // Due to restrictions on how Unity project settings work, we just hackily set up default
  43. // to ignore the water layer here. In your own project, you should set up your collision
  44. // layers properly through the Unity editor.
  45. Physics.IgnoreLayerCollision(0, 4);
  46. }
  47. public void Update()
  48. {
  49. if(OVRInput.GetDown(OVRInput.Button.Two) || OVRInput.GetDown(OVRInput.Button.Start))
  50. {
  51. if (inMenu) DebugUIBuilder.instance.Hide();
  52. else DebugUIBuilder.instance.Show();
  53. inMenu = !inMenu;
  54. }
  55. }
  56. [Conditional("DEBUG_LOCOMOTION_PANEL")]
  57. static void Log(string msg)
  58. {
  59. Debug.Log(msg);
  60. }
  61. /// <summary>
  62. /// This method will ensure only one specific type TActivate in a given group of components derived from the same TCategory type is enabled.
  63. /// This is used by the sample support code to select between different targeting, input, aim, and other handlers.
  64. /// </summary>
  65. /// <typeparam name="TCategory"></typeparam>
  66. /// <typeparam name="TActivate"></typeparam>
  67. /// <param name="target"></param>
  68. public static TActivate ActivateCategory<TCategory, TActivate>(GameObject target) where TCategory : MonoBehaviour where TActivate : MonoBehaviour
  69. {
  70. var components = target.GetComponents<TCategory>();
  71. Log("Activate " + typeof(TActivate) + " derived from " + typeof(TCategory) + "[" + components.Length + "]");
  72. TActivate result = null;
  73. for (int i = 0; i < components.Length; i++)
  74. {
  75. var c = (MonoBehaviour)components[i];
  76. var active = c.GetType() == typeof(TActivate);
  77. Log(c.GetType() + " is " + typeof(TActivate) + " = " + active);
  78. if (active)
  79. {
  80. result = (TActivate)c;
  81. }
  82. if (c.enabled != active)
  83. {
  84. c.enabled = active;
  85. }
  86. }
  87. return result;
  88. }
  89. /// <summary>
  90. /// This generic method is used for activating a specific set of components in the LocomotionController. This is just one way
  91. /// to achieve the goal of enabling one component of each category (input, aim, target, orientation and transition) that
  92. /// the teleport system requires.
  93. /// </summary>
  94. /// <typeparam name="TInput"></typeparam>
  95. /// <typeparam name="TAim"></typeparam>
  96. /// <typeparam name="TTarget"></typeparam>
  97. /// <typeparam name="TOrientation"></typeparam>
  98. /// <typeparam name="TTransition"></typeparam>
  99. protected void ActivateHandlers<TInput, TAim, TTarget, TOrientation, TTransition>()
  100. where TInput : TeleportInputHandler
  101. where TAim : TeleportAimHandler
  102. where TTarget : TeleportTargetHandler
  103. where TOrientation : TeleportOrientationHandler
  104. where TTransition : TeleportTransition
  105. {
  106. ActivateInput<TInput>();
  107. ActivateAim<TAim>();
  108. ActivateTarget<TTarget>();
  109. ActivateOrientation<TOrientation>();
  110. ActivateTransition<TTransition>();
  111. }
  112. protected void ActivateInput<TActivate>() where TActivate : TeleportInputHandler
  113. {
  114. ActivateCategory<TeleportInputHandler, TActivate>();
  115. }
  116. protected void ActivateAim<TActivate>() where TActivate : TeleportAimHandler
  117. {
  118. ActivateCategory<TeleportAimHandler, TActivate>();
  119. }
  120. protected void ActivateTarget<TActivate>() where TActivate : TeleportTargetHandler
  121. {
  122. ActivateCategory<TeleportTargetHandler, TActivate>();
  123. }
  124. protected void ActivateOrientation<TActivate>() where TActivate : TeleportOrientationHandler
  125. {
  126. ActivateCategory<TeleportOrientationHandler, TActivate>();
  127. }
  128. protected void ActivateTransition<TActivate>() where TActivate : TeleportTransition
  129. {
  130. ActivateCategory<TeleportTransition, TActivate>();
  131. }
  132. protected TActivate ActivateCategory<TCategory, TActivate>() where TCategory : MonoBehaviour where TActivate : MonoBehaviour
  133. {
  134. return ActivateCategory<TCategory, TActivate>(lc.gameObject);
  135. }
  136. protected void UpdateToggle(Toggle toggle, bool enabled)
  137. {
  138. if (enabled != toggle.isOn)
  139. {
  140. toggle.isOn = enabled;
  141. }
  142. }
  143. void SetupNonCap()
  144. {
  145. var input = TeleportController.GetComponent<TeleportInputHandlerTouch>();
  146. input.InputMode = TeleportInputHandlerTouch.InputModes.SeparateButtonsForAimAndTeleport;
  147. input.AimButton = OVRInput.RawButton.A;
  148. input.TeleportButton = OVRInput.RawButton.A;
  149. }
  150. void SetupTeleportDefaults()
  151. {
  152. TeleportController.enabled = true;
  153. //lc.PlayerController.SnapRotation = true;
  154. lc.PlayerController.RotationEitherThumbstick = false;
  155. //lc.PlayerController.FixedSpeedSteps = 0;
  156. TeleportController.EnableMovement(false, false, false, false);
  157. TeleportController.EnableRotation(false, false, false, false);
  158. var input = TeleportController.GetComponent<TeleportInputHandlerTouch>();
  159. input.InputMode = TeleportInputHandlerTouch.InputModes.CapacitiveButtonForAimAndTeleport;
  160. input.AimButton = OVRInput.RawButton.A;
  161. input.TeleportButton = OVRInput.RawButton.A;
  162. input.CapacitiveAimAndTeleportButton = TeleportInputHandlerTouch.AimCapTouchButtons.A;
  163. input.FastTeleport = false;
  164. var hmd = TeleportController.GetComponent<TeleportInputHandlerHMD>();
  165. hmd.AimButton = OVRInput.RawButton.A;
  166. hmd.TeleportButton = OVRInput.RawButton.A;
  167. var orient = TeleportController.GetComponent<TeleportOrientationHandlerThumbstick>();
  168. orient.Thumbstick = OVRInput.Controller.LTouch;
  169. }
  170. protected GameObject AddInstance(GameObject template, string label)
  171. {
  172. var go = Instantiate(template);
  173. go.transform.SetParent(transform, false);
  174. go.name = label;
  175. return go;
  176. }
  177. // Teleport between node with A buttons. Display laser to node. Allow snap turns.
  178. void SetupNodeTeleport()
  179. {
  180. SetupTeleportDefaults();
  181. SetupNonCap();
  182. //lc.PlayerController.SnapRotation = true;
  183. //lc.PlayerController.FixedSpeedSteps = 1;
  184. lc.PlayerController.RotationEitherThumbstick = true;
  185. TeleportController.EnableRotation(true, false, false, true);
  186. ActivateHandlers<TeleportInputHandlerTouch, TeleportAimHandlerLaser, TeleportTargetHandlerNode, TeleportOrientationHandlerThumbstick, TeleportTransitionBlink>();
  187. var input = TeleportController.GetComponent<TeleportInputHandlerTouch>();
  188. input.AimingController = OVRInput.Controller.RTouch;
  189. //var input = TeleportController.GetComponent<TeleportAimHandlerLaser>();
  190. //input.AimingController = OVRInput.Controller.RTouch;
  191. }
  192. // Symmetrical controls. Forward or back on stick initiates teleport, then stick allows orient.
  193. // Snap turns allowed.
  194. void SetupTwoStickTeleport()
  195. {
  196. SetupTeleportDefaults();
  197. TeleportController.EnableRotation(true, false, false, true);
  198. TeleportController.EnableMovement(false, false, false, false);
  199. //lc.PlayerController.SnapRotation = true;
  200. lc.PlayerController.RotationEitherThumbstick = true;
  201. //lc.PlayerController.FixedSpeedSteps = 1;
  202. var input = TeleportController.GetComponent<TeleportInputHandlerTouch>();
  203. input.InputMode = TeleportInputHandlerTouch.InputModes.ThumbstickTeleportForwardBackOnly;
  204. input.AimingController = OVRInput.Controller.Touch;
  205. ActivateHandlers<TeleportInputHandlerTouch, TeleportAimHandlerParabolic, TeleportTargetHandlerPhysical, TeleportOrientationHandlerThumbstick, TeleportTransitionBlink>();
  206. var orient = TeleportController.GetComponent<TeleportOrientationHandlerThumbstick>();
  207. orient.Thumbstick = OVRInput.Controller.Touch;
  208. }
  209. /*
  210. void SetupRightTurnLeftTeleport()
  211. {
  212. SetupTeleportDefaults();
  213. TeleportController.EnableRotation(true, false, false, false);
  214. TeleportController.EnableMovement(false, false, false, false);
  215. lc.PlayerController.SnapRotation = true;
  216. lc.PlayerController.FixedSpeedSteps = 1;
  217. var input = TeleportController.GetComponent<TeleportInputHandlerTouch>();
  218. input.InputMode = TeleportInputHandlerTouch.InputModes.ThumbstickTeleport;
  219. input.AimingController = OVRInput.Controller.LTouch;
  220. ActivateHandlers<TeleportInputHandlerTouch, TeleportAimHandlerParabolic, TeleportTargetHandlerPhysical, TeleportOrientationHandlerThumbstick, TeleportTransitionBlink>();
  221. var orient = TeleportController.GetComponent<TeleportOrientationHandlerThumbstick>();
  222. orient.Thumbstick = OVRInput.Controller.LTouch;
  223. }
  224. */
  225. // Shut down teleport. Basically reverts to OVRPlayerController.
  226. void SetupWalkOnly()
  227. {
  228. SetupTeleportDefaults();
  229. TeleportController.enabled = false;
  230. lc.PlayerController.EnableLinearMovement = true;
  231. //lc.PlayerController.SnapRotation = true;
  232. lc.PlayerController.RotationEitherThumbstick = false;
  233. //lc.PlayerController.FixedSpeedSteps = 1;
  234. }
  235. //
  236. void SetupLeftStrafeRightTeleport()
  237. {
  238. SetupTeleportDefaults();
  239. TeleportController.EnableRotation(true, false, false, true);
  240. TeleportController.EnableMovement(true, false, false, false);
  241. //lc.PlayerController.SnapRotation = true;
  242. //lc.PlayerController.FixedSpeedSteps = 1;
  243. var input = TeleportController.GetComponent<TeleportInputHandlerTouch>();
  244. input.InputMode = TeleportInputHandlerTouch.InputModes.ThumbstickTeleportForwardBackOnly;
  245. input.AimingController = OVRInput.Controller.RTouch;
  246. ActivateHandlers<TeleportInputHandlerTouch, TeleportAimHandlerParabolic, TeleportTargetHandlerPhysical, TeleportOrientationHandlerThumbstick, TeleportTransitionBlink>();
  247. var orient = TeleportController.GetComponent<TeleportOrientationHandlerThumbstick>();
  248. orient.Thumbstick = OVRInput.Controller.RTouch;
  249. }
  250. }