public void SaveTileMatrix(TileType[,] map, string path) { IList<TileRecord> records = new List<TileRecord>(); int cols = map.GetLength(0); int rows = map.GetLength(1); for (int x = 0; x < cols; x++) { for (int y = 0; y < rows; y++) { TileType type = map[x, y]; TileRecord record = Mapper.Map<TileType, TileRecord>(type); record.X = x; record.Y = y; records.Add(record); } } if (File.Exists(path)) { File.Delete(path); } using (Stream stream = File.OpenWrite(path)) using (TextWriter textWriter = new StreamWriter(stream)) using (CsvWriter csvWriter = new CsvWriter(textWriter)) { foreach (TileRecord record in records) { csvWriter.WriteRecord(record); } } }
public override void SaveTileMap(TileType[,] map, string format) { int colTotal = map.GetLength(0); int rowTotal = map.GetLength(1); int mapWidth = (int)Math.Ceiling(colTotal / (double)fragmentWidth); int mapHeight = (int)Math.Ceiling(rowTotal / (double)fragmentHeight); for (int i = 0; i < mapWidth; i++) { int x = fragmentWidth * i; int w = fragmentWidth; if (i == mapWidth - 1) { w = colTotal - fragmentWidth * i; } for (int j = 0; j < mapHeight; j++) { int y = fragmentHeight * j; int h = fragmentHeight; if (j == mapHeight - 1) { h = rowTotal - fragmentHeight * j; } string path = string.Format(format, i, j); BuildBitmap(map, x, y, w, h, path); } } }
public virtual void SaveTileMap(TileType[,] map, string path) { int cols = map.GetLength(0); int rows = map.GetLength(1); BuildBitmap(map, 0, 0, cols, rows, path); }
protected override TileType SelectTileType(TileType[] tileTypes, int x, int y) { TileType tile = GetBaseTileSet(tileTypes, x, y); tile = GetDeeperTileSet(tile, tileTypes, x, y); tile = GetSpecificTileSet(tile, tileTypes, x, y); tile = OverrideWithDoorTileSet(tile, tileTypes, x, y); return tile; }
public TileType[,] CreateTileMap(TileType[] tileTypes) { var map = new TileType[Columns, Rows]; for (int x = 0; x < Columns; x++) { for (int y = 0; y < Rows; y++) { TileType tile = SelectTileType(tileTypes, x, y); map[x, y] = tile; } } return map; }
public void SaveFragmentMetadata(TileType[,] map, string path, IBuilder builder) { int colTotal = map.GetLength(0); int rowTotal = map.GetLength(1); int mapWidth = (int)Math.Ceiling(colTotal / (double)fragmentWidth); int mapHeight = (int)Math.Ceiling(rowTotal / (double)fragmentHeight); using (Stream stream = File.OpenWrite(path)) using (TextWriter writer = new StreamWriter(stream)) { writer.WriteLine(mapWidth); writer.WriteLine(mapHeight); writer.WriteLine(builder.Level); writer.WriteLine(builder.StartPosition.X); writer.WriteLine(builder.StartPosition.Y); writer.WriteLine(builder.ScreenTop); writer.WriteLine(builder.ScreenLeft); writer.WriteLine(builder.ScreenBottom); writer.WriteLine(builder.ScreenRight); } }
protected void BuildBitmap(TileType[,] map, int startX, int startY, int width, int height, string path) { Bitmap bitmap = new Bitmap(Tile.Width * width, Tile.Height * height); Graphics graphics = Graphics.FromImage(bitmap); int lengthX = map.GetLength(0); // just for array-bounds sanity check. int lengthY = map.GetLength(1); // just for array-bounds sanity check. for (int x = startX; x < startX + width && x < lengthX; x++) { for (int y = startY; y < startY + height && y < lengthY; y++) { Fill(graphics, map[x, y], x - startX, y - startY); } } if (File.Exists(path)) { File.Delete(path); } bitmap.Save(path, ImageFormat.Png); }
protected abstract TileType SelectTileType(TileType[] tileTypes, int x, int y);
protected void Fill(Graphics graphics, TileType tile, int x, int y) { using (Stream stream = getTileFileStream(tile.Type)) { Image texture = new Bitmap(stream); TextureBrush brush = new TextureBrush(texture); graphics.FillRectangle(brush, x * Tile.Width, y * Tile.Height, Tile.Width, Tile.Height); } }
private TileType GetBaseTileSet(TileType[] tileTypes, int x, int y) { TileType tile = tileTypes[0]; if (y == Rows - 2) { return tileTypes[7]; } else if (y > Rows - 2) { return tileTypes[8]; } if (x > 100 && x < 106) { return tile; } if (y == Rows - 22) { if (x > 20 && x < 25) { tile = tileTypes[0]; } else { tile = tileTypes[2]; } } if (y >= Rows - 21 && y < Rows - 17) { if (x > 20 && x < 25 && y == Rows - 21) { tile = tileTypes[5]; } else if (x > 20 && x < 25 && y == Rows - 20) { tile = tileTypes[6]; } else { tile = tileTypes[1]; } } else if ((x > 35 && x < 55) || (x > 60 && x < 110)) { if (y == Rows - 7) { if (x > 60 && x < 110) { if (x > 75 && x < 85) { tile = tileTypes[5]; } else if (x > 75) { tile = tileTypes[13]; } else { tile = tileTypes[11]; } } else { tile = tileTypes[3]; } } else if (y == Rows - 8) { if ((x > 42 && x < 48) || x > 75) { tile = tileTypes[0]; } else if (x > 60 && x < 110) { tile = tileTypes[13]; } else { tile = tileTypes[4]; } } else if (y == Rows - 6 && x > 60 && x < 110) { if (x > 75 && x < 85) { tile = tileTypes[12]; } else { tile = tileTypes[3]; } } } if (x > 28 && x < 34 && y == 18) { tile = tileTypes[4]; } if (x > 44 && x < 54) { if (y == 16) { tile = tileTypes[3]; } else if (y == 15) { tile = tileTypes[4]; } } return tile; }
private TileType OverrideWithDoorTileSet(TileType tile, TileType[] tileTypes, int x, int y) { if (y == Rows - 25) { if (x == Columns - 7) { tile = tileTypes[14]; } else if (x == Columns - 6) { tile = tileTypes[15]; } else if (x == Columns - 5) { tile = tileTypes[16]; } } else if (y == Rows - 24) { if (x == Columns - 7) { tile = tileTypes[17]; } else if (x == Columns - 6) { tile = tileTypes[18]; } else if (x == Columns - 5) { tile = tileTypes[19]; } } else if (y == Rows - 23) { if (x == Columns - 7) { tile = tileTypes[20]; } else if (x == Columns - 6) { tile = tileTypes[21]; } else if (x == Columns - 5) { tile = tileTypes[22]; } } return tile; }
private TileType GetSpecificTileSet(TileType tile, TileType[] tileTypes, int x, int y) { if (x > 98 && x < 101) { if (y == 22) { tile = tileTypes[0]; } else if (y == 23) { tile = tileTypes[5]; } else if (y == 24) { tile = tileTypes[6]; } } if (x == 101 && y == 24) { tile = tileTypes[9]; } else if (x == 105 && y == 28) { tile = tileTypes[10]; } else if (x == 106 && y == 28) { tile = tileTypes[1]; } else if (x == 107 && y == 28) { tile = tileTypes[9]; } if (x > 98 && x < 103 && y == 32) { tile = tileTypes[2]; } if (x > 88 && x < 92) { if (y == 33) { tile = tileTypes[2]; } else if (y == 34) { tile = tileTypes[1]; } } return tile; }
private TileType GetDeeperTileSet(TileType tile, TileType[] tileTypes, int x, int y) { if (x > 34 && x < 59) { if (y == Rows - 22) { tile = tileTypes[0]; } else if (y == Rows - 21) { tile = tileTypes[5]; } else if (y == Rows - 20) { tile = tileTypes[6]; } } if (x > 9 && x < 14) { if (y >= Rows - 22 && y < Rows - 15) { if (x == 10 && y == Rows - 19) { tile = tileTypes[9]; } else if (x == 13 && y == Rows - 21) { tile = tileTypes[10]; } else { tile = tileTypes[0]; } } } if (x > 12 && x < 19 && y == Rows - 13) { tile = tileTypes[4]; } if (x > 23 && x < 26) { if (y == Rows - 9) { tile = tileTypes[4]; } else if (y == Rows - 8) { tile = tileTypes[3]; } } if (x > 60 && x < 67) { if (y >= Rows - 22 && y < Rows - 15) { if (x == 61 && y == Rows - 21) { tile = tileTypes[9]; } else if (x == 66 && y == Rows - 19) { tile = tileTypes[10]; } else { tile = tileTypes[0]; } } } if (x > 57 && x < 61 && y == Rows - 13) { tile = tileTypes[4]; } if (x == 63 && y == Rows - 16) { tile = tileTypes[4]; } return tile; }
protected override TileType SelectTileType(TileType[] tileTypes, int x, int y) { TileType tile = tileTypes[0]; foreach (LevelBuilderRule rule in rules) { if (x >= rule.X && x < rule.Right) { if (y >= rule.Y && y < rule.Bottom) { if (rule.Incidence.HasValue) { var incidence = rule.Incidence.Value; if (incidence < random.NextDouble()) { continue; } } tile = ParseTileFromRule(rule, tileTypes); } } } return tile; }
private TileType ParseTileFromRule(LevelBuilderRule rule, TileType[] tileTypes) { int index; if (int.TryParse(rule.Tile, out index)) { return tileTypes[index]; } return tileTypes.First(t => t.Type == rule.Tile); }