using System.Diagnostics;\r
using Microsoft.Xna.Framework;\r
using Microsoft.Xna.Framework.Graphics;\r
+using System.Reflection;\r
\r
namespace CarFire\r
{\r
// DEBUG: Tilesets not implemented at all.\r
public static Texture2D DefaultTile;\r
\r
+ #region Public Exceptions\r
+\r
+ /// <summary>\r
+ /// This exception is thrown during the loading of a map if any\r
+ /// part of the map file is inconsistent with the expected format\r
+ /// and order.\r
+ /// </summary>\r
+ public class RuntimeException : System.ApplicationException\r
+ {\r
+ public RuntimeException() { }\r
+\r
+ public RuntimeException(string message) :\r
+ base(message) { }\r
+\r
+ public RuntimeException(string message, System.Exception inner) :\r
+ base(message, inner) { }\r
+\r
+ protected RuntimeException(SerializationInfo info, StreamingContext context) :\r
+ base(info, context) { }\r
+ }\r
+\r
+ #endregion\r
+\r
+\r
#region Public Constants\r
\r
- public const float PixelsToUnitSquares = 64.0f;\r
+ public const float PixelsToUnitSquares = 8.0f;\r
\r
#endregion\r
\r
public int GridHeight;\r
}\r
\r
+ /// <summary>\r
+ /// The container class for information about an entity defined in the map.\r
+ /// </summary>\r
+ public class RawEntity\r
+ {\r
+ public char Id;\r
+ public Point Position;\r
+ public Dictionary<string, string> Attributes = new Dictionary<string, string>();\r
+ }\r
+\r
#endregion\r
\r
\r
// TODO: This should return whatever object we end up using for tilesets.\r
public string Tileset { get { return mData.Metadata.Tileset; } }\r
\r
+ /// <summary>\r
+ /// Get a list of the raw entity containers loaded with the map.\r
+ /// </summary>\r
+ public List<RawEntity> RawEntities { get { return mData.Entities; } }\r
+\r
\r
+ /// <summary>\r
+ /// Get and set the coordinate of the grid cell that should be in\r
+ /// the center of the screen when the map is drawn.\r
+ /// </summary>\r
public Vector2 CenterCell\r
{\r
get { return mView.CenterCell; }\r
/// Construct a map with the provided map data.\r
/// </summary>\r
/// <param name="data">Map data.</param>\r
- public Map(Metadata metadata, char[,] grid, Dictionary<char, Dictionary<string, string>> entities)\r
+ public Map(Metadata metadata, char[,] grid, List<RawEntity> entities)\r
{\r
mData = new Modal(metadata, grid, entities);\r
mView = new View(mData);\r
/// <summary>\r
/// Determine whether or not a cell can be occupied by a game entity.\r
/// </summary>\r
- /// <param name="x">X,Y-coordinates.</param>\r
+ /// <param name="point">X,Y-coordinates.</param>\r
/// <returns>True if cell can be occupied, false otherwise.</returns>\r
public bool IsCellOpen(Point point)\r
{\r
\r
\r
/// <summary>\r
- /// Get the entities loaded from the map file.\r
+ /// Get all the entities loaded from the map file. Exceptions could be\r
+ /// thrown if there are entities without associated classes.\r
+ /// </summary>\r
+ /// <returns>List of entity objects loaded.</returns>\r
+ public List<object> GetAllEntities()\r
+ {\r
+ return mData.GetAllEntities();\r
+ }\r
+\r
+ /// <summary>\r
+ /// Get the entities of a certain type loaded from the map file. Exceptions\r
+ /// could be thrown if there are entities without associated classes.\r
/// </summary>\r
- /// <returns>Dictionary of entities. The keys are the entity\r
- /// identifiers and the value is a dictionary of key-value pairs\r
- /// associated with that entity.</returns>\r
- public Dictionary<char, Dictionary<string, string>> GetEntities()\r
+ /// <typeparam name="T">Type of the entity you want a list of.</typeparam>\r
+ /// <returns>List of entity objects loaded.</returns>\r
+ public List<T> GetEntities<T>()\r
{\r
- return mData.Entities;\r
+ return mData.GetEntities<T>();\r
}\r
\r
#endregion\r
{\r
Metadata mMetadata;\r
char[,] mGrid;\r
- Dictionary<char, Dictionary<string, string>> mEntities;\r
+ List<RawEntity> mEntities;\r
\r
- public Modal(Metadata metadata, char[,] grid, Dictionary<char, Dictionary<string, string>> entities)\r
+ public Modal(Metadata metadata, char[,] grid, List<RawEntity> entities)\r
{\r
Debug.Assert(metadata != null);\r
Debug.Assert(grid != null);\r
\r
\r
public Metadata Metadata { get { return mMetadata; } }\r
- public Dictionary<char, Dictionary<string, string>> Entities { get { return mEntities; } }\r
+ public List<RawEntity> Entities { get { return mEntities; } }\r
\r
\r
public bool IsCellOpen(int x, int y)\r
// TODO: Still need to define characters for types of scenery.\r
return mGrid[x, y] == ' ';\r
}\r
+\r
+\r
+ public List<object> GetAllEntities()\r
+ {\r
+ List<object> list = new List<object>();\r
+\r
+ foreach (RawEntity raw in mEntities)\r
+ {\r
+ if (raw.Attributes.ContainsKey("type"))\r
+ {\r
+ string typename = raw.Attributes["type"];\r
+\r
+ object[] args = new object[3];\r
+ args[0] = raw.Id;\r
+ args[1] = raw.Position;\r
+ args[2] = raw.Attributes;\r
+\r
+ try\r
+ {\r
+\r
+ object entity = Activator.CreateInstance(System.Type.GetType("CarFire." + typename), args);\r
+ if (entity != null) list.Add(entity);\r
+ else throw new RuntimeException();\r
+ }\r
+#pragma warning disable 0168\r
+ catch (System.Exception ex)\r
+#pragma warning restore 0168\r
+ {\r
+ throw new RuntimeException("Entity of type " + typename + " not loaded because an entity class can't be found.");\r
+ }\r
+ }\r
+ else\r
+ {\r
+ Console.WriteLine("Ignoring entity with identifier " + raw.Id + " since it has no type key.");\r
+ }\r
+ }\r
+\r
+ return list;\r
+ }\r
+\r
+ public List<T> GetEntities<T>()\r
+ {\r
+ System.Type type = typeof(T);\r
+ List<T> list = new List<T>();\r
+\r
+ string typename = typeof(T).Name;\r
+ foreach (RawEntity raw in mEntities)\r
+ {\r
+ if (raw.Attributes.ContainsKey("type") && typename == raw.Attributes["type"])\r
+ {\r
+ object[] args = new object[3];\r
+ args[0] = raw.Id;\r
+ args[1] = raw.Position;\r
+ args[2] = raw.Attributes;\r
+\r
+ T entity = (T)Activator.CreateInstance(type, args);\r
+ if (entity != null) list.Add(entity);\r
+ else throw new RuntimeException("Entity of type " + typename + " not loaded because an entity class can't be found.");\r
+ }\r
+ }\r
+\r
+ return list;\r
+ }\r
}\r
\r
class View\r