Exemplo n.º 1
0
    public override void OnInspectorGUI()
    {
        KDNav2DAgent agent = (KDNav2DAgent)target;

        agent.Speed           = EditorGUILayout.FloatField("Speed", agent.Speed);
        agent.RepathThisAgent = EditorGUILayout.Toggle("Repath Agent", agent.RepathThisAgent);

        if (GUI.changed)
        {
            EditorUtility.SetDirty(agent);
        }
    }
Exemplo n.º 2
0
    //----------------------------------------------------------------------------------------------

    /// <summary>
    /// Dispatch routes of the agents on the list by FIFO
    /// </summary>
    /// <returns></returns>
    private IEnumerator GeneratePaths()
    {
        while (true)
        {
            if (m_agents.Count > 0)
            {
                KDNav2DAgent    agent = m_agents.First();
                List <KD2DTile> route = m_pathFinder.FindPath(agent);
                agent.SetRoute(route);
                m_agents.RemoveAt(0);
            }
            yield return(new WaitForEndOfFrame());
        }
    }
Exemplo n.º 3
0
        //------------------------------------------------------------------------------------------

        public List <KD2DTile> FindPath(KDNav2DAgent a_agent)
        {
            GameObject endTile = NearestTile(a_agent.Destination).gameObject;

            RestartMail(a_agent.NearestTile.gameObject, m_openList, m_closedList);

            if (endTile != a_agent.NearestTile.gameObject)
            {
                GameObject tileGo;
                Vector2    end = endTile.transform.position;
                do
                {
                    KD2DTile currentTile;
                    BinaryDelete(m_openList, m_closedList, out tileGo, out currentTile);

                    int         i          = 0;
                    List <Skew> dirs       = PathDirections();
                    Collider2D  collider2D = tileGo.collider2D;
                    collider2D.enabled = false;
                    do
                    {
                        Skew         dir = dirs[i];
                        RaycastHit2D hit = Physics2D.Raycast
                                           (
                            tileGo.transform.position,
                            dir.Direction.normalized,
                            dir.Direction.magnitude,
                            1 << m_tileLayer
                                           );

                        if (hit)
                        {
                            var hitTile = hit.collider.GetComponent <KD2DTile>();
                            if (hitTile && !hitTile.Occupied && !hitTile.IsBloked)
                            {
                                float nextG = currentTile.G + dir.Cost;
                                if (!hitTile.IsWrited)
                                {
                                    WriteTile(hitTile, tileGo, nextG, end);
                                    continue;
                                }

                                if (hitTile.G > nextG)
                                {
                                    WriteTile(hitTile, tileGo, nextG);
                                }
                            }
                            else
                            {
                                dir.RemoveFromList(dirs);
                            }
                        }
                        else
                        {
                            dir.RemoveFromList(dirs);
                        }
                    } while (++i < dirs.Count);
                    collider2D.enabled = true;
                } while (tileGo != endTile && m_openList.Count > 0);
            }

            var finalList = new List <KD2DTile>();

            if (endTile)
            {
                finalList = FinalList(endTile.GetComponent <KD2DTile>());
            }

            return(finalList);
        }
Exemplo n.º 4
0
    //----------------------------------------------------------------------------------------------

    /// <summary>
    /// Creates a request to obtain a route for find the way to reach the goal tile
    /// </summary>
    /// <param name="a_agent">Agent requesting a route</param>
    public void FindWay(KDNav2DAgent a_agent)
    {
        m_agents.Remove(a_agent);
        m_agents.Add(a_agent);
    }