Exemple #1
0
    // Update is called once per frame
    void Update()
    {
        CheckJobs();
        // If destination has been set recently, generate a new path
        // destinationQueued is true when "destination" is outdated and a new path must be generated
        if (destinationQueued)
        {
            Vector2 position = mover.GetDiscretePosition();
            path = Grapher.FindPath(position, destination, maxPathLength);

            if (path.Count > 0 && waypointEnabled)
            {
                Destroy(waypoint);
                waypoint = Instantiate(Globals.WAYPOINT, destination, Quaternion.identity);
            }

            if (path.Count > 1)
            {
                pathProgress = Grapher.ManhattanDistance(path[0], transform.position) == 1 ? 0 : 1;
            }
            destinationQueued = false;

            // If path does not exist, stop trying to navigate there
            if (path.Count == 0)
            {
                if (waypointEnabled)
                {
                    Destroy(waypoint);
                }
                Pause(true);
            }
        }

        if (mover.GetCanTurn() && !pausePathFinding && ActionManager.GetState() == ActionManager.State.Moving)
        {
            // Move along path
            if (path.Count > 1)
            {
                if (pathProgress < path.Count)
                {
                    Vector2 direction = path[pathProgress] - (Vector2)transform.position;

                    //if(pathProgress > 0 && (Vector2)transform.position != path[pathProgress - 1])
                    //    Debug.Log("WARNING: Expected position " + transform.position + " to be " + path[pathProgress - 1]);
                    mover.ChangeDirection(direction, running);
                    pathProgress += 1;

                    GameObject tempNoise = Instantiate(Globals.NOISE, transform.position, Quaternion.identity);
                    tempNoise.GetComponent <Noise>().Initialize(CompareTag("Player"), running ? Globals.RUN_VOLUME : Globals.WALK_VOLUME, Noise.Source.Footsteps); // bad
                }
                else
                {
                    // Upon losing sight of player or reaching waypoint
                    SetIdle(true);
                }
            }
        }
    }
Exemple #2
0
    // Update is called once per frame
    void Update()
    {
        // This is going to get really hard. Check for player AND dead bodies
        // Behave differently when a body is spotted.

        //for(int i = 0; i < subjects.Count; ++i)
        //{
        Vector2 origin    = transform.position; // + rotator.FrontOffset();
        Vector2 direction = (Vector2)player.transform.position - origin;

        targetSighted = false;

        float angle = Vector2.Angle(rotator.FrontOffset(), direction);

        // If player is in cone of vision, and there is an uninterrupted view
        // Or, if player is touching
        if ((angle < peripheralVision && ClearShot(origin, direction)) ||
            GridMover.Touching(gameObject, player.gameObject))
        {
            //rotator.FacePoint(player.GetDiscretePosition());
            targetSighted = true;
            location      = player.GetDiscretePosition();
            // AutoMover should determine what to do with this information
            //mover.SetChasing(true);
        }

        /*if(Vector2.Distance(transform.position, player.transform.position) < 1.5f)
         * {
         *  targetSighted = true;
         *  location = player.GetDiscretePosition();
         *  rotator.FacePoint(player.GetDiscretePosition());
         *  mover.SetChasing(true);
         * }*/
        //}
    }
Exemple #3
0
    // Update is called once per frame
    void Update()
    {
        Vector2 origin    = transform.position;// + rotator.FrontOffset();
        Vector2 direction = (Vector2)player.transform.position - origin;

        float angle = Vector2.Angle(rotator.FrontOffset(), direction);

        if (angle < peripheralVision)
        {
            RaycastHit2D hit = Physics2D.Raycast(origin, direction, sightDistance, mask);
            if (hit.collider != null && hit.collider.CompareTag("Player"))
            {
                mover.SetDestination(player.GetDiscretePosition(), true);
                mover.SetChasing(true);
            }
        }
    }
Exemple #4
0
    // Update is called once per frame
    void Update()
    {
        if (stunCounter > 0)
        {
            stunCounter -= Time.deltaTime;
            return;
        }
        else if (confusedStun || courtesyStun)
        {
            // if (confusedStun)
            //destination = savedDestination;
            if (courtesyStun)
            {
                SetChasing(true);
            }
            confusedStun = false;
            courtesyStun = false;
        }

        float distanceToPlayer = Vector2.Distance(transform.position, player.GetDiscretePosition());

        if (TouchingPlayer())
        {
            SetChasing(true);
            //SetDestination(player.transform.position, true);

            if (!player.gameObject.GetComponent <Flasher>().IsFlashing())
            {
                //player.gameObject.GetComponent<ParticleSystem>().Play();
                Camera.main.GetComponent <Jerk>().Shake(1);
                source.PlayOneShot(punch);
                player.gameObject.GetComponent <Flasher>().Flash(1);
                player.gameObject.GetComponent <Health>().TakeDamage();
            }
        }
    }
