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