eventGenerateBehavior.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class eventGenerateBehavior : MonoBehaviour
  5. {
  6. public GameObject ctrlr;
  7. public GameObject person;
  8. public int currCmd;
  9. public int numOfParkTerm;
  10. public float speed;
  11. // 门对象,开门后自动进入并关门,上锁和开门由controller控制
  12. public GameObject[] doors;
  13. public GameObject[] myObject;
  14. public int[] waitingForDoors = { 0, 0, 0, 0, 0, 0 };
  15. public int[] myState = { 0, 0, 0, 0, 0, 0 };
  16. private float prevTime;
  17. private static float[] termPosX = { 1.2f, 6.2f, 8.7f, 13.7f, 16.2f, 21.2f };
  18. // 0空闲 1停车 2取车
  19. private System.Random rnd;
  20. // Start is called before the first frame update
  21. void Start()
  22. {
  23. speed = 2.5f;
  24. myObject = new GameObject[6];
  25. numOfParkTerm = 3;
  26. doors = GameObject.FindGameObjectsWithTag("door");
  27. // 门排序
  28. float x = 100f;
  29. int index = -1;
  30. for (int i = 0; i < doors.Length; i++)
  31. {
  32. for (int j = i; j < doors.Length; j++)
  33. {
  34. if (doors[j].transform.position.x < x)
  35. {
  36. x = doors[j].transform.position.x;
  37. index = j;
  38. }
  39. }
  40. GameObject temp = doors[index];
  41. doors[index] = doors[i];
  42. doors[i] = temp;
  43. x = 100f;
  44. index = -1;
  45. }
  46. prevTime = Time.time;
  47. currCmd = -1;
  48. rnd = new System.Random();
  49. }
  50. /// <summary>
  51. /// 机械手调用,停车抓车后清除
  52. /// </summary>
  53. public void ClearObject(int termIndex, bool clearCmd)
  54. {
  55. if (clearCmd)
  56. {
  57. myState[termIndex - 1] = 0;
  58. }
  59. if(myObject[termIndex - 1] != null)
  60. {
  61. Destroy(myObject[termIndex - 1]);
  62. myObject[termIndex - 1] = null;
  63. }
  64. }
  65. /// <summary>
  66. /// 生成0-5终端指令
  67. /// </summary>
  68. /// <returns></returns>
  69. int GenerateCmd()
  70. {
  71. int place=-1;
  72. ////myState[place] = 1;
  73. //if (rnd == null)
  74. // rnd = new System.Random();
  75. //// 定时器30s
  76. //if (Time.time - prevTime > 5)
  77. //{
  78. // int count = 5;
  79. // prevTime = Time.time;
  80. // // 创建准备停车车辆或取车用户
  81. // while (count-- > 0 && place == -1)
  82. // {
  83. // place = rnd.Next(0, 6);
  84. // if (myState[place] == 0)
  85. // {
  86. // myState[place] = place < numOfParkTerm ? 1 : 2;
  87. // break;
  88. // }
  89. // else place = -1;
  90. // }
  91. //}
  92. return place;
  93. }
  94. // Update is called once per frame
  95. void Update()
  96. {
  97. int cmd = GenerateCmd();
  98. //currCmd = cmd;
  99. if (currCmd != -1)
  100. {
  101. // 停车
  102. if(myState[currCmd] == 1 && myObject[currCmd] == null)
  103. {
  104. myObject[currCmd] = ctrlr.GetComponent<controller>().CreateCar(new Vector3(termPosX[currCmd], 0, 8));
  105. }
  106. // 取车
  107. else if(myState[currCmd] == 2 && myObject[currCmd] == null)
  108. {
  109. myObject[currCmd] = Instantiate(person);
  110. myObject[currCmd].transform.position = new Vector3(termPosX[currCmd], 0, 8);
  111. }
  112. }
  113. // 更新所有终端前对象状态
  114. for (int i = 0; i < myState.Length; i++)
  115. {
  116. // 停车
  117. if(myState[i] == 1)
  118. {
  119. if (myObject[i] != null)
  120. {
  121. if (!doors[i].GetComponent<doorBehavior>().close)
  122. {
  123. // 开门后,车辆入场, 入场后关门
  124. waitingForDoors[i] = 0;
  125. if (myObject[i].transform.position.z > -2.9f)
  126. {
  127. myObject[i].transform.Translate(Vector3.forward * speed * Time.deltaTime);
  128. //Debug.Log(speed * Time.deltaTime);
  129. //Debug.Log("pos: " + myObject[i].transform.position);
  130. }else if (!doors[i].GetComponent<doorBehavior>().moving)
  131. {
  132. doors[i].GetComponent<doorBehavior>().close = true;
  133. ctrlr.GetComponent<controller>().doorLock[i] = 1;
  134. }
  135. }
  136. else if(myObject[i].transform.position.z > -2.8f && !doors[i].GetComponent<doorBehavior>().moving)
  137. {
  138. waitingForDoors[i] = 1;
  139. }
  140. }
  141. else
  142. {
  143. waitingForDoors[i] = 0;
  144. }
  145. }
  146. // 取车
  147. else if(myState[i] == 2 && myObject[i] != null)
  148. {
  149. // 人往终端走
  150. if (myObject[i].transform.position.z > 0.5f)
  151. {
  152. myObject[i].transform.Translate(Vector3.back * speed * Time.deltaTime);
  153. }
  154. }
  155. }
  156. }
  157. }