using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System.Collections.Generic; using System.Linq; namespace SemiColinGames { enum Terrain { Empty, Grass, GrassL, GrassR, Rock, RockL, RockR, Water, Block } class Tile { readonly Texture2D texture; readonly Rectangle textureSource; public Tile(Texture2D texture, Terrain terrain, Rectangle position) { this.texture = texture; Terrain = terrain; Position = position; this.textureSource = TextureSource(); } public Rectangle Position { get; private set; } public Terrain Terrain { get; private set; } public void Draw(SpriteBatch spriteBatch, Camera camera) { Vector2 drawPos = new Vector2(Position.Left - camera.Left, Position.Top); spriteBatch.Draw(texture, drawPos, textureSource, Color.White); } private Rectangle TextureSource() { int size = World.TileSize; switch (Terrain) { case Terrain.Grass: { return new Rectangle(3 * size, 0 * size, size, size); } case Terrain.GrassL: { return new Rectangle(2 * size, 0 * size, size, size); } case Terrain.GrassR: { return new Rectangle(4 * size, 0 * size, size, size); } case Terrain.Rock: { return new Rectangle(3 * size, 1 * size, size, size); } case Terrain.RockL: { return new Rectangle(1 * size, 2 * size, size, size); } case Terrain.RockR: { return new Rectangle(5 * size, 2 * size, size, size); } case Terrain.Water: { return new Rectangle(9 * size, 2 * size, size, size); } case Terrain.Block: { return new Rectangle(6 * size, 3 * size, size, size); } case Terrain.Empty: default: return new Rectangle(); } } } 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; } } readonly string worldString = @" X . X <======> <==X X <=> XX . XXX . XXXX . XXXXX . X <> <> = <> X X X X <> X X XX X <=X> XXXXXX . <> [] [] XX XX XXX XX XXXXXXX <> [] [] [] XXX XXX XXXX XXX <> <> XXXXXXXX []12345678[]123456[]123456789[]1234567890 123456 123456 12345 1234 12345 1234 123XXXX XXXX1234XXXXX XXXX1234[]123 1234567[]XXXXXXXXX12345678 ====================> <====..========..======..=========..=========> <=============> <==============================================================> <=======..==============..============================== ....................] [............................................] [.............] [..............................................................] [......................................................."; public World(Texture2D texture) { var tilesList = new List(); string[] worldDesc = worldString.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++) { Terrain terrain = Terrain.Empty; if (i < worldDesc[j].Length) { switch (worldDesc[j][i]) { case '=': terrain = Terrain.Grass; break; case '<': terrain = Terrain.GrassL; break; case '>': terrain = Terrain.GrassR; break; case '.': terrain = Terrain.Rock; break; case '[': terrain = Terrain.RockL; break; case ']': terrain = Terrain.RockR; break; case '~': terrain = Terrain.Water; break; case 'X': terrain = Terrain.Block; break; default: terrain = Terrain.Empty; break; } } if (terrain != Terrain.Empty) { var position = new Rectangle(i * TileSize, j * TileSize, TileSize, TileSize); tilesList.Add(new Tile(texture, 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, Camera camera) { foreach (Tile t in tiles) { t.Draw(spriteBatch, camera); } } public AABB[] CollisionTargets { get; } } }