#region Using Statements
using System;
using System.IO;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using DW.Stream;
using DW.Data.GameObjects;
using DW.Globals;
#endregion
namespace DW.Data.World
{
///
/// World map class represents walkable map in the MapWorld.
///
public partial class WorldMap
{
// Class represents exact number of tiles the display is showing
private class TrueDisplaySize
{
public int WidthInTiles;
public int WidthInPixels;
public int HeightInTiles;
public int HeightInPixels;
}
// The TileSet class holds information on an individual tileset that has
// been loaded.
private class TileSet
{
public Texture2D t2dTexture;
public string mapFileName;
public int verticalSpacing;
public int horizontalSpacing;
public int tileWidth;
public int tileHeight;
public int tilesPerRow;
}
// The t2dTileSets list holds our tilesets. Each tileset must be the
// same size, and all tiles are also the same size.
private List tileSets = new List();
// Positions of AI players when the map first loads
private List aiPositions = new List();
// Position of the human player when the map first loads
private TileCoordinates humanPosition;
// Width and Height of a single tile
private int tileWidth = 1, tileHeight = 1;
// Number of active tilesets
private int tileSetCount;
// Map Width and Height
private int entireMapWidthInTiles, entireMapHeightInTiles;
// The currently active game map data
private int[,] map, mapTrans, mapObj, mapData;
// Current Map Location
private int currentMapTileX, currentMapTileY, displayedMapTileSubX, displayedMapSubTileY;
// Will we draw the Base layer?
private bool drawBase = true;
// Will we draw the Trans layer?
private bool drawTrans = true;
// Will we draw the Obj layer?
private bool drawObj = true;
// Draw Top Row Tiles on Trans/Obj layers?
private bool drawFirstRowTiles = true;
// Used in Drawing loop to hold current tile
private int currentTileToDraw;
// Used in Drawing loop to track tile locations
private int tileXLoc, tileYLoc;
// Content Manager Instance
// private ContentManager contentManager;
// map definition file name
string fileName = "";
// Resoluton width and height. Muset be set every time the resolution changes.
private int resolutionWidth, resolutionHeight;
///
/// Constructor.
///
public WorldMap()
{
ResolutionWidth = Renderer.graphics.GraphicsDevice.Viewport.Width;
ResolutionHeight = Renderer.graphics.GraphicsDevice.Viewport.Height;
Initialize();
}
///
/// Allows the game component to perform any initialization it needs to before starting
/// to run. This is where it can query for any required services and load content.
///
public void Initialize()
{
// Set the tileset count to 0
tileSetCount = 0;
// Create a new content manager
//contentManager = new ContentManager(screenManager.Game.Services);
this.DisplayedMapX = 0;
this.DisplayedMapY = 0;
}
///
/// Loads the map from the definition file.
///
/// Map definition file name.
public void LoadMap(string sFileName)
{
fileName = sFileName;
// Load a map from the definition file.
MapLoader.LoadMap(sFileName, this);
}
///
/// Returns a list of AI player position when the map first starts.
///
public IList AIPositions
{
get { return aiPositions.AsReadOnly(); }
}
///
/// Returns a position of the human player when the map first starts.
///
public TileCoordinates HumanPosition
{
get { return humanPosition; }
}
///
/// Load a new tileset from a file and add it to the tileset list.
/// Each tileset must be the same size, and all tiles must be the same size.
///
/// Path to the definition file.
/// Width of the tiles.
/// Heigh of the tiles.
/// Horizontal spacing between tiles.
/// Vertical spacing between tiles.
public void AddTileset(string sFileName, int iTileWidth, int iTileHeight, int iHorizontalSpacing, int iVerticalSpacing)
{
TileSet NewTileSet = new TileSet();
NewTileSet.t2dTexture = Renderer.content.Load("_textures/spriteSheet");
NewTileSet.mapFileName = sFileName;
NewTileSet.tileWidth = iTileWidth;
NewTileSet.tileHeight = iTileHeight;
NewTileSet.horizontalSpacing = iHorizontalSpacing;
NewTileSet.verticalSpacing = iVerticalSpacing;
NewTileSet.tilesPerRow = NewTileSet.t2dTexture.Width /
(NewTileSet.tileWidth + NewTileSet.horizontalSpacing);
tileSets.Add(NewTileSet);
tileSetCount++;
}
///
/// Converts tile coordinates to screen pixel coordinates.
///
/// Tile X coordinate.
/// Tile Y coordinate.
public TileCoordinates TileToScreenPixels(int x, int y)
{
// Calculate coordinates based on what part of the map is showing
int newX = (x - DisplayedMapX) * TileWidth - DisplayedMapSubtileX + DisplayedMapLeft;
int newY = (y - DisplayedMapY) * TileHeight - DisplayedMapSubtileY + DisplayedMapTop;
return new TileCoordinates(newX, newY);
}
///
/// Converts tile coordinates to absolute screen pixel coordinates.
///
/// Tile X coordinate.
/// Tile Y coordinate.
public TileCoordinates TileToAbsolutePixels(int x, int y)
{
int newX = x * TileWidth;
int newY = y * TileHeight;
return new TileCoordinates(newX, newY);
}
private class BoundingCorners
{
TileCoordinates topLeft, topRight, bottomRight, bottomLeft;
BoundingCorners(TileCoordinates topLeft, TileCoordinates topRight, TileCoordinates bottomLeft, TileCoordinates bottomRight)
{
this.topLeft = topLeft;
this.topRight = topRight;
this.bottomLeft = bottomLeft;
this.bottomRight = bottomRight;
}
static BoundingCorners Calculate(Human player, WorldMap map)
{
TileCoordinates topLeft = map.AbsolutePixelToTile((int)(player.position.X), (int)(player.position.Y));
TileCoordinates topRight = map.AbsolutePixelToTile((int)(player.position.X) + (int)player.texture.Width, (int)(player.position.Y));
TileCoordinates bottomRight = map.AbsolutePixelToTile((int)(player.position.X) + (int)player.texture.Width, (int)(player.position.Y) + (int)player.texture.Height);
TileCoordinates bottomLeft = map.AbsolutePixelToTile((int)(player.position.X), (int)(player.position.Y) + (int)player.texture.Height);
return new BoundingCorners(topLeft, topRight, bottomLeft, bottomRight);
}
}
public bool IsOnOneTile(Human player)
{
TileCoordinates topLeft = AbsolutePixelToTile((int)(player.position.X), (int)(player.position.Y));
TileCoordinates topRight = AbsolutePixelToTile((int)(player.position.X) + (int)player.texture.Width, (int)(player.position.Y));
TileCoordinates bottomRight = AbsolutePixelToTile((int)(player.position.X) + (int)player.texture.Width, (int)(player.position.Y) + (int)player.texture.Height);
TileCoordinates bottomLeft = AbsolutePixelToTile((int)(player.position.X), (int)(player.position.Y) + (int)player.texture.Height);
return topLeft.Equals(topRight) &&
topRight.Equals(bottomRight) &&
bottomRight.Equals(bottomLeft);
}
public bool IsPartiallyOnTile(Human player, TileCoordinates tileCoordinates)
{
TileCoordinates topLeft = AbsolutePixelToTile((int)(player.position.X), (int)(player.position.Y));
TileCoordinates topRight = AbsolutePixelToTile((int)(player.position.X) + (int)player.texture.Width, (int)(player.position.Y));
TileCoordinates bottomRight = AbsolutePixelToTile((int)(player.position.X) + (int)player.texture.Width, (int)(player.position.Y) + (int)player.texture.Height);
TileCoordinates bottomLeft = AbsolutePixelToTile((int)(player.position.X), (int)(player.position.Y) + (int)player.texture.Height);
return tileCoordinates.Equals(topRight) ||
tileCoordinates.Equals(topLeft) ||
tileCoordinates.Equals(bottomRight) ||
tileCoordinates.Equals(bottomLeft);
}
///
/// Converts absolute pixel coordinates to tile coordinates.
///
/// Absolute pixel X coordinate.
/// Absolute pixel Y coordinate.
public TileCoordinates AbsolutePixelToTile(int x, int y)
{
int newX = x / TileWidth;
int newY = y / TileHeight;
return new TileCoordinates(newX, newY);
}
///
/// Converts absolute pixel coordinates to tile coordinates.
///
/// Absolute pixel X coordinate.
/// Absolute pixel Y coordinate.
public TileCoordinates AbsolutePixelToTile(Human player)
{
int newX = (int)player.position.X / TileWidth;
int newY = (int)player.position.Y / TileHeight;
return new TileCoordinates(newX, newY);
}
///
/// Converts absolute pixel coordinates to tile coordinates.
///
/// Absolute pixel X coordinate.
/// Absolute pixel Y coordinate.
public TileCoordinates AbsolutePixelToTileRound(int x, int y)
{
int newX = x / TileWidth;
if (x % TileWidth > TileWidth / 2) ++newX;
int newY = y / TileHeight;
if (y % TileHeight > TileHeight / 2) ++newY;
return new TileCoordinates(newX, newY);
}
///
/// Converts absolute pixel coordinates to tile coordinates.
///
/// Absolute pixel X coordinate.
/// Absolute pixel Y coordinate.
public TileCoordinates AbsolutePixelToCompleteTile(Human player)
{
TileCoordinates previousTile = AbsolutePixelToTileRound((int)player.lastPosition.X, (int)player.lastPosition.Y);
// Bounding box of the player on the map
TileCoordinates topLeft = AbsolutePixelToTile((int)(player.position.X), (int)(player.position.Y));
TileCoordinates topRight = AbsolutePixelToTile((int)(player.position.X) + (int)player.texture.Width, (int)(player.position.Y));
TileCoordinates bottomRight = AbsolutePixelToTile((int)(player.position.X) + (int)player.texture.Width, (int)(player.position.Y) + (int)player.texture.Height);
TileCoordinates bottomLeft = AbsolutePixelToTile((int)(player.position.X), (int)(player.position.Y) + (int)player.texture.Height);
if (topLeft.Equals(topRight) &&
topRight.Equals(bottomRight) &&
bottomRight.Equals(bottomLeft))
{
return topLeft;
}
else
{
return previousTile;
}
}
///
/// Converts absolute pixel coordinates to tile coordinates.
///
/// Absolute pixel X coordinate.
/// Absolute pixel Y coordinate.
public TileCoordinates AbsolutePixelToTile(float fx, float fy)
{
return AbsolutePixelToTile((int)fx, (int)fy);
}
///
/// Converts absolute pixel coordinates to screen pixel coordinates.
///
/// Absolute pixel X coordinate.
/// Absolute pixel Y coordinate.
public TileCoordinates AbsolutePixelToScreenPixel(float fx, float fy)
{
return AbsolutePixelToScreenPixel((int)fx, (int)fy);
}
///
/// Converts absolute pixel coordinates to screen pixel coordinates.
///
/// Absolute pixel X coordinate.
/// Absolute pixel Y coordinate.
public TileCoordinates AbsolutePixelToScreenPixel(int x, int y)
{
int newX = x - (DisplayedMapX * TileWidth + DisplayedMapSubtileX) + DisplayedMapLeft;
int newY = y - (DisplayedMapY * TileHeight + DisplayedMapSubtileY) + DisplayedMapTop;
return new TileCoordinates(newX, newY);
}
///
/// Sets the current map location, providing the X,Y coordintate of the tile
/// as well as the Sub-Tile pixel X and Y positions. If the coordinates make
/// the map run off, they will be clamped.
///
/// X coordinate of the tile.
/// Y coordinate of the tile.
/// X pixel coordinate within the tile specified by X, Y
/// Y pixel coordinate within the tile specified by X, Y
public void SetMapLocation(int x, int y, int subX, int subY)
{
// Clam coodinates we we don;t run outside of the map
ClampCoordinatesToFit(ref x, ref y, ref subX, ref subY);
// Set current map coordinates
currentMapTileX = x;
currentMapTileY = y;
displayedMapTileSubX = subX;
displayedMapSubTileY = subY;
}
///
/// Centers map around tile with coordinates X and Y
///
/// Tile X coordinate.
/// Tile Y coordinate.
public void CenterMap(int x, int y)
{
// Calculate where the map display shoudl dtart based on
// the desired center.
int subX = 0, subY = 0;
int offsetX = ((ResolutionWidth - TileWidth) / 2);
int tileOffsetX = offsetX / TileWidth + 2;
subX = TileWidth - offsetX % TileWidth;
int offsetY = (ResolutionHeight - TileHeight) / 2;
int tileOffsetY = offsetY / TileHeight + 2;
subY = TileHeight - offsetY % TileHeight;
// Set map location so it is centered
SetMapLocation(x - tileOffsetX, y - tileOffsetY, subX, subY);
}
///
/// Clamps coordinates so they do not go off the map.
///
/// Tile X coordinate.
/// Tile Y coordinate.
/// SubTile X coordinate.
/// SubTile Y coordinate.
private void ClampCoordinatesToFit(ref int x, ref int y, ref int subX, ref int subY)
{
TrueDisplaySize trueDisplaySize = GetTrueDisplaySize();
if (x < 0)
{
x = 0;
subX = 0;
}
else if ((x + trueDisplaySize.WidthInTiles) * TileWidth + subX > EntireMapWidth * TileWidth)
{
x = EntireMapWidth - trueDisplaySize.WidthInTiles - 1;
subX = 0;
}
if (y < 0)
{
y = 0;
subY = 0;
}
else if ((y + trueDisplaySize.HeightInTiles) * TileHeight + subY > EntireMapHeight * TileHeight)
{
y = EntireMapHeight - trueDisplaySize.HeightInTiles - 1;
subY = 0;
}
}
///
/// returns true display size.
///
/// TrueDisplaySize
object.
private TrueDisplaySize GetTrueDisplaySize()
{
TrueDisplaySize trueDisplaySize = new TrueDisplaySize();
trueDisplaySize.WidthInTiles = (ResolutionWidth - DisplayedMapLeft) / TileWidth;
trueDisplaySize.WidthInPixels = ResolutionWidth - DisplayedMapLeft;
trueDisplaySize.HeightInTiles = (ResolutionHeight - DisplayedMapTop) / TileHeight;
trueDisplaySize.HeightInPixels = ResolutionHeight - DisplayedMapTop;
return trueDisplaySize;
}
///
/// Move the map by X pixels horizontally and Y pixels vertically.
/// If the movements makes the map run off, the scroll will not take place.
///
///
///
public void ScrollByPixels(int x, int y)
{
if (x == 0 && y == 0)
{
return;
}
// See if the scroll will make us run off the map.
if (!CanScroll(0, 0, x, y))
{
return;
}
// Scroll
displayedMapTileSubX += x;
displayedMapSubTileY += y;
while (displayedMapTileSubX >= tileWidth)
{
displayedMapTileSubX -= tileWidth;
currentMapTileX++;
}
while (displayedMapSubTileY >= tileHeight)
{
displayedMapSubTileY -= tileHeight;
currentMapTileY++;
}
while (displayedMapTileSubX < 0)
{
displayedMapTileSubX += tileWidth;
currentMapTileX--;
}
while (displayedMapSubTileY < 0)
{
displayedMapSubTileY += tileHeight;
currentMapTileY--;
}
if (currentMapTileX >= entireMapWidthInTiles)
{
currentMapTileX = entireMapWidthInTiles;
}
if (currentMapTileX < 0)
{
currentMapTileX = 0;
}
if (currentMapTileY >= entireMapHeightInTiles)
{
currentMapTileY = entireMapHeightInTiles;
}
if (currentMapTileY < 0)
{
currentMapTileY = 0;
}
}
///
/// Determines if the map can be scrolled in a specified direction without running off
/// the map.
///
/// Number of tiles to scroll in X direction.
/// Number of tiles to scroll in Y direction.
/// Number of pixels to scroll in X direction.
/// Number of pixels to scroll in Y direction.
/// true
if the map can be scrolled; false
otherwise.
public bool CanScroll(int x, int y, int subX, int subY)
{
return IsXCoordinateWithinDisplay(currentMapTileX + x, displayedMapTileSubX + subX) &&
IsYCoordinateWithinDisplay(currentMapTileY + y, displayedMapSubTileY + subY);
}
///
/// Determines if the supllied X coordinates are within the map.
///
/// Tile X coordinate.
/// SubTile X coordinate.
/// true
if the coordinate is within display; false
otherwise.
private bool IsXCoordinateWithinDisplay(int x, int subX)
{
return x * TileWidth + subX >= 0 && x * TileWidth + ResolutionWidth + subX < EntireMapWidth * TileWidth;
}
///
/// Determines if the supllied Y coordinates are within the map.
///
/// Tile Y coordinate.
/// SubTile Y coordinate.
/// true
if the coordinate is within display; false
otherwise.
private bool IsYCoordinateWithinDisplay(int y, int subY)
{
return y * TileHeight + subY >= 0 && y * TileHeight + ResolutionHeight + subY < EntireMapHeight * TileHeight;
}
///
/// Determines if the player can move to a tile position on the map.
///
/// X coordinate of the tile.
/// Y coordinate of the tile.
/// true
if the tile is walkable; false
otherwise.
public bool IsWalkable(int x, int y)
{
if (x < 0 || x >= EntireMapWidth || y < 0 || y >= EntireMapHeight)
{
return false;
}
return GetMapWalkable(x, y) == Statics.MAP_WALKABLE_CHARACTER;
}
///
/// Determines if the player can move to a tile position on the map.
///
/// Tile coordinates of the player.
/// true
if the tile is walkable; false
otherwise.
public bool IsWalkable(TileCoordinates tc)
{
if (tc.X < 0 || tc.X >= EntireMapWidth || tc.Y < 0 || tc.Y >= EntireMapHeight)
{
return false;
}
return GetMapWalkable(tc.X, tc.Y) == Statics.MAP_WALKABLE_CHARACTER;
}
///
/// Edits current map data.
///
/// X coordinate of the tile.
/// Y coordinate of the tile.
/// Base/Terrain layer value.
/// Transition layer value.
/// Object layer value.
/// Data layer value.
public void EditMap(int x, int y, int iBase, int iTrans, int iObj, int iData)
{
map[y, x] = iBase;
mapTrans[y, x] = iTrans;
mapObj[y, x] = iObj;
mapData[y, x] = iData;
}
///
/// Returns data in the terrain layer at position x, y.
///
/// X coordinate of the tile.
/// Y coordinate of the tile.
/// Integer value in the terrain layer data.
public int GetMapTerrain(int x, int y)
{
return map[y, x];
}
///
/// Returns data in the transition layer at position x, y.
///
/// X coordinate of the tile.
/// Y coordinate of the tile.
/// Integer value in the transition layer data.
public int GetMapTransition(int x, int y)
{
return mapTrans[y, x];
}
///
/// Returns data in the object layer at position x, y.
///
/// X coordinate of the tile.
/// Y coordinate of the tile.
/// Integer value in the object layer data.
public int GetMapObjects(int x, int y)
{
return mapObj[y, x];
}
///
/// Returns data in the walkable layer at position x, y.
///
/// X coordinate of the tile.
/// Y coordinate of the tile.
/// Integer value in the walkable layer data.
public int GetMapWalkable(int x, int y)
{
return mapData[y, x];
}
///
/// Returns the walkable layer.
///
/// Integer 2D array representing the walkable layer data.
public int[,] GetMapWalkable()
{
return mapData;
}
///
/// Returns a rectangle representing the location on the tileset that
/// contains the requested tile.
///
/// Tile number.
/// Rectangle
of the tile.
private Rectangle GetTileRectangle(int iTileNumber)
{
// Returns a rectangle representing the location on the tileset that
// contains the requested tile.
// The 10000's digit determines the tileset number. The remainder
// determines the tile within the set, so seperate out the remainder
// to determine where to pull the rectangle from.
int iTile = iTileNumber % 10000;
int iTileSet = iTileNumber / 10000;
// Return a rectangle representing a location of a tile on the tileset
return new Rectangle(
(iTile % tileSets[iTileSet].tilesPerRow) * tileSets[iTileSet].tileWidth,
(iTile / tileSets[iTileSet].tilesPerRow) * tileSets[iTileSet].tileHeight,
tileSets[iTileSet].tileWidth,
tileSets[iTileSet].tileHeight
);
}
///
/// Determines if the coordinates specified are at the castle for the final map.
///
/// X coordinate on the map.
/// Y coordinate on the map.
/// true
if the coordinates are at a castle; false
otherise.
public bool IsAtCastle(int mapX, int mapY)
{
return mapX >= GetCastleStart();
}
///
/// Return X coordinate where the castle start on the final map.
///
/// X coordinate of the castle on the map.
private int GetCastleStart()
{
if (fileName.Contains("king"))
{
for (int i = 0; i < this.EntireMapHeight; ++i)
{
for (int j = 0; j < this.EntireMapWidth; ++j)
{
if (this.GetMapTerrain(j, i) == 8)
{
return j * this.TileWidth;
}
}
}
}
return (this.EntireMapWidth + 5) * this.TileWidth;
}
///
/// Draws the map.
///
/// Game time.
public void Draw(GameTime gameTime)
{
// Begin sprite batch
Renderer.spriteBatch.Begin(SpriteBlendMode.None);
// Draw the base layer of the map
if (drawBase)
{
for (int y = 0; y < DisplayedMapHeight; y++)
{
for (int x = 0; x < DisplayedMapWidth; x++)
{
tileXLoc = x + currentMapTileX;
tileYLoc = y + currentMapTileY;
// Account for map wrap-arounds
if (tileXLoc >= entireMapHeightInTiles)
{
tileXLoc -= entireMapWidthInTiles;
}
if (tileXLoc < 0)
{
tileXLoc += entireMapWidthInTiles;
}
if (tileYLoc >= entireMapHeightInTiles)
{
tileYLoc -= entireMapHeightInTiles;
}
if (tileYLoc < 0)
{
tileYLoc += entireMapHeightInTiles;
}
currentTileToDraw = map[tileYLoc, tileXLoc];
if (currentTileToDraw >= 0)
{
// Draw the tile. We divide the tile number by 10000 to
// determine what tileset the tile is on (0-9999=tileset 0,
// 10000-19999=Tileset 2, etc)
Renderer.spriteBatch.Draw(tileSets[(currentTileToDraw / 10000)].t2dTexture,
new Rectangle(((x * tileWidth) + DisplayedMapLeft) - displayedMapTileSubX,
((y * tileHeight) + DisplayedMapTop) - displayedMapSubTileY,
tileSets[(currentTileToDraw / 10000)].tileWidth,
tileSets[(currentTileToDraw / 10000)].tileHeight),
GetTileRectangle(currentTileToDraw),
Color.White);
}
}
}
}
Renderer.spriteBatch.End();
Renderer.spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
// Draw transitions and Objects layers of the map
for (int y = 0; y < DisplayedMapHeight; y++)
{
for (int x = 0; x < DisplayedMapWidth; x++)
{
Rectangle recDest = new Rectangle(((x * tileWidth) + DisplayedMapLeft) - displayedMapTileSubX,
((y * tileHeight) + DisplayedMapTop) - displayedMapSubTileY,
tileSets[currentTileToDraw / 10000].tileWidth,
tileSets[currentTileToDraw / 10000].tileHeight);
tileXLoc = x + currentMapTileX;
tileYLoc = y + currentMapTileY;
if (tileXLoc >= entireMapHeightInTiles)
{
tileXLoc -= entireMapWidthInTiles;
}
if (tileXLoc < 0)
{
tileXLoc += entireMapWidthInTiles;
}
if (tileYLoc >= entireMapHeightInTiles)
{
tileYLoc -= entireMapHeightInTiles;
}
if (tileYLoc < 0)
{
tileYLoc += entireMapHeightInTiles;
}
if (drawTrans)
{
currentTileToDraw = mapTrans[tileYLoc, tileXLoc];
if (currentTileToDraw >= 0 && ((currentTileToDraw >
tileSets[currentTileToDraw / 10000].tilesPerRow) || drawFirstRowTiles))
{
Renderer.spriteBatch.Draw(tileSets[currentTileToDraw / 10000].t2dTexture,
recDest, GetTileRectangle(currentTileToDraw),
Color.White);
}
}
}
}
Renderer.spriteBatch.End();
}
///
/// Class is resposible for loading data from map definition files.
///
private class MapLoader
{
public static void LoadMap(string mapPath, WorldMap map)
{
// Read data from the map definition file
using (StreamReader sr = new StreamReader(mapPath))
{
string line;
map.entireMapWidthInTiles = Convert.ToInt32(sr.ReadLine().Split('=')[1]);
map.entireMapHeightInTiles = Convert.ToInt32(sr.ReadLine().Split('=')[1]);
line = sr.ReadLine().Trim().Split('=')[1];
map.humanPosition = new TileCoordinates(Convert.ToInt32(line.Split(',')[0]), Convert.ToInt32(line.Split(',')[1]));
line = sr.ReadLine().Trim().Split('=')[1];
map.aiPositions.Add(new TileCoordinates(Convert.ToInt32(line.Split(',')[0]), Convert.ToInt32(line.Split(',')[1])));
line = sr.ReadLine().Trim().Split('=')[1];
map.aiPositions.Add(new TileCoordinates(Convert.ToInt32(line.Split(',')[0]), Convert.ToInt32(line.Split(',')[1])));
line = sr.ReadLine().Trim().Split('=')[1];
map.aiPositions.Add(new TileCoordinates(Convert.ToInt32(line.Split(',')[0]), Convert.ToInt32(line.Split(',')[1])));
map.map = new int[map.entireMapHeightInTiles, map.entireMapWidthInTiles];
map.mapTrans = new int[map.entireMapHeightInTiles, map.entireMapWidthInTiles];
map.mapObj = new int[map.entireMapHeightInTiles, map.entireMapWidthInTiles];
map.mapData = new int[map.entireMapHeightInTiles, map.entireMapWidthInTiles];
int arrayNumber = -1;
int rowNumber = 0;
while ((line = sr.ReadLine()) != null)
{
line = line.Trim();
if (line.Length == 0 || line.StartsWith("//"))
{
if (line.IndexOf("// Terrain layer") >= 0)
{
arrayNumber = 0;
}
else if (line.IndexOf("// Transition layer") >= 0)
{
arrayNumber = 1;
}
else if (line.IndexOf("// Object layer") >= 0)
{
arrayNumber = 2;
}
else if (line.IndexOf("// Is map walkable") >= 0)
{
arrayNumber = 3;
}
else
{
arrayNumber = -1;
}
if (rowNumber != 0 && rowNumber != map.entireMapHeightInTiles)
{
throw new Exception("Invalid definition file. Too many or too little rows in the file.");
}
rowNumber = 0;
continue;
}
if (arrayNumber == 0)
{
LoadArray(map.map, rowNumber, line, map);
++rowNumber;
}
else if (arrayNumber == 1)
{
LoadArray(map.mapTrans, rowNumber, line, map);
++rowNumber;
}
else if (arrayNumber == 2)
{
LoadArray(map.mapObj, rowNumber, line, map);
++rowNumber;
}
else if (arrayNumber == 3)
{
LoadArray(map.mapData, rowNumber, line, map);
++rowNumber;
}
}
sr.Close();
}
}
///
/// Loads the 2D array with values form a comma separated list of values in
/// a string.
///
/// 2D array to load.
/// Row number in which to store the values.
/// LIne of text containing comma separated values.
/// Screen object.
private static void LoadArray(int[, ] array, int rowNumber, string line, WorldMap screen)
{
// Split into comma separated values
string[] values = line.Split(new char[] { ',' });
// Check the appropriate length
if (values.Length != screen.entireMapWidthInTiles)
{
throw new Exception("Invalid definition file. Too many or too little values on the line)");
}
// Load the row
for (int j = 0; j < values.Length; ++j)
{
array[rowNumber, j] = int.Parse(values[j]);
}
}
}
#region Properties
///
/// Resolution width.
///
public int ResolutionWidth
{
get { return resolutionWidth; }
set
{
resolutionWidth = (int)(value * (1.0f - 2 * Statics.XBOX_CUT));
}
}
///
/// Resolution height.
///
public int ResolutionHeight
{
get { return resolutionHeight; }
set { resolutionHeight = (int)(value * (1.0f - 2 * Statics.XBOX_CUT)); }
}
///
/// Left pixel position of the tile engine drawing area on the display.
///
public int DisplayedMapLeft
{
get { return (int)(ResolutionWidth * Statics.XBOX_CUT); }
}
///
/// Top pixel position of the tile engine drawing area on the display.
///
public int DisplayedMapTop
{
get { return (int)(ResolutionHeight * Statics.XBOX_CUT); }
}
///
/// Width of the display in tiles.
///
public int DisplayedMapWidth
{
get { return ResolutionWidth / TileWidth + 2; }
}
///
/// Height of the display in tiles.
///
public int DisplayedMapHeight
{
get { return ResolutionHeight / TileHeight + 2; }
}
///
/// Width of an individual tile in pixels.
///
public int TileWidth
{
get { return this.tileWidth; }
set { this.tileWidth = value; }
}
///
/// Height of an individual tile in pixels.
///
public int TileHeight
{
// Determines the height of an individual tile in pixels.
get { return this.tileHeight; }
set { this.tileHeight = value; }
}
///
/// X value of the left-most displayed map tile.
///
public int DisplayedMapX
{
get { return this.currentMapTileX; }
set { this.currentMapTileX = value; }
}
///
/// Sub-tile X value of the left-most displayed map tile.
///
public int DisplayedMapSubtileX
{
get { return displayedMapTileSubX; }
}
///
/// Y value of the left-most displayed map tile.
///
public int DisplayedMapY
{
get { return this.currentMapTileY; }
set { this.currentMapTileY = value; }
}
///
/// Sub-tile Y value of the left-most displayed map tile.
///
public int DisplayedMapSubtileY
{
get { return displayedMapSubTileY; }
}
///
/// Width of the entire map in tiles.
///
public int EntireMapWidth
{
get { return this.entireMapWidthInTiles; }
}
///
/// Height of the entire map in tiles.
///
public int EntireMapHeight
{
get { return this.entireMapHeightInTiles; }
}
///
/// Determines if base layer is visible.
///
public bool BaseVisible
{
get { return this.drawBase; }
set { this.drawBase = value; }
}
///
/// Determines if transition layer is visible.
///
public bool TransVisible
{
get { return this.drawTrans; }
set { this.drawTrans = value; }
}
///
/// Determines if object layer is visible.
///
public bool ObjVisible
{
get { return this.drawObj; }
set { this.drawObj = value; }
}
///
/// Determines wether to draw first row of tiles on a transition layer.
///
public bool DrawFirstRowTilesOnTransLayers
{
get { return this.drawFirstRowTiles; }
set { this.drawFirstRowTiles = value; }
}
#endregion
}
}