Prechádzať zdrojové kódy

取车逻辑debug,中跑车楼层5-6楼运动问题debug

yct 5 rokov pred
rodič
commit
6a7649d170
2 zmenil súbory, kde vykonal 75 pridanie a 69 odobranie
  1. 53 47
      Assets/controller.cs
  2. 22 22
      Assets/eventGenerateBehavior.cs

+ 53 - 47
Assets/controller.cs

@@ -26,11 +26,11 @@ public class controller : MonoBehaviour
     /// </summary>
     public int numOfParkTerm;
     public int[] termState = { 0, 0, 0, 0, 0, 0 };
-    private int[] currTermVar = { 0, 0, 0, 0, 0, 0 };
+    public int[] currTermVar = { 0, 0, 0, 0, 0, 0 };
     private int[] scanned = { 0, 0, 0, 0, 0, 0 };
     private static int[] termIndex = { 4, 6, 7, 9, 10, 12 };
     private List<CarInfo> movingCars;
-    private Dictionary<int, int> parkSpace;
+    public int[] parkSpace;
     private bool initialized;
     private float prevTime;
     // 当前处理指令0-5
@@ -78,14 +78,10 @@ public class controller : MonoBehaviour
         {
             temp = movingCars.Find((CarInfo ci) => { return ci.Equals(temp); });
             int space = (temp.floor - 2) * 15 + temp.space;
-            if (parkSpace.ContainsKey(space))
+            if (parkSpace[space] != 0)
             {
                 parkSpace[space] = 0;
             }
-            else
-            {
-                parkSpace.Add(space, 0);
-            }
             movingCars.Remove(temp);
             Destroy(car);
         }
@@ -99,14 +95,7 @@ public class controller : MonoBehaviour
             temp.state = 2;
             movingCars.Add(temp);
             int spaceNo = (floor - 2) * 15 + space;
-            if (!parkSpace.ContainsKey(spaceNo))
-            {
-                parkSpace.Add(spaceNo, 1);
-            }
-            else
-            {
-                parkSpace[spaceNo] = 1;
-            }
+            parkSpace[spaceNo] = 1;
         }
     }
 
@@ -180,7 +169,7 @@ public class controller : MonoBehaviour
             movingCars = new List<CarInfo>();
             initialized = true;
             currIndex = 0;
-            parkSpace = new Dictionary<int, int>();
+            parkSpace = new int[165];
             //// 中跑车排序
             //int floor = 13;
             //int index = -1;
@@ -228,14 +217,18 @@ public class controller : MonoBehaviour
         //Debug.Log("00000");
         if (parkSpace == null)
         {
-            parkSpace = new Dictionary<int, int>();
+            parkSpace = new int[165];
             for (int i = 1; i < 166; i++)
             {
-                parkSpace.Add(i, 0);
+                parkSpace[i] = 0;
             }
         }
-        //Debug.Log("11111");
+        //Debug.Log("currIndex: "+currIndex);
         //Debug.Log("currIndex: "+currIndex +", state: "+currTermVar[currIndex]);
