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,41 +111,44 @@ 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;
switch (worldDesc[j][i]) { if (i < worldDesc[j].Length) {
case '=': switch (worldDesc[j][i]) {
terrain = Terrain.Grass; case '=':
break; terrain = Terrain.Grass;
case '<': break;
terrain = Terrain.GrassL; case '<':
break; terrain = Terrain.GrassL;
case '>': break;
terrain = Terrain.GrassR; case '>':
break; terrain = Terrain.GrassR;
case '.': break;
terrain = Terrain.Rock; case '.':
break; terrain = Terrain.Rock;
case '[': break;
terrain = Terrain.RockL; case '[':
break; terrain = Terrain.RockL;
case ']': break;
terrain = Terrain.RockR; case ']':
break; terrain = Terrain.RockR;
case '~': break;
terrain = Terrain.Water; case '~':
break; terrain = Terrain.Water;
case 'X': break;
terrain = Terrain.Block; case 'X':
break; terrain = Terrain.Block;
case ' ': break;
default: case ' ':
terrain = Terrain.Empty; default:
break; terrain = Terrain.Empty;
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);