Lab.cs 6.1 KB

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