using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System; using System.Collections.Generic; namespace Jumpy { enum Terrain { Empty, Grass, GrassL, GrassR, Rock, Water } class Tile { Texture2D texture; Terrain terrain; Rectangle position; public Tile(Texture2D texture, Terrain terrain, Rectangle position) { this.texture = texture; this.terrain = terrain; this.position = position; } public Rectangle Position { get { return position; } } public Terrain Terrain { get { return terrain; } } public void Draw(SpriteBatch spriteBatch) { int size = World.TileSize; switch (terrain) { case Terrain.Grass: { // TODO: hold these rectangles statically instead of making them anew constantly. Rectangle source = new Rectangle(3 * size, 0 * size, size, size); spriteBatch.Draw(texture, position, source, Color.White); break; } case Terrain.GrassL: { Rectangle source = new Rectangle(2 * size, 0 * size, size, size); spriteBatch.Draw(texture, position, source, Color.White); break; } case Terrain.GrassR: { Rectangle source = new Rectangle(4 * size, 0 * size, size, size); spriteBatch.Draw(texture, position, source, Color.White); break; } case Terrain.Rock: { Rectangle source = new Rectangle(3 * size, 1 * size, size, size); spriteBatch.Draw(texture, position, source, Color.White); break; } case Terrain.Water: { Rectangle source = new Rectangle(9 * size, 2 * size, size, size); spriteBatch.Draw(texture, position, source, Color.White); break; } case Terrain.Empty: default: break; } } } class World { public const int TileSize = 16; int width; int height; Tile[,] tiles; public int Width { get; } public int Height { get; } string[] worldDesc = new string[] { " ", " ", " ", " <=> ", " ", " ", " <=> <=> ", " = ", " ", " ", "=============> <===", "..............~~~~~." }; public World(Texture2D texture) { // TODO: better error handling for if the string[] isn't rectangular. width = worldDesc[0].Length; height = worldDesc.Length; tiles = new Tile[width, height]; for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { Terrain terrain; 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.Water; break; case ' ': default: terrain = Terrain.Empty; break; } var position = new Rectangle(i * TileSize, j * TileSize, TileSize, TileSize); tiles[i, j] = new Tile(texture, terrain, position); } } } public void Draw(SpriteBatch spriteBatch) { for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { tiles[i, j].Draw(spriteBatch); } } } public List CollisionTargets() { var result = new List(); for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { var t = tiles[i, j]; if (t.Terrain != Terrain.Empty) { result.Add(t.Position); } } } return result; } } }