machineArmBehavior.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class machineArmBehavior : MonoBehaviour
  5. {
  6. // 非空闲,第一位2表示已就位,1表示运行中。
  7. //0 idle, x1 park x forward, x2 park y downward, x3 park y upward, x4 park put car and y reset
  8. //x6 fetch x forward, x7 fetch y downward, x8 fetch y downward
  9. public GameObject ctrlr;
  10. public int state;
  11. public float xSpeed;
  12. public float ySpeed;
  13. public bool park;
  14. public int terminalIndex;
  15. //private const float minX = 0;
  16. //private const float maxX = 22.8f;
  17. private float[] termX = { 1.2f, 6.2f, 8.7f, 13.7f, 16.2f, 21.2f };
  18. private Vector3 currPos;
  19. private Transform armFront, roller;
  20. public GameObject myCar;
  21. // Start is called before the first frame update
  22. void Start()
  23. {
  24. state = 0;
  25. park = true;
  26. terminalIndex = -1;
  27. xSpeed = 6.0f;
  28. ySpeed = 6.0f;
  29. armFront = transform.Find("arm_front");
  30. roller = transform.Find("roller");
  31. }
  32. /// <summary>
  33. /// 获取离机械手最近终端编号0-5
  34. /// </summary>
  35. /// <returns></returns>
  36. public int GetNearestTerm()
  37. {
  38. // 已靠边
  39. if (transform.position.x < -1 || transform.position.x > 23)
  40. {
  41. return -1;
  42. }
  43. else
  44. {
  45. float diff = 99f;
  46. int index = -1;
  47. for (int i = 0; i < termX.Length; i++)
  48. {
  49. if (diff > Mathf.Abs(transform.position.x - termX[i]))
  50. {
  51. diff = Mathf.Abs(transform.position.x - termX[i]);
  52. index = i;
  53. }
  54. }
  55. return index;
  56. }
  57. }
  58. /// <summary>
  59. /// move arm
  60. /// </summary>
  61. /// <param name="axis"> 0x 1y </param>
  62. /// <param name="diff"></param>
  63. /// <param name=""></param>
  64. public void ArmMove(int axis, float diff, int toState, GameObject car = null)
  65. {
  66. if (axis == 0)
  67. {
  68. if (Mathf.Abs(diff) < Time.deltaTime * 15)
  69. {
  70. state = toState;
  71. }
  72. else if (diff < 0)
  73. {
  74. for (int i = 0; i < termX.Length; i++)
  75. {
  76. // 找到其左边第一个终端
  77. if (currPos.x < termX[i])
  78. {
  79. // 未上锁等待
  80. if (ctrlr.GetComponent<controller>().doorLock[i] != 1)
  81. {
  82. Debug.Log("机械手等待锁门");
  83. return;
  84. }
  85. }
  86. }
  87. transform.Translate(Vector3.right * xSpeed * Time.deltaTime);
  88. }
  89. else
  90. {
  91. for (int i = termX.Length - 1; i >= 0; i--)
  92. {
  93. // 找到其右边第一个终端
  94. if (currPos.x > termX[i])
  95. {
  96. // 未上锁等待
  97. if (ctrlr.GetComponent<controller>().doorLock[i] != 1)
  98. {
  99. Debug.Log("机械手等待锁门");
  100. return;
  101. }
  102. }
  103. }
  104. transform.Translate(Vector3.left * xSpeed * Time.deltaTime);
  105. }
  106. }
  107. else if (axis == 1)
  108. {
  109. if (Mathf.Abs(diff) < 0.1)
  110. {
  111. state = toState;
  112. }
  113. else
  114. {
  115. if (diff < 0)
  116. {
  117. roller.localScale += new Vector3(0f, ySpeed * Time.deltaTime);
  118. roller.position -= new Vector3(0f, ySpeed * Time.deltaTime);
  119. }
  120. else
  121. {
  122. roller.localScale -= new Vector3(0f, ySpeed * Time.deltaTime);
  123. roller.position += new Vector3(0f, ySpeed * Time.deltaTime);
  124. }
  125. if (car != null)
  126. {
  127. car.GetComponent<Transform>().position = new Vector3(currPos.x, currPos.y - 1.25f - roller.localScale.y * 2 - 1.5f, currPos.z);
  128. }
  129. }
  130. }
  131. }
  132. // Update is called once per frame
  133. void Update()
  134. {
  135. if (armFront == null || roller == null)
  136. return;
  137. currPos = GetComponent<Transform>().position;
  138. switch (state)
  139. {
  140. // 空闲状态
  141. case 0:
  142. if (terminalIndex > 0)
  143. {
  144. if (terminalIndex < 7)
  145. state = 11;
  146. else
  147. state = 15;
  148. }
  149. break;
  150. // 停车状态
  151. case 11://x轴平移
  152. float diffX = currPos.x - termX[(terminalIndex - 1) % 6];
  153. ArmMove(0, diffX, 21);
  154. break;
  155. case 21:
  156. state = 12;
  157. break;
  158. case 12:// y轴高度下降
  159. float diffY = roller.localScale.y - 4;
  160. ArmMove(1, diffY, 22);
  161. break;
  162. case 22:// 创建车模
  163. state = 13;
  164. myCar = ctrlr.GetComponent<controller>().CreateCar(new Vector3(currPos.x, currPos.y - 1.25f - roller.localScale.y * 2 - 1.5f, currPos.z));
  165. break;
  166. case 13:// y轴高度上升,移动车模
  167. diffY = roller.localScale.y - 2;
  168. ArmMove(1, diffY, 23, myCar);
  169. break;
  170. case 23:// 判断是否下方已存在等待的中跑车
  171. GameObject[] transfers = ctrlr.GetComponent<controller>().transferVehicles;
  172. for (int i = 0; i < transfers.Length; i++)
  173. {
  174. Vector3 temp = transfers[i].GetComponent<Transform>().position;
  175. transferBehavior tb = transfers[i].GetComponent<transferBehavior>();
  176. if (tb.state == 4 && tb.cmd == 0 && Mathf.Abs(temp.y - 3) < 0.25f && Mathf.Abs(temp.x - currPos.x) < 1.5f && Mathf.Abs(temp.z - currPos.z) < 0.25f)
  177. {
  178. tb.cmd = 1;// 发送接车指令
  179. state = 14;
  180. }
  181. }
  182. break;
  183. case 14:// 收起y轴,保持车辆位置
  184. diffY = roller.localScale.y - 1;
  185. ArmMove(1, diffY, 24);
  186. break;
  187. case 24:// 恢复空闲,复位编号,需controller手动控制
  188. terminalIndex = -1;
  189. state = 0;
  190. myCar = null;
  191. break;
  192. // 取车状态
  193. case 15:// 移动x轴
  194. int index = (terminalIndex - 1) % 6 + 1;
  195. diffX = currPos.x - termX[index - 1];
  196. ArmMove(0, diffX, 25);
  197. break;
  198. case 25:
  199. // 等中跑车到达且状态为4
  200. transfers = ctrlr.GetComponent<controller>().transferVehicles;
  201. for (int i = 0; i < transfers.Length; i++)
  202. {
  203. Vector3 temp = transfers[i].GetComponent<Transform>().position;
  204. if (Mathf.Abs(temp.y - 3) < 0.25f && Mathf.Abs(temp.x - currPos.x) < 2f && Mathf.Abs(temp.z - currPos.z) < 0.25f)
  205. {
  206. if (transfers[i].GetComponent<transferBehavior>().state == 4)
  207. {
  208. state = 160;
  209. break;
  210. }
  211. }
  212. }
  213. break;
  214. case 160:// 放下y轴,抓车, 由中跑车更新车辆句柄
  215. diffY = roller.localScale.y - 2;
  216. ArmMove(1, diffY, 160, myCar);
  217. if (myCar != null)
  218. {
  219. state = 161;
  220. }
  221. break;
  222. case 161:
  223. diffY = roller.localScale.y - 1.5f;//收一点高度,让中跑车离开
  224. ArmMove(1, diffY, 26, myCar);
  225. break;
  226. case 26:// 等待中跑车移开
  227. transfers = ctrlr.GetComponent<controller>().transferVehicles;
  228. for (int i = 0; i < transfers.Length; i++)
  229. {
  230. Vector3 temp = transfers[i].GetComponent<Transform>().position;
  231. if (Mathf.Abs(temp.y - 3) < 0.25f && Mathf.Abs(temp.x - currPos.x) < 2f && Mathf.Abs(temp.z - currPos.z) < 0.25f)
  232. {
  233. return;
  234. }
  235. }
  236. state = 17;
  237. break;
  238. case 17:// 放下y轴
  239. diffY = roller.localScale.y - 4;
  240. ArmMove(1, diffY, 27, myCar);
  241. break;
  242. case 27:
  243. state = 18;
  244. break;
  245. case 18:// 收起y轴,移动到旁边
  246. index = (terminalIndex - 1) % 6 + 1;
  247. diffY = roller.localScale.y - 1;
  248. ArmMove(1, diffY, 18);
  249. // 移动机械手到旁边,锁条件由controller调度
  250. if (index == 1)
  251. {
  252. diffX = transform.position.x - (-1.5f);
  253. ArmMove(0, diffX, 28);
  254. }else if(index == 6)
  255. {
  256. diffX = transform.position.x - 24f;
  257. ArmMove(0, diffX, 28);
  258. }
  259. // 右边已锁住
  260. else if (ctrlr.GetComponent<controller>().doorLock[index - 2] == 1 && ctrlr.GetComponent<controller>().doors[index - 2].GetComponent<doorBehavior>().close && !ctrlr.GetComponent<controller>().doors[index - 2].GetComponent<doorBehavior>().moving)
  261. {
  262. diffX = transform.position.x - termX[index - 2];
  263. ArmMove(0, diffX, 28);
  264. }
  265. // 左边已锁住
  266. else if (ctrlr.GetComponent<controller>().doorLock[index] == 1 && ctrlr.GetComponent<controller>().doors[index].GetComponent<doorBehavior>().close && !ctrlr.GetComponent<controller>().doors[index].GetComponent<doorBehavior>().moving)
  267. {
  268. diffX = transform.position.x - termX[index];
  269. ArmMove(0, diffX, 28);
  270. }
  271. break;
  272. case 28:// 待车辆离开后清除车模,恢复空闲,复位编号
  273. index = (terminalIndex - 1) % 6 + 1;
  274. // 机械手取车开门
  275. if (/*ctrlr.GetComponent<controller>().doorLock[index - 1] == 0 && */ctrlr.GetComponent<controller>().doors[index - 1].GetComponent<doorBehavior>().close && !ctrlr.GetComponent<controller>().doors[index - 1].GetComponent<doorBehavior>().moving)
  276. {
  277. ctrlr.GetComponent<controller>().doors[index - 1].GetComponent<doorBehavior>().close = false;
  278. }
  279. // 移动车模到车库外
  280. if (myCar.transform.position.z > 8f)
  281. {
  282. ctrlr.GetComponent<controller>().DestroyCar(myCar);
  283. terminalIndex = -1;
  284. state = 0;
  285. }else if (myCar.transform.position.z > 2.6f || (!ctrlr.GetComponent<controller>().doors[index - 1].GetComponent<doorBehavior>().close ))
  286. {
  287. myCar.transform.Translate(Vector3.back * xSpeed * Time.deltaTime);
  288. }
  289. // 移动出去则关门
  290. if(myCar.transform.position.z > 2.6f)
  291. {
  292. ctrlr.GetComponent<controller>().doors[index - 1].GetComponent<doorBehavior>().close = true;
  293. }
  294. break;
  295. default: break;
  296. }
  297. }
  298. }