public void StartAlgorithmBFS() { BreadthFirstSearch algo = new BreadthFirstSearch(); algo.StartAlgorithm(map.GetStartPoint(), map.GetEndPoint()); Visualize(algo.GetPath(), algo.GetAlgoSteps()); }
public override Command Act(Actor me, GameState gameState) { var bfs = new BreadthFirstSearch(gameState.CurrentMap.GetPassabilityGrid()); var path = bfs.GetPath(me.Position, startToEnd ? end : start); if (path.Count == 0) { startToEnd = !startToEnd; return(new WaitCommand(me)); } return(new MoveCommand(me, (path[0] - me.Position).DirectionFromVector())); }
private List <Coord> GetPath(Coord A, Coord B, MapGrid mapGrid, bool isVisualize) { switch (AppConfig.searchAlgorithm) { case Algorithm.DFS: return(DepthFirstSearch.GetPath(A, B, mapGrid, isVisualize)); case Algorithm.BFS: return(BreadthFirstSearch.GetPath(A, B, mapGrid, isVisualize)); case Algorithm.Dijkstra: return(DijkstraSearch.GetPath(A, B, mapGrid, isVisualize)); case Algorithm.AStar: return(AStarSearch.GetPath(A, B, mapGrid, isVisualize)); default: return(AStarSearch.GetPath(A, B, mapGrid, isVisualize)); } }
// Use this for initialization void Start() { player = GameObject.FindGameObjectWithTag("body"); destination = patrolRoute [patrolIndex].transform.position; pathToDestination = BreadthFirstSearch.GetPath( new Vector2(bottomLeft.transform.position.x, bottomLeft.transform.position.y), new Vector2(topRight.transform.position.x, topRight.transform.position.y), transform.position, destination); rb = gameObject.GetComponent <Rigidbody2D> (); animator = GetComponentInChildren <Animator> (); speaker = gameObject.GetComponent <AudioSource> (); }
public Unit Execute(Unit unit, object parameters) { // find closest non-visible cell Dictionary <Point2, Point2> cameFrom = BreadthFirstSearch.CalculateCameFrom(unit.Location, Globals.Instance.GameWorld); Point2 closest = FindClosestNonVisibleCell(cameFrom); if (closest != Point2.Null) { Point2[] path = BreadthFirstSearch.GetPath(unit.Location, closest, cameFrom); // move towards there if (path.Length > 0) { //return path[0]; } } return(unit); }
// Update is called once per frame void Update() { LayerMask playerAndWalls = LayerMask.GetMask("Player", "Unpassable"); if ( Physics2D.Linecast(transform.position + 0.4f * transform.up, destination, LayerMask.GetMask("Unpassable")) || Physics2D.Linecast(transform.position - 0.4f * transform.up, destination, LayerMask.GetMask("Unpassable")) || Physics2D.Linecast(transform.position + 0.4f * transform.right, destination, LayerMask.GetMask("Unpassable")) || Physics2D.Linecast(transform.position - 0.4f * transform.right, destination, LayerMask.GetMask("Unpassable"))) { pathToDestination = BreadthFirstSearch.GetPath( new Vector2(bottomLeft.transform.position.x, bottomLeft.transform.position.y), new Vector2(topRight.transform.position.x, topRight.transform.position.y), transform.position, destination); } else { pathToDestination = new Vector2[] { transform.position, destination }; } DrawPath(pathToDestination); Debug.DrawRay(transform.position, transform.right, Color.cyan, 0f, false); Debug.DrawRay(transform.position, Quaternion.AngleAxis(sightAngle, Vector3.forward) * transform.right * sightRange, Color.magenta, 0f, false); Debug.DrawRay(transform.position, Quaternion.AngleAxis(-sightAngle, Vector3.forward) * transform.right * sightRange, Color.magenta, 0f, false); Vector2 playerVector = player.transform.position - transform.position; RaycastHit2D playerCast = Physics2D.Raycast(transform.position, playerVector.normalized, sightRange, playerAndWalls); if (Vector2.Angle(transform.right, playerVector) < sightAngle) { if (playerCast.collider == null) { Debug.DrawRay(transform.position, playerVector.normalized * sightRange, Color.red, 0f, false); //destination = patrolRoute [patrolIndex]; if (((Vector2)transform.position - destination).sqrMagnitude < 0.25f) { patrolIndex = (patrolIndex + 1) % patrolRoute.Length; destination = patrolRoute [patrolIndex].transform.position; } } else if (playerCast.collider.gameObject == player) { if (destination != lastKnownLocation) { speaker.PlayOneShot(alertSound); } Debug.DrawLine(transform.position, playerCast.point, Color.green, 0f, false); lastKnownLocation = player.transform.position; destination = lastKnownLocation; } else { Debug.DrawLine(transform.position, playerCast.point, Color.red, 0f, false); //destination = patrolRoute [patrolIndex]; if (((Vector2)transform.position - destination).sqrMagnitude < 0.25f) { patrolIndex = (patrolIndex + 1) % patrolRoute.Length; destination = patrolRoute [patrolIndex].transform.position; } } } else { if (((Vector2)transform.position - destination).sqrMagnitude < 0.25f) { patrolIndex = (patrolIndex + 1) % patrolRoute.Length; destination = patrolRoute [patrolIndex].transform.position; } } Debug.DrawLine(transform.position, destination, Color.white); if (Vector3.Distance(transform.position, player.transform.position) < 0.7f) { //got too close, catch the player Application.LoadLevel("EndScene"); } }