properly handle non-rectangular input string[]s

GitOrigin-RevId: eb580f2e9c5a6d93827982240fd295ba30fdb0c4
This commit is contained in:
Colin McMillen 2020-01-15 16:45:55 -05:00
parent f58faa0b9f
commit 9a1c6646dd

View File

@ -1,7 +1,7 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace SemiColinGames { namespace SemiColinGames {
enum Terrain { enum Terrain {
@ -111,12 +111,14 @@ namespace SemiColinGames {
"...................................................................] [.............] [..............................................................] [......................................................." }; "...................................................................] [.............] [..............................................................] [......................................................." };
public World(Texture2D texture) { public World(Texture2D texture) {
width = worldDesc[0].Length; width = worldDesc.AsQueryable().Max(a => a.Length);
height = worldDesc.Length; height = worldDesc.Length;
Debug.WriteLine("world size: {0}x{1}", width, height);
tiles = new Tile[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 terrain = Terrain.Empty;
if (i < worldDesc[j].Length) {
switch (worldDesc[j][i]) { switch (worldDesc[j][i]) {
case '=': case '=':
terrain = Terrain.Grass; terrain = Terrain.Grass;
@ -147,6 +149,7 @@ namespace SemiColinGames {
terrain = Terrain.Empty; terrain = Terrain.Empty;
break; break;
} }
}
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[i, j] = new Tile(texture, terrain, position);
} }