using System; using System.Collections.Generic; using System.Text; using System.Xml; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework; namespace OperationDrawDown { public class SceneGraph { Cell[,] cells; public static List tiles; Texture2D mapImage; Color[] map; int mapWidth; int mapHeight; public SceneGraph() { } public void Load(XmlNode node) { mapImage = Engine.content.Load(node.Attributes["locationMap"].Value); mapWidth = mapImage.Width; mapHeight = mapImage.Height; map = new Color[mapWidth * mapHeight]; mapImage.GetData(map); cells = new Cell[mapWidth, mapHeight]; // Unpack Xml Linearly tiles = new List(); for (int k = 0; k < node.ChildNodes.Count; k++) { // Create cell : // String ID and IconTexture; // RGB value corresponding PixelMap // Is tile walkable.. tiles.Add( new Cell( node.ChildNodes[k].Attributes["id"].Value, Engine.content.Load(node.ChildNodes[k].Attributes["texture"].Value), new Color( byte.Parse(node.ChildNodes[k].Attributes["r"].Value), byte.Parse(node.ChildNodes[k].Attributes["g"].Value), byte.Parse(node.ChildNodes[k].Attributes["b"].Value) ), bool.Parse(node.ChildNodes[k].Attributes["walkable"].Value) )); } // Load Tiles into Cells Map using ugly O(n^3) loop // Assumes a relatively short list of xml templated characters. for (int j = 0; j < mapHeight; j++) { for (int i = 0; i < mapWidth; i++) { for (int k = 0; k < tiles.Count; k++) { if (map[j * mapHeight + i] == tiles[k].color) { // Clone Tile w/ new position cells[i, j] = tiles[k].Clone(i, j); } } } } } public void SetCell_ByScreenCoord(int screenX, int screenY, Cell cell) { int blockX = screenX / Cell.scale; int blockY = screenY / Cell.scale; if (blockX < this.mapWidth && blockY < this.mapHeight) { cell.x = blockX; cell.y = blockY; this.cells[blockX, blockY] = cell; } } public void SetCell_ByBlockCoord(int blockX, int blockY, Cell cell) { if (blockX < this.mapWidth && blockY < this.mapHeight) { cell.x = blockX; cell.y = blockY; this.cells[blockX, blockY] = cell; } } public Cell GetCell_ByScreenCoord(int screenX, int screenY) { int blockX = screenX / Cell.scale; int blockY = screenY / Cell.scale; if (blockX < mapWidth && blockY < mapHeight) { return this.cells[ blockX, blockX]; } return null; } public Cell GetCell_ByBlockCoord(int blockX, int blockY) { if (blockX < mapWidth && blockY < mapHeight) { return this.cells[blockX, blockY]; } return null; } public void MoveCells_ByScreenCoord(SelectionRectangle srcLocation, int dx, int dy) { dx /= Cell.scale; dy /= Cell.scale; for (int x = (srcLocation.StartX / Cell.scale); x < (srcLocation.EndX / Cell.scale); x++) for (int y = (srcLocation.StartY / Cell.scale); (y < srcLocation.EndY / Cell.scale); y++) { cells[x + dx, y + dy] = cells[x, y]; cells[x + dx, y + dy].x = x + dx; cells[x + dx, y + dy].y = y + dy; cells[x, y] = new Cell(x, y); } } public void CopyCells_ByScreenCoord(SelectionRectangle srcLocation, int dx, int dy) { dx /= Cell.scale; dy /= Cell.scale; for (int x = (srcLocation.StartX / Cell.scale); x < (srcLocation.EndX / Cell.scale); x++) for (int y = (srcLocation.StartY / Cell.scale); (y < srcLocation.EndY / Cell.scale); y++) { cells[x + dx, y + dy] = cells[x, y].Clone(x + dx, y + dy); } } public void Update(float dt) { } public void Draw() { Engine.spriteBatch.Draw( mapImage, new Rectangle(0, 0, mapWidth * Cell.scale, mapHeight * Cell.scale), Color.White ); // Draw Glyphs for (int i = 0; i < mapWidth; i++) { for (int j = 0; j < mapHeight; j++) { if (cells[i, j].id != "Blank" && cells[i, j] != null) { cells[i, j].Draw(); } } } } } }