Exemple #1
0
        private bool _Search(ref RayMap inRayMap, ref Queue <MyV2IPair> supplyQueue, ref List <Vector2Int> avoidList, ref Stack <MovementStatus> revMovementStack, ref StorageTree storageTree)
        {
            //Queue<MyV2IPair> myV2IPairQueue_Saved = new Queue<MyV2IPair>();

            while (supplyQueue.Count > 0)
            {
                MyV2IPair myV2IPair = supplyQueue.Dequeue();

                Vector2Int currentPos = myV2IPair.current;

                if (avoidList.Contains(currentPos))
                {
                    continue;
                }
                else if (currentPos.x < 0 || currentPos.x > inRayMap.size.x || currentPos.y < 0 || currentPos.y > inRayMap.size.y)
                {
                    continue;
                }

                else if (currentPos == inRayMap.endPoint) // founded
                {
                    int             tempIndexn = storageTree.Add(myV2IPair);
                    StorageTreeNode iterator   = storageTree.data[tempIndexn];// this point is not registered in the tree yet

                    try
                    {
                        while (iterator.father != null)
                        {
                            if (iterator.data.x == iterator.father.data.x)
                            {
                                if (iterator.data.y == iterator.father.data.y + 1)
                                {
                                    revMovementStack.Push(MovementStatus.MovingUp);
                                }
                                else if (iterator.data.y == iterator.father.data.y - 1)
                                {
                                    revMovementStack.Push(MovementStatus.MovingDown);
                                }
                                else
                                {
                                    //error
                                }
                            }
                            else if (iterator.data.y == iterator.father.data.y)
                            {
                                if (iterator.data.x == iterator.father.data.x + 1)
                                {
                                    revMovementStack.Push(MovementStatus.MovingRight);
                                }
                                else if (iterator.data.x == iterator.father.data.x - 1)
                                {
                                    revMovementStack.Push(MovementStatus.MovingLeft);
                                }
                                else
                                {
                                    //error
                                }
                            }
                            else
                            {
                                //error
                            }
                            iterator = iterator.father;
                        }
                        return(true);
                    }
                    catch (System.Exception e)
                    {
                        EditorControl.EditorPause();
                        Debug.Log(e);
                        return(false);
                    }
                }
                else //normal Node
                {
                    avoidList.Add(currentPos);
                    storageTree.Add(myV2IPair);
                    //myV2IPairQueue_Saved.Enqueue(myV2IPair);

                    //enqueue
                    switch (_IdentifyDirection(myV2IPair.current, myV2IPair.previous)) //trend to walk a straight line
                    {
                    case MovementStatus.MovingUp:
                        if (currentPos.y < inRayMap.size.y - 1 && !avoidList.Contains(currentPos + new Vector2Int(0, 1)))
                        {
                            supplyQueue.Enqueue(new MyV2IPair(currentPos + new Vector2Int(0, 1), currentPos));
                        }
                        if (currentPos.x < inRayMap.size.x - 1 && !avoidList.Contains(currentPos + new Vector2Int(1, 0)))
                        {
                            supplyQueue.Enqueue(new MyV2IPair(currentPos + new Vector2Int(1, 0), currentPos));
                        }
                        if (currentPos.x > 0 && !avoidList.Contains(currentPos + new Vector2Int(-1, 0)))
                        {
                            supplyQueue.Enqueue(new MyV2IPair(currentPos + new Vector2Int(-1, 0), currentPos));
                        }
                        if (currentPos.y > 0 && !avoidList.Contains(currentPos + new Vector2Int(0, -1)))
                        {
                            supplyQueue.Enqueue(new MyV2IPair(currentPos + new Vector2Int(0, -1), currentPos));
                        }
                        break;

                    case MovementStatus.MovingRight:
                        if (currentPos.x < inRayMap.size.x - 1 && !avoidList.Contains(currentPos + new Vector2Int(1, 0)))
                        {
                            supplyQueue.Enqueue(new MyV2IPair(currentPos + new Vector2Int(1, 0), currentPos));
                        }
                        if (currentPos.y < inRayMap.size.y - 1 && !avoidList.Contains(currentPos + new Vector2Int(0, 1)))
                        {
                            supplyQueue.Enqueue(new MyV2IPair(currentPos + new Vector2Int(0, 1), currentPos));
                        }
                        if (currentPos.y > 0 && !avoidList.Contains(currentPos + new Vector2Int(0, -1)))
                        {
                            supplyQueue.Enqueue(new MyV2IPair(currentPos + new Vector2Int(0, -1), currentPos));
                        }
                        if (currentPos.x > 0 && !avoidList.Contains(currentPos + new Vector2Int(-1, 0)))
                        {
                            supplyQueue.Enqueue(new MyV2IPair(currentPos + new Vector2Int(-1, 0), currentPos));
                        }
                        break;

                    case MovementStatus.MovingDown:
                        if (currentPos.y > 0 && !avoidList.Contains(currentPos + new Vector2Int(0, -1)))
                        {
                            supplyQueue.Enqueue(new MyV2IPair(currentPos + new Vector2Int(0, -1), currentPos));
                        }
                        if (currentPos.x < inRayMap.size.x - 1 && !avoidList.Contains(currentPos + new Vector2Int(1, 0)))
                        {
                            supplyQueue.Enqueue(new MyV2IPair(currentPos + new Vector2Int(1, 0), currentPos));
                        }
                        if (currentPos.x > 0 && !avoidList.Contains(currentPos + new Vector2Int(-1, 0)))
                        {
                            supplyQueue.Enqueue(new MyV2IPair(currentPos + new Vector2Int(-1, 0), currentPos));
                        }
                        if (currentPos.y < inRayMap.size.y - 1 && !avoidList.Contains(currentPos + new Vector2Int(0, 1)))
                        {
                            supplyQueue.Enqueue(new MyV2IPair(currentPos + new Vector2Int(0, 1), currentPos));
                        }
                        break;

                    case MovementStatus.MovingLeft:
                        if (currentPos.x > 0 && !avoidList.Contains(currentPos + new Vector2Int(-1, 0)))
                        {
                            supplyQueue.Enqueue(new MyV2IPair(currentPos + new Vector2Int(-1, 0), currentPos));
                        }
                        if (currentPos.y < inRayMap.size.y - 1 && !avoidList.Contains(currentPos + new Vector2Int(0, 1)))
                        {
                            supplyQueue.Enqueue(new MyV2IPair(currentPos + new Vector2Int(0, 1), currentPos));
                        }
                        if (currentPos.y > 0 && !avoidList.Contains(currentPos + new Vector2Int(0, -1)))
                        {
                            supplyQueue.Enqueue(new MyV2IPair(currentPos + new Vector2Int(0, -1), currentPos));
                        }
                        if (currentPos.x < inRayMap.size.x - 1 && !avoidList.Contains(currentPos + new Vector2Int(1, 0)))
                        {
                            supplyQueue.Enqueue(new MyV2IPair(currentPos + new Vector2Int(1, 0), currentPos));
                        }
                        break;

                    default:
                        if (currentPos.y < inRayMap.size.y - 1 && !avoidList.Contains(currentPos + new Vector2Int(0, 1)))
                        {
                            supplyQueue.Enqueue(new MyV2IPair(currentPos + new Vector2Int(0, 1), currentPos));
                        }
                        if (currentPos.x < inRayMap.size.x - 1 && !avoidList.Contains(currentPos + new Vector2Int(1, 0)))
                        {
                            supplyQueue.Enqueue(new MyV2IPair(currentPos + new Vector2Int(1, 0), currentPos));
                        }
                        if (currentPos.y > 0 && !avoidList.Contains(currentPos + new Vector2Int(0, -1)))
                        {
                            supplyQueue.Enqueue(new MyV2IPair(currentPos + new Vector2Int(0, -1), currentPos));
                        }
                        if (currentPos.x > 0 && !avoidList.Contains(currentPos + new Vector2Int(-1, 0)))
                        {
                            supplyQueue.Enqueue(new MyV2IPair(currentPos + new Vector2Int(-1, 0), currentPos));
                        }
                        break;
                    }
                }
            }
            return(false);
        }