using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class Enemy : MonoBehaviour { [SerializeField] private int MaxHealth = 2; private int Health; public bool Alive = true; public event Action OnHealthChanged = delegate { }; private Animator enemyAnimator = null; private PlayerManagement pm; private void Awake() { Health = MaxHealth; enemyAnimator = GetComponent(); pm = GameObject.Find("/OmniCharacterController").GetComponent(); GetComponent().setEnemyAnimatior(enemyAnimator); SetUpEnemyBodyParts(); } private void SetUpEnemyBodyParts() { EnemyBodyPart[] bodyParts = GetComponentsInChildren(); foreach(EnemyBodyPart enemyBodyPart in bodyParts) { enemyBodyPart.Setup(this); } } public void TakeDamage(int damage, bool arrowOnFire, bool headShot) { if (!Alive) return; if (headShot || arrowOnFire) damage *= 2; Health -= damage; float currentHealth = (float)Health / (float)MaxHealth; OnHealthChanged(currentHealth); if (Health <= 0) Kill(arrowOnFire, headShot); else // Add 1 point when hitting an enemy pm.AddPoints(1); } public void Kill(bool arrowOnFire, bool headShot) { Alive = false; Destroy(gameObject.transform.Find("HealthBarCanvas").gameObject); pm.EvaluatePointsOnKill(headShot, arrowOnFire); if(enemyAnimator != null) { if (!arrowOnFire) enemyAnimator.runtimeAnimatorController = Resources.Load("Animations/DeathByArrow/DeathByArrowController") as RuntimeAnimatorController; else enemyAnimator.runtimeAnimatorController = Resources.Load("Animations/DeathByBurn/DeathByBurnController") as RuntimeAnimatorController; GetComponent().removeEnemySocket(); // Find animation time and destroy enemy once animation ends Destroy(gameObject, enemyAnimator.runtimeAnimatorController.animationClips[0].length - 0.2f); } else { Destroy(gameObject); } } }