More refactoring of Tile loading.

GitOrigin-RevId: 96a697bc78cdaefc874b9eb7b9d4cca28efe27f2
This commit is contained in:
Colin McMillen 2020-02-18 16:50:26 -05:00
parent 2934296649
commit 8f5514b776

View File

@ -31,13 +31,14 @@ namespace SemiColinGames {
} }
class TileFactory { class TileFactory {
struct TileSetAndPoint {
public TileSet tileSet;
public Point point;
public TileSetAndPoint(TileSet tileSet, int x, int y) { struct TextureSource {
this.tileSet = tileSet; public Texture2D texture;
point = new Point(x, y); public Rectangle source;
public TextureSource(Texture2D texture, Rectangle source) {
this.texture = texture;
this.source = source;
} }
} }
@ -55,17 +56,8 @@ namespace SemiColinGames {
{ TileSet.Village, "tiles/anokolisa/village" }, { TileSet.Village, "tiles/anokolisa/village" },
}; };
static readonly Dictionary<Terrain, TileSetAndPoint> terrainToTilePosition = readonly Dictionary<Terrain, TextureSource> terrainToTexture =
new Dictionary<Terrain, TileSetAndPoint>() { new Dictionary<Terrain, TextureSource>();
{ Terrain.Grass, new TileSetAndPoint(TileSet.Grassland, 3, 0) },
{ Terrain.GrassL, new TileSetAndPoint(TileSet.Grassland, 2, 0) },
{ Terrain.GrassR, new TileSetAndPoint(TileSet.Grassland, 4, 0) },
{ Terrain.Rock, new TileSetAndPoint(TileSet.Grassland, 3, 1) },
{ Terrain.RockL, new TileSetAndPoint(TileSet.Grassland, 1, 2) },
{ Terrain.RockR, new TileSetAndPoint(TileSet.Grassland, 5, 2) },
{ Terrain.Water, new TileSetAndPoint(TileSet.Grassland, 9, 2) },
{ Terrain.Block, new TileSetAndPoint(TileSet.Grassland, 6, 3) },
};
readonly Texture2D[] textures; readonly Texture2D[] textures;
@ -75,18 +67,27 @@ namespace SemiColinGames {
foreach (TileSet tileSet in tileSets) { foreach (TileSet tileSet in tileSets) {
textures[(int) tileSet] = content.Load<Texture2D>(tileSetToContentPath[tileSet]); textures[(int) tileSet] = content.Load<Texture2D>(tileSetToContentPath[tileSet]);
} }
terrainToTexture[Terrain.Grass] = GetTextureSource(TileSet.Grassland, 3, 0);
terrainToTexture[Terrain.GrassL] = GetTextureSource(TileSet.Grassland, 2, 0);
terrainToTexture[Terrain.GrassR] = GetTextureSource(TileSet.Grassland, 4, 0);
terrainToTexture[Terrain.Rock] = GetTextureSource(TileSet.Grassland, 3, 1);
terrainToTexture[Terrain.RockL] = GetTextureSource(TileSet.Grassland, 1, 2);
terrainToTexture[Terrain.RockR] = GetTextureSource(TileSet.Grassland, 5, 2);
terrainToTexture[Terrain.Water] = GetTextureSource(TileSet.Grassland, 9, 2);
terrainToTexture[Terrain.Block] = GetTextureSource(TileSet.Grassland, 6, 3);
} }
public Tile MakeTile(Terrain terrain, Rectangle position) { public Tile MakeTile(Terrain terrain, Rectangle position) {
TileSet tileSet = terrainToTilePosition[terrain].tileSet; TextureSource textureSource = terrainToTexture[terrain];
Texture2D texture = textures[(int) tileSet]; return new Tile(terrain, position, textureSource.texture, textureSource.source);
return new Tile(terrain, position, texture, TextureSource(terrain));
} }
private static Rectangle TextureSource(Terrain terrain) { private TextureSource GetTextureSource(TileSet tileSet, int x, int y) {
Texture2D texture = textures[(int) tileSet];
int size = World.TileSize; int size = World.TileSize;
Point pos = terrainToTilePosition[terrain].point; Rectangle position = new Rectangle(x * size, y * size, size, size);
return new Rectangle(pos.X * size, pos.Y * size, size, size); return new TextureSource(texture, position);
} }
} }