using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System; using System.Collections.Generic; using System.Linq; namespace SemiColinGames { public class Terrain { public static Terrain FromSymbol(char symbol) { if (mapping.ContainsKey(symbol)) { return mapping[symbol]; } else { return null; } } private readonly static Dictionary mapping = new Dictionary(); public static Terrain Empty = new Terrain('\0', false, Textures.Grassland, 0, 0); public static Terrain Grass = new Terrain('=', true, Textures.Grassland, 3, 0); public static Terrain GrassL = new Terrain('<', true, Textures.Grassland, 2, 0); public static Terrain GrassR = new Terrain('>', true, Textures.Grassland, 4, 0); public static Terrain Rock = new Terrain('.', true, Textures.Grassland, 3, 1); public static Terrain RockL = new Terrain('[', true, Textures.Grassland, 1, 2); public static Terrain RockR = new Terrain(']', true, Textures.Grassland, 5, 2); public static Terrain WaterL = new Terrain('~', false, Textures.Grassland, 9, 2); public static Terrain WaterR = new Terrain('`', false, Textures.Grassland, 10, 2); public static Terrain Block = new Terrain('X', true, Textures.Ruins, 2, 0); public static Terrain Spike = new Terrain('^', true, Textures.Grassland, 11, 8); public static Terrain Wood = new Terrain('_', true, Textures.Grassland, 10, 3); public static Terrain WoodL = new Terrain('(', true, Textures.Grassland, 9, 3); public static Terrain WoodR = new Terrain(')', true, Textures.Grassland, 12, 3); public static Terrain WoodVert = new Terrain('|', false, Textures.Grassland, 9, 5); public static Terrain WoodVertL = new Terrain('/', false, Textures.Grassland, 9, 4); public static Terrain WoodVertR = new Terrain('\\', false, Textures.Grassland, 12, 4); public static Terrain WoodBottom = new Terrain('v', false, Textures.Grassland, 10, 5); public static Terrain FenceL = new Terrain('d', false, Textures.Grassland, 5, 4); public static Terrain Fence = new Terrain('f', false, Textures.Grassland, 6, 4); public static Terrain FencePost = new Terrain('x', false, Textures.Grassland, 7, 4); public static Terrain FenceR = new Terrain('b', false, Textures.Grassland, 8, 4); public static Terrain VineTop = new Terrain('{', false, Textures.Ruins, 12, 5); public static Terrain VineMid = new Terrain(';', false, Textures.Ruins, 12, 6); public static Terrain VineBottom = new Terrain('}', false, Textures.Ruins, 12, 7); public static Terrain GrassTall = new Terrain('q', false, Textures.Grassland, 13, 0); public static Terrain GrassShort = new Terrain('w', false, Textures.Grassland, 14, 0); public static Terrain Shoots = new Terrain('e', false, Textures.Grassland, 15, 0); public static Terrain Bush = new Terrain('r', false, Textures.Grassland, 13, 2); public static Terrain Mushroom = new Terrain('t', false, Textures.Grassland, 17, 2); public bool IsObstacle { get; private set; } public bool IsHarmful { get; private set; } = false; public TextureRef Texture { get; private set; } public Rectangle TextureSource { get; private set; } private Terrain(char symbol, bool isObstacle, TextureRef texture, int x, int y) { if (mapping.ContainsKey(symbol)) { throw new ArgumentException("already have a terrain with symbol " + symbol); } mapping[symbol] = this; IsObstacle = isObstacle; // TODO: don't hard-code just the one spike. if (symbol == '^') { IsHarmful = true; } Texture = texture; int size = World.TileSize; TextureSource = new Rectangle(x * size, y * size, size, size); } } class Tile { public Tile(Terrain terrain, Rectangle position) { Terrain = terrain; Position = position; } public Rectangle Position { get; private set; } public Terrain Terrain { get; private set; } public void Draw(SpriteBatch spriteBatch) { spriteBatch.Draw( Terrain.Texture.Get, Position.Location.ToVector2(), Terrain.TextureSource, Color.White); } } class World { public const int TileSize = 16; readonly Tile[] tiles; readonly Tile[] decorations; Player player; readonly NPC[] npcs = new NPC[1]; // Size of World in terms of tile grid. private readonly int tileWidth; private readonly int tileHeight; public Player Player { get { return player; } } // Size of World in pixels. public int Width { get { return tileWidth * TileSize; } } public int Height { get { return tileHeight * TileSize; } } public World(string levelSpecification) { player = new Player(); npcs[0] = new NPC(new Point(16 * 38, 16 * 12)); var tilesList = new List(); var decorationsList = new List(); 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 = new Tile(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, tiles[i].Terrain); } // 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 Update(float modelTime, History input) { player.Update(modelTime, input, CollisionTargets); foreach (NPC npc in npcs) { npc.Update(modelTime); } if (player.Health <= 0) { Reset(); } } void Reset() { player = new Player(); } public void DrawBackground(SpriteBatch spriteBatch) { foreach (Tile t in decorations) { t.Draw(spriteBatch); } foreach (NPC npc in npcs) { npc.Draw(spriteBatch); } } public void DrawForeground(SpriteBatch spriteBatch) { foreach (Tile t in tiles) { t.Draw(spriteBatch); } } public AABB[] CollisionTargets { get; } } }