Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- public class Unit : Seeker
- {
- public Faction faction;
- public Vector3 LastTargetDestination;
- public int CurrentHealth;
- public Troop identity;
- public Vector3 pos()
- {
- return transform.position;
- }
- [HideInInspector]
- public GameObject Healthbar;
- [HideInInspector]
- public GameObject HealthbarMax;
- [HideInInspector]
- public GameObject HealthbarBorder;
- public Weapon weapon;
- MeshFilter weaponfilter;
- public Image[] healthbarIcons = new Image[3];
- protected override void Start()
- {
- transform.Find("Spearman@Idle/Mesh-Swordsman").GetComponent<SkinnedMeshRenderer>().sharedMaterial = faction.color;
- weaponfilter = transform.Find("Spearman@Idle/Spear").GetComponent<MeshFilter>();
- weapon = identity.EquipWeapon(weaponfilter);
- CurrentHealth = identity.MaxHealth;
- anim = transform.GetChild(0).GetComponent<Animator>();
- LastTargetDestination = pos();
- Speed = identity.UnitSpeed;
- base.Start();
- }
- public void UpdateHealthbar()
- {
- if (healthbarIcons[2] != null)
- healthbarIcons[2].fillAmount = (float)CurrentHealth / identity.MaxHealth;
- }
- public void GoTo(Vector3 pos, bool combat)
- {
- if ((combat == true) || (combat == false && Enemy == null))
- {
- SetDestination(pos);
- }
- }
- public UnitSelection.Squadron squad;
- public float SightDistance = 5;
- float attacktimer = 0;
- NodeElement recalcNode;
- protected override void RecalculatePath()
- {
- if (recalcNode != curNode && !curNode.Passable(this))
- {
- recalcNode = curNode;
- if (Enemy != null)
- {
- SetDestination(Enemy.curNode, true);
- }
- else
- {
- SetDestination(FindNearestReachable(LastDestination, true), true);
- }
- }
- }
- void Attack()
- {
- float DistToEnemy = Vector3.Distance(pos(), Enemy.pos());
- if (DistToEnemy > SightDistance && identity.StayWhenAttacking)
- {
- FindEnemy();
- }
- else
- if (DistToEnemy > weapon.AttackDistance)
- {
- if (!identity.StayWhenAttacking)
- GoTo(Enemy.pos(), true);
- }
- else
- {
- if (attacktimer <= 0 && path.Count < 2)
- {
- anim.SetTrigger("Attack");
- attacktimer = weapon.AttackCooldown;
- }
- else
- {
- attacktimer -= Time.deltaTime;
- }
- }
- }
- protected override void Rotate()
- {
- if (Enemy == null)
- {
- base.Rotate();
- }
- else
- {
- if (path.Count > 1)
- {
- if (path[1] != curNode)
- DesiredRotation = Quaternion.LookRotation(path[1].pos() - curNode.pos());
- }
- else
- {
- DesiredRotation = Quaternion.LookRotation(Enemy.pos() - curNode.pos());
- }
- transform.rotation = Quaternion.Slerp(transform.rotation, DesiredRotation, Time.deltaTime * 4);
- }
- }
- protected override void LateUpdate()
- {
- RecalculatePath();
- if (Enemy == null)
- {
- if (squad != null)
- {
- if (squad.moving == false)
- FindEnemy();
- }
- else
- FindEnemy();
- }
- if (Enemy != null)
- {
- Attack();
- }
- base.LateUpdate();
- }
- public Unit Enemy;
- public float Distance(NodeElement a, NodeElement b)
- {
- return Mathf.Sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
- }
- public bool FindEnemy()
- {
- float dist = SightDistance;
- if (Enemy != null)
- {
- dist = Distance(Enemy.curNode, curNode);
- }
- float temp = 0;
- for (int i = UnitSelection.Instance.units.Count - 1; i >= 0; i--)
- {
- Unit unit = UnitSelection.Instance.units[i];
- if (faction != unit.faction)
- {
- temp = Distance(unit.curNode, curNode);
- if (temp < dist)
- {
- dist = temp;
- Enemy = unit;
- }
- }
- }
- if (Enemy == null && squad != null)
- {
- for (int i = 0; i < squad.units.Count; i++)
- {
- if (squad.units[i].Enemy != null)
- {
- Enemy = squad.units[i].Enemy;
- break;
- }
- }
- }
- return Enemy != null;
- }
- }
Add Comment
Please, Sign In to add comment