Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Unit : MonoBehaviour, IDamageable
- {
- [SerializeField]
- private Actor3D agent;
- [SerializeField]
- private Actor2D unitSprite;
- [SerializeField]
- private GameObject target;
- [SerializeField]
- private BaseStats stats;
- [SerializeField]
- private List<GameObject> hitTargets;
- public BaseStats Stats
- {
- get { return stats; }
- }
- public Actor3D Agent
- {
- get { return agent; }
- }
- public Actor2D UnitSprite
- {
- get { return unitSprite; }
- }
- public GameObject Target
- {
- get { return target; }
- set { target = value; }
- }
- public List<GameObject> HitTargets
- {
- get { return hitTargets; }
- }
- private void Update()
- {
- if(stats.CurrHealth > 0)
- {
- agent.Agent.speed = stats.MoveSpeed;
- stats.UpdateStats();
- Attack();
- if (target != null)
- agent.Agent.SetDestination(target.transform.position);
- }
- }
- void Attack()
- {
- if(target != null)
- {
- if(stats.CurrAttackDelay >= stats.AttackDelay)
- {
- Component damageable = target.GetComponent(typeof(IDamageable));
- if(damageable)
- {
- if(hitTargets.Contains(target))
- {
- if(GameFunctions.CanAttack(gameObject.tag,target.tag,damageable,stats))
- {
- GameFunctions.Attack(damageable, stats.BaseDamage);
- stats.CurrAttackDelay = 0;
- }
- }
- }
- }
- }
- }
- public void OnTriggerEnter(Collider other)
- {
- if(!other.transform.parent.parent.CompareTag(gameObject.tag))
- {
- Component damageable = other.transform.parent.parent.gameObject.GetComponent(typeof(IDamageable));
- if(damageable)
- {
- if(!hitTargets.Contains(damageable.gameObject))
- {
- hitTargets.Add(damageable.gameObject);
- }
- }
- }
- }
- public void OnTriggerStay(Collider other)
- {
- if(!other.gameObject.CompareTag(gameObject.tag))
- {
- if(hitTargets.Count > 0)
- {
- GameObject go = GameFunctions.GetNearestTarget(hitTargets, stats.DetectionObject, gameObject.tag, stats.Range);
- if(go != null)
- {
- target = go;
- }
- }
- }
- }
- void IDamageable.TakeDamage(float amount)
- {
- stats.CurrHealth -= amount;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement