Exemplo n.º 1
0
        public Chunk Get(ChunkSides match, Random random)
        {
            List <Chunk> list = _chunks[match];

            int value = random.Next(list.Sum(c => c.Rarity));

            int chunk = 0;

            if (value >= list[chunk].Rarity)
            {
                value -= list[chunk].Rarity;
                chunk++;
            }

            return(list[chunk]);
        }
Exemplo n.º 2
0
        private static bool TunnelUncertainChunk(ChunkSides[,] chunkSides, Random random)
        {
            int width  = chunkSides.GetLength(0);
            int height = chunkSides.GetLength(1);

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    ChunkSides sides = chunkSides[x, y];

                    if (sides.Left == SideStatus.Undecided || sides.Right == SideStatus.Undecided ||
                        sides.Top == SideStatus.Undecided || sides.Bottom == SideStatus.Undecided)
                    {
                        Tunnel(chunkSides, x, y, random);
                        return(true);
                    }
                }
            }

            return(false);
        }
Exemplo n.º 3
0
        private static void Tunnel(ChunkSides[,] chunkSides, int x, int y, Random random)
        {
            ChunkSides current = chunkSides[x, y];

            var choices = new List <Point>();

            if (current.Left == SideStatus.Undecided)
            {
                choices.Add(new Point(-1, 0));
                current.Left = SideStatus.Closed;
            }
            if (current.Right == SideStatus.Undecided)
            {
                choices.Add(new Point(1, 0));
                current.Right = SideStatus.Closed;
            }
            if (current.Top == SideStatus.Undecided)
            {
                choices.Add(new Point(0, -1));
                current.Top = SideStatus.Closed;
            }
            if (current.Bottom == SideStatus.Undecided)
            {
                choices.Add(new Point(0, 1));
                current.Bottom = SideStatus.Closed;
            }

            if (choices.Count == 0)
            {
                return;
            }

            Point      choice      = choices[random.Next(choices.Count)];
            ChunkSides choiceSides = chunkSides[x + choice.X, y + choice.Y];

            if (choice.X < 0)
            {
                current.Left      = SideStatus.Open;
                choiceSides.Right = SideStatus.Open;
            }
            if (choice.X > 0)
            {
                current.Right    = SideStatus.Open;
                choiceSides.Left = SideStatus.Open;
            }
            if (choice.Y < 0)
            {
                current.Top        = SideStatus.Open;
                choiceSides.Bottom = SideStatus.Open;
            }
            if (choice.Y > 0)
            {
                current.Bottom  = SideStatus.Open;
                choiceSides.Top = SideStatus.Open;
            }

            chunkSides[x, y] = current;
            chunkSides[x + choice.X, y + choice.Y] = choiceSides;

            Tunnel(chunkSides, x + choice.X, y + choice.Y, random);
        }
Exemplo n.º 4
0
        public static Level GenerateLevel(LD44Game game, int width, int height, ChunkSet chunkSet, bool sky, string background, Random random)
        {
            ChunkSides[,] chunkSides = new ChunkSides[width, height];

            if (width > 1)
            {
                chunkSides[0, height - 1].Right = SideStatus.Open;
                chunkSides[1, height - 1].Left  = SideStatus.Open;
            }
            if (height > 1)
            {
                chunkSides[0, height - 1].Top    = SideStatus.Closed;
                chunkSides[0, height - 2].Bottom = SideStatus.Closed;
            }

            if (width > 1)
            {
                chunkSides[width - 1, 0].Left  = SideStatus.Open;
                chunkSides[width - 2, 0].Right = SideStatus.Open;
            }
            if (height > 1)
            {
                chunkSides[width - 1, 0].Bottom = SideStatus.Closed;
                chunkSides[width - 1, 1].Top    = SideStatus.Closed;
            }

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    if (x == 0)
                    {
                        chunkSides[x, y].Left = SideStatus.Closed;
                    }
                    if (y == 0)
                    {
                        chunkSides[x, y].Top = sky ? SideStatus.Edge : SideStatus.Closed;
                    }
                    if (x == width - 1)
                    {
                        chunkSides[x, y].Right = SideStatus.Closed;
                    }
                    if (y == height - 1)
                    {
                        chunkSides[x, y].Bottom = SideStatus.Closed;
                    }
                }
            }

            if (width > 1)
            {
                Tunnel(chunkSides, 1, 0, random);

                while (TunnelUncertainChunk(chunkSides, random))
                {
                    ;
                }
            }

            int cx = 1;
            int cy = height - 1;

            while (cx < width - 2 || cy > 0)
            {
                if (cx < width - 2 && cy > 0)
                {
                    if (_random.Next(3) == 0)
                    {
                        chunkSides[cx, cy].Top        = SideStatus.Open;
                        chunkSides[cx, cy - 1].Bottom = SideStatus.Open;
                        cy--;
                    }
                    else
                    {
                        chunkSides[cx, cy].Right    = SideStatus.Open;
                        chunkSides[cx + 1, cy].Left = SideStatus.Open;
                        cx++;
                    }
                }
                else if (cx < width - 2)
                {
                    chunkSides[cx, cy].Right    = SideStatus.Open;
                    chunkSides[cx + 1, cy].Left = SideStatus.Open;
                    cx++;
                }
                else
                {
                    chunkSides[cx, cy].Top        = SideStatus.Open;
                    chunkSides[cx, cy - 1].Bottom = SideStatus.Open;
                    cy--;
                }
            }

            var level = new Level(chunkSet.Width * width, chunkSet.Height * height);

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    Chunk chunk = chunkSet.Get(chunkSides[x, y], random);

                    for (int y2 = 0; y2 < chunkSet.Height; y2++)
                    {
                        for (int x2 = 0; x2 < chunkSet.Width; x2++)
                        {
                            InitializeTile(game, level, x * chunkSet.Width + x2, y * chunkSet.Height + y2, chunk[x2, y2]);
                        }
                    }
                }
            }

            int fullHeight = height * chunkSet.Height;
            int fullWidth  = width * chunkSet.Width;

            for (int y = 0; y < fullHeight; y++)
            {
                for (int x = 0; x < fullWidth; x++)
                {
                    Tile tile = level.GetTile(x, y);

                    if (tile.TileType != TileType.Solid ||
                        (tile.FrontSprite.Texture != "rock" &&
                         tile.FrontSprite.Texture != "leaf" &&
                         tile.FrontSprite.Texture != "marble"))
                    {
                        continue;
                    }

                    if (y == 0 || level.GetTile(x, y - 1).TileType == TileType.Solid)
                    {
                        if (y == fullHeight - 1 || level.GetTile(x, y + 1).TileType == TileType.Solid)
                        {
                            tile.FrontSprite.Texture += "_center";
                        }
                        else
                        {
                            tile.FrontSprite.Texture += "_bottom";
                        }
                    }
                    else
                    {
                        tile.FrontSprite.Texture += "_top";
                    }
                }
            }

            level.Background.Texture = background;

            return(level);
        }