Example #1
0
    public void Execute()
    {
        List <Node> path = null;

        switch (m_searchType)
        {
        case eSearchType.DFS:
            SearchDFS.Build(m_source, m_destination, out path);
            break;

        case eSearchType.BFS:
            SearchBFS.Build(m_source, m_destination, out path);
            break;

        case eSearchType.DIJKSTRA:
            //SearchDIJKSTRA.Build(m_source, m_destination, out path);
            break;

        case eSearchType.A_STAR:
            SearchAStar.Build(m_source, m_destination, out path);
            break;

        default:
            break;
        }
        if (path != null)
        {
            m_agent.waypoint = path[0].GetComponentInChildren <Waypoint>();
            if (m_warpAgentToSource)
            {
                m_agent.transform.position = m_agent.waypoint.transform.position;
            }
            for (int i = 0; i < path.Count - 1; i++)
            {
                Waypoint waypoint = path[i].GetComponentInChildren <Waypoint>();
                waypoint.nextWaypoint = path[i + 1].GetComponentInChildren <Waypoint>();
            }

            foreach (Node pathNode in path)
            {
                if (pathNode.type == Node.eType.STANDARD)
                {
                    pathNode.type = Node.eType.PATH;
                }
            }

            GameObject[] gameObjects = GameObject.FindGameObjectsWithTag("Node");
            foreach (GameObject go in gameObjects)
            {
                Node node = go.GetComponent <Node>();
                if (node)
                {
                    if (node.visited && node.type == Node.eType.STANDARD)
                    {
                        node.type = Node.eType.VISITED;
                    }
                }
            }
        }
    }
        public Vector Execute(Tank tank, int timeElapsed)
        {
            Vector steeringForce = new Vector(0, 0);

            //Keep following path until finished

            if (tank.PlayerInAttackZone())
            {
                ClearPath();
                tank.ChangeState(new TankAttackPlayer());
            }
            else if (tank.PlayerNotSeenAtLastLocation())
            {
                ClearPath();
                tank.ChangeState(new TankPatrol(tank));
            }
            else if (path.Count == 0)
            {
                tank.gameWorld.GridLogic.CalculateNeighborsEntities(tank, Tank.MaxRadiusOfTankSeight);
                foreach (MovingEntity entity in tank.gameWorld.GridLogic.EntitiesInRange)
                {
                    if (entity is Player)
                    {
                        searchAStar = new SearchAStar(tank, entity.InCell);
                        searchAStar.Search();
                        path = searchAStar.GetPathToTarget();
                        if (path.Count == 0)
                        {
                            return(steeringForce);
                        }
                        //Path starts from end of list so the path following needs to be started at the end of the list
                        i             = path.Count - 1;
                        steeringForce = seek.Execute(tank, path[i].Position) * GlobalVars.SeekingWeight;
                    }
                }
            }

            else
            {
                if (i > 0)
                {
                    //Seek current cell in path
                    steeringForce = seek.Execute(tank, path[i].Position) * GlobalVars.SeekingWeight;
                    // steeringForce += avoid.Execute(tank) * GlobalVars.ObstacleAvoidanceWeight;

                    //go to next step in path if the tank approximatly reached the cell
                    if (tank.DistanceToPosition(path[i].Position) <= GlobalVars.cellSize / 2)
                    {
                        i--;
                    }
                }
                else
                {
                    ClearPath();
                }
            }


            return(steeringForce);
        }
 public void UpdateSearchInfoForSoldier(SearchAStar.PathInfo pathinfo)
 {
     mSearchTimeText.text = pathinfo.TimeTaken.ToString();
     mNodesSearchedText.text = pathinfo.NodesSearched.ToString();
     mEdgesSearchedText.text = pathinfo.EdgesSearched.ToString();
     mDistance.text = pathinfo.CostToTarget.ToString();
 }
Example #4
0
    /// <summary>
    ///     Attempts to move this enemy towards the player.
    /// </summary>
    public void SeekPlayer()
    {
        var playerObj = FindObjectOfType <Player>();

        var pathFinder = new SearchAStar(this, transform.position, playerObj.transform.position,
                                         new ManhattanDistance(playerObj.transform.position));
        var destination = pathFinder.Search();

        if (destination == null)
        {
            Debug.Log("Pathfinding: Enemy cannot find valid path to target! " + transform);
            return;
        }

        var direction = destination[0].Destination - (Vector2)transform.position;

        AttemptMove <Component>((int)direction.x, (int)direction.y);
    }
Example #5
0
 // Use this for initialization
 void Start()
 {
     mPathFinder = FindObjectOfType (typeof(PathFinder)) as PathFinder;
     Debug.Assert (mPathFinder != null);
     mNavGraph = mPathFinder.NavGraph;
     mAstarSearch = new SearchAStar(mNavGraph, mSourceCellIndex, mTargetCellIndex, mIgnoreWall, mStrickDistance, mHCostPercentage, mBDrawExplorePath, mExplorePathRemainTime);
     Debug.Assert(mNavGraph != null);
 }