示例#1
0
        public void Reinizialize()
        {
            pathfound = false;
            if (pathfinder != null)
            {
                pathfinder.PathFound -= pathfinder_PathFound;
                pathfinder.Abort();
                pathfinder.Stop();
            }
            Random rand = new Random((int)DateTime.Now.Ticks);

            mapnodes = new Node[mapsize, mapsize];
            for (int y = 0; y < mapsize; y++)
            {
                for (int x = 0; x < mapsize; x++)
                {
                    mapnodes[y, x] = new Node(x, y, size, rand.Next(0, 100) < percentage ? Walkable.Blocked : Walkable.Walkable);
                }
            }
            mapnodes[yaim, xaim] = new Node(xaim, yaim, size, Walkable.Walkable);
            mapnodes[5, 2]       = new Node(2, 5, size, Walkable.Walkable);
            AStarMap.InitializeMap(mapnodes);
            pathfinder            = new AStar();
            pathfinder.PathFound += new PathFoundEventHandler(pathfinder_PathFound);

            pathfinder.FindWay(mapnodes[5, 2], mapnodes[yaim, xaim]);
        }
示例#2
0
        /// <summary>
        /// Loads the Level Tiles
        /// </summary>
        /// <param name="leveldata">the Content from Level = leveldata ;</param>
        /// <returns>if it was successfull</returns>
        private bool load_level_tiles(string leveldata)
        {
            bool successfull = true;

            try
            {
                //Split Lines
                string[] level_lines = leveldata.Split(':');
                int x_max = 0;
                int y_max = level_lines.Count();

                string[][] tiledata = new string[y_max][];

                //Split  each Line into Tiles
                for (int y = 0; y < y_max; y++)
                {
                    tiledata[y] = level_lines[y].Split(',');
                }

                //Search for longest Line
                foreach (string[] temp in tiledata)
                {
                    x_max = (int)MathHelper.Max(temp.Count(), x_max);
                }

                // Initializes the 2 dimensional Array
                tiles = new Tile[y_max, x_max];

                //Initialize every Tile
                for (int y = 0; y < y_max; y++)
                {
                    //Invoke LevelLoadLineEnhanced Event
                    if (LevelLoadLineEnhanced != null)
                        if ((y + 1) % 20 == 0 || y + 1 == y_max)
                        {
                            LevelLoadLineEnhanced(y + 1, y_max);
                            //System.Threading.Thread.Sleep(500);
                        }
                    for (int x = 0; x < x_max; x++)
                    {
                        //Set Tile Position
                        tiles[y, x] = new Tile(x, y);

                        //If this line is shorter than the longest Line,
                        //then fill rest with Empty tiles
                        if (x >= tiledata[y].Count() )
                        {
                            tiles[y, x].load_tile(((int)TileType.Empty),null);

                        }
                        else
                        {
                            //Load the Tile
                            //FAIL
                            bool parsing;
                            int tileint;
                            parsing = int.TryParse(tiledata[y][x],out tileint);
                            if (parsing == true)
                            {
                                if(y-1>=0)
                                    tiles[y, x].load_tile(int.Parse(tiledata[y][x]), tiles[y - 1, x]);
                                else
                                    tiles[y, x].load_tile(int.Parse(tiledata[y][x]),null);

                                //Search for the Start of the Level
                                if (tiles[y, x].TileType == TileType.Start)
                                {
                                    startpos.X = tiles[y, x].get_rect.Location.X + Tile.TILE_SIZE / 2;
                                    startpos.Y = tiles[y, x].get_rect.Location.Y + Tile.TILE_SIZE / 2;
                                }
                            }
                            else
                            {

                                tiles[y, x].load_tile((int)TileType.Empty,null);
                            }
                        }
                    }
                }

                //If Tiles is wall then load grass
                foreach (Tile tile in tiles)
                {
                    if (tile.TileType == TileType.Wall)
                    {
                        tile.loadGrass(tiles);
                    }
                }
                AStarMap map = new AStarMap();
                map.InitializeMapFromLevel(tiles);
            }
            catch (Exception ex)
            {
                successfull = false;
                throw new Exception(ex.Message);
            }

            return successfull;
        }