SHARE
TWEET

Untitled

a guest Oct 21st, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Task.Tank;
  4. using UnityEngine;
  5. using UnityEngine.AI;
  6.  
  7. namespace Task.Enemy
  8. {
  9.     public enum EnemyType { MummyEnemy = 0, OrcEnemy = 1, SkeletonEnemy = 2 }
  10.     public enum EnemyState { Seek = 0, Attack = 1, Dead = 3, Idle = 4}
  11.     public abstract class Enemy : MonoBehaviour
  12.     {
  13.         [Header("Enemy Settings")]
  14.         [SerializeField] private NavMeshAgent _navMeshAgent;
  15.         [SerializeField] private Animation _enemyAnimation;
  16.         [SerializeField] private EnemyType _type;
  17.         [SerializeField] private EnemySettings _settings;
  18.         [SerializeField] private Health _health;
  19.  
  20.         private TankController _target;
  21.  
  22.         EnemyState _currentState;
  23.         float _currentReloadingTime;
  24.  
  25.         private void Start()
  26.         {
  27.             _health.AnounceDeath += Die;
  28.             _navMeshAgent.speed = _settings.MoveSpeed;
  29.             _enemyAnimation.Play("mummy_idle1");
  30.         }
  31.  
  32.         public virtual void Attack()
  33.         {
  34.             if(_currentReloadingTime  <= 0 && !IsAnimationPlaying(_settings.AttackAnimationName)
  35.             {
  36.                 _enemyAnimation.Play(_settings.AttackAnimationName);
  37.             }
  38.         }
  39.  
  40.         private void StartReloading()
  41.         {
  42.             _currentReloadingTime = _settings.ReloadingTime;
  43.         }
  44.  
  45.         public virtual void MoveToPlayer()
  46.         {
  47.             _navMeshAgent.SetDestination(_target.transform.position);
  48.         }
  49.  
  50.         public virtual void Die()
  51.         {
  52.             _enemyAnimation.Play(_settings.DeathAnimationName);
  53.         }
  54.  
  55.  
  56.  
  57.         public void SetState(EnemyState state)
  58.         {
  59.             _currentState = state;
  60.         }
  61.  
  62.         private void Update()
  63.         {
  64.  
  65.             if(_currentReloadingTime > 0)
  66.             {
  67.                 _currentReloadingTime -= Time.deltaTime;
  68.             }
  69.  
  70.             ResolveState();
  71.         }
  72.  
  73.         private void PlayIdleAnimation()
  74.         {
  75.             if (!IsAnimationPlaying(_settings.IdleAnimationName))
  76.             {
  77.                 PlayAnimation(_settings.IdleAnimationName);
  78.             }
  79.         }
  80.  
  81.         private void SeekPlayer()
  82.         {
  83.             _navMeshAgent.SetDestination(_target.transform.position);
  84.  
  85.             if (!IsAnimationPlaying(_settings.MoveAnimationName))
  86.             {
  87.                 PlayAnimation(_settings.MoveAnimationName);
  88.             }
  89.         }
  90.  
  91.         private bool IsAnimationPlaying(string animationName)
  92.         {
  93.             return _enemyAnimation.IsPlaying(animationName);
  94.         }
  95.  
  96.         private void PlayAnimation(string animationName)
  97.         {
  98.             _enemyAnimation.Play(animationName);
  99.         }
  100.  
  101.         public void ResolveState()
  102.         {
  103.             switch (_currentState)
  104.             {
  105.                 case EnemyState.Seek:
  106.                     SeekPlayer();
  107.                     break;
  108.                 case EnemyState.Idle:
  109.                     PlayIdleAnimation();
  110.                     break;
  111.                 case EnemyState.Attack:
  112.                     Attack();
  113.                     break;
  114.                 case EnemyState.Dead:
  115.                     Die();
  116.                     break;
  117.             }
  118.  
  119.         }
  120.     }
  121. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top