void Update() { if (_alive && chasing) { //if very far away zombie speed = 0: idle animation //if somewhat close zombie speed = walking speed: walking animation //if near zombie speed = 8.0*walking speed:running animation Quaternion initial = playerObject.transform.rotation; Vector3 diff = playerObject.transform.position - transform.position; float range = diff.magnitude; if (range > maxDistance && !waiting) //very far away and we want to redirect toward player //anim.SetInteger("zombieToState", 0); { transform.rotation = Quaternion.LookRotation(Vector3.forward, diff); _multiplier = 0.1f; //playerCaught = false; } else if (range > closeToPlayer && range <= maxDistance && !waiting) //off screen so can just stay still { _multiplier = 0f; playerCaught = false; if (siren != null) { siren.TurnOff(); } } else if (range <= closeToPlayer && !waiting) //on screen so have wander { _multiplier = 4f; if (range <= visibility && (playerObject.GetComponent <PlayerMovement>().isBoost() || playerCaught || playerObject.GetComponent <PlayerMovement>().isLooting() || isPirate)) { if (!playerCaught) { playerCaught = true; } transform.rotation = Quaternion.LookRotation(Vector3.forward, diff); if (running) { transform.Rotate(new Vector3(0, 0, 180)); } if (range <= tooClose) { _multiplier = 0.0f; } else { _multiplier = 7.0f; } } else { playerCaught = false; if (siren != null) { siren.TurnOff(); } if (shouldTurn >= 4) { shouldTurn = 0; //float angle = Random.Range(0, 360); //Debug.Log("randomTurn"); transform.Rotate(new Vector3(0, 0, Random.Range(0, 360))); } else { shouldTurn += Time.deltaTime; } } } else { //normal distance away, let wander if (range <= visibility && !running && !isPirate && (playerObject.GetComponent <PlayerMovement>().isBoost() || playerCaught || playerObject.GetComponent <PlayerMovement>().isLooting())) { if (siren != null) { siren.TurnOn(); } playerCaught = true; } _multiplier = 4.0f; //dont move if not close to player } //cargoship is within range and player has not yet looted it if (running && !playerObject.GetComponent <PlayerMovement>().isLooting()) { if (!canLoot && range <= tooClose) { canLoot = true; gameObject.GetComponent <CargoShip>().CanLoot(); } else if (canLoot && range > tooClose) { canLoot = false; gameObject.GetComponent <CargoShip>().ClearLootState(); } } //cargoship was being looted, but player stopped, so reset progress if (canLoot && !(playerObject.GetComponent <PlayerMovement>().isLooting()) && beingLooted > 0) { Debug.Log("Resetloot timer"); beingLooted = 0; gameObject.GetComponent <CargoShip>().StartLooting(); } //Looting cargo ship if (running && range <= tooClose && playerObject.GetComponent <PlayerMovement>().isLooting()) { //playerObject.transform.rotation = initial; Debug.Log("Cargoship being looted"); playerCaught = true; _multiplier = 0.0f; if (beingLooted < Time.deltaTime) { GetComponent <CargoShip>().StartLooting(); } if (beingLooted > 2) { GetComponent <ReactiveTarget>().ReactToHit(); _scoreController.AddScore(1000); RunStats.Current.CargoShipsLooted++; } else { beingLooted += Time.deltaTime; GetComponent <CargoShip>().SetLootProgress(beingLooted / 2f); } //add different particle effect? } _leftThrust.SetIntensity(_multiplier / 7f); _rightThrust.SetIntensity(_multiplier / 7f); transform.Translate(0, 0.5f * Time.deltaTime * _multiplier * speed, 0); //Debug.DrawRay(transform.position + transform.up*0.57f , transform.up * 10f, Color.red); // If it hits something... int grnd = 1 << LayerMask.NameToLayer("Default"); int fly = 1 << LayerMask.NameToLayer("Player"); int mask = grnd | fly; RaycastHit2D hit = Physics2D.Raycast(transform.position + transform.up * 0.57f, transform.up, 8f, mask); if (hit.collider != null) { GameObject hitObject = hit.transform.gameObject; if (hitObject.GetComponent <PlayerMovement>()) { if (!running && hitObject.GetComponent <PlayerMovement>().alive&& playerCaught) { if (siren != null && !isPirate) { siren.TurnOn(); } // if(hit.distance < 0.05f){ // hitObject.GetComponent<PlayerHealth>().Die(); // Debug.Log("caught charcter"); // chasing = false; // } //else{ //shoot at player if (reloadTime >= timeBetweenshot) { reloadTime = 0; AudioSource.PlayClipAtPoint(lazerShot, Camera.main.transform.position); Instantiate(lazer, new Vector3(transform.position.x, transform.position.y, 0), transform.rotation); } else { reloadTime += Time.deltaTime * 50; } //} } } else if (hitObject.GetComponent <EnemyLazer>() || hitObject.GetComponent <RepairStation>() || hitObject.GetComponent <FuelStation>()) { Debug.Log("saw collier that should be ignored"); } else { if (hit.distance < 0.2) { GetComponent <ReactiveTarget>().ReactToHit(); } else if (hit.distance < obstacleRange) { if (goLeft(hit)) { transform.Rotate(0, 0, 10); } else { transform.Rotate(0, 0, -10); } waitFor = 300f * Time.deltaTime; waiting = true; //extraTurn = true; } } } else { _leftThrust.SetIntensity(0f); _rightThrust.SetIntensity(0f); if (waitFor > 0) { // if(extraTurn){ // transform.Rotate(0,0,10); // extraTurn = false; // } waitFor -= 1f * Time.deltaTime; } else if (waiting) { waiting = false; } } } }
// Update is called once per frame void Update() { if (alive) { if (Input.GetKeyDown(KeyCode.Escape)) { if (GameState.shared.paused) { _hud.Unpause(); } else { _hud.Pause(); } } if (isDisabled()) { _leftThruster.SetIntensity(0); _rightThruster.SetIntensity(0); } else { RunStats.Current.Duration += Time.deltaTime; } if (Input.GetKeyDown(KeyCode.Space) && !boost && !isDisabled()) { // Start charging boost _chargingBoost = true; _boostAmount = _minBoost; dashIndicator.size = DashIndicatorSize(); } else if (!isDisabled() && _chargingBoost && Input.GetKey(KeyCode.Space)) { // Continue charging boost _boostAmount += Time.deltaTime * _chargeRate; if (_boostAmount > _maxBoost) { _boostAmount = _maxBoost; } dashIndicator.size = DashIndicatorSize(); } else if (_chargingBoost && (Input.GetKeyUp(KeyCode.Space) || _boostAmount > _maxBoost)) { // End charging and actually boost _chargingBoost = false; dashIndicator.size = DashIndicatorSize(); boostTime = _boostAmount; boost = true; curBoostTime = 0; boostEffect.Play(); _boostAmount = 0f; } if (boost) { curBoostTime += Time.deltaTime; if (curBoostTime > boostTime) //boost end { boost = false; velocity = this.transform.up * 8; boostEffect.Stop(); } else { var BOOST_HIT_RADIUS = 0.4f; this.transform.position += this.transform.up * Time.deltaTime * boostSpeed; _leftThruster.SetIntensity(1); _rightThruster.SetIntensity(1); LayerMask mask = LayerMask.GetMask("Default"); var overlappedColliders = Physics2D.OverlapCircleAll(transform.position, BOOST_HIT_RADIUS, mask); foreach (var collider in overlappedColliders) { var hitObject = collider.gameObject; var ai = hitObject.GetComponent <WanderingAI>(); if (ai != null && ai._alive) { var isCargoship = ai.running; ai.looseHealth(1); if (ai.getHealth() <= 0) { hitObject.GetComponent <ReactiveTarget>().ReactToHit(); if (isCargoship) { _scoreController.AddScore(200); } else if (ai.isPirate) { _scoreController.AddScore(500); } else { _scoreController.AddScore(50); } if (isCargoship) { RunStats.Current.CargoShipsDestroyed++; } else { RunStats.Current.PoliceShipsDestroyed++; } } } } } } else { var hInput = Input.GetAxis("Horizontal"); var vInput = Input.GetAxis("Vertical"); //Rotating the ship float rotation = -hInput * rotationSpeed * Time.deltaTime; this.transform.Rotate(0, 0, rotation); var leftThrust = vInput; var rightThrust = vInput; if (Mathf.Abs(hInput) > 0.1f) { if (Mathf.Abs(vInput) < 0.1f) { leftThrust = hInput; rightThrust = -hInput; } else { leftThrust += hInput * 0.1f; rightThrust -= hInput * 0.1f; } } //The propulsion force, in the direction the ship is pointed Vector2 propulsion = Vector2.zero; if (!isDisabled()) { propulsion = transform.up * (propForce * vInput * propulsionCoeff); _leftThruster.SetIntensity(leftThrust); _mainThruster.SetIntensity(vInput); _rightThruster.SetIntensity(rightThrust); } Vector2 totalForce = Vector2.zero; //If the player presses shift if ((Input.GetKey(KeyCode.LeftShift) && !isDisabled())) { //Don't calculate forces from gravity or propulsion //Apply a force opposite to the velocity to stop the ship //Should we add gravity to this? Or is that too much totalForce = -(velocity.normalized) * stoppingForce; } else if (Input.GetKey(KeyCode.L) && _fuel.GetFuel() > 0) { totalForce = -(velocity.normalized) * stoppingForce; } //Otherwise, calculate gravity and propulsion like normal else { GameObject[] cachedPlanets = new GameObject[planets.Count]; planets.CopyTo(cachedPlanets); Vector2 drag = 0.5f * velocity.magnitude * velocity.magnitude * dragForce * velocity.normalized; totalForce = gravity(cachedPlanets) + propulsion - drag; } if (Input.GetKeyDown(KeyCode.L)) { _looting = true; setDisabled(true); //want to apply some force here to slow down so stays in range, similar to leftshift } if (Input.GetKeyUp(KeyCode.L)) { _looting = false; setDisabled(false); } //Applying the force to the ship applyForce(totalForce); } } if (propulsionCoeff > 1) { speedUpTimer -= Time.deltaTime; } if (speedUpTimer <= 0) { propulsionCoeff = 1; speedEffect.Stop(); } }