private void OnDrawGizmos() { if (doPath) { if (epf == null) { epf = new EntityPathFinder(GameManager.PathFinder); } Path_ = null; if (epf.IsRunning) { epf.ForceStop(); } else { epf.FindPath(Player.TilePos, Player.TilePos + GameManager.RNG.RandomVec2i(-100, 100)); } /*Debug.Log("calculating path"); * GenerationRandom genRan = new GenerationRandom(Time.frameCount); * Path_ = GameManager.PathFinder.GeneratePath(Vec2i.FromVector3(Player.Position), Vec2i.FromVector3(Player.Position) + genRan.RandomVec2i(-100, 100)); */ return; Settlement t = GameManager.TestSettle; if (t == null) { return; } int curDist = -1; Vec2i pPos = Vec2i.FromVector2(Player.Position2); foreach (SettlementPathNode pn in t.tNodes) { if (pn == null) { continue; } if (NearestNode == null) { NearestNode = pn; curDist = Vec2i.QuickDistance(pn.Position + t.BaseCoord, pPos); } else { if (Vec2i.QuickDistance(pn.Position + t.BaseCoord, pPos) < curDist) { curDist = Vec2i.QuickDistance(pn.Position + t.BaseCoord, pPos); NearestNode = pn; } } } path = new List <SettlementPathNode>(); if (SettlementPathFinder.SettlementPath(NearestNode, t.IMPORTANT, out path, debug: true)) { Debug.Log("Path found!"); } Debug.Log("Path len: " + path.Count); } if (Path_ == null && epf != null) { if (epf.IsComplete()) { Path_ = epf.GetPath(); } } if (Path_ != null && Path_.Count > 1) { Color old = Gizmos.color; Gizmos.color = Color.yellow; //Debug.Log(Vec2i.ToVector3(path[0].Position + t.BaseCoord)); //Gizmos.DrawCube(Vec2i.ToVector3(path[0].Position + GameManager.TestSettle.BaseCoord), Vector3.one * 2); Gizmos.DrawCube(Vec2i.ToVector3(Path_[0]), Vector3.one * 5); Gizmos.DrawCube(Vec2i.ToVector3(Path_[Path_.Count - 1]), Vector3.one * 5); for (int i = 0; i < Path_.Count - 1; i++) { Gizmos.DrawLine(Vec2i.ToVector3(Path_[i]), Vec2i.ToVector3(Path_[i + 1])); Gizmos.DrawCube(Vec2i.ToVector3(Path_[i]), Vector3.one * 0.5f); } Gizmos.color = old; } return; if (NearestNode == null) { Settlement t = GameManager.TestSettle; if (t == null) { return; } int curDist = -1; Vec2i pPos = Vec2i.FromVector2(Player.Position2); foreach (SettlementPathNode pn in t.tNodes) { if (pn == null) { continue; } if (NearestNode == null) { NearestNode = pn; curDist = Vec2i.QuickDistance(pn.Position + t.BaseCoord, pPos); } else { if (Vec2i.QuickDistance(pn.Position + t.BaseCoord, pPos) < curDist) { curDist = Vec2i.QuickDistance(pn.Position + t.BaseCoord, pPos); NearestNode = pn; } } } path = new List <SettlementPathNode>(); if (SettlementPathFinder.SettlementPath(NearestNode, t.IMPORTANT, out path, debug: true)) { Debug.Log("Path found!"); } Debug.Log("Path len: " + path.Count); } }
public bool GeneratePath(Vec2i target) { //if no target, null previous paths and return. if (target == null) { Debug.Log("Target is null - no path"); EntityPathTarget = null; EntityPath = null; return(false); } if (EPF == null) { //TODO - get free path finder from parent EPF = new EntityPathFinder(GameManager.PathFinder); } Vec2i tilePos = Vec2i.FromVector3(Entity.Position); Debug.Log("Attempting path from " + tilePos + " to " + target); /*if(GameManager.PathFinder.NodeValue(tilePos) > 100) * { * tilePos = Vec2i.FromVector3(Entity.Position + new Vector3(0.5f, 0, 0.5f)); * if(GameManager.PathFinder.NodeValue(tilePos) > 100) * { * Debug.Log("hm"); * } * }*/ //if we have no path if (EntityPath == null) { //We check if one is being generated if (!EPF.IsRunning) { //If not, we start generating it EPF.FindPath(tilePos, target); return(false); } //Check if the path finder has completed its work if (EPF.IsComplete()) { //If the path is found, we set it and return true if (EPF.FoundPath) { EntityPathTarget = target; EntityPath = new EntityPath(EPF.GetPath()); return(true); } else { Debug.Log("Path from " + tilePos + " to " + target + " not found"); return(false); } } return(false); /* * //If there is no path, Attempt to generate it * List<Vec2i> path = GameManager.PathFinder.GeneratePath(tilePos, target); * if (path==null || path.Count == 0) * { * Debug.Log("Path from " + tilePos + " to " + target + " not found"); * return false; * } * EntityPathTarget = target; * EntityPath = new EntityPath(path); * return true;*/ } else if (EntityPath.Target == target) {//If the current path has the same target, return true return(true); } else { if (Vec2i.QuickDistance(EntityPath.Target, target) < 5) { //If the path exists but doesn't have the same target, but targets are close, we attempt to re-calculate the path return(EntityPath.UpdateTarget(target, EPF)); } else { EntityPath = null; //We check if one is being generated if (!EPF.IsRunning) { //If not, we start generating it EPF.FindPath(tilePos, target); return(false); }/* * List<Vec2i> newPath = GameManager.PathFinder.GeneratePath(tilePos, target); * if(newPath==null || newPath.Count == 0) * { * Debug.Log("No path found from " + tilePos + " to " + target + " for entity " + Entity); * return false; * } * EntityPath = new EntityPath(newPath); * return true;*/ } } return(false); }