Skip to content
Snippets Groups Projects
Enemy.cs 1.59 KiB
Newer Older
  • Learn to ignore specific revisions
  • using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public abstract class Enemy : Fighter
    {
    
    
        [SerializeField, Tooltip("How close the AntiPlayer has to be in order for this Enemy to start attacking")]
    
        protected float playerDistanceToAttack = 1.25f;
    
        protected GameObject antiPlayer;
    
        protected override void Awake()
        {
            base.Awake();
            antiPlayer = GameObject.FindGameObjectWithTag("AntiPlayer");
    
            opponentTag = antiPlayer.tag;
    
        }
    
        protected override void Update()
        {
            base.Update();
    
            if (alive)
            {
                // Always face the AntiPlayer
                var scale = transform.localScale;
                scale.x = transform.position.x < antiPlayer.transform.position.x ? -initalScale.x : initalScale.x;
                transform.localScale = scale;
            }
    
        }
    
        protected override bool CanAttack()
        {
            return GetDistanceToAntiPlayer() < playerDistanceToAttack;
        }
    
        protected override void Attack()
        {
            animator.SetTrigger("Attack");
        }
    
    
        public override void OnRoundStart()
        {
            base.OnRoundStart();
            animator.ResetTrigger("Attack");
        }
    
    
        public override void OnRoundEnd()
        {
            // Only run this when a round was actually running; OnRoundEnd is also called in the initialization phase.
            if (roundRunning && !alive)
            {
                Destroy(gameObject);
                return;
            }
            base.OnRoundEnd();
        }
    
    
        protected float GetDistanceToAntiPlayer()
        {
            return Vector3.Distance(transform.position, antiPlayer.transform.position);
        }
    
    }