diff --git a/MLEM.Data/ContentExtensions.cs b/MLEM.Data/ContentExtensions.cs
index 6e44212..0e64a9e 100644
--- a/MLEM.Data/ContentExtensions.cs
+++ b/MLEM.Data/ContentExtensions.cs
@@ -5,14 +5,29 @@ using MLEM.Data.Json;
using Newtonsoft.Json;
namespace MLEM.Data {
+ ///
+ /// A set of extensions for dealing with
+ ///
public static class ContentExtensions {
private static readonly Dictionary Serializers = new Dictionary();
+ ///
+ /// Adds a to the given content manager, which allows to load JSON-based content.
+ /// Note that calls this method implicitly if no serializer exists.
+ ///
+ /// The content manager to add the json serializer to
+ /// The json serializer to add
public static void SetJsonSerializer(this ContentManager content, JsonSerializer serializer) {
Serializers[content] = serializer;
}
+ ///
+ /// Returns the given content manager's json serializer.
+ /// This method sets a new json serializer using if the given content manager does not yet have one.
+ ///
+ /// The content manager whose serializer to get
+ /// The content manager's serializer
public static JsonSerializer GetJsonSerializer(this ContentManager content) {
if (!Serializers.TryGetValue(content, out var serializer)) {
serializer = JsonConverters.AddAll(new JsonSerializer());
@@ -21,11 +36,24 @@ namespace MLEM.Data {
return serializer;
}
+ ///
+ /// Adds a to the given content manager's .
+ ///
+ /// The content manager to add the converter to
+ /// The converter to add
public static void AddJsonConverter(this ContentManager content, JsonConverter converter) {
var serializer = GetJsonSerializer(content);
serializer.Converters.Add(converter);
}
+ ///
+ /// Loads any kind of JSON data using the given content manager's .
+ ///
+ /// The content manager to load content with
+ /// The name of the file to load
+ /// The file extension of the file to load, or ".json" by default
+ /// The type of asset to load
+ /// The loaded asset
public static T LoadJson(this ContentManager content, string name, string extension = ".json") {
using (var stream = File.OpenText(Path.Combine(content.RootDirectory, name + extension))) {
using (var reader = new JsonTextReader(stream)) {
diff --git a/MLEM.Data/Json/Direction2Converter.cs b/MLEM.Data/Json/Direction2Converter.cs
index ecceff2..1fbc8b9 100644
--- a/MLEM.Data/Json/Direction2Converter.cs
+++ b/MLEM.Data/Json/Direction2Converter.cs
@@ -3,12 +3,15 @@ using MLEM.Misc;
using Newtonsoft.Json;
namespace MLEM.Data.Json {
+ ///
public class Direction2Converter : JsonConverter {
+ ///
public override void WriteJson(JsonWriter writer, Direction2 value, JsonSerializer serializer) {
writer.WriteValue(value.ToString());
}
+ ///
public override Direction2 ReadJson(JsonReader reader, Type objectType, Direction2 existingValue, bool hasExistingValue, JsonSerializer serializer) {
Enum.TryParse(reader.Value.ToString(), out var dir);
return dir;
diff --git a/MLEM.Data/Json/JsonConverters.cs b/MLEM.Data/Json/JsonConverters.cs
index 8553cb5..e4428f5 100644
--- a/MLEM.Data/Json/JsonConverters.cs
+++ b/MLEM.Data/Json/JsonConverters.cs
@@ -4,12 +4,23 @@ using System.Reflection;
using Newtonsoft.Json;
namespace MLEM.Data.Json {
+ ///
+ /// A helper class that stores all of the types that are part of MLEM.Data.
+ ///
public class JsonConverters {
+ ///
+ /// An array of all of the s that are part of MLEM.Data
+ ///
public static readonly JsonConverter[] Converters = typeof(JsonConverters).Assembly.GetExportedTypes()
.Where(t => t.IsSubclassOf(typeof(JsonConverter)))
.Select(t => t.GetConstructor(Type.EmptyTypes).Invoke(null)).Cast().ToArray();
+ ///
+ /// Adds all of the objects that are part of MLEM.Data to the given
+ ///
+ /// The serializer to add the converters to
+ /// The given serializer, for chaining
public static JsonSerializer AddAll(JsonSerializer serializer) {
foreach (var converter in Converters)
serializer.Converters.Add(converter);
diff --git a/MLEM.Data/Json/PointConverter.cs b/MLEM.Data/Json/PointConverter.cs
index 76525bf..d049793 100644
--- a/MLEM.Data/Json/PointConverter.cs
+++ b/MLEM.Data/Json/PointConverter.cs
@@ -4,12 +4,15 @@ using Microsoft.Xna.Framework;
using Newtonsoft.Json;
namespace MLEM.Data.Json {
+ ///
public class PointConverter : JsonConverter {
+ ///
public override void WriteJson(JsonWriter writer, Point value, JsonSerializer serializer) {
writer.WriteValue(value.X.ToString(CultureInfo.InvariantCulture) + " " + value.Y.ToString(CultureInfo.InvariantCulture));
}
+ ///
public override Point ReadJson(JsonReader reader, Type objectType, Point existingValue, bool hasExistingValue, JsonSerializer serializer) {
var value = reader.Value.ToString().Split(' ');
return new Point(int.Parse(value[0], CultureInfo.InvariantCulture), int.Parse(value[1], CultureInfo.InvariantCulture));
diff --git a/MLEM.Data/Json/RawJsonReader.cs b/MLEM.Data/Json/RawJsonReader.cs
index 2b94b9d..050185f 100644
--- a/MLEM.Data/Json/RawJsonReader.cs
+++ b/MLEM.Data/Json/RawJsonReader.cs
@@ -4,17 +4,21 @@ using MLEM.Content;
using Newtonsoft.Json;
namespace MLEM.Data.Json {
+ ///
public class RawJsonReader : RawContentReader {
+ ///
public override bool CanRead(Type t) {
return true;
}
+ ///
public override object Read(RawContentManager manager, string assetPath, Stream stream, Type t, object existing) {
using (var reader = new JsonTextReader(new StreamReader(stream)))
return manager.GetJsonSerializer().Deserialize(reader);
}
+ ///
public override string[] GetFileExtensions() {
return new[] {"json"};
}
diff --git a/MLEM.Data/Json/RectangleConverter.cs b/MLEM.Data/Json/RectangleConverter.cs
index df6eaa6..41a6b2d 100644
--- a/MLEM.Data/Json/RectangleConverter.cs
+++ b/MLEM.Data/Json/RectangleConverter.cs
@@ -4,14 +4,17 @@ using Microsoft.Xna.Framework;
using Newtonsoft.Json;
namespace MLEM.Data.Json {
+ ///
public class RectangleConverter : JsonConverter {
+ ///
public override void WriteJson(JsonWriter writer, Rectangle value, JsonSerializer serializer) {
writer.WriteValue(
value.X.ToString(CultureInfo.InvariantCulture) + " " + value.Y.ToString(CultureInfo.InvariantCulture) + " " +
value.Width.ToString(CultureInfo.InvariantCulture) + " " + value.Height.ToString(CultureInfo.InvariantCulture));
}
+ ///
public override Rectangle ReadJson(JsonReader reader, Type objectType, Rectangle existingValue, bool hasExistingValue, JsonSerializer serializer) {
var value = reader.Value.ToString().Split(' ');
return new Rectangle(
diff --git a/MLEM.Data/Json/RectangleFConverter.cs b/MLEM.Data/Json/RectangleFConverter.cs
index 7261e6a..dcf1116 100644
--- a/MLEM.Data/Json/RectangleFConverter.cs
+++ b/MLEM.Data/Json/RectangleFConverter.cs
@@ -4,14 +4,17 @@ using MLEM.Misc;
using Newtonsoft.Json;
namespace MLEM.Data.Json {
+ ///
public class RectangleFConverter : JsonConverter {
+ ///
public override void WriteJson(JsonWriter writer, RectangleF value, JsonSerializer serializer) {
writer.WriteValue(
value.X.ToString(CultureInfo.InvariantCulture) + " " + value.Y.ToString(CultureInfo.InvariantCulture) + " " +
value.Width.ToString(CultureInfo.InvariantCulture) + " " + value.Height.ToString(CultureInfo.InvariantCulture));
}
+ ///
public override RectangleF ReadJson(JsonReader reader, Type objectType, RectangleF existingValue, bool hasExistingValue, JsonSerializer serializer) {
var value = reader.Value.ToString().Split(' ');
return new RectangleF(
diff --git a/MLEM.Data/Json/Vector2Converter.cs b/MLEM.Data/Json/Vector2Converter.cs
index 2525daa..1f5f00d 100644
--- a/MLEM.Data/Json/Vector2Converter.cs
+++ b/MLEM.Data/Json/Vector2Converter.cs
@@ -4,12 +4,15 @@ using Microsoft.Xna.Framework;
using Newtonsoft.Json;
namespace MLEM.Data.Json {
+ ///
public class Vector2Converter : JsonConverter {
+ ///
public override void WriteJson(JsonWriter writer, Vector2 value, JsonSerializer serializer) {
writer.WriteValue(value.X.ToString(CultureInfo.InvariantCulture) + " " + value.Y.ToString(CultureInfo.InvariantCulture));
}
+ ///
public override Vector2 ReadJson(JsonReader reader, Type objectType, Vector2 existingValue, bool hasExistingValue, JsonSerializer serializer) {
var value = reader.Value.ToString().Split(' ');
return new Vector2(float.Parse(value[0], CultureInfo.InvariantCulture), float.Parse(value[1], CultureInfo.InvariantCulture));
diff --git a/MLEM.Data/NetBufferSerializer.cs b/MLEM.Data/NetBufferSerializer.cs
index 90a7d42..05f23cf 100644
--- a/MLEM.Data/NetBufferSerializer.cs
+++ b/MLEM.Data/NetBufferSerializer.cs
@@ -5,12 +5,20 @@ using Lidgren.Network;
using Newtonsoft.Json;
namespace MLEM.Data {
+ ///
+ /// A net buffer serializer allows easily writing generic objects into a Lidgren.Network .
+ /// It can be used both for serialization of outgoing packets, and deserialization of incoming packets.
+ /// Before serializing and deserializing an object, each of the object's fields has to have a handler. New handlers can be added using or .
+ ///
public class NetBufferSerializer {
private readonly Dictionary> writeFunctions = new Dictionary>();
private readonly Dictionary> readFunctions = new Dictionary>();
private readonly Dictionary fieldCache = new Dictionary();
+ ///
+ /// Create a new net buffer serializer with some default serialization and deserialization implementations for various types.
+ ///
public NetBufferSerializer() {
foreach (var method in typeof(NetBuffer).GetMethods(BindingFlags.Instance | BindingFlags.Public)) {
if (method.GetParameters().Length == 0 && method.Name.StartsWith("Read", StringComparison.Ordinal) && method.Name.Substring(4) == method.ReturnType.Name)
@@ -28,6 +36,14 @@ namespace MLEM.Data {
this.AddHandler((buffer, o) => buffer.Write(o), buffer => buffer.ReadDirection());
}
+ ///
+ /// Serializes the given object into the given net buffer.
+ /// Note that each field in the object has to have a handler ()
+ ///
+ /// The buffer to serialize into
+ /// The object to serialize
+ /// The binding flags to search for fields in the object by
+ /// If any of the object's fields has no writer
public void Serialize(NetBuffer buffer, object o, BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) {
foreach (var field in this.GetFields(o.GetType(), flags)) {
if (!this.writeFunctions.TryGetValue(field.FieldType, out var func))
@@ -36,6 +52,14 @@ namespace MLEM.Data {
}
}
+ ///
+ /// Deserializes the net buffer's content into the given object.
+ /// If this is used for packet serialization, a new instance of the required type has to be created before this method is called.
+ ///
+ /// The buffer to read the data from
+ /// The object to serialize into
+ /// The binding flags to search for fields in the object by
+ /// If any of the object's fields has no reader
public void Deserialize(NetBuffer buffer, object o, BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) {
foreach (var field in this.GetFields(o.GetType(), flags)) {
if (!this.readFunctions.TryGetValue(field.FieldType, out var func))
@@ -53,11 +77,23 @@ namespace MLEM.Data {
return fields;
}
+ ///
+ /// Adds a manually created deserialization and serialization handler to this net buffer serializer.
+ ///
+ /// The function to write the given object into the net buffer
+ /// The function to read the given object out of the net buffer
+ /// The type that will be serialized and deserialized
public void AddHandler(Action write, Func read) {
this.writeFunctions.Add(typeof(T), (buffer, o) => write(buffer, (T) o));
this.readFunctions.Add(typeof(T), buffer => read(buffer));
}
+ ///
+ /// Adds a JSON-based deserialization and serialization handler to this net buffer serializer.
+ /// Objects that are serialized in this way are converted to JSON, and the resulting JSON is compressed.
+ ///
+ /// The JSON serializer to use
+ /// The type that will be serialized and deserialized
public void AddHandler(JsonSerializer serializer) {
this.AddHandler((buffer, o) => buffer.WriteObject(o, serializer), buffer => buffer.ReadObject(serializer));
}
diff --git a/MLEM.Data/NetExtensions.cs b/MLEM.Data/NetExtensions.cs
index e7b0edc..7149b90 100644
--- a/MLEM.Data/NetExtensions.cs
+++ b/MLEM.Data/NetExtensions.cs
@@ -9,33 +9,73 @@ using Newtonsoft.Json;
using Newtonsoft.Json.Bson;
namespace MLEM.Data {
+ ///
+ /// A set of extensions for dealing with .
+ ///
public static class NetExtensions {
+ ///
+ /// Writes a to the given net buffer
+ ///
+ /// The buffer to write to
+ /// The vector to write
public static void Write(this NetBuffer buffer, Vector2 vector) {
buffer.Write(vector.X);
buffer.Write(vector.Y);
}
+ ///
+ /// Reads a from the given net buffer
+ ///
+ /// The buffer to read from
+ /// The read vector
public static Vector2 ReadVector2(this NetBuffer buffer) {
return new Vector2(buffer.ReadFloat(), buffer.ReadFloat());
}
+ ///
+ /// Writes a to the given net buffer
+ ///
+ /// The buffer to write to
+ /// The guid to write
public static void Write(this NetBuffer buffer, Guid guid) {
buffer.Write(guid.ToByteArray());
}
+ ///
+ /// Reads a from the given net buffer
+ ///
+ /// The buffer to read from
+ /// The read guid
public static Guid ReadGuid(this NetBuffer buffer) {
return new Guid(buffer.ReadBytes(16));
}
+ ///
+ /// Writes a to the given net buffer
+ ///
+ /// The buffer to write to
+ /// The direction to write
public static void Write(this NetBuffer buffer, Direction2 direction) {
buffer.Write((short) direction);
}
+ ///
+ /// Reads a from the given net buffer
+ ///
+ /// The buffer to read from
+ /// The read direction
public static Direction2 ReadDirection(this NetBuffer buffer) {
return (Direction2) buffer.ReadInt16();
}
+ ///
+ /// Writes a generic object to the given net buffer using a .
+ ///
+ /// The buffer to write to
+ /// The object to write
+ /// The JSON serializer to use
+ /// The type of object written
public static void WriteObject(this NetBuffer buffer, T obj, JsonSerializer serializer) {
if (EqualityComparer.Default.Equals(obj, default)) {
buffer.Write(0);
@@ -50,6 +90,13 @@ namespace MLEM.Data {
}
}
+ ///
+ /// Reads a generic object from the given buffer using a .
+ ///
+ /// The buffer to read from
+ /// The JSON serializer to use
+ /// The type of object read
+ /// The read object
public static T ReadObject(this NetBuffer buffer, JsonSerializer serializer) {
var length = buffer.ReadInt32();
if (length <= 0)
diff --git a/MLEM.Extended/Extensions/NumberExtensions.cs b/MLEM.Extended/Extensions/NumberExtensions.cs
index b13d160..94d4fb2 100644
--- a/MLEM.Extended/Extensions/NumberExtensions.cs
+++ b/MLEM.Extended/Extensions/NumberExtensions.cs
@@ -1,12 +1,25 @@
using MonoGame.Extended;
namespace MLEM.Extended.Extensions {
+ ///
+ /// A set of extension methods that convert MLEM types to MonoGame.Extended types and vice versa.
+ ///
public static class NumberExtensions {
+ ///
+ /// Converts a MLEM to a MonoGame.Extended .
+ ///
+ /// The rectangle to convert
+ /// The converted rectangle
public static RectangleF ToExtended(this Misc.RectangleF rect) {
return new RectangleF(rect.X, rect.Y, rect.Width, rect.Height);
}
+ ///
+ /// Converts a MonoGame.Extended to a MLEM .
+ ///
+ /// The rectangle to convert
+ /// The converted rectangle
public static Misc.RectangleF ToMlem(this RectangleF rect) {
return new Misc.RectangleF(rect.X, rect.Y, rect.Width, rect.Height);
}
diff --git a/MLEM.Extended/Extensions/RandomExtensions.cs b/MLEM.Extended/Extensions/RandomExtensions.cs
index 4a6939e..de0bcea 100644
--- a/MLEM.Extended/Extensions/RandomExtensions.cs
+++ b/MLEM.Extended/Extensions/RandomExtensions.cs
@@ -3,16 +3,38 @@ using Microsoft.Xna.Framework;
using MonoGame.Extended;
namespace MLEM.Extended.Extensions {
+ ///
+ /// A set of extension methods for dealing with
+ ///
public static class RandomExtensions {
+ ///
+ /// Returns a random number in the given range.
+ ///
+ /// The random to use for generation
+ /// The range in which numbers will be generated
+ /// A number in the given range
public static int Range(this Random random, Range range) {
return random.Next(range.Min, range.Max);
}
+ ///
+ /// Returns a random number in the given range.
+ ///
+ /// The random to use for generation
+ /// The range in which numbers will be generated
+ /// A number in the given range
public static float Range(this Random random, Range range) {
return random.NextSingle(range.Min, range.Max);
}
+ ///
+ /// Returns a random vector whose x and y values are in the given range.
+ ///
+ /// The random to use for generation
+ /// The minimum value for each coordinate
+ /// The maximum value for each coordinate
+ /// A random vector in the given range
public static Vector2 NextVector2(this Random random, float min, float max) {
return new Vector2(random.NextSingle(min, max), random.NextSingle(min, max));
}
diff --git a/MLEM.Extended/Extensions/SpriteBatchExtensions.cs b/MLEM.Extended/Extensions/SpriteBatchExtensions.cs
index f51a2a2..376ac30 100644
--- a/MLEM.Extended/Extensions/SpriteBatchExtensions.cs
+++ b/MLEM.Extended/Extensions/SpriteBatchExtensions.cs
@@ -6,16 +6,22 @@ using MonoGame.Extended;
using MonoGame.Extended.BitmapFonts;
namespace MLEM.Extended.Extensions {
+ ///
+ /// A set of extension methods for dealing with and in combination.
+ ///
public static class SpriteBatchExtensions {
+ ///
public static void Draw(this SpriteBatch batch, Texture2D texture, RectangleF destinationRectangle, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth) {
batch.Draw(texture, destinationRectangle.ToMlem(), sourceRectangle, color, rotation, origin, effects, layerDepth);
}
+ ///
public static void Draw(this SpriteBatch batch, Texture2D texture, RectangleF destinationRectangle, Rectangle? sourceRectangle, Color color) {
batch.Draw(texture, destinationRectangle, sourceRectangle, color, 0, Vector2.Zero, SpriteEffects.None, 0);
}
+ ///
public static void Draw(this SpriteBatch batch, Texture2D texture, RectangleF destinationRectangle, Color color) {
batch.Draw(texture, destinationRectangle, null, color);
}
diff --git a/MLEM.Extended/Extensions/TextureExtensions.cs b/MLEM.Extended/Extensions/TextureExtensions.cs
index 87f3132..765c8cb 100644
--- a/MLEM.Extended/Extensions/TextureExtensions.cs
+++ b/MLEM.Extended/Extensions/TextureExtensions.cs
@@ -3,20 +3,43 @@ using MLEM.Textures;
using MonoGame.Extended.TextureAtlases;
namespace MLEM.Extended.Extensions {
+ ///
+ /// A set of extensions for converting texture-related types between MLEM and MonoGame.Extended.
+ ///
public static class TextureExtensions {
+ ///
+ /// Converts a MLEM to a MonoGame.Extended .
+ ///
+ /// The nine patch to convert
+ /// The converted nine patch
public static NinePatchRegion2D ToExtended(this NinePatch patch) {
return new NinePatchRegion2D(patch.Region.ToExtended(), patch.Padding.Left.Floor(), patch.Padding.Top.Floor(), patch.Padding.Right.Floor(), patch.Padding.Bottom.Floor());
}
+ ///
+ /// Converts a MLEM to a MonoGame.Extended .
+ ///
+ /// The nine patch to convert
+ /// The converted nine patch
public static TextureRegion2D ToExtended(this TextureRegion region) {
return new TextureRegion2D(region.Texture, region.U, region.V, region.Width, region.Height);
}
+ ///
+ /// Converts a MonoGame.Extended to a MLEM .
+ ///
+ /// The nine patch to convert
+ /// The converted nine patch
public static NinePatch ToMlem(this NinePatchRegion2D patch) {
return new NinePatch(new TextureRegion(patch.Texture, patch.Bounds), patch.LeftPadding, patch.RightPadding, patch.TopPadding, patch.BottomPadding);
}
+ ///
+ /// Converts a MonoGame.Extended to a MLEM .
+ ///
+ /// The nine patch to convert
+ /// The converted nine patch
public static TextureRegion ToMlem(this TextureRegion2D region) {
return new TextureRegion(region.Texture, region.Bounds);
}
diff --git a/MLEM.Extended/Font/GenericBitmapFont.cs b/MLEM.Extended/Font/GenericBitmapFont.cs
index fb3adc9..7e35f43 100644
--- a/MLEM.Extended/Font/GenericBitmapFont.cs
+++ b/MLEM.Extended/Font/GenericBitmapFont.cs
@@ -7,51 +7,73 @@ using MLEM.Font;
using MonoGame.Extended.BitmapFonts;
namespace MLEM.Extended.Font {
+ ///
public class GenericBitmapFont : GenericFont {
+ ///
+ /// The that is being wrapped by this generic font
+ ///
public readonly BitmapFont Font;
+ ///
public override GenericFont Bold { get; }
+ ///
public override GenericFont Italic { get; }
+ ///
public override float LineHeight => this.Font.LineHeight;
+ ///
+ /// Creates a new generic font using .
+ /// Optionally, a bold and italic version of the font can be supplied.
+ ///
+ /// The font to wrap
+ /// A bold version of the font
+ /// An italic version of the font
public GenericBitmapFont(BitmapFont font, BitmapFont bold = null, BitmapFont italic = null) {
this.Font = font;
this.Bold = bold != null ? new GenericBitmapFont(bold) : this;
this.Italic = italic != null ? new GenericBitmapFont(italic) : this;
}
+ ///
public override Vector2 MeasureString(string text) {
if (text.Length == 1 && this.SingleCharacterWidthFix(text, out var size))
return size;
return this.Font.MeasureString(text);
}
+ ///
public override Vector2 MeasureString(StringBuilder text) {
if (text.Length == 1 && this.SingleCharacterWidthFix(text.ToString(), out var size))
return size;
return this.Font.MeasureString(text);
}
+ ///
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color) {
batch.DrawString(this.Font, text, position, color);
}
+ ///
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
}
+ ///
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
}
+ ///
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color) {
batch.DrawString(this.Font, text, position, color);
}
+ ///
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
}
+ ///
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
}
diff --git a/MLEM.Extended/Tiled/IndividualTiledMapRenderer.cs b/MLEM.Extended/Tiled/IndividualTiledMapRenderer.cs
index d844edc..0ba5e72 100644
--- a/MLEM.Extended/Tiled/IndividualTiledMapRenderer.cs
+++ b/MLEM.Extended/Tiled/IndividualTiledMapRenderer.cs
@@ -2,12 +2,17 @@ using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
+using MLEM.Cameras;
using MLEM.Extensions;
using MLEM.Misc;
using MonoGame.Extended.Tiled;
using RectangleF = MonoGame.Extended.RectangleF;
namespace MLEM.Extended.Tiled {
+ ///
+ /// A tiled map renderer that renders each tile individually, while optionally supplying a depth for each tile.
+ /// Rendering in this manner allows for entities to be behind or in front of buildings based on their y height.
+ ///
public class IndividualTiledMapRenderer {
private TiledMap map;
@@ -15,11 +20,21 @@ namespace MLEM.Extended.Tiled {
private GetDepth depthFunction;
private List animatedTiles;
+ ///
+ /// Creates a new individual tiled map renderer using the given map and depth function
+ ///
+ /// The map to use
+ /// The depth function to use
public IndividualTiledMapRenderer(TiledMap map = null, GetDepth depthFunction = null) {
if (map != null)
this.SetMap(map, depthFunction);
}
+ ///
+ /// Sets this individual tiled map renderer's map and depth function
+ ///
+ /// The map to use
+ /// The depth function to use
public void SetMap(TiledMap map, GetDepth depthFunction = null) {
this.map = map;
this.depthFunction = depthFunction ?? ((tile, layer, layerIndex, position) => 0);
@@ -43,6 +58,12 @@ namespace MLEM.Extended.Tiled {
}
}
+ ///
+ /// Updates the rendering information for the tile in the given layer, x and y.
+ ///
+ /// The index of the layer in
+ /// The x coordinate of the tile
+ /// The y coordinate of the tile
public void UpdateDrawInfo(int layerIndex, int x, int y) {
var layer = this.map.TileLayers[layerIndex];
var tile = layer.GetTile(x, y);
@@ -57,6 +78,13 @@ namespace MLEM.Extended.Tiled {
this.drawInfos[layerIndex, x, y] = new TileDrawInfo(this, tile, tileset, tilesetTile, pos, depth);
}
+ ///
+ /// Draws this individual tiled map renderer.
+ /// Optionally, a frustum can be supplied that determines which positions, in pixel space, are visible at this time. provides for this purpose.
+ ///
+ /// The sprite batch to use
+ /// The area that is visible, in pixel space.
+ /// The draw function to use, or null for default
public void Draw(SpriteBatch batch, RectangleF? frustum = null, DrawDelegate drawFunction = null) {
for (var i = 0; i < this.map.TileLayers.Count; i++) {
if (this.map.TileLayers[i].IsVisible)
@@ -64,6 +92,14 @@ namespace MLEM.Extended.Tiled {
}
}
+ ///
+ /// Draws the given layer of this individual tiled map renderer.
+ /// Optionally, a frustum can be supplied that determines which positions, in pixel space, are visible at this time. provides for this purpose.
+ ///
+ /// The sprite batch to use
+ /// The index of the layer in
+ /// The area that is visible, in pixel space.
+ /// The draw function to use, or null for default
public void DrawLayer(SpriteBatch batch, int layerIndex, RectangleF? frustum = null, DrawDelegate drawFunction = null) {
var frust = frustum ?? new RectangleF(0, 0, float.MaxValue, float.MaxValue);
var minX = Math.Max(0, frust.Left / this.map.TileWidth).Floor();
@@ -79,25 +115,65 @@ namespace MLEM.Extended.Tiled {
}
}
+ ///
+ /// Update all of the animated tiles in this individual tiled map renderer
+ ///
+ /// The game's time
public void UpdateAnimations(GameTime time) {
foreach (var animation in this.animatedTiles)
animation.Update(time);
}
+ ///
+ /// A delegate method used for .
+ /// The idea is to return a depth (between 0 and 1) for the given tile that determines where in the sprite batch it should be rendererd.
+ /// Note that, for this depth function to take effect, the sprite batch needs to begin with or .
+ ///
+ /// The tile whose depth to get
+ /// The layer the tile is on
+ /// The index of the layer in
+ /// The tile position of this tile
public delegate float GetDepth(TiledMapTile tile, TiledMapTileLayer layer, int layerIndex, Point position);
+ ///
+ /// A delegate method used for drawing an .
+ ///
+ /// The sprite batch to use for drawing
+ /// The to draw
public delegate void DrawDelegate(SpriteBatch batch, TileDrawInfo info);
+ ///
+ /// A tile draw info contains information about a tile at a given map location.
+ /// It caches a lot of data that is required for drawing a tile efficiently.
+ ///
public class TileDrawInfo : GenericDataHolder {
+ ///
+ /// The renderer used by this info
+ ///
public readonly IndividualTiledMapRenderer Renderer;
+ ///
+ /// The tiled map tile to draw
+ ///
public readonly TiledMapTile Tile;
+ ///
+ /// The tileset that is on
+ ///
public readonly TiledMapTileset Tileset;
+ ///
+ /// The tileset tile that corresponds to
+ ///
public readonly TiledMapTilesetTile TilesetTile;
+ ///
+ /// The position, in tile space, of
+ ///
public readonly Point Position;
+ ///
+ /// The depth calculated by the depth function
+ ///
public readonly float Depth;
- public TileDrawInfo(IndividualTiledMapRenderer renderer, TiledMapTile tile, TiledMapTileset tileset, TiledMapTilesetTile tilesetTile, Point position, float depth) {
+ internal TileDrawInfo(IndividualTiledMapRenderer renderer, TiledMapTile tile, TiledMapTileset tileset, TiledMapTilesetTile tilesetTile, Point position, float depth) {
this.Renderer = renderer;
this.Tile = tile;
this.Tileset = tileset;
@@ -106,6 +182,11 @@ namespace MLEM.Extended.Tiled {
this.Depth = depth;
}
+ ///
+ /// Draws this tile draw info with the default settings.
+ ///
+ /// The sprite batch to use for drawing
+ /// The draw function used to draw, or null if there is no override
public void Draw(SpriteBatch batch, DrawDelegate drawFunction) {
if (drawFunction == null) {
var region = this.Tileset.GetTextureRegion(this.TilesetTile);
diff --git a/MLEM.Extended/Tiled/LayerPosition.cs b/MLEM.Extended/Tiled/LayerPosition.cs
index 594e540..c25c1b7 100644
--- a/MLEM.Extended/Tiled/LayerPosition.cs
+++ b/MLEM.Extended/Tiled/LayerPosition.cs
@@ -1,38 +1,62 @@
using System.Runtime.Serialization;
+using MonoGame.Extended.Tiled;
namespace MLEM.Extended.Tiled {
+ ///
+ /// A struct that represents a position on a with multiple layers.
+ ///
[DataContract]
public struct LayerPosition {
+ ///
+ /// The name of the layer that this position is on
+ ///
[DataMember]
public string Layer;
+ ///
+ /// The x coordinate of this position
+ ///
[DataMember]
public int X;
+ ///
+ /// The y coordinate of this position
+ ///
[DataMember]
public int Y;
+ ///
+ /// Creates a new layer position with the given settings
+ ///
+ /// The layer name
+ /// The x coordinate
+ /// The y coordinate
public LayerPosition(string layerName, int x, int y) {
this.Layer = layerName;
this.X = x;
this.Y = y;
}
+ ///
public static bool operator ==(LayerPosition left, LayerPosition right) {
return left.Equals(right);
}
+ ///
public static bool operator !=(LayerPosition left, LayerPosition right) {
return !left.Equals(right);
}
+ ///
public bool Equals(LayerPosition other) {
return this.Layer == other.Layer && this.X == other.X && this.Y == other.Y;
}
+ ///
public override bool Equals(object obj) {
return obj is LayerPosition other && this.Equals(other);
}
+ ///
public override int GetHashCode() {
var hashCode = this.Layer.GetHashCode();
hashCode = (hashCode * 397) ^ this.X;
@@ -40,6 +64,7 @@ namespace MLEM.Extended.Tiled {
return hashCode;
}
+ ///
public override string ToString() {
return $"{nameof(this.Layer)}: {this.Layer}, {nameof(this.X)}: {this.X}, {nameof(this.Y)}: {this.Y}";
}
diff --git a/MLEM.Extended/Tiled/TiledExtensions.cs b/MLEM.Extended/Tiled/TiledExtensions.cs
index 867b115..66b2fc8 100644
--- a/MLEM.Extended/Tiled/TiledExtensions.cs
+++ b/MLEM.Extended/Tiled/TiledExtensions.cs
@@ -9,46 +9,109 @@ using MonoGame.Extended.Tiled;
using ColorExtensions = MLEM.Extensions.ColorExtensions;
namespace MLEM.Extended.Tiled {
+ ///
+ /// A set of extensions for dealing with MonoGame.Extended tiled maps
+ ///
public static class TiledExtensions {
private static readonly Dictionary StubTilesetTiles = new Dictionary();
+ ///
+ /// Gets the property with the given key, or null if there is none.
+ ///
+ /// The set of properties
+ /// The key by which to get a property
+ /// The property, or null if there is none
public static string Get(this TiledMapProperties properties, string key) {
properties.TryGetValue(key, out var val);
return val;
}
+ ///
+ /// Gets a boolean property with the given key, or null if there is none.
+ ///
+ /// The set of properties
+ /// The key by which to get a property
+ /// The boolean property, or false if there is none
public static bool GetBool(this TiledMapProperties properties, string key) {
bool.TryParse(properties.Get(key), out var val);
return val;
}
+ ///
+ /// Gets a Color property with the given key, or null if there is none.
+ ///
+ /// The set of properties
+ /// The key by which to get a property
+ /// The color property
public static Color GetColor(this TiledMapProperties properties, string key) {
return ColorExtensions.FromHex(properties.Get(key));
}
+ ///
+ /// Gets a float property with the given key, or null if there is none.
+ ///
+ /// The set of properties
+ /// The key by which to get a property
+ /// The float property, or 0 if there is none
public static float GetFloat(this TiledMapProperties properties, string key) {
float.TryParse(properties.Get(key), NumberStyles.Number, NumberFormatInfo.InvariantInfo, out var val);
return val;
}
+ ///
+ /// Gets an int property with the given key, or null if there is none.
+ ///
+ /// The set of properties
+ /// The key by which to get a property
+ /// The int property, or 0 if there is none
public static int GetInt(this TiledMapProperties properties, string key) {
int.TryParse(properties.Get(key), NumberStyles.Number, NumberFormatInfo.InvariantInfo, out var val);
return val;
}
+ ///
+ /// Gets the tileset for the given map tile on the given map.
+ ///
+ /// The tile
+ /// The map the tile is on
+ /// The tileset that the tile came from
public static TiledMapTileset GetTileset(this TiledMapTile tile, TiledMap map) {
return map.GetTilesetByTileGlobalIdentifier(tile.GlobalIdentifier);
}
+ ///
+ /// Gets the local tile identifier for the given tiled map tile.
+ /// The local tile identifier is the identifier within the tile's tileset.
+ ///
+ /// The tile whose identifier to get
+ /// The tileset the tile is from
+ /// The map the tile is on
+ /// The local identifier
public static int GetLocalIdentifier(this TiledMapTile tile, TiledMapTileset tileset, TiledMap map) {
return tile.GlobalIdentifier - map.GetTilesetFirstGlobalIdentifier(tileset);
}
+ ///
+ /// Gets the global tile identifier for the given tiled map tileset tile.
+ /// The global tile identifier is the identifier within all of the tile sets that the map has.
+ ///
+ /// The tile whose global identifier to get
+ /// The tileset the tile is from
+ /// The map the tile is on
+ /// The global identifier
public static int GetGlobalIdentifier(this TiledMapTilesetTile tile, TiledMapTileset tileset, TiledMap map) {
return map.GetTilesetFirstGlobalIdentifier(tileset) + tile.LocalTileIdentifier;
}
+ ///
+ /// Gets the tileset tile on the given tileset for the given tile.
+ ///
+ /// The tileset
+ /// The tile
+ /// The map the tile is on
+ /// If a tileset tile has no special properties, there is no pre-made object for it. If this boolean is true, a stub object with no extra data will be created instead of returning null.
+ /// null if the tile is blank or the tileset tile if there is one or createStub is true
public static TiledMapTilesetTile GetTilesetTile(this TiledMapTileset tileset, TiledMapTile tile, TiledMap map, bool createStub = true) {
if (tile.IsBlank)
return null;
@@ -64,6 +127,14 @@ namespace MLEM.Extended.Tiled {
return tilesetTile;
}
+ ///
+ /// Gets the tileset tile on the given tileset for the given tile.
+ /// If the tileset is already known, you should use instead for performance.
+ ///
+ /// The tile
+ /// The map the tile is on
+ /// If a tileset tile has no special properties, there is no pre-made object for it. If this boolean is true, a stub object with no extra data will be created instead of returning null.
+ /// null if the tile is blank or the tileset tile if there is one or createStub is true
public static TiledMapTilesetTile GetTilesetTile(this TiledMapTile tile, TiledMap map, bool createStub = true) {
if (tile.IsBlank)
return null;
@@ -71,22 +142,51 @@ namespace MLEM.Extended.Tiled {
return tileset.GetTilesetTile(tile, map, createStub);
}
+ ///
+ /// Gets the layer index of the layer with the given name in the array.
+ ///
+ /// The map
+ /// The name of the layer
+ /// The resulting index
public static int GetTileLayerIndex(this TiledMap map, string layerName) {
var layer = map.GetLayer(layerName);
return map.TileLayers.IndexOf(layer);
}
+ ///
+ /// Returns the tiled map tile at the given location on the layer with the given name.
+ ///
+ /// The map
+ /// The name of the layer the tile is on
+ /// The x coordinate of the tile
+ /// The y coordinate of the tile
+ /// The tile at the given location, or default if the layer does not exist
public static TiledMapTile GetTile(this TiledMap map, string layerName, int x, int y) {
var layer = map.GetLayer(layerName);
return layer != null ? layer.GetTile(x, y) : default;
}
+ ///
+ /// Sets the tiled map tile at the given location to the given global tile identifier.
+ ///
+ /// The map
+ /// The name of the layer
+ /// The x coordinate
+ /// The y coordinate
+ /// The tile's global identifier to set
public static void SetTile(this TiledMap map, string layerName, int x, int y, int globalTile) {
var layer = map.GetLayer(layerName);
if (layer != null)
layer.SetTile((ushort) x, (ushort) y, (uint) globalTile);
}
+ ///
+ /// For an x and y coordinate, returns an enumerable of all of the tiles on each of the map's .
+ ///
+ /// The map
+ /// The x coordinate
+ /// The y coordinate
+ /// All of the tiles on the map at the given location
public static IEnumerable GetTiles(this TiledMap map, int x, int y) {
foreach (var layer in map.TileLayers) {
var tile = layer.GetTile(x, y);
@@ -95,24 +195,59 @@ namespace MLEM.Extended.Tiled {
}
}
+ ///
+ /// Returns the tiled map at the given location on the given layer
+ ///
+ /// The layer to get the tile from
+ /// The tile's x coordinate
+ /// The tile's y coordinate
+ /// The tiled map tile at the location, or default if the location is out of bounds
public static TiledMapTile GetTile(this TiledMapTileLayer layer, int x, int y) {
return !layer.IsInBounds(x, y) ? default : layer.GetTile((ushort) x, (ushort) y);
}
+ ///
+ /// Returns the area that a tiled map object covers.
+ /// The area returned is in percent, meaning that an area that covers a full tile has a size of 1,1.
+ ///
+ /// The object whose area to get
+ /// The map
+ /// The position to add to the object's position
+ /// The area that the tile covers
public static RectangleF GetArea(this TiledMapObject obj, TiledMap map, Vector2? position = null) {
var tileSize = map.GetTileSize();
var pos = position ?? Vector2.Zero;
return new RectangleF(obj.Position / tileSize + pos, obj.Size / tileSize);
}
+ ///
+ /// Returns the width and height of a tile on the given map, as a vector.
+ ///
+ /// The map
+ /// The width and height of a tile
public static Vector2 GetTileSize(this TiledMap map) {
return new Vector2(map.TileWidth, map.TileHeight);
}
+ ///
+ /// Returns whether the given position is in the bounds of the layer (that is, if each coordinate is >= 0 and if they are both smaller than the layer's width and height).
+ ///
+ /// The layer
+ /// The x coordinate
+ /// The y coordinate
+ /// Whether the position is in bounds of the layer
public static bool IsInBounds(this TiledMapTileLayer layer, int x, int y) {
return x >= 0 && y >= 0 && x < layer.Width && y < layer.Height;
}
+ ///
+ /// Returns all of the objects by the given name, or by the given type, in an object layer.
+ ///
+ /// The layer whose objects to search
+ /// The name or type name of the objects to find
+ /// Whether to search object names
+ /// Whether to search object types
+ /// An enumerable of tiled map objects that match the search
public static IEnumerable GetObjects(this TiledMapObjectLayer layer, string id, bool searchName = true, bool searchType = false) {
foreach (var obj in layer.Objects) {
if (searchName && obj.Name == id || searchType && obj.Type == id)
@@ -120,13 +255,27 @@ namespace MLEM.Extended.Tiled {
}
}
- public static IEnumerable GetObjects(this TiledMap map, string name, bool searchName = true, bool searchType = false) {
+ ///
+ /// Returns all of the objects by the given name, or by the given type, on the given map.
+ ///
+ /// The layer whose objects to search
+ /// The name or type name of the objects to find
+ /// Whether to search object names
+ /// Whether to search object types
+ /// An enumerable of tiled map objects that match the search
+ public static IEnumerable GetObjects(this TiledMap map, string id, bool searchName = true, bool searchType = false) {
foreach (var layer in map.ObjectLayers) {
- foreach (var obj in layer.GetObjects(name, searchName, searchType))
+ foreach (var obj in layer.GetObjects(id, searchName, searchType))
yield return obj;
}
}
+ ///
+ /// Returns the texture region, as a rectangle, that the given tile uses for rendering.
+ ///
+ /// The tileset the tile is on
+ /// The tile
+ /// The tile's texture region, in pixels.
public static Rectangle GetTextureRegion(this TiledMapTileset tileset, TiledMapTilesetTile tile) {
var id = tile.LocalTileIdentifier;
if (tile is TiledMapTilesetAnimatedTile animated)
@@ -134,6 +283,11 @@ namespace MLEM.Extended.Tiled {
return tileset.GetTileRegion(id);
}
+ ///
+ /// Converts a tile's flip settings into .
+ ///
+ /// The tile whose flip settings to convert
+ /// The tile's flip settings as sprite effects
public static SpriteEffects GetSpriteEffects(this TiledMapTile tile) {
var flipping = SpriteEffects.None;
if (tile.IsFlippedHorizontally)
diff --git a/MLEM.Extended/Tiled/TiledMapCollisions.cs b/MLEM.Extended/Tiled/TiledMapCollisions.cs
index 55461ec..e59b393 100644
--- a/MLEM.Extended/Tiled/TiledMapCollisions.cs
+++ b/MLEM.Extended/Tiled/TiledMapCollisions.cs
@@ -9,17 +9,30 @@ using MonoGame.Extended.Tiled;
using RectangleF = MonoGame.Extended.RectangleF;
namespace MLEM.Extended.Tiled {
+ ///
+ /// A collision handler for a MonoGame.Extended tiled tile map.
+ /// The idea behind this collision handler is that, on the map's tileset, each tile is assigned a certain rectangular area. That area is converted into a collision map that is dealt with in tile units, where each tile's covered area is 1x1 units big.
+ ///
public class TiledMapCollisions {
private TiledMap map;
private TileCollisionInfo[,,] collisionInfos;
private CollectCollisions collisionFunction;
+ ///
+ /// Creates a new tiled map collision handler for the given map
+ ///
+ /// The map
public TiledMapCollisions(TiledMap map = null) {
if (map != null)
this.SetMap(map);
}
+ ///
+ /// Sets this collision handler's handled map
+ ///
+ /// The map
+ /// The function used to collect the collision info of a tile, or null for the default handling
public void SetMap(TiledMap map, CollectCollisions collisionFunction = null) {
this.map = map;
this.collisionFunction = collisionFunction ?? ((collisions, tile) => {
@@ -44,6 +57,12 @@ namespace MLEM.Extended.Tiled {
}
}
+ ///
+ /// Updates the collision info for the tile at the given position.
+ ///
+ /// The index of the tile's layer in
+ /// The tile's x coordinate
+ /// The tile's y coordinate
public void UpdateCollisionInfo(int layerIndex, int x, int y) {
var layer = this.map.TileLayers[layerIndex];
var tile = layer.GetTile(x, y);
@@ -55,6 +74,13 @@ namespace MLEM.Extended.Tiled {
this.collisionInfos[layerIndex, x, y] = new TileCollisionInfo(this.map, new Vector2(x, y), tile, layer, tilesetTile, this.collisionFunction);
}
+ ///
+ /// Returns an enumerable of tile collision infos that intersect the given area.
+ /// Optionally, a predicate can be supplied that narrows the search.
+ ///
+ /// The area to check for collisions in
+ /// A function that determines if a certain info should be included or not
+ /// An enumerable of collision infos for that area
public IEnumerable GetCollidingTiles(RectangleF area, Func included = null) {
var inclusionFunc = included ?? (tile => tile.Collisions.Any(c => c.Intersects(area)));
var minX = Math.Max(0, area.Left.Floor());
@@ -74,22 +100,55 @@ namespace MLEM.Extended.Tiled {
}
}
+ ///
+ /// Returns whether there are any colliding tiles in the given area.
+ /// Optionally, a predicate can be supplied that narrows the search.
+ ///
+ /// The area to check for collisions in
+ /// A function that determines if a certain info should be included or not
+ /// True if there are any colliders in the area, false otherwise
public bool IsColliding(RectangleF area, Func included = null) {
return this.GetCollidingTiles(area, included).Any();
}
+ ///
+ /// A delegate method used to override the default collision checking behavior.
+ ///
+ /// The list of collisions to add to
+ /// The tile's collision information
public delegate void CollectCollisions(List collisions, TileCollisionInfo tile);
+ ///
+ /// A tile collision info stores information about a tile at a given location on a given layer, including its objects and their bounds.
+ ///
public class TileCollisionInfo : GenericDataHolder {
+ ///
+ /// The map the tile is on
+ ///
public readonly TiledMap Map;
+ ///
+ /// The position of the tile, in tile units
+ ///
public readonly Vector2 Position;
+ ///
+ /// The tiled map tile
+ ///
public readonly TiledMapTile Tile;
+ ///
+ /// The layer that this tile is on
+ ///
public readonly TiledMapTileLayer Layer;
+ ///
+ /// The tileset tile for this tile
+ ///
public readonly TiledMapTilesetTile TilesetTile;
+ ///
+ /// The list of colliders for this tile
+ ///
public readonly List Collisions;
- public TileCollisionInfo(TiledMap map, Vector2 position, TiledMapTile tile, TiledMapTileLayer layer, TiledMapTilesetTile tilesetTile, CollectCollisions collisionFunction) {
+ internal TileCollisionInfo(TiledMap map, Vector2 position, TiledMapTile tile, TiledMapTileLayer layer, TiledMapTilesetTile tilesetTile, CollectCollisions collisionFunction) {
this.TilesetTile = tilesetTile;
this.Layer = layer;
this.Tile = tile;
diff --git a/MLEM.Startup/CoroutineEvents.cs b/MLEM.Startup/CoroutineEvents.cs
index f303483..64788e1 100644
--- a/MLEM.Startup/CoroutineEvents.cs
+++ b/MLEM.Startup/CoroutineEvents.cs
@@ -1,9 +1,18 @@
using Coroutine;
namespace MLEM.Startup {
+ ///
+ /// This class contains a set of events for the coroutine system that are automatically fired in .
+ ///
public static class CoroutineEvents {
+ ///
+ /// This event is fired in
+ ///
public static readonly Event Update = new Event();
+ ///
+ /// This event is fired in
+ ///
public static readonly Event Draw = new Event();
}
diff --git a/MLEM.Startup/MlemGame.cs b/MLEM.Startup/MlemGame.cs
index eaac895..a8d3116 100644
--- a/MLEM.Startup/MlemGame.cs
+++ b/MLEM.Startup/MlemGame.cs
@@ -1,5 +1,6 @@
using Coroutine;
using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MLEM.Input;
@@ -7,20 +8,54 @@ using MLEM.Ui;
using MLEM.Ui.Style;
namespace MLEM.Startup {
+ ///
+ /// MlemGame is an extension of MonoGame's default class.
+ /// It features the default template setup, as well as an , a and some additional callback events.
+ /// It also runs all of the callbacks which can be used through .
+ ///
public class MlemGame : Game {
private static MlemGame instance;
+ ///
+ /// The static game instance's input handler
+ ///
public static InputHandler Input => instance.InputHandler;
+ ///
+ /// This game's graphics device manager, initialized in the constructor
+ ///
public readonly GraphicsDeviceManager GraphicsDeviceManager;
+ ///
+ /// This game's sprite batch
+ ///
public SpriteBatch SpriteBatch { get; protected set; }
+ ///
+ /// This game's input handler. This can easily be accessed through .
+ ///
public InputHandler InputHandler { get; protected set; }
+ ///
+ /// This game's ui system
+ ///
public UiSystem UiSystem { get; protected set; }
+ ///
+ /// An event that is invoked in
+ ///
public GenericCallback OnLoadContent;
+ ///
+ /// An event that is invoked in
+ ///
public TimeCallback OnUpdate;
+ ///
+ /// An event that is invoked in
+ ///
public TimeCallback OnDraw;
+ ///
+ /// Creates a new MlemGame instance with some default settings
+ ///
+ /// The default window width
+ /// The default window height
public MlemGame(int windowWidth = 1280, int windowHeight = 720) {
instance = this;
@@ -33,6 +68,7 @@ namespace MLEM.Startup {
this.Content.RootDirectory = "Content";
}
+ ///
protected override void LoadContent() {
this.SpriteBatch = new SpriteBatch(this.GraphicsDevice);
this.InputHandler = new InputHandler();
@@ -42,6 +78,7 @@ namespace MLEM.Startup {
this.OnLoadContent?.Invoke(this);
}
+ ///
protected sealed override void Update(GameTime gameTime) {
this.DoUpdate(gameTime);
this.OnUpdate?.Invoke(this, gameTime);
@@ -49,6 +86,7 @@ namespace MLEM.Startup {
CoroutineHandler.RaiseEvent(CoroutineEvents.Update);
}
+ ///
protected sealed override void Draw(GameTime gameTime) {
this.UiSystem.DrawEarly(gameTime, this.SpriteBatch);
this.DoDraw(gameTime);
@@ -57,20 +95,46 @@ namespace MLEM.Startup {
CoroutineHandler.RaiseEvent(CoroutineEvents.Draw);
}
+ ///
+ /// This method is called in .
+ /// It is the version that should be overridden by implementors to draw game content.
+ ///
+ /// The game's time
protected virtual void DoDraw(GameTime gameTime) {
base.Draw(gameTime);
}
+ ///
+ /// This method is called in .
+ /// It is the version that should be overridden by implementors to update game content.
+ ///
+ /// The game's time
protected virtual void DoUpdate(GameTime gameTime) {
base.Update(gameTime);
}
+ ///
+ /// Static helper method for .
+ /// This just invokes the game instance's load method.
+ ///
+ /// The name of the content file to load
+ /// The type of content to load
+ /// The loaded content
public static T LoadContent(string name) {
return instance.Content.Load(name);
}
+ ///
+ /// A delegate method used by .
+ ///
+ /// The game in question
public delegate void GenericCallback(MlemGame game);
+ ///
+ /// A delegate method used by and .
+ ///
+ /// The game in question
+ /// The game's current time
public delegate void TimeCallback(MlemGame game, GameTime time);
}