Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class SimpleEnemyAI : MonoBehaviour {
- public enum EnemyType
- {
- Knight,
- Archer
- }
- public GameObject arrow;
- public EnemyType enemy;
- public Transform nextPoint;
- public List<GameObject> targets = new List<GameObject>();
- public float speed;
- public float attackRange;
- public Animator anim;
- public float damage;
- public float attackCooldown;
- public Image background;
- public Image bar, secondBar;
- public Text showHP;
- public float maxHP;
- public float hp;
- public float HP
- {
- get
- {
- return hp;
- }
- set
- {
- hp = value;
- hp = Mathf.Clamp(hp, 0, maxHP);
- tempHP = hp;
- StartCoroutine(ReduceHealth());
- if(hp == 0)
- {
- if(OnEnemyDead != null)
- OnEnemyDead(this.gameObject);
- Destroy(this.gameObject);
- }
- }
- }
- private float tempHP;
- private bool isAttacking;
- private float timer;
- private SimpleDefenderAI tempDefenderAIScript;
- public delegate void OnEnemyAttacked(GameObject enemy);
- public static event OnEnemyAttacked OnEnemyDead;
- // Update is called once per frame
- void Update () {
- if(targets.Count == 0)
- {
- if(nextPoint!= null)
- {
- MoveToNextWaypoint();
- }
- }
- else
- {
- if(this.enemy == EnemyType.Archer)
- {
- Shoot();
- }
- else if(this.enemy == EnemyType.Knight)
- {
- Attack();
- }
- }
- }
- void MoveToNextWaypoint()
- {
- transform.position = Vector2.MoveTowards(transform.position, nextPoint.position, speed * Time.deltaTime);
- }
- void Attack()
- {
- if(Vector2.Distance(transform.position, targets[0].transform.position) >= attackRange)
- {
- transform.position = Vector2.MoveTowards(transform.position, targets[0].transform.position, speed * Time.deltaTime);
- isAttacking = false;
- }
- else
- {
- isAttacking = true;
- if(tempDefenderAIScript == null)
- tempDefenderAIScript = targets[0].GetComponent<SimpleDefenderAI>();
- if(Time.time > timer)
- {
- anim.SetTrigger("attack");
- tempDefenderAIScript.HP -= damage;
- timer = Time.time + attackCooldown;
- }
- }
- }
- void Shoot()
- {
- if(Vector2.Distance(transform.position, targets[0].transform.position) <= attackRange)
- {
- if(tempDefenderAIScript == null)
- tempDefenderAIScript = targets[0].GetComponent<SimpleDefenderAI>();
- if(Time.time > timer)
- {
- Vector3 dir = targets[0].transform.position - transform.position;
- dir = targets[0].transform.InverseTransformDirection(dir);
- float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
- anim.SetTrigger("attack");
- GameObject shotArrow = Instantiate(arrow, transform.position, Quaternion.Euler(new Vector3(0,0,angle)));
- timer = Time.time + attackCooldown;
- }
- }
- }
- void OnTriggerEnter2D(Collider2D newColl)
- {
- if(newColl.CompareTag("Defender") && newColl.isTrigger == false)
- {
- targets.Add(newColl.gameObject);
- SimpleDefenderAI.OnDefenderDead += RemoveTarget;// как отписаться от события, когда объект уничтожается?
- }
- }
- void RemoveTarget(GameObject target)
- {
- targets.Remove(target);
- //SimpleDefenderAI.OnDefenderDead -= RemoveTarget;
- }
- IEnumerator ReduceHealth()
- {
- while(showHP.text != tempHP.ToString())
- {
- showHP.text = ((int) Mathf.Lerp(float.Parse(showHP.text), tempHP, Time.deltaTime * 2)).ToString();
- bar.fillAmount = float.Parse(showHP.text)/maxHP;
- yield return null;
- }
- while(secondBar.fillAmount != bar.fillAmount)
- {
- secondBar.fillAmount -= 0.01f;
- secondBar.fillAmount = Mathf.Clamp(secondBar.fillAmount, bar.fillAmount, 1);
- yield return new WaitForSeconds(0.025f);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement