// 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); } } } }
// Update is called once per frame void Update() { Vector2 input = GetInput(); if (input != Vector2.zero) { if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) { mover.moveSpeed = 6; } else { mover.moveSpeed = 3; } if (mover.ChangeDirection(input) && mover.moveSpeed == 6) { source.pitch = Random.Range(1f, 1.2f); source.Play(); Instantiate(noise, transform.position, Quaternion.identity); } } }
// 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]; } } }