//set g to follow the closest unfollowed node private FormationBase FollowClosest(FormationBase m) { Debug.Assert(m != null); FormationBase toFollow = m.following; float closest = Mathf.Infinity; if (toFollow != null) { closest = Vector3.Distance(m.transform.position, toFollow.transform.position); } foreach (FormationBase nf in notFollowed) { if (m != nf && !FollowLoop(m, nf)) { float dist = Vector3.Distance(m.transform.position, nf.transform.position); if (dist < closest) { closest = dist; toFollow = nf; } } } //print(m + " wants to follow " + toFollow); if (toFollow != null && toFollow != m.following) { FollowNode(m, toFollow.GetComponent <FormationBase>()); } return(toFollow); }
public FormationSpot(Vector3 offset, float radius, FormationBase formation) { this.offset = offset; this.radius = radius; MyFormation = formation; marksPath = new List <Vector3>(); }
//reset following of g, and the follower of whatever g was following private void Unfollow(FormationBase g) { if (g.following != null) { g.following.follower = null; notFollowed.Add(g.following); } g.following = null; notFollowing.Add(g); }
//set follower as following toFollow private void FollowNode(FormationBase follower, FormationBase toFollow) { Debug.Assert(follower != null); Debug.Assert(toFollow != null); Debug.Assert(follower != toFollow); if (toFollow != null) { Unfollow(follower); follower.following = toFollow; toFollow.follower = follower; //print(follower + " is following " + toFollow); notFollowed.Remove(toFollow); notFollowing.Remove(follower); } }
private bool FollowLoop(FormationBase a, FormationBase b) { HashSet <FormationBase> chain = new HashSet <FormationBase>(); chain.Add(a); FormationBase check = b; while (check != null) { if (chain.Contains(check)) { return(true); } chain.Add(check); check = check.following; } return(false); }