Exemple #5
0
    // Update is called once per frame
    void Update()
    {
        clearView    = ClearView();
        canSeePlayer = CanSeePoint(PlayerMover.instance.transform.position);

        if (stunTimer <= 0)
        {
            if (awareness == State.Stun)
            {
                SetAwareness(State.Suspicious);
            }
            attackCooldown -= Time.deltaTime;
        }
        else
        {
            stunTimer -= Time.deltaTime;
            // Only execute right when stunTimer flips to 0
            if (stunTimer <= 0)
            {
                navigator.Pause(false);
            }
        }

        if (glancesLeft > 0 || glanceTimer > 0)
        {
            LookAround();
        }

        timeSinceVisual += Time.deltaTime;
        if (awareness != State.Alert)
        {
            CheckForCorpses();
        }
        CheckForTarget();

        // If the player can be seen attack them
        // THIS DOES NOT ACCOUNT FOR ANGLE
        if (attackCooldown <= 0 && canSeePlayer)
        {
            VisualToPosition(player.GetDiscretePosition());
            Attack();
        }

        // Resolve direction enemy should be facing (lock onto player while alerted)
        Vector2 destination = navigator.GetDestination();

        // Lock on if suspicious or alert, AND clear view to point of interest,
        // Lock on if player can be seen, OR distance away is > 1
        Vector2 lockOnPoint = canSeePlayer ? (Vector2)player.transform.position : destination;
        bool    closeLockOn = Grapher.ManhattanDistance(destination, transform.position) > 1 || canSeePlayer;

        // bool lockedOn = awareness >= State.Suspicious && ClearView(destination) && closeLockOn;
        //Debug.Log(lockedOn);
        rotator.ToggleLock(awareness >= State.Suspicious && ClearView(destination) && closeLockOn, lockOnPoint);

        /* The two above should probably be combined, but we want to address the case where an enemy might be in the way of another enemy...
         * Double check if mask makes this possible? Enemies should be able to hit each other, but NOT if they're overlapping each other.
         * Could fake raycast to start one normalized length away from center point? */

        // Move toward position (maybe create EnemyNavigator derived from Navigator, replaces AutoMover)
        // float distanceToPlayer = Vector2.Distance(transform.position, player.GetDiscretePosition());

        EnemyMove();
    }
Exemple #6
0
    // Update is called once per frame
    void Update()
    {
        if (stunCounter > 0)
        {
            stunCounter -= Time.deltaTime;
            return;
        }
        else if (confusedStun || courtesyStun)
        {
            if (confusedStun)
            {
                destination = savedDestination;
            }
            if (courtesyStun)
            {
                SetChasing(true);
            }
            confusedStun = false;
            courtesyStun = false;
        }

        float distanceToPlayer = Vector2.Distance(transform.position, player.GetDiscretePosition());

        if (TouchingPlayer())
        {
            SetChasing(true);
            SetDestination(player.transform.position, true);

            if (!player.gameObject.GetComponent <Flasher>().IsFlashing())
            {
                //player.gameObject.GetComponent<ParticleSystem>().Play();
                Camera.main.GetComponent <Jerk>().Shake(1);
                source.PlayOneShot(punch);
                player.gameObject.GetComponent <Flasher>().Flash(1);
                player.gameObject.GetComponent <Health>().TakeDamage();
            }
        }

        if (!chasing)
        {
            destination = route[routeProgress % route.Count];
            if (Vector2.Distance(transform.position, destination) == 0)
            {
                ++routeProgress;
                if (route.Count == 1 && !chasing)
                {
                    GetComponent <Rotator>().Rotate(startAngle);
                }
            }
        }

        if (mover.GetCanTurn() && (distanceToPlayer > 1 || !chasing))
        {
            graph = MakeGraph(transform.position);
            List <int> path = FindPath(VectorToIndex(destination));
            if (path.Count > 1)
            {
                int     nextTileIndex = path[1];
                Vector2 nextTile      = graph[nextTileIndex];
                Vector2 hereToThere   = nextTile - (Vector2)transform.position;
                mover.ChangeDirection(hereToThere);
            }
            else
            {
                if (chasing)
                {
                    crossInstance.GetComponent <SpriteRenderer>().enabled = false;
                    ConfuseStun(2);
                }
                SetChasing(false);
                destination = route[routeProgress % route.Count];
            }
        }
    }