using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Reflection;
namespace CarFire
{
///
/// A map object represents the map or virtual world where players and other
/// game entities exist. The map consists of a grid where each grid space can
/// contain static scenery and/or game entities which can move and interact
/// with other game entities.
///
public class Map
{
// DEBUG: Tilesets not implemented at all.
public static Texture2D DefaultTile;
#region Public Exceptions
///
/// This exception is thrown during the loading of a map if any
/// part of the map file is inconsistent with the expected format
/// and order.
///
public class RuntimeException : System.ApplicationException
{
public RuntimeException() { }
public RuntimeException(string message) :
base(message) { }
public RuntimeException(string message, System.Exception inner) :
base(message, inner) { }
protected RuntimeException(SerializationInfo info, StreamingContext context) :
base(info, context) { }
}
#endregion
#region Public Constants
public const float PixelsToUnitSquares = 64.0f;
#endregion
#region Public Types
///
/// The type of a map helps determine how the map is intended to be used.
///
public enum Mode
{
None,
Campaign,
Battle
}
///
/// The container class for map metadata.
///
public class Metadata
{
public string Name;
public Mode Type;
public string Author;
public HashSet NumPlayers = new HashSet();
public string Tileset;
public int GridWidth;
public int GridHeight;
}
///
/// The container class for information about an entity defined in the map.
///
public class RawEntity
{
public char Id;
public Point Position;
public Dictionary Attributes = new Dictionary();
}
#endregion
#region Public Properties
///
/// Get the name of the map.
///
public string Name { get { return mData.Metadata.Name; } }
///
/// Get the type of the map.
///
public Mode Type { get { return mData.Metadata.Type; } }
///
/// Get the author of the map.
///
public string Author { get { return mData.Metadata.Author; } }
///
/// Get a set of integers containing each allowable number of players.
///
public HashSet NumPlayers { get { return mData.Metadata.NumPlayers; } }
///
/// Get the width of the map, in grid units.
///
public int Width { get { return mData.Metadata.GridWidth; } }
///
/// Get the height of the map, in grid units.
///
public int Height { get { return mData.Metadata.GridHeight; } }
// TODO: This should return whatever object we end up using for tilesets.
public string Tileset { get { return mData.Metadata.Tileset; } }
///
/// Get the current grid of open cells. On the grid, true means
/// the cell is open (i.e. an entity can occupy that cell), and false
/// means the cell is closed. Note that, just like Map.IsCellOpen,
/// only static scenery is considered; the grid cannot tell you
/// whether or not an entity is already occupying the cell.
///
public bool[,] Grid { get { return mData.Grid; } }
///
/// Get a list of the raw entity containers loaded with the map. If you
/// want to get the actual entity objects, use Map.GetEntities and
/// Map.GetAllEntities instead.
///
public List RawEntities { get { return mData.Entities; } }
///
/// Get and set the coordinate of the grid cell that should be in
/// the center of the screen when the map is drawn. Setting this
/// will change the viewport of the map and will effect the return
/// values of Map.GetPointFromCoordinates and Map.GetRectangleFromCoordinates.
///
public Vector2 CenterCell
{
get { return mView.CenterCell; }
set { mView.CenterCell = value; }
}
///
/// Get and set the zoom of the map view. The default zoom is
/// Map.PixelsToUnitSquares.
///
public float Zoom
{
get { return mView.Zoom; }
set { mView.Zoom = value; }
}
///
/// Get and set the associated game object.
///
public Game Game
{
get { return mData.Game; }
set { mData.Game = value; }
}
#endregion
#region Public Methods
///
/// Construct a map with the provided map data.
///
/// The metadata.
/// The grid.
/// The entities.
public Map(Metadata metadata, char[,] grid, char defaultTile,
List entities, Point[] playerPositions)
{
mData = new Model(metadata, grid, defaultTile, entities, playerPositions);
mView = new View(mData);
}
///
/// Draw a representation of the map to the screen.
///
/// The jeewiz.
public void Draw(SpriteBatch spriteBatch)
{
mView.Draw(spriteBatch);
}
///
/// Get a point in screen-space from a coordinate in gridspace.
///
/// X-coordinate.
/// Y-coordinate.
/// Transformed point.
public Point GetPointFromCoordinates(float x, float y)
{
return mView.GetPointFromCoordinates(x, y);
}
///
/// Get a point in screen-space from a coordinate in gridspace.
///
/// X,Y-coordinates.
/// Transformed point.
public Point GetPointFromCoordinates(Vector2 point)
{
return mView.GetPointFromCoordinates(point.X, point.Y);
}
///
/// Get a rectangle in screen-space centered around a coordinate in gridspace.
///
/// X-coordinate.
/// Y-coordinate.
/// Transformed rectangle.
public Rectangle GetRectangleFromCoordinates(float x, float y)
{
return mView.GetRectangleFromCoordinates(x, y);
}
///
/// Get a rectangle in screen-space centered around a coordinate in gridspace.
///
/// X,Y-coordinates.
/// Transformed rectangle.
public Rectangle GetRectangleFromCoordinates(Vector2 point)
{
return mView.GetRectangleFromCoordinates(point.X, point.Y);
}
///
/// Determine whether or not a cell can be occupied by a game entity.
///
/// X-coordinate.
/// Y-coordinate.
/// True if cell can be occupied, false otherwise.
public bool IsCellOpen(int x, int y)
{
return mData.IsCellOpen(x, y);
}
///
/// Determine whether or not a cell can be occupied by a game entity.
///
/// X,Y-coordinates.
/// True if cell can be occupied, false otherwise.
public bool IsCellOpen(Point point)
{
return mData.IsCellOpen(point.X, point.Y);
}
///
/// Get the starting position of a player.
///
/// The number of the player (i.e. 1-4).
/// This number must be a valid player number.
/// The starting position of the player.
public Point GetStartingPositionForPlayer(int playerNumber)
{
Debug.Assert(1 <= playerNumber && playerNumber <= NumPlayers.Max());
return mData.PlayerPositions[playerNumber];
}
///
/// Get all the entities loaded from the map file. Exceptions could be
/// thrown if there are entities without associated classes.
///
/// List of entity objects loaded.
public List GetAllEntities()
{
return mData.GetAllEntities();
}
///
/// Get the entities of a certain type loaded from the map file. Exceptions
/// could be thrown if there are entities without associated classes.
///
/// Type of the entity you want a list of.
/// List of entity objects loaded.
public List GetEntities()
{
return mData.GetEntities();
}
///
/// Set the tile of a cell.
///
/// X-coordinate.
/// Y-coordinate.
/// The character representing the tile.
public void SetCell(int x, int y, char tile)
{
mData.SetCell(x, y, tile);
}
///
/// Set the tile of a cell.
///
/// X,Y-coordinates.
/// The character representing the tile.
public void SetCell(Point point, char tile)
{
mData.SetCell(point.X, point.Y, tile);
}
///
/// Clear a cell to the default tile.
///
/// X-coordinate.
/// Y-coordinate.
public void ClearCell(int x, int y)
{
mData.ClearCell(x, y);
}
///
/// Clear a cell to the default tile.
///
/// X,Y-coordinates.
public void ClearCell(Point point)
{
mData.ClearCell(point.X, point.Y);
}
///
/// Reset the map to the state it was at right after loading.
///
public void Reset()
{
mData.Reset();
mView.Reset();
}
#endregion
#region Private Types
class Model
{
public Metadata Metadata { get { return mMetadata; } }
public List Entities { get { return mEntities; } }
public Point[] PlayerPositions { get { return mPlayerPositions; } }
public bool[,] Grid { get { return mBooleanGrid; } }
public Game Game;
public Model(Metadata metadata, char[,] grid, char defaultTile,
List entities, Point[] playerPositions)
{
Debug.Assert(metadata != null);
Debug.Assert(grid != null);
Debug.Assert(entities != null);
Debug.Assert(metadata.GridWidth * metadata.GridHeight == grid.Length);
mMetadata = metadata;
mCleanGrid = grid;
mDefaultTile = defaultTile;
mEntities = entities;
mPlayerPositions = playerPositions;
Reset();
#if DEBUG
Console.WriteLine("Loaded map {0} of type {1} written by {2}.",
metadata.Name,
metadata.Type,
metadata.Author);
#endif
}
public void Reset()
{
mGrid = (char[,])mCleanGrid.Clone();
mBooleanGrid = new bool[mMetadata.GridWidth, mMetadata.GridHeight];
for (int x = 0; x < mMetadata.GridWidth; x++)
{
for (int y = 0; y < mMetadata.GridHeight; y++)
{
mBooleanGrid[x, y] = IsCellOpen(x, y);
}
}
}
public bool IsCellOpen(int x, int y)
{
// TODO: Still need to define characters for types of scenery.
if (IsOnMap(x, y)) return mGrid[x, y] == ' ';
return false;
}
public void SetCell(int x, int y, char tile)
{
if (IsOnMap(x, y))
{
mGrid[x, y] = tile;
mBooleanGrid[x, y] = IsCellOpen(x, y);
}
}
public void ClearCell(int x, int y)
{
SetCell(x, y, mDefaultTile);
}
public bool IsOnMap(int x, int y)
{
return 0 <= x && x < Metadata.GridWidth && 0 <= y && y < Metadata.GridHeight;
}
public List GetAllEntities()
{
List list = new List();
foreach (RawEntity raw in mEntities)
{
if (raw.Attributes.ContainsKey("type"))
{
string typename = raw.Attributes["type"];
object[] args = new object[4];
args[0] = raw.Id;
args[1] = raw.Position;
args[2] = raw.Attributes;
args[3] = Game;
try
{
IEntity entity = (IEntity)Activator.CreateInstance(System.Type.GetType("CarFire." + typename), args);
if (entity != null)
{
entity.LoadContent(Game.ContentManager);
list.Add(entity);
}
else throw new RuntimeException();
}
#pragma warning disable 0168
catch (System.Exception ex)
#pragma warning restore 0168
{
throw new RuntimeException("Entity of type " + typename + " not loaded because an entity class can't be found.");
}
}
else
{
Console.WriteLine("Ignoring entity with identifier " + raw.Id + " since it has no type key.");
}
}
return list;
}
public List GetEntities()
{
System.Type type = typeof(T);
List list = new List();
string typename = typeof(T).Name;
foreach (RawEntity raw in mEntities)
{
if (raw.Attributes.ContainsKey("type") && typename == raw.Attributes["type"])
{
object[] args = new object[4];
args[0] = raw.Id;
args[1] = raw.Position;
args[2] = raw.Attributes;
args[3] = Game;
T entity = (T)Activator.CreateInstance(type, args);
if (entity != null)
{
((IEntity)entity).LoadContent(Game.ContentManager);
list.Add(entity);
}
else throw new RuntimeException("Entity of type " + typename + " not loaded because an entity class can't be found.");
}
}
return list;
}
Metadata mMetadata;
char[,] mGrid;
char[,] mCleanGrid;
bool[,] mBooleanGrid;
char mDefaultTile;
List mEntities;
Point[] mPlayerPositions;
}
class View
{
public Vector2 CenterCell;
public float Zoom;
public View(Model data)
{
Debug.Assert(data != null);
mData = data;
Reset();
}
public void Reset()
{
CenterCell = Vector2.Zero;
Zoom = PixelsToUnitSquares;
}
public void Draw(SpriteBatch spriteBatch)
{
mViewport = spriteBatch.GraphicsDevice.Viewport;
// TODO: There is no culling yet, but it runs so fast that it probably won't ever need it.
for (int y = 0; y < mData.Metadata.GridHeight; y++)
{
for (int x = 0; x < mData.Metadata.GridWidth; x++)
{
if (mData.IsCellOpen(x, y))
{
spriteBatch.Draw(Map.DefaultTile, GetRectangleFromCoordinates(x, y), Color.White);
}
else
{
spriteBatch.Draw(Map.DefaultTile, GetRectangleFromCoordinates(x, y), Color.DarkBlue);
}
}
}
}
public Point GetPointFromCoordinates(float x, float y)
{
Matrix transform = GetTransformation(CenterCell);
Vector2 point = Vector2.Transform(new Vector2(x, y), transform);
return new Point((int)point.X, (int)point.Y);
}
public Rectangle GetRectangleFromCoordinates(float x, float y)
{
Matrix transform = GetTransformation(CenterCell);
Vector2 point = Vector2.Transform(new Vector2(x, y), transform);
return new Rectangle((int)Math.Round(point.X, 0), (int)Math.Round(point.Y, 0), (int)Math.Round(Zoom, 0), (int)Math.Round(Zoom, 0));
}
Matrix GetTransformation(Vector2 center)
{
float halfRatio = Zoom * 0.5f;
Matrix transform = Matrix.CreateTranslation(-center.X, -center.Y, 0.0f);
transform *= Matrix.CreateScale(Zoom);
transform *= Matrix.CreateTranslation(mViewport.Width * 0.5f - halfRatio,
mViewport.Height * 0.5f - halfRatio, 0.0f);
Vector2 topLeft = Vector2.Transform(new Vector2(0.0f, 0.0f), transform);
topLeft.X = Math.Max(mViewport.X, topLeft.X);
topLeft.Y = Math.Max(mViewport.Y, topLeft.Y);
transform *= Matrix.CreateTranslation(-topLeft.X, -topLeft.Y, 0.0f);
Vector2 bottomRight = Vector2.Transform(new Vector2((float)mData.Metadata.GridWidth,
(float)mData.Metadata.GridHeight), transform);
float right = mViewport.X + mViewport.Width;
float bottom = mViewport.Y + mViewport.Height;
bottomRight.X = Math.Min(right, bottomRight.X) - right;
bottomRight.Y = Math.Min(bottom, bottomRight.Y) - bottom;
transform *= Matrix.CreateTranslation(-bottomRight.X, -bottomRight.Y, 0.0f);
return transform;
}
Model mData;
Viewport mViewport;
}
#endregion
#region Private Variables
Model mData;
View mView;
#endregion
}
}