//The main function! This EXACT coroutine will be executed, even across frames. //See GameAction.cs for more information on how this function should work! public override IEnumerator TakeAction() { while (true) { List <Monster> enemies = caller.view.visibleMonsters.FindAll(x => x.IsEnemy(caller)); if (enemies.Count == 0) { Debug.Log("Monster is fleeing without seeing anyone. Resting instead."); //TODO: Use rest action instead! GameAction act = new WaitAction(); act.Setup(caller); while (act.action.MoveNext()) { yield return(act.action.Current); } yield break; } float[,] fleeMap = Pathfinding.CreateFleeMap(enemies.Select(x => x.location).ToList()); while (true) { Vector2Int next = nextSpot(caller.location, fleeMap); if (next == caller.location) { Debug.Log($"{caller.name} has been cornered - stopping flee mode."); //TODO: attack instead! GameAction act = new WaitAction(); act.Setup(caller); while (act.action.MoveNext()) { yield return(act.action.Current); } yield break; } else { MoveAction act = new MoveAction(next); act.Setup(caller); while (act.action.MoveNext()) { yield return(act.action.Current); } yield return(GameAction.StateCheck); } } } }