sneak/Shared/World.cs
Colin McMillen 2934296649 Refactor tile creation to support assets from multiple tilesets.
GitOrigin-RevId: 9863c368214536e09009e27dcb68b8d42f3d04f0
2020-02-18 15:24:49 -05:00

193 lines
6.3 KiB
C#

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
namespace SemiColinGames {
enum Terrain {
Grass,
GrassL,
GrassR,
Rock,
RockL,
RockR,
Water,
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 {
readonly Texture2D texture;
readonly Rectangle textureSource;
public Tile(Terrain terrain, Rectangle position, Texture2D texture, Rectangle textureSource) {
Terrain = terrain;
Position = position;
this.texture = texture;
this.textureSource = textureSource;
}
public Rectangle Position { get; private set; }
public Terrain Terrain { get; private set; }
public void Draw(SpriteBatch spriteBatch) {
spriteBatch.Draw(texture, Position.Location.ToVector2(), textureSource, Color.White);
}
}
class World {
public const int TileSize = 16;
readonly Tile[] tiles;
// Size of World in terms of tile grid.
private readonly int tileWidth;
private readonly int tileHeight;
// Size of World in pixels.
public int Width {
get { return tileWidth * TileSize; }
}
public int Height {
get { return tileHeight * TileSize; }
}
private static readonly Dictionary<char, Terrain> charToTerrain =
new Dictionary<char, Terrain>() {
{ '=', Terrain.Grass },
{ '<', Terrain.GrassL },
{ '>', Terrain.GrassR },
{ '.', Terrain.Rock },
{ '[', Terrain.RockL },
{ ']', Terrain.RockR },
{ '~', Terrain.Water },
{ 'X', Terrain.Block }
};
public World(ContentManager content, string levelSpecification) {
TileFactory factory = new TileFactory(content);
var tilesList = new List<Tile>();
string[] worldDesc = levelSpecification.Split('\n');
tileWidth = worldDesc.AsQueryable().Max(a => a.Length);
tileHeight = worldDesc.Length;
Debug.WriteLine("world size: {0}x{1}", tileWidth, tileHeight);
for (int i = 0; i < tileWidth; i++) {
for (int j = 0; j < tileHeight; j++) {
if (i < worldDesc[j].Length) {
char key = worldDesc[j][i];
if (charToTerrain.ContainsKey(key)) {
Terrain terrain = charToTerrain[key];
var position = new Rectangle(i * TileSize, j * TileSize, TileSize, TileSize);
tilesList.Add(factory.MakeTile(terrain, position));
}
}
}
}
tiles = tilesList.ToArray();
// Because we added tiles from left to right, the CollisionTargets are sorted by x-position.
// We maintain this invariant so that it's possible to efficiently find CollisionTargets that
// are nearby a given x-position.
CollisionTargets = new AABB[tiles.Length + 2];
// Add a synthetic collisionTarget on the left side of the world.
CollisionTargets[0] = new AABB(new Vector2(-1, 0), new Vector2(1, float.MaxValue));
// Now add all the normal collisionTargets for every static terrain tile.
Vector2 halfSize = new Vector2(TileSize / 2, TileSize / 2);
for (int i = 0; i < tiles.Length; i++) {
Vector2 center = new Vector2(
tiles[i].Position.Left + halfSize.X, tiles[i].Position.Top + halfSize.Y);
CollisionTargets[i + 1] = new AABB(center, halfSize);
}
// Add a final synthetic collisionTarget on the right side of the world.
CollisionTargets[tiles.Length + 1] = new AABB(
new Vector2(Width + 1, 0), new Vector2(1, float.MaxValue));
}
public void Draw(SpriteBatch spriteBatch) {
foreach (Tile t in tiles) {
t.Draw(spriteBatch);
}
}
public AABB[] CollisionTargets { get; }
}
}