Refactor tile creation to support assets from multiple tilesets.
GitOrigin-RevId: 9863c368214536e09009e27dcb68b8d42f3d04f0
This commit is contained in:
parent
02aba3ad84
commit
2934296649
101
Shared/World.cs
101
Shared/World.cs
@ -17,27 +17,88 @@ namespace SemiColinGames {
|
|||||||
Block
|
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<TileSet, string> tileSetToContentPath =
|
||||||
|
new Dictionary<TileSet, string>() {
|
||||||
|
{ 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<Terrain, TileSetAndPoint> terrainToTilePosition =
|
||||||
|
new Dictionary<Terrain, TileSetAndPoint>() {
|
||||||
|
{ 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<Texture2D>(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 {
|
class Tile {
|
||||||
readonly Texture2D texture;
|
readonly Texture2D texture;
|
||||||
readonly Rectangle textureSource;
|
readonly Rectangle textureSource;
|
||||||
|
|
||||||
static readonly Dictionary<Terrain, Point> terrainToTilePosition =
|
public Tile(Terrain terrain, Rectangle position, Texture2D texture, Rectangle textureSource) {
|
||||||
new Dictionary<Terrain, Point>() {
|
|
||||||
{ 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;
|
|
||||||
Terrain = terrain;
|
Terrain = terrain;
|
||||||
Position = position;
|
Position = position;
|
||||||
this.textureSource = TextureSource();
|
this.texture = texture;
|
||||||
|
this.textureSource = textureSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Rectangle Position { get; private set; }
|
public Rectangle Position { get; private set; }
|
||||||
@ -46,12 +107,6 @@ namespace SemiColinGames {
|
|||||||
public void Draw(SpriteBatch spriteBatch) {
|
public void Draw(SpriteBatch spriteBatch) {
|
||||||
spriteBatch.Draw(texture, Position.Location.ToVector2(), textureSource, Color.White);
|
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 {
|
class World {
|
||||||
@ -85,7 +140,7 @@ namespace SemiColinGames {
|
|||||||
};
|
};
|
||||||
|
|
||||||
public World(ContentManager content, string levelSpecification) {
|
public World(ContentManager content, string levelSpecification) {
|
||||||
Texture2D texture = content.Load<Texture2D>("tiles/anokolisa/grassland");
|
TileFactory factory = new TileFactory(content);
|
||||||
var tilesList = new List<Tile>();
|
var tilesList = new List<Tile>();
|
||||||
string[] worldDesc = levelSpecification.Split('\n');
|
string[] worldDesc = levelSpecification.Split('\n');
|
||||||
tileWidth = worldDesc.AsQueryable().Max(a => a.Length);
|
tileWidth = worldDesc.AsQueryable().Max(a => a.Length);
|
||||||
@ -98,7 +153,7 @@ namespace SemiColinGames {
|
|||||||
if (charToTerrain.ContainsKey(key)) {
|
if (charToTerrain.ContainsKey(key)) {
|
||||||
Terrain terrain = charToTerrain[key];
|
Terrain terrain = charToTerrain[key];
|
||||||
var position = new Rectangle(i * TileSize, j * TileSize, TileSize, TileSize);
|
var position = new Rectangle(i * TileSize, j * TileSize, TileSize, TileSize);
|
||||||
tilesList.Add(new Tile(texture, terrain, position));
|
tilesList.Add(factory.MakeTile(terrain, position));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user