World: convert from Tile[][] to List<Tile>

GitOrigin-RevId: 27db59aa1170c63d815d9661ef6e5328a17c6a99
This commit is contained in:
Colin McMillen 2020-01-25 11:23:37 -05:00
parent e6fa78e30f
commit 16062f65c3

View File

@ -84,7 +84,7 @@ namespace SemiColinGames {
public const int TileSize = 16; public const int TileSize = 16;
readonly int width; readonly int width;
readonly int height; readonly int height;
readonly Tile[,] tiles; readonly List<Tile> tiles = new List<Tile>();
public int Width { get; } public int Width { get; }
public int Height { get; } public int Height { get; }
@ -112,7 +112,6 @@ namespace SemiColinGames {
width = worldDesc.AsQueryable().Max(a => a.Length); width = worldDesc.AsQueryable().Max(a => a.Length);
height = worldDesc.Length; height = worldDesc.Length;
Debug.WriteLine("world size: {0}x{1}", width, height); Debug.WriteLine("world size: {0}x{1}", width, height);
tiles = new Tile[width, height];
for (int j = 0; j < height; j++) { for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) { for (int i = 0; i < width; i++) {
Terrain terrain = Terrain.Empty; Terrain terrain = Terrain.Empty;
@ -148,30 +147,25 @@ namespace SemiColinGames {
break; break;
} }
} }
if (terrain != Terrain.Empty) {
var position = new Rectangle(i * TileSize, j * TileSize, TileSize, TileSize); var position = new Rectangle(i * TileSize, j * TileSize, TileSize, TileSize);
tiles[i, j] = new Tile(texture, terrain, position); tiles.Add(new Tile(texture, terrain, position));
}
} }
} }
} }
public void Draw(SpriteBatch spriteBatch, Camera camera) { public void Draw(SpriteBatch spriteBatch, Camera camera) {
for (int j = 0; j < height; j++) { foreach (Tile t in tiles) {
for (int i = 0; i < width; i++) { t.Draw(spriteBatch, camera);
tiles[i, j].Draw(spriteBatch, camera);
}
} }
} }
public List<Rectangle> CollisionTargets() { public List<Rectangle> CollisionTargets() {
var result = new List<Rectangle>(); var result = new List<Rectangle>();
for (int j = 0; j < height; j++) { foreach (Tile t in tiles) {
for (int i = 0; i < width; i++) {
var t = tiles[i, j];
if (t.Terrain != Terrain.Empty) {
result.Add(t.Position); result.Add(t.Position);
} }
}
}
return result; return result;
} }
} }