custom exceptions, methods to create modules and states of specific type from generic ones

This commit is contained in:
2022-11-20 22:17:32 +01:00
parent 8ee43d6823
commit b0df3f303a
9 changed files with 92 additions and 54 deletions

View File

@@ -56,17 +56,18 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
/// Place this module with a position relative to the room.
/// </summary>
/// <param name="rrPosition">The room relative (<i>RR</i>) position of this module. Must be inside the space dimensions.</param>
/// <exception cref="Exception">If the position is not inside the space dimensions.</exception>
/// <exception cref="EngineException">If the position is not inside the space dimensions.</exception>
internal void PlaceRoomRelative(Vector2Int rrPosition) => Place(space.ToSpaceRelative(rrPosition));
/// <summary>
/// Place this module with a position relative to the space it is in.
/// </summary>
/// <param name="srPosition">The space relative (<i>SR</i>) position of this module. Must be inside the space dimensions.</param>
/// <exception cref="Exception">If the position is not inside the space dimensions.</exception>
/// <exception cref="EngineException">If the position is not inside the space dimensions.</exception>
internal void Place(Vector2Int srPosition) {
if (space != null && !srPosition.IsInsideRelative(space.rrDimensions))
{
throw new Exception($"Trying to place {this} at {srPosition}, which is outside space dimensions {space.rrDimensions}.");
throw new EngineException($"Trying to place {this} at {srPosition}, which is outside space dimensions {space.rrDimensions}.");
}
srDimensions.Position = srPosition;
@@ -103,6 +104,15 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
State.SetModule(this);
}
/// <summary>
/// Creates a module of the correct type given a module description.
///
/// <example>If the module description shows "Puzzle" as a type, this will try to create a puzzle module.</example>
/// </summary>
/// <param name="space">The space to create the module for.</param>
/// <param name="description">The module description to create the module from.</param>
/// <returns>A <see cref="PuzzleModule"/> or <see cref="DoorModule"/>.</returns>
/// <exception cref="WrongTypeException">If there is no module type fitting the description.</exception>
internal static Module CreateModuleByType(Space space, ModuleDescription description)
{
if (description.HasType(ModuleType.Puzzle) &&
@@ -110,15 +120,14 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
{
return new PuzzleModule(space, puzzleModuleDescription);
}
else if((description.HasType(ModuleType.DoorEntrance) || description.HasType(ModuleType.DoorExit)) &&
description is DoorModuleDescription doorModuleDescription)
if((description.HasType(ModuleType.DoorEntrance) || description.HasType(ModuleType.DoorExit)) &&
description is DoorModuleDescription doorModuleDescription)
{
return new DoorModule(space, doorModuleDescription);
}
else
{
throw new Exception("Module description does not have fitting type.");
}
throw new WrongTypeException("There is no module type fitting this description.");
}
public override string ToString()