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
        //Unfortunately this ghost is too good to lose sight of you most of the time, but if you want you can add this to make it harder
        //if (agent.TimerComplete() && ObstacleHandler.Instance.AnyIntersect(pacmanLocation,agent.GetPosition()))
        if (agent.TimerComplete())
        {
            return(new CustomTiredChaseState());
        }

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

        //If we didn't return follow Pacman
        agent.SetTarget(pacmanLocation);

        //Stay in this state
        return(this);
    }
Exemplo n.º 2
0
    public override State Update(FSMAgent agent)
    {
        //Handle Following Pacman
        Vector3 pacmanLocation = PacmanInfo.Instance.transform.position;
        Vector3 pacmanFacing   = PacmanInfo.Instance.Facing;

        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 we didn't return follow Pacman
        //Debug.Log(pacmanFacing);
        Vector3   targetLocation = pacmanLocation + pacmanFacing * 0.8f;
        GraphNode g = HW3NavigationHandler.Instance.NodeHandler.ClosestNode(targetLocation);
        Vector3   realtargetPosition = g.Location;

        agent.SetTarget(realtargetPosition);

        //Stay in this state
        return(this);
    }
    public override State Update(FSMAgent agent)
    {
        //Check and see if we've been eaten, if so become eyes
        Vector3 pacmanLocation = PacmanInfo.Instance.transform.position;

        if (agent.CloseEnough(pacmanLocation))
        {
            return(new EyesState(returnState));
        }

        //Check and see if our timer completed, if so return to returnState
        if (agent.TimerComplete())
        {
            return(returnState);
        }

        //Handle random movement
        if (randomTimer < RANDOM_TIMER_MAX)
        {
            randomTimer += Time.deltaTime;
        }
        else
        {
            randomTimer = 0;
            agent.SetTarget(new Vector3(Random.RandomRange(-1 * ObstacleHandler.Instance.Width, ObstacleHandler.Instance.Width), Random.RandomRange(-1 * ObstacleHandler.Instance.Height, ObstacleHandler.Instance.Height)));
        }
        //Stay in this state
        return(this);
    }
    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);
    }
Exemplo n.º 5
0
    //Check if we are close enough to start position, if so return to returnState
    public override State Update(FSMAgent agent)
    {
        if (agent.CloseEnough(AgentConstants.GHOST_START_POS))
        {
            return(returnState);
        }

        //Stay in this state
        return(this);
    }
Exemplo n.º 6
0
    //Upon entering state set timer and calculate scatter positions
    public override void EnterState(FSMAgent agent)
    {
        agent.SetTimer(7f);

        GraphNode g = HW3NavigationHandler.Instance.NodeHandler.ClosestNode(scatterPosition);

        realScatterPosition = g.Location;
        agent.SetTarget(realScatterPosition);
        Vector3 innerPosition = Vector3.Lerp(Vector3.zero, realScatterPosition, 0.8f);

        realInnerPosition = HW3NavigationHandler.Instance.NodeHandler.ClosestNode(innerPosition + Vector3.left * realScatterPosition.x / 3 + Vector3.down * realScatterPosition.y / 6).Location;
    }
Exemplo n.º 7
0
    void Awake()
    {
        if( mInstance != null )
        {
            Debug.LogError( string.Format( "Only one instance of FSMAgent allowed! Destroying:" + gameObject.name +", Other:" + mInstance.gameObject.name ) );
            return;
        }

        mInstance = this;

        FSMs = new List<PlayMakerFSM>();
    }
Exemplo n.º 8
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);
    }
Exemplo n.º 9
0
    public override void OnEnter(FSMAgent agent, Action callback)
    {
        base.OnEnter(agent, callback);

        if (currentTransform == null)
        {
            currentIndex     = GetNearlyTarget();
            currentTransform = patrollerPoints[currentIndex];
        }

        navmesh.isStopped = false;
        navmesh.SetDestination(currentTransform.position);
    }
Exemplo n.º 10
0
    public FSMAgent GetClosestGhost(Vector3 position)
    {
        float    minDist = 1000;
        FSMAgent closest = null;

        foreach (FSMAgent ghost in ghostsInPlay)
        {
            float dist = (ghost.GetPosition() - position).sqrMagnitude;
            if (dist < minDist)
            {
                minDist = dist;
                closest = ghost;
            }
        }
        return(closest);
    }
