void Refresh() { if (activationStrategy == ActivationStrategy.OnlyClosestZone) { var newClosest = InfZone.ClosestZone(transform.position, zones); if (newClosest != closest) { if (closest) { closest.Freeze(); } newClosest.Unfreeze(); closest = newClosest; } } else if (activationStrategy == ActivationStrategy.AllZonesWithinDistance) { foreach (var zone in zones) { float distance = Vector3.Distance(transform.position, zone.transform.position); if (distance < distanceThreshold) { zone.Unfreeze(); } else { zone.Freeze(); } } } }
/// <summary> /// Alters the activity status of the actor's GameObject. /// If the status is changed from it's previous value the approperiate callback is called (FixAfterFreeze / FixAfterUnfreeze). /// </summary> public void SetActive(bool newActiveStatus, InfZone zone) { if (!isActiveAndEnabled && newActiveStatus) { FixAfterUnfreeze(zone); } else if (isActiveAndEnabled && !newActiveStatus) { FixAfterFreeze(zone); } gameObject.SetActive(newActiveStatus); }
/// <summary> /// The correct way to move an actor from its current Zone to another one. /// </summary> public void TransferToZone(InfZone newZone) { if (zone == newZone) { return; // Already in that zone } if (zone != null) { zone.RemoveActor(this); } newZone.AddActor(this); zone = newZone; SetActive(!zone.frozen, zone); }
public static InfZone ClosestZone(Vector3 position, InfZone[] zones) { if (zones.Length == 0) { throw new UnityException("No zones to choose from"); } float minimum = float.MaxValue; InfZone closest = null; foreach (var zone in zones) { float distance = Vector3.Distance(position, zone.transform.position); if (distance < minimum) { minimum = distance; closest = zone; } } return(closest); }
public void TransferToClosestZone() { TransferToZone(InfZone.ClosestZone(transform.position, zones)); }
public void FixAfterFreeze(InfZone zone) { onFreeze(zone); }
public void FixAfterUnfreeze(InfZone zone) { onUnfreeze(zone); }
public void Unfreeze(InfZone zone) { float t = distanceTravelledWhileFrozen / Vector3.Distance(freezePosition, target); transform.position = Vector3.Lerp(freezePosition, target, t); }
public void Freeze(InfZone zone) { distanceTravelledWhileFrozen = 0; freezePosition = transform.position; }
public void UpdateWhileFrozen(InfZone zone) { distanceTravelledWhileFrozen += Time.deltaTime * 2f; }