public Transform GetClosestTarget(Transform otherTransform) { Targetable target = null; var targets = gameTargets.Targets.Where(t => !t.IsInvulnerable).ToArray(); Vector2 pos = otherTransform.position; float distance = float.PositiveInfinity; foreach (Targetable currentTarget in targets) { float currentDis = Vector2.Distance(currentTarget.transform.position, pos); if (currentDis < distance) { distance = currentDis; target = currentTarget; } } if (target == null) { return(null); } return(target.transform); }
public override void OnCollisionEnter2D(Collision2D other) { if (other.gameObject.tag == "target") { Targetable target = other.gameObject.GetComponent <Targetable>(); if (target != null && target.isAlive && !target.IsInvulnerable) { ExitTo(new EnemyAttackState(target)); } } }
public override void OnCollisionExit2D(Collision2D other) { if (other.gameObject.tag == "target") { Targetable _target = other.gameObject.GetComponent <Targetable>(); if (_target != null && target == _target) { ExitTo(new EnemyPursueState()); } } }
private void Init() { targetable = GetComponent <Targetable>(); targetable.RegisterOnTargetDestroyed(OnTargetDestroyed); targetable.RegisterOnHealthChanged(OnHealthChanged); foreach (var node in batteriesNodes) { node.gameObject.SetActive(false); } RegisterBattery(); OnHealthChanged(0); }
private void OnTargetInvulnerable(Targetable target) { ExitTo(new EnemyPursueState()); }
public EnemyAttackState(Targetable targetable) { target = targetable; target.RegisterOnTargetDestroyed(OnTargetDestroyed); target.BecameInvulnerable += OnTargetInvulnerable; }