public override void Move() { // Handle enemy AI states switch (enemyPawn.GetState()) { // If the state is Recover, then the enemy will rise case EnemyPawn.State.Recover: if (enemyPawn.transform.position.y < enemyPawn.maxSmashHeight && !enemyPawn.smashing) { enemyPawn.transform.position += new Vector3(0f, pawn.speed * Time.deltaTime, 0f); // If the max smash height is reached, then the state is switched to smash } else { enemyPawn.SetState(EnemyPawn.State.Smash); } break; // If the state is Smash, then the enemy will drop super quickly and possibly kill the player case EnemyPawn.State.Smash: if (enemyPawn.transform.position.y > -1.42f) { // Set smashing to true enemyPawn.smashing = true; // Lower the enemy object each frame enemyPawn.transform.position -= new Vector3(0f, pawn.speed * Time.deltaTime * 15, 0f); // If the enemy pawn has reached the ground then switch to ram or recover } else { // Set smashing to false to allow the enemy to recover enemyPawn.smashing = false; if (enemyPawn.ramming == true) { enemyPawn.SetState(EnemyPawn.State.Ram); } else { enemyPawn.SetState(EnemyPawn.State.Recover); } } break; // If the state is Ram, then the enemy will move toward the player to try to kill them case EnemyPawn.State.Ram: if (!GameManager.instance.isDead) { float step = enemyPawn.speed * Time.deltaTime; enemyPawn.transform.position = Vector2.MoveTowards(enemyPawn.transform.position, enemyPawn.target.position, step * 2); } else { enemyPawn.SetState(EnemyPawn.State.Recover); } break; } }
// Once the player is out of eye sight, go back to the recover state private void OnTriggerExit2D(Collider2D collision) { EnemyPawn pawn = GetComponent <EnemyPawn>(); if (collision.gameObject.tag == "Player") { pawn.ramming = false; pawn.SetState(EnemyPawn.State.Recover); } }
// Destroy the enemy and play the enemy death sound private void OnCollisionEnter2D(Collision2D other) { // Find the audio source audioSource = GameObject.Find("Audio").GetComponent <AudioSource>(); /* * If the enemy and the player collided then the player will die * If the players ability collided with the enemy then the enemy will die */ if (other.gameObject.tag == "Enemy") { if (gameObject.tag == "Player") { if (!(other.collider is CircleCollider2D)) { audioSource.clip = GameManager.instance.playerDeath; audioSource.Play(); // Set isDead to true so that the respawn process can start GameManager.instance.isDead = true; // Decrement the player lives by one GameManager.instance.livesLeft -= 1; // Update the UI to show the new lives GameManager.instance.UpdateLives(GameManager.instance.livesLeft); EnemyPawn enemyPawn = other.gameObject.GetComponent <EnemyPawn>(); // Set the enemy state to recover since the player is dead enemyPawn.SetState(EnemyPawn.State.Recover); if (enemyPawn.ramming) { // Set ramming to false if its true other.gameObject.GetComponent <EnemyPawn>().ramming = false; } } } else { audioSource.clip = GameManager.instance.enemyDeath; audioSource.Play(); // Destroy the enemy game object Destroy(other.gameObject); GameManager.instance.currentEnemies -= 1; // Destroy the players fireball too if (gameObject.tag == "Weapon") { Destroy(gameObject); } } } }