コード例 #1
0
 public static void CreateDungeon(MapTile map)
 {
     map.Dungeon = DataReader.GetNextDungeon(Program.Player.Stats.Level.Lvl);
 }
コード例 #2
0
        public static void GenerateTrees(Random rng, MapTile map, int numOfSeedTrees, int growthGenerations)
        {
            // place seed trees
            List <Point> availableSpots = map.GetAllGrassTiles();
            List <Point> treeSpots      = new List <Point>();
            Point        nextSpot       = null;
            int          placedTrees    = 0;

            // placement algorithm
            while (placedTrees < numOfSeedTrees)
            {
                Point potentialSpot = null;
                int   count         = 0;
                while (nextSpot == null && count < availableSpots.Count)
                {
                    count++;
                    potentialSpot = availableSpots[rng.Next(0, availableSpots.Count)];
                    if (potentialSpot.DistFrom(map.GetClosestOfTileTypeToPos(potentialSpot, new Water())) <= 8)
                    {
                        nextSpot = potentialSpot;
                    }
                }

                if (nextSpot != null)
                {
                    treeSpots.Add(nextSpot);
                    map.Blocks[nextSpot.X * map.Width + nextSpot.Y] = new Tree(Material.Wood);
                }
                placedTrees++;
            }

            // run growth algorithm
            for (int i = 0; i < growthGenerations; i++)
            {
                int treesThisGen = treeSpots.Count;
                for (int j = 0; j < treesThisGen; j++)
                {
                    Point currentTree = treeSpots[j];
                    nextSpot = new Point(rng.Next(Math.Max(currentTree.X - 8, 0), Math.Min(currentTree.X + 8, map.Width - 1)), rng.Next(Math.Max(currentTree.Y - 8, 0), Math.Min(currentTree.Y + 8, map.Height - 1)));
                    int count = 0;
                    do
                    {
                        count++;
                        nextSpot = new Point(rng.Next(Math.Max(currentTree.X - 8, 0), Math.Min(currentTree.X + 8, map.Width - 1)), rng.Next(Math.Max(currentTree.Y - 8, 0), Math.Min(currentTree.Y + 8, map.Height - 1)));
                        for (int x = Math.Max(nextSpot.X - 3, 0); x <= Math.Min(nextSpot.X + 3, map.Width - 1); x++)
                        {
                            for (int y = Math.Max(nextSpot.Y - 3, 0); y <= Math.Min(nextSpot.Y + 3, map.Height - 1); y++)
                            {
                                if (map[x, y] is Tree)
                                {
                                    availableSpots.Remove(nextSpot);
                                }
                            }
                        }
                    } while ((map.GetEmptyAdjacentBlocks(nextSpot).Count != 8 || availableSpots.Contains(nextSpot) == false) && count <= 5);
                    if (count <= 5)
                    {
                        treeSpots.Add(nextSpot);
                        map.Blocks[nextSpot.X * map.Width + nextSpot.Y] = new Tree(Material.Wood);
                    }
                }
            }
        }