public override State Update(FSMAgent agent)
    {
        //Handle Following Pacman
        Vector3 pacmanLocation = PacmanInfo.Instance.transform.position;

        if (agent.CloseEnough(pacmanLocation))
        {
            ScoreHandler.Instance.KillPacman();
        }

        //If timer complete, go to Scatter State
        if (agent.TimerComplete())
        {
            return(new ScatterState(new Vector3(-ObstacleHandler.Instance.Width, -ObstacleHandler.Instance.Height), new CustomHuntState()));
        }

        //If Pacman ate a power pellet, go to Frightened State
        if (PelletHandler.Instance.JustEatenPowerPellet)
        {
            return(new FrightenedState(this));
        }

        if (!ObstacleHandler.Instance.AnyIntersect(pacmanLocation, agent.GetPosition()))
        {
            return(new CustomExcitedChaseState());
        }
        //If we didn't return follow Pacman
        agent.SetTarget(pacmanLocation);

        //Stay in this state
        return(this);
    }
Пример #2
0
    public override State Update(FSMAgent agent)
    {
        //Handle Following Pacman
        Vector3  pacmanLocation = PacmanInfo.Instance.transform.position;
        Vector3  pacmanFacing = PacmanInfo.Instance.Facing;
        Vector3  location, realLocation;
        FSMAgent ghost = GhostManager.Instance.GetClosestGhost(pacmanLocation);

        if (agent.CloseEnough(pacmanLocation))
        {
            ScoreHandler.Instance.KillPacman();
        }

        //If timer complete, go to Scatter State
        if (agent.TimerComplete())
        {
            return(new CustomChaseState());
        }

        //If Pacman ate a power pellet, go to Frightened State
        if (PelletHandler.Instance.JustEatenPowerPellet)
        {
            return(new FrightenedState(this));
        }

        if (!ObstacleHandler.Instance.AnyIntersect(pacmanLocation, agent.GetPosition()))
        {
            return(new CustomExcitedChaseState());
        }

        //If we didn't return follow Pacman
        if (ghost is CustomGhost)
        {
            agent.SetTarget(pacmanLocation);
        }
        else
        {
            if (pacmanFacing.x != 0)
            {
                location = ghost.GetPosition() + Vector3.up * 2 * (pacmanLocation.y - ghost.GetPosition().y);
            }
            else
            {
                location = ghost.GetPosition() + Vector3.right * 2 * (pacmanLocation.x - ghost.GetPosition().x);
            }
            GraphNode g = HW3NavigationHandler.Instance.NodeHandler.ClosestNode(location);
            realLocation = g.Location;
            agent.SetTarget(realLocation);
        }
        //Stay in this state
        return(this);
    }
Пример #3
0
    public override State Update(FSMAgent agent)
    {
        //Handle Following Pacman
        Vector3 targetLocation;
        Vector3 pacmanLocation = PacmanInfo.Instance.transform.position;

        if (agent.CloseEnough(pacmanLocation))
        {
            ScoreHandler.Instance.KillPacman();
        }

        //If timer complete, go to Scatter State
        if (agent.TimerComplete())
        {
            return(new ScatterState(new Vector3(-ObstacleHandler.Instance.Width, -ObstacleHandler.Instance.Height), this));
        }

        //If Pacman ate a power pellet, go to Frightened State
        if (PelletHandler.Instance.JustEatenPowerPellet)
        {
            return(new FrightenedState(this));
        }
        if (Vector3.Distance(pacmanLocation, agent.GetPosition()) < 1.6f)
        {
            targetLocation = new Vector3(-ObstacleHandler.Instance.Width, -ObstacleHandler.Instance.Height);
        }
        //If we didn't return follow Pacman
        else
        {
            targetLocation = pacmanLocation;
        }

        GraphNode g = HW3NavigationHandler.Instance.NodeHandler.ClosestNode(targetLocation);
        Vector3   realtargetPosition = g.Location;

        agent.SetTarget(realtargetPosition);
        //Stay in this state
        return(this);
    }
Пример #4
0
    // Update is called once per frame
    void Update()
    {
        bool     runningAway = false;
        FSMAgent ghost       = GhostManager.Instance.GetClosestGhost(transform.position);

        if (ghost != null)
        {
            Vector3 vecToGhost = ghost.GetPosition() - transform.position;
            if (vecToGhost.sqrMagnitude <= 1f)
            {
                runningAway = true;
                //CalculatePath
                GraphNode closestStart = HW3NavigationHandler.Instance.NodeHandler.ClosestNode(transform.position);
                GraphNode closestGoal  = HW3NavigationHandler.Instance.NodeHandler.ClosestNode(transform.position + vecToGhost.normalized * -0.6f + Vector3.right * Random.Range(-0.1f, 0.1f) + Vector3.down * Random.Range(-0.1f, 0.1f));
                path = HW3NavigationHandler.Instance.PathFinder.CalculatePath(closestStart, closestGoal);
                if (path == null || path.Length < 1)
                {
                    SetTarget(new Vector3(Random.Range(-1 * ObstacleHandler.Instance.Width, ObstacleHandler.Instance.Width), Random.Range(-1 * ObstacleHandler.Instance.Height, ObstacleHandler.Instance.Height)));
                }
                else
                {
                    pathIndex = 0;
                    SetTarget(path[pathIndex]);
                }
            }
        }

        if (!runningAway)
        {
            Pellet p = PelletHandler.Instance.GetClosestPellet(transform.position);
            if (p != null)
            {
                Vector3 target = p.transform.position;

                //CalculatePath
                GraphNode closestStart = HW3NavigationHandler.Instance.NodeHandler.ClosestNode(transform.position);
                GraphNode closestGoal  = HW3NavigationHandler.Instance.NodeHandler.ClosestNode(target);
                path = HW3NavigationHandler.Instance.PathFinder.CalculatePath(closestStart, closestGoal);

                if (path == null || path.Length <= 1)
                {
                    SetTarget(target);
                }
                else
                {
                    pathIndex = 0;
                    SetTarget(path[pathIndex]);
                }
            }
            else
            {
                movingTowardTarget = false;
            }
        }


        if (movingTowardTarget)
        {
            if ((target - transform.position).sqrMagnitude < AgentConstants.THRESHOLD)
            {
                movingTowardTarget = false;
                transform.position = target;
            }
            else
            {
                Vector3 potentialNewPosition = transform.position + (target - transform.position).normalized * Time.deltaTime * speed;
                if (ObstacleHandler.Instance.AnyIntersect(new Vector2(transform.position.x, transform.position.y), new Vector2(potentialNewPosition.x, potentialNewPosition.y)))
                {
                    movingTowardTarget = false;
                }
                else
                {
                    transform.position = potentialNewPosition;
                }
            }
        }
    }