コード例 #1
0
    public void AStartAlgorithm()       //A*.
    {
        _ListHoler = _StartRoom;

        #region PreSearch

        if (_ListHoler.Count == 1)          //if im standing on the roomconnector, then this is true (and if im inside a room with only 1 roomconnector)
        {
            _CurrentRoom = _ListHoler [0];
            _CurrentRoom.RoomNode.NodeSearchedThrough = true;
            _OpenList [_OpenListAtIndex++]            = _CurrentRoom; //adding the only roomconnector

            _ListHoler = _CurrentRoom.ConnectorHubOne.Connectors;     //one of the rooms connectorhubs connected to this roomconnector o_O
            if (_ListHoler != null)
            {
                for (int i = 0; i < _ListHoler.Count; i++)
                {
                    _ObjectHolder = _ListHoler [i];
                    if (_ObjectHolder.RoomNode.NodeSearchedThrough == false)
                    {
                        _ObjectHolder.RoomNode.SetParentAndEndRoom(_CurrentRoom.RoomNode, _EndNode [0]);
                        _ObjectHolder.GetLeftOrRight   = _CurrentRoom.ConnectorHubOne;         //this tells me that when im going through this room im going to search on the other side of the room (1 roomconnector have 2 sides)
                        _OpenList [_OpenListAtIndex++] = _ObjectHolder;                        //adding the roomconnector to an array
                    }
                }
            }

            _ListHoler = _CurrentRoom.ConnectorHubTwo.Connectors;
            if (_ListHoler != null)
            {
                for (int i = 0; i < _ListHoler.Count; i++)
                {
                    _ObjectHolder = _ListHoler [i];
                    if (_ObjectHolder.RoomNode.NodeSearchedThrough == false)
                    {
                        _ObjectHolder.RoomNode.SetParentAndEndRoom(_CurrentRoom.RoomNode, _EndNode [0]);
                        _ObjectHolder.GetLeftOrRight   = _CurrentRoom.ConnectorHubTwo;
                        _OpenList [_OpenListAtIndex++] = _ObjectHolder;
                    }
                }
            }
        }
        else            //if true then im inside a room which means that i must add all roomconnectors connected to this room

        {
            if (_ListHoler == _ListHoler [0].ConnectorHubOne.Connectors)
            {
                _ConnectorHub = _ListHoler [0].ConnectorHubOne;
                for (int i = 0; i < _ListHoler.Count; i++)
                {
                    _ListHoler [i].RoomNode.NodeSearchedThrough = true;
                    _ListHoler [i].GetLeftOrRight  = _ConnectorHub;
                    _OpenList [_OpenListAtIndex++] = _ListHoler [i];
                }
            }
            else
            {
                _ConnectorHub = _ListHoler [0].ConnectorHubTwo;
                for (int i = 0; i < _ListHoler.Count; i++)
                {
                    _ListHoler [i].RoomNode.NodeSearchedThrough = true;
                    _ListHoler [i].GetLeftOrRight  = _ConnectorHub;
                    _OpenList [_OpenListAtIndex++] = _ListHoler [i];
                }
            }
        }

        #endregion

        while (_OpenListAtIndex > 0)          //if the loop has itterated enough times to fill the array then stop

        {
            _LowerstFScore = 100000;

            for (int i = 0; i < _OpenListAtIndex; i++)              //searching through the array to find the room closest to the end
            {
                _ObjectHolder = _OpenList[i];
                if (_ObjectHolder.RoomNode.FCost < _LowerstFScore)                  //it was faster to make fcost public and just get the variable then go through a getmethod
                {
                    _CurrentRoom    = _ObjectHolder;
                    _RoomIndexSaved = i;
                    _LowerstFScore  = _ObjectHolder.RoomNode.FCost;
                }
            }

            _ClosedList [_ClosedListAtIndex++] = _CurrentRoom;                   //adding room to list
            _OpenList [_RoomIndexSaved]        = _OpenList [--_OpenListAtIndex]; //moving the last room in openlist to the index currentroom were at

            _ListHoler = _EndRoom;
            for (int i = 0; i < _ListHoler.Count; i++)              //checking if the room im in is connected to the end room
            {
                if (_CurrentRoom != _ListHoler [i])
                {
                }
                else
                {
                    RemakePath(_CurrentRoom);
                    return;
                }
            }

            if (_CurrentRoom.GetLeftOrRight != _CurrentRoom.ConnectorHubOne)              //checking which side of the pathconnector that have been searched, then go through its neighbours and see if something is closer to the target
            {
                _ListHoler = _CurrentRoom.ConnectorHubOne.Connectors;
                for (int i = 0; i < _ListHoler.Count; i++)
                {
                    _ObjectHolder = _ListHoler [i];
                    if (_ObjectHolder.RoomNode.NodeSearchedThrough != true)                      //if true then this object have been searched through , if not set info
                    {
                        _ObjectHolder.RoomNode.SetParentAndEndRoom(_CurrentRoom.RoomNode, _EndNode [0]);
                        _ObjectHolder.GetLeftOrRight   = _CurrentRoom.ConnectorHubOne;
                        _OpenList [_OpenListAtIndex++] = _ObjectHolder;
                    }
                    else if (_ObjectHolder.RoomNode.GCost > _CurrentRoom.RoomNode.GCost + _CurrentRoom.RoomNode.GetJustWorldSpaceDistance(_ObjectHolder.RoomNode))                         //checking if currentroom is closer to the objectholder then its parent is
                    {
                        _ObjectHolder.RoomNode.SetParentRoom(_CurrentRoom.RoomNode);
                    }
                }
            }
            else                //same logic just different side of the room
            {
                _ListHoler = _CurrentRoom.ConnectorHubTwo.Connectors;
                for (int i = 0; i < _ListHoler.Count; i++)
                {
                    _ObjectHolder = _ListHoler [i];
                    if (_ObjectHolder.RoomNode.NodeSearchedThrough != true)                      //if n dont have a parent and n isnt the starting node (starts must be there else it will cause an FATAL error ;-P, your warned :D)
                    {
                        _ObjectHolder.RoomNode.SetParentAndEndRoom(_CurrentRoom.RoomNode, _EndNode [0]);
                        _ObjectHolder.GetLeftOrRight   = _CurrentRoom.ConnectorHubTwo;
                        _OpenList [_OpenListAtIndex++] = _ObjectHolder;
                    }
                    else if (_ObjectHolder.RoomNode.GCost > _CurrentRoom.RoomNode.GCost + _CurrentRoom.RoomNode.GetJustWorldSpaceDistance(_ObjectHolder.RoomNode))
                    {
                        _ObjectHolder.RoomNode.SetParentRoom(_CurrentRoom.RoomNode);
                    }
                }
            }
        }
    }
コード例 #2
0
    [HideInInspector] public List <RoomConnectorCreating> NeighbourGroups = new List <RoomConnectorCreating>(); //all objects that are moving need to know in which room they are


    public virtual void SetAiRoom(Wall_ID room)     //just called once, and that is when spawning an object
    {
        NeighbourGroups = room.Connectors;
    }
コード例 #3
0
    public BoxCollider2D WalkingColliders;  //Used For The AI To Calculate The Node Path Towards An Object


    public override void SetAiRoom(Wall_ID room)     //just called once, and that is when spawning an object
    {
        NeighbourGroups = room.Connectors;
        TheObject._CreateThePath.SetStartRoom(room.Connectors);
    }