using System.Collections; using System.Collections.Generic; using UnityEngine; public interface IFighterCallback { void OnFighterDeath(Fighter fighter); } // Generic subclass for any Fighter on the field, whether it's an Enemy or the AntiPlayer. // Contains stats management such as health, attacks, damage, death, etc. public abstract class Fighter : MonoBehaviour { [SerializeField] int baseHealth = 100; [SerializeField, Tooltip("Attack every x seconds")] float baseAttackSpeed = 1f; [SerializeField] int baseAttackDamage = 10; [SerializeField] int baseArmor = 1; public List<IFighterCallback> callbacks = new List<IFighterCallback>(); [SerializeField] protected int currentHealth = 100; protected Animator animator; protected new Rigidbody2D rigidbody; protected BoxCollider2D collider; protected bool alive { get => currentHealth > 0; } protected FighterTypes fighterType; protected string opponentTag; protected Vector3 initalScale; protected Vector3 initalPosition; protected abstract bool CanAttack(); protected abstract void Attack(); float timeSinceLastAttack = float.PositiveInfinity; protected virtual void Awake() { animator = GetComponent<Animator>(); rigidbody = GetComponent<Rigidbody2D>(); collider = GetComponent<BoxCollider2D>(); fighterType = FighterTypes.ENEMY; initalPosition = transform.position; initalScale = transform.localScale; } protected virtual void Start() { currentHealth = Mathf.RoundToInt(baseHealth * GetStats().healthMultiplier); } protected virtual void Update() { if (alive) { if (CanAttack()) { timeSinceLastAttack += Time.deltaTime; var timeUntilNextAttack = baseAttackSpeed - timeSinceLastAttack; if (timeUntilNextAttack <= 0) { Attack(); timeSinceLastAttack = float.IsInfinity(timeUntilNextAttack) ? 0 : -timeUntilNextAttack; // To account for overshoot } } else { timeSinceLastAttack = float.PositiveInfinity; } } } public void DealDamage(int dmg) { if (!alive) { return; } var actualDamage = Mathf.Max(0, Mathf.RoundToInt(dmg - baseArmor * GetStats().armorMultiplier)); currentHealth = Mathf.Max(currentHealth - actualDamage, 0); if (currentHealth == 0) { animator.SetTrigger("Death"); callbacks.ForEach(c => c.OnFighterDeath(this)); // Disable own collider so that other Fighters can run over our dead body collider.enabled = false; // Let's not fall through the world, without any active colliders rigidbody.bodyType = RigidbodyType2D.Static; } else { animator.SetTrigger("Hurt"); } } protected FighterStats GetStats() { return StatsManager.instance.fighterStats[fighterType]; } void OnTriggerEnter2D(Collider2D other) { #if false Debug.Log(string.Format("'{0}' (tag '{1}') collided with '{2}' (tag '{3}') in parent '{4}' (tag '{5}')", name, tag, other.name, other.tag, other.transform.parent ? other.transform.parent.name : null, other.transform.parent ? other.transform.parent.tag : null)); #endif if (other.CompareTag(opponentTag)) { var damageToDeal = Mathf.RoundToInt(baseAttackDamage * GetStats().damageMultiplier); other.GetComponent<Fighter>().DealDamage(damageToDeal); } } }