+        if (currIndex > 5)
+        {
+            currIndex = 0;
+        }
         if (currTermVar[currIndex] == 0)
         {
             currIndex++;
@@ -275,34 +268,39 @@ public class controller : MonoBehaviour
                 //Debug.Log("找空位");
                 for (int i = 46; i < 166; i++)
                 {
-                    if (parkSpace.ContainsKey(i) && parkSpace[i] == 0)
-                    {
-                        parkSpaceIndex = i;
-                    }
-                    else if (!parkSpace.ContainsKey(i))
+                    //Debug.Log("检测到停车指令,车位" + i + ": " + parkSpace[i]);
+                    if (parkSpace[i] == 0)
                     {
-                        parkSpace.Add(i, 0);
-                        parkSpaceIndex = i;
+                        parkSpaceIndex = i; break;
                     }
-                    for (int j = 0; j < movingCars.Count; j++)
-                    {
-                        if ((movingCars[j].floor - 2) * 15 + movingCars[j].space == parkSpaceIndex)
-                        {
-                            parkSpaceIndex = 0;
-                            break;
-                        }
-                    }
-                    if (parkSpaceIndex != 0) break;
+                    //for (int j = 0; j < movingCars.Count; j++)
+                    //{
+                    //    if ((movingCars[j].floor - 2) * 15 + movingCars[j].space == parkSpaceIndex)
+                    //    {
+                    //        parkSpaceIndex = 0;
+                    //        break;
+                    //    }
+                    //}
+                    //if (parkSpaceIndex != 0) break;
                 }
             }
         }
         // 取车位指令不断遍历
         else if (currTermVar[currIndex] == 2)
         {
-            //Debug.Log("55555");
+            Debug.Log(movingCars.Count);
+            if (movingCars.Count <= 0)
+            {
+                currIndex++;
+                if (currIndex > 5)
+                {
+                    currIndex = 0;
+                }
+            }
             // 取车指令已就位
-            if (fetchTermIndex != -1)
+            else if (fetchTermIndex != -1)
             {
+                Debug.Log("55555");
                 //不是遍历到的编号,则跳过直到找到正确编号为止
                 if (fetchTermIndex != currIndex)
                 {
@@ -311,10 +309,14 @@ public class controller : MonoBehaviour
                 }
                 else
                 {
-                    while (fetchSpaceIndex <= 0 && movingCars.Count>0)
+                    while (fetchSpaceIndex <= 0 && movingCars.Count > 0)
                     {
                         int index = Mathf.FloorToInt(Random.value * movingCars.Count);
-                        fetchSpaceIndex = (movingCars[index].floor-2)*15+ movingCars[index].space;
+                        fetchSpaceIndex = (movingCars[index].floor - 2) * 15 + movingCars[index].space;
+                    }
+                    if (parkSpace[fetchSpaceIndex] > 0)
+                    {
+                        parkSpace[fetchSpaceIndex] = 0;
                     }
                 }
             }
@@ -358,7 +360,8 @@ public class controller : MonoBehaviour
                 }
                 if (orderedTrans[2].GetComponent<transferBehavior>().state == 0)
                 {
-                    Debug.Log("上左 车状态0");
+                    //Debug.Log("上左 车状态0");
+                    //Debug.Log(parkX +", "+ orderedTrans[2].GetComponent<transferBehavior>().GetSpace());
                     // 空中跑车不同层不取车
                     if (fetchFloor != floor && orderedTrans[2].GetComponent<transferBehavior>().load == null)
                     {
@@ -385,7 +388,7 @@ public class controller : MonoBehaviour
                         orderedTrans[2].GetComponent<transferBehavior>().SetTarget(floor, 13);
                     }
                     // 停车中跑车有车,送入车位
-                    else if (parkUsingTrans == orderedTrans[2] && orderedTrans[2].GetComponent<transferBehavior>().load != null && parkFloor == floor && parkX != orderedTrans[2].GetComponent<transferBehavior>().GetSpace())
+                    else if (parkUsingTrans == orderedTrans[2] && orderedTrans[2].GetComponent<transferBehavior>().load != null && parkFloor == floor /*&& parkX != orderedTrans[2].GetComponent<transferBehavior>().GetSpace()*/)
                     {
                         orderedTrans[2].GetComponent<transferBehavior>().SetTarget(floor, parkX);
                         if (parkX <= 2 && elevator_right.GetComponent<elevatorBehavior>().state == 0 && elevator_right.GetComponent<elevatorBehavior>().load == null)
@@ -437,7 +440,7 @@ public class controller : MonoBehaviour
                     targetFloor = 6;
                 }
                 // 停车中跑车上行,修改目标楼层
-                else if (parkFloor > 2 && parkUsingTrans == orderedTrans[1] && orderedTrans[1].GetComponent<transferBehavior>().load != null)
+                if (parkFloor > 2 && parkUsingTrans == orderedTrans[1] && orderedTrans[1].GetComponent<transferBehavior>().load != null)
                 {
                     targetFloor = parkFloor;
                 }
@@ -595,10 +598,10 @@ public class controller : MonoBehaviour
     {
         if (parkSpace == null)
         {
-            parkSpace = new Dictionary<int, int>();
+            parkSpace = new int[165];
             for (int i = 1; i < 166; i++)
             {
-                parkSpace.Add(i, 0);
+                parkSpace[i] = 0;
             }
         }
         switch (state)
@@ -986,8 +989,11 @@ public class controller : MonoBehaviour
             // 检查取车终端是否空闲,且有人等待,有则发送取车指令
             else if (i >= numOfParkTerm && termState[i] == 0 && objs[i] != null && objs[i].transform.position.z <= 0.5f)
             {
-                termState[i] = 2;
-                eventGenerator.GetComponent<eventGenerateBehavior>().ClearObject(i + 1, true);
+                if (movingCars != null && movingCars.Count > 0)
+                {
+                    termState[i] = 2;
+                    eventGenerator.GetComponent<eventGenerateBehavior>().ClearObject(i + 1, true);
+                }
             }
         }
     }
@@ -1006,7 +1012,7 @@ public class controller : MonoBehaviour
             {
                 UpdateCmd();
             }
-            catch (System.Exception e){ Debug.Log(e.StackTrace+"\n异常。。。。。"); }
+            catch (System.Exception e) { Debug.Log(e.StackTrace + "\n异常。。。。。\n" + e.Message); }
             CheckState();
             TransferMove();
             //BasicStateMachine();

+ 22 - 22
Assets/eventGenerateBehavior.cs

@@ -23,7 +23,7 @@ public class eventGenerateBehavior : MonoBehaviour
     // Start is called before the first frame update
     void Start()
     {
-        speed = 0.5f;
+        speed = 2.5f;
         myObject = new GameObject[6];
         numOfParkTerm = 3;
         doors = GameObject.FindGameObjectsWithTag("door");
@@ -74,33 +74,33 @@ public class eventGenerateBehavior : MonoBehaviour
     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 : -1;// 2;
-                    break;
-                }
-                else 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();
+        int cmd = GenerateCmd();
         //currCmd = cmd;
         if (currCmd != -1)
         {