public List<Node> GetPossibleNode(Map map, Tile Destination) { List<Node> result = new List<Node>(); // Bottom if (map.ValidCoordinate(tile.X, tile.Y + 1) && map.Tilelist[tile.X, tile.Y + 1].Type != Templar.Tile.TileType.wall) { result.Add(new Node(map.Tilelist[tile.X, tile.Y + 1], this, Destination)); } // Right if (map.ValidCoordinate((tile.X + 1), tile.Y ) && map.Tilelist[tile.X + 1, tile.Y].Type != Templar.Tile.TileType.wall) { // result.Add(new Node(map.Tilelist[tile.Y, tile.X+1], this, Destination)); result.Add(new Node(map.Tilelist[tile.X + 1, tile.Y], this, Destination)); } // Top if (map.ValidCoordinate(tile.X, tile.Y - 1) && map.Tilelist[tile.X, tile.Y - 1].Type != Templar.Tile.TileType.wall) { //result.Add(new Node(map.Tilelist[tile.Y-1, tile.X], this, Destination)); result.Add(new Node(map.Tilelist[tile.X, tile.Y - 1], this, Destination)); } //Left if (map.ValidCoordinate(tile.X - 1, tile.Y) && map.Tilelist[tile.X - 1, tile.Y].Type != Templar.Tile.TileType.wall) { //result.Add(new Node(map.Tilelist[tile.Y , tile.X - 1], this, Destination)); result.Add(new Node(map.Tilelist[tile.X - 1, tile.Y], this, Destination)); } return result; }
public static List<Tile> Astar(Map map, Tile Start, Tile End) { Nodelist<Node> openlist = new Nodelist<Node>(); Nodelist<Node> closelist = new Nodelist<Node>(); List<Node> possibleNode; int possibleNodeCount; Node StartNode = new Node(Start, null, End); List<Tile> sol = new List<Tile>(); openlist.Add(StartNode); while (openlist.Count>0) { Node current = openlist[0]; openlist.RemoveAt(0); closelist.Add(current); if (current.Tile.X==End.X && current.Tile.Y == End.Y) { while (current.Parent != null) { sol.Insert(0, current.Tile); current = current.Parent; } return sol; } possibleNode = current.GetPossibleNode(map, End); possibleNodeCount = possibleNode.Count; for (int i = 0; i < possibleNodeCount; i++) { if (!closelist.Contains(possibleNode[i])) { if (openlist.Contains(possibleNode[i])) { if (possibleNode[i].Heuristic < openlist[possibleNode[i]].Heuristic) openlist[possibleNode[i]].Parent = current; } else openlist.DichotomicInsertion(possibleNode[i]); } } } sol.Add(Start); return sol; }
//initialise la map public void init(string path) { StreamWriter sw = new StreamWriter(path); for (int j = 0; j < tiles.GetLength(1); j++) { for (int i = 0; i < tiles.GetLength(0); i++) { tiles[i, j] = new Vector2(1, 0); tilelist[i, j] = new Tile(i, j, 1);//penser a modifier ca voir avec louis comment faire sw.Write("1"); } sw.WriteLine(); } sw.Close(); }
public void init() { StreamWriter sw = new StreamWriter("map_1.txt"); for (int j = 0; j < tiles.GetLength(1); j++) { for (int i = 0; i < tiles.GetLength(0); i++) { tiles[i, j] = 1; sw.Write(tiles[i,j]); tilelist[i, j] = new Tile(i, j, tiles[i, j]); } sw.WriteLine(); } sw.Close(); }
public void load(string path) { int j = 0; //instanci un nouveau lecteur StreamReader sr = new StreamReader(path); string ligne; //lis les lignes et stocke chaque caractere de la ligne dans la matrice while ((ligne = sr.ReadLine()) != null) { for (int i = 0; i < tiles.GetLength(0); i++) { tiles[i, j] = (ligne[i] - 48); tilelist[i, j] = new Tile(i, j, tiles[i, j]); } j += 1; } sr.Close(); }
public EDM(Game game, SpriteBatch spriteBatch, bool language) : base(game, spriteBatch) { XmlReader reader; reader = XmlReader.Create("Francais.xml"); if (!language) { reader = XmlReader.Create("English.xml"); } while (reader.Read()) while (reader.NodeType != XmlNodeType.EndElement) { reader.Read(); if (reader.Name == "pos") { while (reader.NodeType != XmlNodeType.EndElement) { reader.Read(); if (reader.NodeType == XmlNodeType.Text) op1 = reader.Value.ToString(); } reader.Read(); } if (reader.Name == "debut") { while (reader.NodeType != XmlNodeType.EndElement) { reader.Read(); if (reader.NodeType == XmlNodeType.Text) op2 = reader.Value.ToString(); } reader.Read(); } if (reader.Name == "err") { while (reader.NodeType != XmlNodeType.EndElement) { reader.Read(); if (reader.NodeType == XmlNodeType.Text) op3 = reader.Value.ToString(); } reader.Read(); } if (reader.Name == "tuto") { while (reader.NodeType != XmlNodeType.EndElement) { reader.Read(); if (reader.NodeType == XmlNodeType.Text) op4 = reader.Value.ToString(); } reader.Read(); } if (reader.Name == "tuile") { while (reader.NodeType != XmlNodeType.EndElement) { reader.Read(); if (reader.NodeType == XmlNodeType.Text) op5 = reader.Value.ToString(); } reader.Read(); } if (reader.Name == "map") { while (reader.NodeType != XmlNodeType.EndElement) { reader.Read(); if (reader.NodeType == XmlNodeType.Text) op6 = reader.Value.ToString(); } reader.Read(); } if (reader.Name == "mob") { while (reader.NodeType != XmlNodeType.EndElement) { reader.Read(); if (reader.NodeType == XmlNodeType.Text) op7 = reader.Value.ToString(); } reader.Read(); } if (reader.Name == "messa") { while (reader.NodeType != XmlNodeType.EndElement) { reader.Read(); if (reader.NodeType == XmlNodeType.Text) op8 = reader.Value.ToString(); } reader.Read(); } if (reader.Name == "poser") { while (reader.NodeType != XmlNodeType.EndElement) { reader.Read(); if (reader.NodeType == XmlNodeType.Text) op9 = reader.Value.ToString(); } reader.Read(); } if (reader.Name == "ouvrir") { while (reader.NodeType != XmlNodeType.EndElement) { reader.Read(); if (reader.NodeType == XmlNodeType.Text) op10 = reader.Value.ToString(); } reader.Read(); } if (reader.Name == "ajouter") { while (reader.NodeType != XmlNodeType.EndElement) { reader.Read(); if (reader.NodeType == XmlNodeType.Text) op11 = reader.Value.ToString(); } reader.Read(); } } cursor.langue = language; fenetre = new Rectangle(0, 0, game.Window.ClientBounds.Width, game.Window.ClientBounds.Height); //taille de la fenetre MediaPlayer.IsMuted = true; text = new textbox(new Rectangle(game.Window.ClientBounds.Width / 3, game.Window.ClientBounds.Height / 3, 200, 100)); message = new textbox(new Rectangle(game.Window.ClientBounds.Width / 8, 2 * game.Window.ClientBounds.Height / 3, 600, 200)); message.Is_shown = false; current_tile = new Tile(0, 0, 0); tileset = new Rectangle(fenetre.Width - ressource.objet_map.Width, 0, ressource.objet_map.Width, ressource.objet_map.Height); listes_map = new Map[5, 5]; nb = 0; actuel = new Point(); selec = false; }
public void cheminement() { int NPCx = (int)(position.X / 32); int NPCy = (int)position.Y / 32; int playerx = (int)player.Position.X / 32; int playery = (int)player.Position.Y / 32; Tile start = new Tile(NPCx, NPCy, 1); Tile End = new Tile(playerx, playery, 1); List<Tile> sol = new List<Tile>(); sol = Pathfinding.Astar(Map, start, End); int i = 0; while (i < sol.Count) { Tile end = sol[i]; i++; //CanMove = false; deplacement = new Vector2(player.Position.X - position.X, player.Position.Y - position.Y); if (start.X == end.X && start.Y == end.Y) { direction = Direction.None; deplacement = new Vector2(0, 0); } if (start.X < end.X && start.Y == end.Y) { direction = Direction.Right; start.X++; //position.X += 32; } if (start.Y < end.Y && start.X == end.X) { direction = Direction.Down; start.Y++; //position.Y += 32; } if (start.Y > end.Y && start.X == end.X) { direction = Direction.Up; start.Y--; //position.Y -= 32; } if (start.X > end.X && start.Y == end.Y) { direction = Direction.Left; start.X--; // position.X -= 32; } Thread.Sleep(266); } Direction = Direction.None; }
public void load_collision(string path) { int j = 0; StreamReader sr = new StreamReader(path); string ligne; while ((ligne = sr.ReadLine()) != null) { for (int i = 0; i < colision.GetLength(0); i++) { tilelist[i, j] = new Tile(i, j, Convert.ToInt32(Convert.ToString(ligne[i]))); colision[i, j] = Convert.ToInt32(Convert.ToString(ligne[i])); } j += 1; } sr.Close(); }
public Node(Tile tile, Node Parent, Tile destination) { this.tile = tile; this.Parent = Parent; this.heuristic = Math.Abs(tile.X - destination.X) + Math.Abs(tile.Y - destination.Y) + (int)tile.Type; }