Exemplo n.º 11
0
    public override State Update(FSMAgent agent)
    {
        //Determine if we've killed Pacman
        Vector3 pacmanLocation = PacmanInfo.Instance.transform.position;

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

        //If we're done scattering set up return state
        if (agent.TimerComplete())
        {
            return(returnState);
        }

        //Handle Pacman eating power pellet and transitioning to Frightened State
        if (PelletHandler.Instance.JustEatenPowerPellet)
        {
            return(new FrightenedState(this));
        }

        //Handle scatter state movement logic
        if (currHeading == EDGE)
        {
            if (agent.CloseEnough(realScatterPosition))
            {
                currHeading = INNER1;

                agent.SetTarget(realInnerPosition);
            }
        }
        else if (currHeading == INNER1)
        {
            if (agent.CloseEnough(realInnerPosition))
            {
                currHeading = EDGE;

                agent.SetTarget(realScatterPosition);
            }
        }

        //Stay in state
        return(this);
    }
Exemplo n.º 12
0
    public override State Update(FSMAgent agent)
    {
        //Check if you killed pacman
        Vector3 pacmanLocation = PacmanInfo.Instance.transform.position;

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

        //Check if timer completes and should transition to scatter state
        if (agent.TimerComplete())
        {
            return(new ScatterState(new Vector3(ObstacleHandler.Instance.Width, -1 * ObstacleHandler.Instance.Height), this));
        }

        //Check if Pacman ate a power pellet and we should become frightened
        if (PelletHandler.Instance.JustEatenPowerPellet)
        {
            return(new FrightenedState(this));
        }

        //Handle movement logic
        pacmanLocation += PacmanInfo.Instance.Facing * 0.4f;

        if (blinky != null)
        {
            Vector3 relativeVector = pacmanLocation - blinky.GetPosition();
            pacmanLocation = blinky.GetPosition() + relativeVector * 2;
        }

        agent.SetTarget(pacmanLocation);

        //Stay in state
        return(this);
    }
Exemplo n.º 13
0
 //Upon entering state, set timer to enter Scatter State
 public override void EnterState(FSMAgent agent)
 {
     agent.SetTimer(20f);
 }
 //Upon entering state, start a timer, set speed modifier to half, and set up animations
 public override void EnterState(FSMAgent agent)
 {
     agent.SetAnimationStateFrightened();
     agent.SetSpeedModifierHalf();
     agent.SetTimer(12);
 }
Exemplo n.º 15
0
 public override void OnEnter(FSMAgent agent, Action callback)
 {
     base.OnEnter(agent, callback);
 }
Exemplo n.º 16
0
 public override void ExitState(FSMAgent agent)
 {
     base.ExitState(agent);
 }
Exemplo n.º 17
0
 //Upon entering state, set timer to enter Scatter State
 public override void EnterState(FSMAgent agent)
 {
     agent.SetTimer(5f);
     agent.SetSpeedModifierHalf();
 }
Exemplo n.º 18
0
 //Handle logic on exiting this state
 public virtual void ExitState(FSMAgent agent)
 {
 }
Exemplo n.º 19
0
 //Upon entering state set up modification, speed change, and set our target
 public override void EnterState(FSMAgent agent)
 {
     agent.SetAnimationStateEyes();
     agent.SetSpeedModifierDouble();
     agent.SetTarget(AgentConstants.GHOST_START_POS);
 }
 //Upon entering state, set timer to enter Scatter State
 public override void EnterState(FSMAgent agent)
 {
     agent.SetTimer(2f);
     agent.SetSpeedModifierDouble();
 }
Exemplo n.º 21
0
 //Handle each tick of behavior while in this state
 public virtual State Update(FSMAgent agent)
 {
     return(this);
 }
Exemplo n.º 22
0
 public override void ExitState(FSMAgent agent)
 {
 }
Exemplo n.º 23
0
 //Upon exiting state reset speed and animation state
 public override void ExitState(FSMAgent agent)
 {
     agent.SetAnimationStateNormal();
     agent.SetSpeedModifierNormal();
 }
Exemplo n.º 24
0
 //EnterState Start timer for scatter state and find the blinky ghost if it exists
 public override void EnterState(FSMAgent agent)
 {
     agent.SetTimer(20f);
     blinky = GameObject.FindObjectOfType <Blinky>();
 }
Exemplo n.º 25
0
 //Handle logic on entering this state
 public virtual void EnterState(FSMAgent agent)
 {
 }
Exemplo n.º 26
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;
                }
            }
        }
    }
 //Upon entering state, set timer to enter Scatter State
 public override void EnterState(FSMAgent agent)
 {
     agent.SetTimer(10f);
     agent.SetSpeedModifierNormal();
 }