Guest User

Untitled

a guest
Nov 7th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.85 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. public class Unit : Seeker
  5. {
  6.     public Faction faction;
  7.     public Vector3 LastTargetDestination;
  8.     public int CurrentHealth;
  9.     public Troop identity;
  10.     public Vector3 pos()
  11.     {
  12.         return transform.position;
  13.     }
  14.  
  15.     [HideInInspector]
  16.     public GameObject Healthbar;
  17.     [HideInInspector]
  18.     public GameObject HealthbarMax;
  19.     [HideInInspector]
  20.     public GameObject HealthbarBorder;
  21.     public Weapon weapon;
  22.     MeshFilter weaponfilter;
  23.     public Image[] healthbarIcons = new Image[3];
  24.     protected override void Start()
  25.     {
  26.         transform.Find("Spearman@Idle/Mesh-Swordsman").GetComponent<SkinnedMeshRenderer>().sharedMaterial = faction.color;
  27.         weaponfilter = transform.Find("Spearman@Idle/Spear").GetComponent<MeshFilter>();
  28.         weapon = identity.EquipWeapon(weaponfilter);
  29.         CurrentHealth = identity.MaxHealth;
  30.         anim = transform.GetChild(0).GetComponent<Animator>();
  31.         LastTargetDestination = pos();
  32.         Speed = identity.UnitSpeed;
  33.         base.Start();
  34.     }
  35.     public void UpdateHealthbar()
  36.     {
  37.         if (healthbarIcons[2] != null)
  38.             healthbarIcons[2].fillAmount = (float)CurrentHealth / identity.MaxHealth;
  39.     }
  40.     public void GoTo(Vector3 pos, bool combat)
  41.     {
  42.         if ((combat == true) || (combat == false && Enemy == null))
  43.         {
  44.             SetDestination(pos);
  45.         }
  46.     }
  47.     public UnitSelection.Squadron squad;
  48.     public float SightDistance = 5;
  49.     float attacktimer = 0;
  50.     NodeElement recalcNode;
  51.     protected override void RecalculatePath()
  52.     {
  53.         if (recalcNode != curNode && !curNode.Passable(this))
  54.         {
  55.             recalcNode = curNode;
  56.             if (Enemy != null)
  57.             {
  58.                 SetDestination(Enemy.curNode, true);
  59.             }
  60.             else
  61.             {
  62.                 SetDestination(FindNearestReachable(LastDestination, true), true);
  63.             }
  64.         }
  65.     }
  66.     void Attack()
  67.     {
  68.         float DistToEnemy = Vector3.Distance(pos(), Enemy.pos());
  69.         if (DistToEnemy > SightDistance && identity.StayWhenAttacking)
  70.         {
  71.             FindEnemy();
  72.         }
  73.         else
  74.         if (DistToEnemy > weapon.AttackDistance)
  75.         {
  76.             if (!identity.StayWhenAttacking)
  77.                 GoTo(Enemy.pos(), true);
  78.         }
  79.         else
  80.         {
  81.             if (attacktimer <= 0 && path.Count < 2)
  82.             {
  83.                 anim.SetTrigger("Attack");
  84.                 attacktimer = weapon.AttackCooldown;
  85.             }
  86.             else
  87.             {
  88.                 attacktimer -= Time.deltaTime;
  89.             }
  90.         }
  91.     }
  92.     protected override void Rotate()
  93.     {
  94.         if (Enemy == null)
  95.         {
  96.             base.Rotate();
  97.         }
  98.         else
  99.         {
  100.             if (path.Count > 1)
  101.             {
  102.                 if (path[1] != curNode)
  103.                     DesiredRotation = Quaternion.LookRotation(path[1].pos() - curNode.pos());
  104.             }
  105.             else
  106.             {
  107.                 DesiredRotation = Quaternion.LookRotation(Enemy.pos() - curNode.pos());
  108.             }
  109.             transform.rotation = Quaternion.Slerp(transform.rotation, DesiredRotation, Time.deltaTime * 4);
  110.         }
  111.     }
  112.     protected override void LateUpdate()
  113.     {
  114.         RecalculatePath();
  115.         if (Enemy == null)
  116.         {
  117.             if (squad != null)
  118.             {
  119.                 if (squad.moving == false)
  120.                     FindEnemy();
  121.             }
  122.             else
  123.                 FindEnemy();
  124.         }
  125.         if (Enemy != null)
  126.         {
  127.             Attack();
  128.         }
  129.         base.LateUpdate();
  130.     }
  131.     public Unit Enemy;
  132.     public float Distance(NodeElement a, NodeElement b)
  133.     {
  134.         return Mathf.Sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
  135.     }
  136.     public bool FindEnemy()
  137.     {
  138.         float dist = SightDistance;
  139.         if (Enemy != null)
  140.         {
  141.             dist = Distance(Enemy.curNode, curNode);
  142.         }
  143.         float temp = 0;
  144.         for (int i = UnitSelection.Instance.units.Count - 1; i >= 0; i--)
  145.         {
  146.             Unit unit = UnitSelection.Instance.units[i];
  147.             if (faction != unit.faction)
  148.             {
  149.                 temp = Distance(unit.curNode, curNode);
  150.                 if (temp < dist)
  151.                 {
  152.                     dist = temp;
  153.                     Enemy = unit;
  154.                 }
  155.             }
  156.         }
  157.         if (Enemy == null && squad != null)
  158.         {
  159.             for (int i = 0; i < squad.units.Count; i++)
  160.             {
  161.                 if (squad.units[i].Enemy != null)
  162.                 {
  163.                     Enemy = squad.units[i].Enemy;
  164.                     break;
  165.                 }
  166.             }
  167.         }
  168.         return Enemy != null;
  169.     }
  170. }
Add Comment
Please, Sign In to add comment