Пример #1
0
 public LogicalTile(Tile[] tiles, Region region)
 {
     this.tiles = tiles;
     this.region = region;
 }
Пример #2
0
        public Map(Palette palette, int width, int height)
        {
            this.id = 0;
            this.palette = palette;
            this.width = width;
            this.height = height;
            this.name = "o2d random map";

            entities = new Dictionary<uint, Entity>();

            Queue<Rectangle> regionQ = new Queue<Rectangle>();
            List<Rectangle> regionList = new List<Rectangle>();
            regionQ.Enqueue(new Rectangle(OceanBuffer, OceanBuffer, width - (OceanBuffer << 1), height - (OceanBuffer << 1)));

            Random rand = new Random();
            while (regionQ.Count > 0) {
                Rectangle r1 = regionQ.Dequeue();
                Rectangle r2;
                if (rand.Next(0, 2) == 0) {
                    if (r1.Width < DimensionThreshold) {
                        regionList.Add(r1);
                        continue;
                    }
                    int slice = rand.Next(MinimumDimension, r1.Width - MinimumDimension);
                    r2 = new Rectangle(r1.X + slice, r1.Y, r1.Width - slice, r1.Height);
                    r1.Width = slice;
                } else {
                    if (r1.Height < DimensionThreshold) {
                        regionList.Add(r1);
                        continue;
                    }
                    int slice = rand.Next(MinimumDimension, r1.Height - MinimumDimension);
                    r2 = new Rectangle(r1.X, r1.Y + slice, r1.Width, r1.Height - slice);
                    r1.Height = slice;
                }

                if (Math.Max(r1.Width, r1.Height) >= DimensionThreshold)
                    regionQ.Enqueue(r1);
                else
                    regionList.Add(r1);

                if (Math.Max(r2.Width, r2.Height) >= DimensionThreshold)
                    regionQ.Enqueue(r2);
                else
                    regionList.Add(r2);
            }
            tiles = new Tile[width, height, Layers];
            regions = new Region[width, height];
            foreach (Rectangle regionSpace in regionList) {
                int regionType = rand.Next(0, 4);
                Region region = new Region((RegionType)regionType, regionSpace);

                int maxX = regionSpace.X + regionSpace.Width;
                int maxY = regionSpace.Y + regionSpace.Height;
                for (int i = regionSpace.X; i < maxX; ++i)
                    for (int j = regionSpace.Y; j < maxY; ++j) {
                        tiles[i, j, 0] = palette[regionType];
                        regions[i, j] = region;
                    }
            }

            for (int i = 0; i < width; ++i) {
                for (int j = 0; j < height; ++j) {
                    if (j == OceanBuffer && i >= OceanBuffer && i < width - OceanBuffer)
                        j = height - OceanBuffer;
                    tiles[i, j, 0] = palette[4];
                }
            }

            InitializeNeighbors();
        }