From 293429664939fc0b3b8f0e1671e9083d35442ef9 Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Tue, 18 Feb 2020 15:24:08 -0500 Subject: [PATCH] Refactor tile creation to support assets from multiple tilesets. GitOrigin-RevId: 9863c368214536e09009e27dcb68b8d42f3d04f0 --- Shared/World.cs | 101 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 78 insertions(+), 23 deletions(-) diff --git a/Shared/World.cs b/Shared/World.cs index 9c8d28c..d8eaeeb 100644 --- a/Shared/World.cs +++ b/Shared/World.cs @@ -17,27 +17,88 @@ namespace SemiColinGames { Block } + enum TileSet { + Cemetery, + Crypt, + Dungeon, + Forest, + Garden, + Grassland, + Ruins, + Sewer, + Temple, + Village, + } + + class TileFactory { + struct TileSetAndPoint { + public TileSet tileSet; + public Point point; + + public TileSetAndPoint(TileSet tileSet, int x, int y) { + this.tileSet = tileSet; + point = new Point(x, y); + } + } + + static readonly Dictionary tileSetToContentPath = + new Dictionary() { + { TileSet.Cemetery, "tiles/anokolisa/cemetery" }, + { TileSet.Crypt, "tiles/anokolisa/crypt" }, + { TileSet.Dungeon, "tiles/anokolisa/dungeon" }, + { TileSet.Forest, "tiles/anokolisa/forest" }, + { TileSet.Garden, "tiles/anokolisa/garden" }, + { TileSet.Grassland, "tiles/anokolisa/grassland" }, + { TileSet.Ruins, "tiles/anokolisa/ruins" }, + { TileSet.Sewer, "tiles/anokolisa/sewer" }, + { TileSet.Temple, "tiles/anokolisa/temple" }, + { TileSet.Village, "tiles/anokolisa/village" }, + }; + + static readonly Dictionary terrainToTilePosition = + new Dictionary() { + { 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; + + public TileFactory(ContentManager content) { + Array tileSets = Enum.GetValues(typeof(TileSet)); + textures = new Texture2D[tileSets.Length]; + foreach (TileSet tileSet in tileSets) { + textures[(int) tileSet] = content.Load(tileSetToContentPath[tileSet]); + } + } + + public Tile MakeTile(Terrain terrain, Rectangle position) { + TileSet tileSet = terrainToTilePosition[terrain].tileSet; + Texture2D texture = textures[(int) tileSet]; + return new Tile(terrain, position, texture, TextureSource(terrain)); + } + + private static Rectangle TextureSource(Terrain terrain) { + int size = World.TileSize; + Point pos = terrainToTilePosition[terrain].point; + return new Rectangle(pos.X * size, pos.Y * size, size, size); + } + } + class Tile { readonly Texture2D texture; readonly Rectangle textureSource; - static readonly Dictionary terrainToTilePosition = - new Dictionary() { - { Terrain.Grass, new Point(3, 0) }, - { Terrain.GrassL, new Point(2, 0) }, - { Terrain.GrassR, new Point(4, 0) }, - { Terrain.Rock, new Point(3, 1) }, - { Terrain.RockL, new Point(1, 2) }, - { Terrain.RockR, new Point(5, 2) }, - { Terrain.Water, new Point(9, 2) }, - { Terrain.Block, new Point(6, 3) }, - }; - - public Tile(Texture2D texture, Terrain terrain, Rectangle position) { - this.texture = texture; + public Tile(Terrain terrain, Rectangle position, Texture2D texture, Rectangle textureSource) { Terrain = terrain; Position = position; - this.textureSource = TextureSource(); + this.texture = texture; + this.textureSource = textureSource; } public Rectangle Position { get; private set; } @@ -46,12 +107,6 @@ namespace SemiColinGames { public void Draw(SpriteBatch spriteBatch) { spriteBatch.Draw(texture, Position.Location.ToVector2(), textureSource, Color.White); } - - private Rectangle TextureSource() { - int size = World.TileSize; - Point pos = terrainToTilePosition[Terrain]; - return new Rectangle(pos.X * size, pos.Y * size, size, size); - } } class World { @@ -85,7 +140,7 @@ namespace SemiColinGames { }; public World(ContentManager content, string levelSpecification) { - Texture2D texture = content.Load("tiles/anokolisa/grassland"); + TileFactory factory = new TileFactory(content); var tilesList = new List(); string[] worldDesc = levelSpecification.Split('\n'); tileWidth = worldDesc.AsQueryable().Max(a => a.Length); @@ -98,7 +153,7 @@ namespace SemiColinGames { if (charToTerrain.ContainsKey(key)) { Terrain terrain = charToTerrain[key]; var position = new Rectangle(i * TileSize, j * TileSize, TileSize, TileSize); - tilesList.Add(new Tile(texture, terrain, position)); + tilesList.Add(factory.MakeTile(terrain, position)); } } }