Lab.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Handle the Event in Lab Mode
  3. *
  4. *
  5. */
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using UnityEngine;
  10. using Valve.VR;
  11. using Valve.VR.InteractionSystem.Sample;
  12. using UnityEngine.UI;
  13. using Valve.VR.InteractionSystem;
  14. public class Lab : MonoBehaviour,IMode
  15. {
  16. public Player_Control Player_Control;
  17. public GameObject[] ModeMenu;
  18. public MeshRenderer LockView;
  19. public GameObject[] CustomHand;
  20. public Hand[] OriginHand;
  21. public Vector2 signal;
  22. public Vector2 Signal => signal;
  23. public SpeedLinearEditor velocitySpeedEditor;
  24. public SpeedLinearEditor angleSpeedEditor;
  25. public float velocitySpeed;
  26. public float angleSpeed;
  27. // change the speed, so that the robot can run faster after editing
  28. public Joystick angle_joystick;
  29. public Joystick velocity_joystick;
  30. public float angle,velocity;
  31. SteamVR_Action_Vibration vibration = SteamVR_Input.GetVibrationAction("Haptic");
  32. Player player;
  33. InteractionManagement interactionManagement;
  34. GameObject robot;
  35. bool forward = false;
  36. bool angle_joystickAttached = false;
  37. bool velocity_joystickAttached = false;
  38. private void OnEnable() {
  39. GameObject.Find("Input").GetComponent<VRInput>().SetMode(this.gameObject);
  40. // The player cannot be raised or lowered (the aerial perspective is disabled)
  41. Player_Control.upDownEnabled = false;
  42. interactionManagement = InteractionManagement.Instance;
  43. interactionManagement.SetPlayerText( "Control Mode : " + gameObject.name, 7, false);
  44. interactionManagement.HideController();
  45. robot = GameObject.FindGameObjectWithTag("robot");
  46. player = Player.instance;
  47. player.transform.position = gameObject.transform.position + new Vector3(0,0,26);
  48. player.transform.LookAt(gameObject.transform);
  49. // reset the menu position
  50. interactionManagement.SetMenu(false);
  51. interactionManagement.SetMenu(true);
  52. // Add SteamVR Actions Listener
  53. SteamVR_Actions.default_Menu.AddOnStateDownListener(MenuActionHandler,SteamVR_Input_Sources.Any);
  54. // if not do this, it will be some problems with the display of custom hands. Reseason is unknown
  55. SetCustomHand(false);
  56. SetCustomHand(true);
  57. }
  58. // Start is called before the first frame update
  59. void Start()
  60. {
  61. }
  62. // Update is called once per frame
  63. void Update()
  64. {
  65. velocitySpeed = velocitySpeedEditor.speed;
  66. angleSpeed = angleSpeedEditor.speed;
  67. if(InteractionManagement.Instance.Robot_Locked){
  68. signal = new Vector2(0,0);
  69. LockView.material.color = new Color32(183, 0, 14, 255);
  70. }
  71. else{
  72. LockView.material.color = new Color32(74, 126, 52, 255);
  73. UpdateSignal();
  74. }
  75. }
  76. public void StartSettingAngle(){
  77. angle_joystickAttached = true;
  78. }
  79. public void StopSettingAngle(){
  80. angle_joystickAttached = false;
  81. this.angle = 0;
  82. }
  83. public void StartSettingVelocity(){
  84. velocity_joystickAttached = true;
  85. }
  86. public void StopSettinVelocity(){
  87. velocity_joystickAttached = false;
  88. this.velocity = 0;
  89. }
  90. public void UpdateSignal()
  91. { velocity = 0;
  92. angle = 0;
  93. // the robot will stop/start move forward automatically
  94. if(forward){
  95. velocity = velocitySpeed * 0.5f; // justify the speed so that it will not run too fast
  96. }
  97. // change the direction of the robot
  98. if(angle_joystickAttached){
  99. angle = System.Math.Abs(angle_joystick.value.y)>0.1 ? angle_joystick.value.y * angleSpeed : 0;
  100. }
  101. // the robot will move forwards or backwards
  102. if(velocity_joystickAttached){
  103. velocity = System.Math.Abs(velocity_joystick.value.y)>0.1 ? -velocity_joystick.value.y * velocitySpeed : 0;
  104. // close the auto drive
  105. if(forward){
  106. interactionManagement.SetPlayerText("Start Auto Driving",5,false);
  107. forward = false;
  108. }
  109. }
  110. signal = new Vector2(angle,velocity);
  111. }
  112. // Handle Button Click Effect: the robot will stop/start move forward automatically
  113. public void goForward(GameObject button){
  114. forward = !forward;
  115. if(forward){
  116. interactionManagement.SetPlayerText("Start Auto Driving",5,false);
  117. }
  118. else{
  119. interactionManagement.SetPlayerText("Stop Auto Driving",5,false);
  120. }
  121. vibration.Execute(0f, 0.1f, 50f, 1f, SteamVR_Input_Sources.Any);
  122. }
  123. // Handle Button Click Effect: Rescue the target
  124. public void rescue(){
  125. foreach(GameObject target in TestManagement.Instance.targetList){
  126. if(target!=null){
  127. float distance = Vector3.Distance(target.transform.position, robot.transform.position);
  128. if(distance <= 5){
  129. TestManagement.Instance.rescue(target);
  130. }
  131. }
  132. }
  133. vibration.Execute(0f, 0.1f, 50f, 1f, SteamVR_Input_Sources.Any);
  134. }
  135. public void LockButtonDown(){
  136. interactionManagement.LockRobot();
  137. vibration.Execute(0f, 0.1f, 50f, 1f, SteamVR_Input_Sources.Any);
  138. }
  139. private void MenuActionHandler(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
  140. {
  141. interactionManagement.SetMenu();
  142. // Mode Selektoon Setting
  143. // in Lab Mode, the user can not see the Controller
  144. if(!interactionManagement.Menu_Opened){
  145. interactionManagement.HideController();
  146. SetCustomHand(true);
  147. }
  148. else{
  149. SetCustomHand(false);
  150. }
  151. vibration.Execute(0f, 0.1f, 50f, 1f, SteamVR_Input_Sources.Any);
  152. }
  153. public void SetCustomHand(bool open){
  154. foreach(GameObject hand in CustomHand){
  155. hand.SetActive(open);
  156. }
  157. foreach(Hand hand in OriginHand){
  158. hand.mainRenderModel.SetVisibility(!open);
  159. }
  160. }
  161. private void OnDisable() {
  162. // Remove SteamVR Actions Listener
  163. SteamVR_Actions.default_Menu.RemoveOnStateDownListener(MenuActionHandler,SteamVR_Input_Sources.Any);
  164. player.transform.position = robot.transform.position + new Vector3(-1,0,0);;
  165. // close the custom hand
  166. SetCustomHand(false);
  167. }
  168. }