Browse Source

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

GitOrigin-RevId: 27db59aa11
master
Colin McMillen 4 years ago
parent
commit
16062f65c3
  1. 24
      Shared/World.cs

24
Shared/World.cs

@ -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;
}

Loading…
Cancel
Save