UI_Mode.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Handle the Event in UI Mode
  3. *
  4. * Author: Jingyi Jia
  5. *
  6. */
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using UnityEngine;
  11. using Valve.VR;
  12. using Valve.VR.InteractionSystem.Sample;
  13. using UnityEngine.UI;
  14. using Valve.VR.InteractionSystem;
  15. using UnityEngine.AI;
  16. public class UI_Mode: MonoBehaviour,IMode
  17. {
  18. // ------------------------ UI Menu
  19. public UnityEngine.UI.Toggle lock_robot_Toggle;
  20. public UnityEngine.UI.Toggle follow_me_Toggle;
  21. public UnityEngine.UI.Slider velocity_Slider;
  22. public bool userControl = false;
  23. // ------------------------
  24. InteractionManagement interactionManagement;
  25. public Player_Control Player_Control;
  26. public GameObject[] ModeMenu;
  27. public Vector2 signal;
  28. public Vector2 Signal => signal;
  29. SteamVR_Action_Vibration vibration = SteamVR_Input.GetVibrationAction("Haptic");
  30. GameObject robot;
  31. Player player;
  32. private NavMeshAgent navMeshAgent;
  33. private void OnEnable() {
  34. // The player can be raised or lowered (the aerial perspective is enabled)
  35. Player_Control.upDownEnabled = true;
  36. robot = GameObject.FindGameObjectWithTag("robot");
  37. navMeshAgent = robot.GetComponent<NavMeshAgent>();
  38. navMeshAgent.stoppingDistance = 2;
  39. player = Player.instance;
  40. interactionManagement = InteractionManagement.Instance;
  41. interactionManagement.SetPlayerText( "Control Mode : " + gameObject.name, 7, false);
  42. interactionManagement.SetMenu(false);
  43. // open the mode menu
  44. foreach(GameObject child in ModeMenu){
  45. child.SetActive(true);
  46. }
  47. // Add SteamVR Actions Listener
  48. SteamVR_Actions.default_Menu.AddOnStateDownListener(MenuActionHandler,SteamVR_Input_Sources.Any);
  49. SteamVR_Actions.default_ModeMenu.AddOnStateDownListener(ModeMenuHandler,SteamVR_Input_Sources.Any);
  50. }
  51. // Start is called before the first frame update
  52. void Start()
  53. {
  54. }
  55. // Update is called once per frame
  56. void Update()
  57. {
  58. if(interactionManagement.Robot_Locked){
  59. signal = new Vector2(0,0);
  60. lock_robot_Toggle.isOn = true;
  61. navMeshAgent.ResetPath();
  62. }
  63. else{
  64. UpdateSignal();
  65. lock_robot_Toggle.isOn = false;
  66. }
  67. }
  68. public void UpdateSignal()
  69. {
  70. if(!userControl){
  71. signal = Vector2.zero;
  72. }
  73. // Auto driving
  74. if(follow_me_Toggle.isOn){
  75. Vector3 targetPosition = player.transform.position;
  76. targetPosition.y = robot.transform.position.y;
  77. navMeshAgent.SetDestination(targetPosition);
  78. }
  79. else{
  80. navMeshAgent.ResetPath();
  81. }
  82. }
  83. public void setAngleStart(float angle){
  84. if(interactionManagement.Robot_Locked){
  85. interactionManagement.SetPlayerText("Please unlock the robot");
  86. }
  87. else{
  88. userControl = true;
  89. this.signal = new Vector2(angle,0);
  90. }
  91. }
  92. public void setVelocityStart(float velocity){
  93. if(interactionManagement.Robot_Locked){
  94. interactionManagement.SetPlayerText("Please unlock the robot");
  95. }
  96. else{
  97. userControl = true;
  98. this.signal = new Vector2(0, velocity * velocity_Slider.value);
  99. }
  100. }
  101. public void setControlEnde(){
  102. this.signal = new Vector2(0,0);
  103. userControl = false;
  104. }
  105. public void lockRobot(){
  106. interactionManagement.LockRobot();
  107. }
  108. private void MenuActionHandler(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
  109. {
  110. interactionManagement.SetMenu();
  111. vibration.Execute(0f, 0.1f, 50f, 1f, SteamVR_Input_Sources.Any);
  112. }
  113. private void ModeMenuHandler(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
  114. {
  115. bool active = false;
  116. foreach(GameObject child in ModeMenu){
  117. active = !child.activeSelf;
  118. child.SetActive(!child.activeSelf);
  119. }
  120. vibration.Execute(0f, 0.1f, 50f, 1f, SteamVR_Input_Sources.Any);
  121. }
  122. private void OnDisable() {
  123. foreach(GameObject child in ModeMenu){
  124. if(child != null)
  125. child.SetActive(false);
  126. }
  127. // Remove SteamVR Actions Listener
  128. SteamVR_Actions.default_Menu.RemoveOnStateDownListener(MenuActionHandler,SteamVR_Input_Sources.Any);
  129. SteamVR_Actions.default_ModeMenu.RemoveOnStateDownListener(ModeMenuHandler,SteamVR_Input_Sources.Any);
  130. }
  131. }