123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class eventGenerateBehavior : MonoBehaviour
- {
- public GameObject ctrlr;
- public GameObject person;
- public int currCmd;
- public int numOfParkTerm;
- public float speed;
- // 门对象,开门后自动进入并关门,上锁和开门由controller控制
- public GameObject[] doors;
- public GameObject[] myObject;
- public int[] waitingForDoors = { 0, 0, 0, 0, 0, 0 };
- public int[] myState = { 0, 0, 0, 0, 0, 0 };
- private float prevTime;
- private static float[] termPosX = { 1.2f, 6.2f, 8.7f, 13.7f, 16.2f, 21.2f };
- // 0空闲 1停车 2取车
- private System.Random rnd;
- // Start is called before the first frame update
- void Start()
- {
- speed = 2.5f;
- myObject = new GameObject[6];
- numOfParkTerm = 3;
- doors = GameObject.FindGameObjectsWithTag("door");
- // 门排序
- float x = 100f;
- int index = -1;
- for (int i = 0; i < doors.Length; i++)
- {
- for (int j = i; j < doors.Length; j++)
- {
- if (doors[j].transform.position.x < x)
- {
- x = doors[j].transform.position.x;
- index = j;
- }
- }
- GameObject temp = doors[index];
- doors[index] = doors[i];
- doors[i] = temp;
- x = 100f;
- index = -1;
- }
- prevTime = Time.time;
- currCmd = -1;
- rnd = new System.Random();
- }
- /// <summary>
- /// 机械手调用,停车抓车后清除
- /// </summary>
- public void ClearObject(int termIndex, bool clearCmd)
- {
- if (clearCmd)
- {
- myState[termIndex - 1] = 0;
- }
- if(myObject[termIndex - 1] != null)
- {
- Destroy(myObject[termIndex - 1]);
- myObject[termIndex - 1] = null;
- }
- }
- /// <summary>
- /// 生成0-5终端指令
- /// </summary>
- /// <returns></returns>
- int GenerateCmd()
- {
- int place=-1;
- ////myState[place] = 1;
- //if (rnd == null)
- // rnd = new System.Random();
- //// 定时器30s
- //if (Time.time - prevTime > 5)
- //{
- // int count = 5;
- // prevTime = Time.time;
- // // 创建准备停车车辆或取车用户
- // while (count-- > 0 && place == -1)
- // {
- // place = rnd.Next(0, 6);
- // if (myState[place] == 0)
- // {
- // myState[place] = place < numOfParkTerm ? 1 : 2;
- // break;
- // }
- // else place = -1;
- // }
- //}
- return place;
- }
- // Update is called once per frame
- void Update()
- {
- int cmd = GenerateCmd();
- //currCmd = cmd;
- if (currCmd != -1)
- {
- // 停车
- if(myState[currCmd] == 1 && myObject[currCmd] == null)
- {
- myObject[currCmd] = ctrlr.GetComponent<controller>().CreateCar(new Vector3(termPosX[currCmd], 0, 8));
- }
- // 取车
- else if(myState[currCmd] == 2 && myObject[currCmd] == null)
- {
- myObject[currCmd] = Instantiate(person);
- myObject[currCmd].transform.position = new Vector3(termPosX[currCmd], 0, 8);
- }
- }
- // 更新所有终端前对象状态
- for (int i = 0; i < myState.Length; i++)
- {
- // 停车
- if(myState[i] == 1)
- {
- if (myObject[i] != null)
- {
- if (!doors[i].GetComponent<doorBehavior>().close)
- {
- // 开门后,车辆入场, 入场后关门
- waitingForDoors[i] = 0;
- if (myObject[i].transform.position.z > -2.9f)
- {
- myObject[i].transform.Translate(Vector3.forward * speed * Time.deltaTime);
- //Debug.Log(speed * Time.deltaTime);
- //Debug.Log("pos: " + myObject[i].transform.position);
- }else if (!doors[i].GetComponent<doorBehavior>().moving)
- {
- doors[i].GetComponent<doorBehavior>().close = true;
- ctrlr.GetComponent<controller>().doorLock[i] = 1;
- }
- }
- else if(myObject[i].transform.position.z > -2.8f && !doors[i].GetComponent<doorBehavior>().moving)
- {
- waitingForDoors[i] = 1;
- }
- }
- else
- {
- waitingForDoors[i] = 0;
- }
- }
- // 取车
- else if(myState[i] == 2 && myObject[i] != null)
- {
- // 人往终端走
- if (myObject[i].transform.position.z > 0.5f)
- {
- myObject[i].transform.Translate(Vector3.back * speed * Time.deltaTime);
- }
- }
- }
- }
- }
|