public MovementNode GetNodeNearest(Vector3 position, LocationTerrainType locationType, bool interior, int occupationFlags) { MovementNode closest = MovementNode.Empty; MovementNode m = MovementNode.Empty; float closestSoFar = Mathf.Infinity; float current = 0f; bool checkFlags = (occupationFlags < Int32.MaxValue); for (int i = 0; i < State.MovementNodes.Count; i++) { m = State.MovementNodes [i]; if (!checkFlags || Flags.Check(occupationFlags, m.OccupationFlags, Flags.CheckType.MatchAny)) { current = Vector3.Distance(position, m.Position); if (current < 2f) { return(closest = m); break; } else if (current < closestSoFar) { closestSoFar = current; closest = m; } } } if (MovementNode.IsEmpty(m)) { Debug.Log("Returning empty in closest from " + name); } return(closest); }
void OnDrawGizmos( ) { Gizmos.color = Color.yellow; if (!MovementNode.IsEmpty(LastMovementNode)) { Gizmos.DrawWireSphere(LastMovementNode.Position, 0.5f); Gizmos.DrawLine(transform.position, LastMovementNode.Position); } else { Gizmos.color = Color.red; Gizmos.DrawCube(transform.position, Vector3.one); } }
public IEnumerator FollowRoutineOverTime() { while (!motile.Initialized || !motile.HasBody) { yield return(null); } //wait for a random interval to reduce the load on the city double waitUntil = WorldClock.AdjustedRealTime + UnityEngine.Random.value; while (WorldClock.AdjustedRealTime < waitUntil) { yield return(null); } if (ParentSite == null) { //Debug.Log ("Parent site was null in " + name + ", removing routine"); Finish(); yield break; } //Debug.Log (worlditem.Group.Props.TerrainType.ToString () + " in " + worlditem.name); //if we don't have a parent site within a second of spawning then we're toast if (!ParentSite.HasMovementNodes(worlditem.Group.Props.TerrainType, worlditem.Group.Props.Interior, OccupationFlags)) { //Debug.Log ("Parent site " + ParentSite.name + " had no movement nodes, quitting routine"); Finish(); yield break; } while (!ParentSite.IsActive(worlditem.Group.Props.TerrainType, worlditem.Group.Props.Interior, OccupationFlags)) { yield return(null); } //get the very first node we'll be using LastMovementNode = ParentSite.GetNodeNearest(worlditem.Position, worlditem.Group.Props.TerrainType, worlditem.Group.Props.Interior, OccupationFlags); //DAILY ROUTINE START while (!(mFinished | mDestroyed)) { //wait a random amount waitUntil = WorldClock.AdjustedRealTime + (UnityEngine.Random.value * 4f); while (WorldClock.AdjustedRealTime < waitUntil) { yield return(null); } //if we're not visible then just hang out while (worlditem.Is(WIActiveState.Invisible)) { waitUntil = WorldClock.AdjustedRealTime + 0.25f; while (WorldClock.AdjustedRealTime < waitUntil) { yield return(null); } } //get the next movement node LastMovementNode = ParentSite.GetNextNode(LastMovementNode, worlditem.Group.Props.TerrainType, worlditem.Group.Props.Interior, OccupationFlags); if (MovementNode.IsEmpty(LastMovementNode)) { //Debug.Log ("Node was empty in routine from " + ParentSite.name + ", ending routine"); mFollowingRoutine = false; Finish(); yield break; } //Debug.Log ("Going to last movement node " + LastMovementNode.Index.ToString () + " at position " + LastMovementNode.Position.ToString ()); motile.GoalObject.position = LastMovementNode.Position; MotileAction goToAction = character.GoToThing(null); goToAction.Range = 1.5f; goToAction.WalkingSpeed = true; //now wait until we get there float maxWaitTime = 30f; double startTime = WorldClock.AdjustedRealTime; var waitUntilFinished = goToAction.WaitForActionToFinish(0.15f); while (waitUntilFinished.MoveNext()) { //Debug.Log ("Waiting for action to finish, state is " + goToAction.State.ToString ()); if (goToAction.State != MotileActionState.Waiting) { motile.GoalObject.position = LastMovementNode.Position; } yield return(waitUntilFinished.Current); if (WorldClock.AdjustedRealTime > startTime + maxWaitTime) { //Debug.Log ("Timed out, finishing now"); goToAction.TryToFinish(); break; } } yield return(null); } mFollowingRoutine = false; yield break; }