public void DrawPath(Node goal,ref Grid grid,ref Tree[] _trees) { Point startingPoint = StateToPoint(nodeState.ORIGIN); int pointX = startingPoint.X; int pointY = startingPoint.Y; path.Add(origin); if (pointX == -1 && pointY == -1) { return; } while (true) { Point lowestPoint = Point.Zero; int lowest = 10000; foreach (Point movePoint in CheckStep(pointX, pointY)) { int count = _squares[movePoint.X, movePoint.Y].distanceSteps; if (count < lowest) { lowest = count; lowestPoint.X = movePoint.X; lowestPoint.Y = movePoint.Y; } } if (lowest != 10000) { _squares[lowestPoint.X, lowestPoint.Y].hasPath = true; _squares[lowestPoint.X, lowestPoint.Y].ChangeTexture(ImageLibrary.getInstance().getImage("Clear")); path.Add(_squares[lowestPoint.X, lowestPoint.Y]); pointX = lowestPoint.X; pointY = lowestPoint.Y; } else { Search(origin, goal, ref grid, this.game, ref _trees, ref this.path); return; } if (_squares[pointX, pointY].state == nodeState.GOAL) { _squares[pointX, pointY].ChangeTexture(ImageLibrary.getInstance().getImage("Origin")); origin.ChangeTexture(ImageLibrary.getInstance().getImage("Origin")); path.Add(_squares[pointX, pointY]); CreateTrees(ref _trees); break; } } }
public void PathfindForOrigin(Node alvo, ref Grid grid) { Point pontoInicial = ProcurarNode(alvo.EstadoNode); int alvoX = pontoInicial.X; int alvoY = pontoInicial.Y; if (alvoX == -1 || alvoY == -1) { return; } matrizNodes[alvoX, alvoY].DistanciaPassos = 0; while (true) { bool verificando = false; foreach (Point pontoAtual in TodosNodes()) { int x = pontoAtual.X; int y = pontoAtual.Y; if (CampoAberto(x,y)) { int andandoAqui = matrizNodes[x, y].DistanciaPassos; foreach (Point pontoDeLocomocao in ValidarMovimentos(x, y)) { int nX = pontoDeLocomocao.X; int nY = pontoDeLocomocao.Y; int novoAndar = andandoAqui + 1; if (matrizNodes[nX, nY].DistanciaPassos > novoAndar) { matrizNodes[nX, nY].DistanciaPassos = novoAndar; verificando = true; } } } } if (!verificando) { break; } } }
protected override void LoadContent() { ordenation = new QuickSort(); spriteBatch = new SpriteBatch(GraphicsDevice); ImageLibrary.getInstance().putImage("Tree", Content.Load<Texture2D>(@"tree")); ImageLibrary.getInstance().putImage("Clear", Content.Load<Texture2D>(@"clear")); ImageLibrary.getInstance().putImage("Floor", Content.Load<Texture2D>(@"ground")); ImageLibrary.getInstance().putImage("FloorSelected", Content.Load<Texture2D>(@"ground_selected")); ImageLibrary.getInstance().putImage("Origin", Content.Load<Texture2D>(@"origin")); ImageLibrary.getInstance().putImage("Player", Content.Load<Texture2D>(@"player")); grid = new Grid(this, cam, 6, 6); this.path = new AStar(); Random rand = new Random(); int x = rand.Next(3, 6); int y = rand.Next(3, 6); this.path.Search(this.grid.nodes[0, 0], this.grid.nodes[x, y], ref this.grid, this, ref allTrees, ref this.nodePath); ArrangeNodes(ref this.nodePath); player = new Character(this, new Vector3(1.25f, 0, 1.25f), new Vector3(0, 90, 0), new Vector3(0, 1, 0), ImageLibrary.getInstance().getImage("Player"), cam, path.origin); enemys = new List<Character>(); enemys.Add(player); activeNode = grid.nodes[0, 0]; }
public void Search(Node origin, Node goal, ref Grid grid, ref Node[,] matriz, ref List<Node> listaDoCaminho) { this.matrizNodes = matriz; PathfindForOrigin(goal, ref grid); PathfindForTarget(origin, ref grid); for (int x = 0; x < this.matrizNodes.GetLength(0); x++) { for (int y = 0; y < this.matrizNodes.GetLength(1); y++) { if (this.matrizNodes[x, y].IsPath == true) { if(this.matrizNodes[x,y].EstadoNode != EstadoNode.alvo) listaDoCaminho.Add(this.matrizNodes[x, y]); } } } }
public void PathfindForTarget(Node alvo, ref Grid grid) { Point pontoInicial = ProcurarNode(alvo.EstadoNode); int origemX = pontoInicial.X; int origemY = pontoInicial.Y; if (origemX == -1 && origemY == -1) { return; } while (true) { Point menorPonto = Point.Zero; int menor = 10000; foreach (Point pontoDeLocomocao in ValidarMovimentos(origemX, origemY)) { int tempPassos = matrizNodes[pontoDeLocomocao.X, pontoDeLocomocao.Y].DistanciaPassos; if (tempPassos < menor) { menor = tempPassos; menorPonto.X = pontoDeLocomocao.X; menorPonto.Y = pontoDeLocomocao.Y; } } if (menor != 10000) { matrizNodes[menorPonto.X, menorPonto.Y].IsPath = true; origemX = menorPonto.X; origemY = menorPonto.Y; } else { break; } if (matrizNodes[origemX, origemY].EstadoNode == EstadoNode.alvo) { break; } } }
private void Awake() { Grid.SetLayermask(_pathfindingObstacles); CreateNewLevel(_seed); }
public void Search(Node origin, Node goal, ref Grid grid, Game1 game, ref Tree[] allTrees, ref List<Node> _path) { this.path = _path; this.game = game; allNodes.Clear(); ResetNodes(); this.origin = origin; this._squares = grid.nodes; this.row = grid.Rows; this.column = grid.Columns; foreach (Point p in GetPoints()) { allNodes.Add(_squares[p.X,p.Y]); _squares[p.X, p.Y].ChangeTexture(ImageLibrary.getInstance().getImage("Floor")); } allNodes.Remove(goal); allNodes.Remove(origin); foreach (Node tree in SortTrees(6)) { tree.state = nodeState.CLOSED; } goal.state = nodeState.GOAL; origin.state = nodeState.ORIGIN; Pathfind(); DrawPath(goal,ref grid,ref allTrees); }
protected override void Initialize() { this.keyboard = new KeyboardManager(); this.grid = new Grid(ROW, COLUMN); matrizNodes = new Node[ROW, COLUMN]; General.ROW = ROW; General.COLUMN = COLUMN; this.lRaffle = new List<Node>(); this.lPathfinding = new List<Node>(); this.pathfinding = new General(); base.Initialize(); }