public static ChunkSet FromTexture(Texture2D texture, int count) { var set = new ChunkSet(); int width = texture.Width / count; var region = new Rectangle(0, 0, width, texture.Height); for (int i = 0; i < count; i++) { set.Add(Chunk.FromTexture(texture, region)); region.X += width; } return(set); }
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); }