Skip to content
Snippets Groups Projects
Enemy.cs 2.28 KiB
Newer Older
  • Learn to ignore specific revisions
  • Adrian Paschkowski's avatar
    Adrian Paschkowski committed
    // Base Enemy class, includes some functionality common to all Enemies, such as a reference
    // to the AntiPlayer and that they always face towards it.
    
    public abstract class Enemy : Fighter
    {
    
    
    Adrian Paschkowski's avatar
    Adrian Paschkowski committed
        [SerializeField] public Opponent opponentType;
    
        [SerializeField, Tooltip("How close the AntiPlayer has to be in order for this Enemy to start attacking")]
    
        protected float playerDistanceToAttack = 1.25f;
    
    Adrian Paschkowski's avatar
    Adrian Paschkowski committed
        [SerializeField, Tooltip("Whether the Enemy is facing left in the Sprite Sheet. Used to determine which way to face.")]
        bool spriteIsFacingLeft = true;
    
        protected GameObject antiPlayer;
    
        protected override void Awake()
        {
            base.Awake();
            antiPlayer = GameObject.FindGameObjectWithTag("AntiPlayer");
    
            opponentTag = antiPlayer.tag;
    
            RoundController.instance.placedEnemies.Add(this);
    
        }
    
        protected override void Update()
        {
            base.Update();
    
            if (alive && roundRunning)
    
            {
                // Always face the AntiPlayer
                var scale = transform.localScale;
                scale.x = transform.position.x < antiPlayer.transform.position.x ? -initalScale.x : initalScale.x;
    
    Adrian Paschkowski's avatar
    Adrian Paschkowski committed
                if (!spriteIsFacingLeft)
                {
                    scale.x = -scale.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(bool won)
    
    Adrian Paschkowski's avatar
    Adrian Paschkowski committed
            // Destroy all dead Enemies after a successful round. Otherwise, if either
            // the round was lost of the Enemy was still alive at the end of the round,
            // it will be resurrected.
    
            if (won && !alive)
    
            {
                Destroy(gameObject);
                return;
            }
    
            base.OnRoundEnd(won);
    
        protected float GetDistanceToAntiPlayer()
        {
            return Vector3.Distance(transform.position, antiPlayer.transform.position);
        }
    
    
        protected override void OnDestroy()
        {
            base.OnDestroy();
            RoundController.instance.placedEnemies.Remove(this);
        }