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 { private static SceneGraph _self; Texture2D[,] mapZones; Texture2D mapImage; Cell[,] cells; public static List tiles; int mapWidth; int mapHeight; // Overhead looking at =Z with X/Y motion. // Camera camera; public SceneGraph() { _self = this; } public void Load(XmlNode node) { mapImage = Engine.content.Load(node.Attributes["locationMap"].Value); mapWidth = mapImage.Width; mapHeight = mapImage.Height; Color[] map = new Color[mapWidth * mapHeight]; mapImage.GetData(map); cells = new Cell[mapWidth, mapHeight]; // Unpack Xml Tiles Linearly and store tiles = new List(); for (int k = 0; k < node.ChildNodes.Count; k++) { CreateNodeFromXml(node, k); } InitTileMap(map); } private static void CreateNodeFromXml(XmlNode node, int 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) )); } private void InitTileMap(Color[] map) { // 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); } } } } } private void UpdateTileMap(int dxPixels, int dyPixels) { // Up // if( dyPixels > camera.y) { // Load new cells // Move camera } // Down // if( dyPixels < camera.y) { // Load new cells // Move camera } // Right // if( dxPixels > camera.x) { // Load new cells // Move camera } // Left // if( dxPixels < camera.x) { // Load new cells // Move camera } } 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, blockY]; } 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 static bool IsWalkable_ByScreenCoord(int screenX, int screenY) { // Check if Cell is Walkable return SceneGraph._self.GetCell_ByScreenCoord(screenX, screenY).walkable; } 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(); } } } } } }