sneak/Shared/World.cs

223 lines
8.8 KiB
C#
Raw Normal View History

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
namespace SemiColinGames {
class Terrain {
public static Terrain FromSymbol(char symbol) {
if (mapping.ContainsKey(symbol)) {
return mapping[symbol];
} else {
return null;
}
}
private readonly static Dictionary<char, Terrain> mapping = new Dictionary<char, Terrain>();
public static Terrain Grass = new Terrain('=', true);
public static Terrain GrassL = new Terrain('<', true);
public static Terrain GrassR = new Terrain('>', true);
public static Terrain Rock = new Terrain('.', true);
public static Terrain RockL = new Terrain('[', true);
public static Terrain RockR = new Terrain(']', true);
public static Terrain WaterL = new Terrain('~', false);
public static Terrain WaterR = new Terrain('`', false);
public static Terrain Block = new Terrain('X', true);
public static Terrain Spike = new Terrain('^', true);
public static Terrain Wood = new Terrain('_', true);
public static Terrain WoodL = new Terrain('(', true);
public static Terrain WoodR = new Terrain(')', true);
public static Terrain WoodVert = new Terrain('|', false);
public static Terrain WoodVertL = new Terrain('/', false);
public static Terrain WoodVertR = new Terrain('\\', false);
public static Terrain WoodBottom = new Terrain('v', false);
public static Terrain FenceL = new Terrain('d', false);
public static Terrain Fence = new Terrain('f', false);
public static Terrain FencePost = new Terrain('x', false);
public static Terrain FenceR = new Terrain('b', false);
public static Terrain VineTop = new Terrain('{', false);
public static Terrain VineMid = new Terrain(';', false);
public static Terrain VineBottom = new Terrain('}', false);
public static Terrain GrassTall = new Terrain('q', false);
public static Terrain GrassShort = new Terrain('w', false);
public static Terrain Shoots = new Terrain('e', false);
public static Terrain Bush = new Terrain('r', false);
public static Terrain Mushroom = new Terrain('t', false);
public bool IsObstacle { get; private set; }
private Terrain(char symbol, bool isObstacle) {
if (mapping.ContainsKey(symbol)) {
throw new ArgumentException("already have a terrain with symbol " + symbol);
}
IsObstacle = isObstacle;
mapping[symbol] = this;
}
}
class TileFactory {
struct TextureSource {
public Texture2D texture;
public Rectangle source;
public TextureSource(Texture2D texture, Rectangle source) {
this.texture = texture;
this.source = source;
}
}
readonly Dictionary<Terrain, TextureSource> terrainToTexture;
public TileFactory() {
terrainToTexture = new Dictionary<Terrain, TextureSource>() {
{ Terrain.GrassL, GetTextureSource(Textures.Grassland, 2, 0) },
{ Terrain.Grass, GetTextureSource(Textures.Grassland, 3, 0) },
{ Terrain.GrassR, GetTextureSource(Textures.Grassland, 4, 0) },
{ Terrain.Rock, GetTextureSource(Textures.Grassland, 3, 1) },
{ Terrain.RockL, GetTextureSource(Textures.Grassland, 1, 2) },
{ Terrain.RockR, GetTextureSource(Textures.Grassland, 5, 2) },
{ Terrain.WaterL, GetTextureSource(Textures.Grassland, 9, 2) },
{ Terrain.WaterR, GetTextureSource(Textures.Grassland, 10, 2) },
{ Terrain.Block, GetTextureSource(Textures.Ruins, 2, 0) },
{ Terrain.Spike, GetTextureSource(Textures.Grassland, 11, 8) },
{ Terrain.Wood, GetTextureSource(Textures.Grassland, 10, 3) },
{ Terrain.WoodL, GetTextureSource(Textures.Grassland, 9, 3) },
{ Terrain.WoodR, GetTextureSource(Textures.Grassland, 12, 3) },
{ Terrain.WoodVert, GetTextureSource(Textures.Grassland, 9, 5) },
{ Terrain.WoodVertL, GetTextureSource(Textures.Grassland, 9, 4) },
{ Terrain.WoodVertR, GetTextureSource(Textures.Grassland, 12, 4) },
{ Terrain.WoodBottom, GetTextureSource(Textures.Grassland, 10, 5) },
{ Terrain.FenceL, GetTextureSource(Textures.Grassland, 5, 4) },
{ Terrain.Fence, GetTextureSource(Textures.Grassland, 6, 4) },
{ Terrain.FencePost, GetTextureSource(Textures.Grassland, 7, 4) },
{ Terrain.FenceR, GetTextureSource(Textures.Grassland, 8, 4) },
{ Terrain.VineTop, GetTextureSource(Textures.Ruins, 12, 5) },
{ Terrain.VineMid, GetTextureSource(Textures.Ruins, 12, 6) },
{ Terrain.VineBottom, GetTextureSource(Textures.Ruins, 12, 7) },
{ Terrain.GrassTall, GetTextureSource(Textures.Grassland, 13, 0) },
{ Terrain.GrassShort, GetTextureSource(Textures.Grassland, 14, 0) },
{ Terrain.Shoots, GetTextureSource(Textures.Grassland, 15, 0) },
{ Terrain.Bush, GetTextureSource(Textures.Grassland, 13, 2) },
{ Terrain.Mushroom, GetTextureSource(Textures.Grassland, 17, 2) },
};
}
public Tile MakeTile(Terrain terrain, Rectangle position) {
TextureSource textureSource = terrainToTexture[terrain];
return new Tile(terrain, position, textureSource.texture, textureSource.source);
}
private TextureSource GetTextureSource(Texture2D texture, int x, int y) {
int size = World.TileSize;
Rectangle position = new Rectangle(x * size, y * size, size, size);
return new TextureSource(texture, position);
}
}
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;
readonly Tile[] decorations;
// 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; }
}
public World(string levelSpecification) {
TileFactory factory = new TileFactory();
var tilesList = new List<Tile>();
var decorationsList = new List<Tile>();
string[] worldDesc = levelSpecification.Substring(1).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];
Terrain terrain = Terrain.FromSymbol(key);
if (terrain != null) {
var position = new Rectangle(i * TileSize, j * TileSize, TileSize, TileSize);
Tile tile = factory.MakeTile(terrain, position);
if (tile.Terrain.IsObstacle) {
tilesList.Add(tile);
} else {
decorationsList.Add(tile);
}
}
}
}
}
tiles = tilesList.ToArray();
decorations = decorationsList.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 DrawBackground(SpriteBatch spriteBatch) {
foreach (Tile t in decorations) {
t.Draw(spriteBatch);
}
}
public void DrawForeground(SpriteBatch spriteBatch) {
foreach (Tile t in tiles) {
t.Draw(spriteBatch);
}
}
public AABB[] CollisionTargets { get; }
}
}