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); } }
/// <summary> /// Called when the end target of out path has changed. /// We iterate through the points in our path, and check the distance to /// the new target. If the distance is lower, we re-calculate the path /// </summary> /// <param name="target"></param> /// <returns></returns> public bool UpdateTarget(Vec2i target, EntityPathFinder epf) { //If we are currently calculating a path if (epf.IsRunning) { if (epf.Target == target) { //Target is still valid, so we return true return(true); } else { //Debug.LogError("Im not sure what to do here?"); epf.ForceStop(); return(false); } } if (epf.FoundPath) { } //Check the finder has the same target. if (epf.Target == target) { return(true); } Vec2i closestPoint = Path[index]; int closestDist = Vec2i.QuickDistance(target, closestPoint); int closestPointIndex = index; for (int i = index; i < Path.Count; i += 2) { int thisDist = Vec2i.QuickDistance(target, Path[i]) + (i - index); if (thisDist < closestDist) { closestDist = thisDist; closestPoint = Path[i]; closestPointIndex = i; } } if (closestPointIndex + 1 < Path.Count) { int trimAmount = Path.Count - 1 - (closestPointIndex + 1); if (trimAmount > 0) { //Remove end of path Path.RemoveRange(closestPointIndex + 1, Path.Count - 1 - (closestPointIndex + 1)); } } if (index > 1) { Path.RemoveRange(0, index - 1); index = 0; } /* * List<Vec2i> extension = GameManager.PathFinder.GeneratePath(closestPoint, target); * if (extension.Count == 0) * return false; * * * Path.AddRange(extension); * * Target = target; */ return(true); }