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

@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using EscapeRoomEngine.Engine.Runtime.Modules;
@@ -77,7 +76,7 @@ namespace EscapeRoomEngine.Engine.Runtime
// add exit
var exitDoor = new DoorModule(space, theme.exitDoorTypes.RandomElement());
if (!space.AddModuleWithRequirements(exitDoor))
throw new Exception("Could not satisfy requirements for exit door.");
throw new EngineException("Could not satisfy requirements for exit door.");
var exit = new Passage(exitDoor);
// add puzzles

View File

@@ -1,4 +1,5 @@
using System;
using EscapeRoomEngine.Engine.Runtime.Utilities;
using UnityEngine;
namespace EscapeRoomEngine.Engine.Runtime.Modules
@@ -14,18 +15,7 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
public bool IsEntrance => IsType((ModuleType)DoorType.Entrance);
public bool IsExit => IsType((ModuleType)DoorType.Exit);
internal DoorState DoorState
{
get
{
if (State is DoorState doorState)
{
return doorState;
}
throw new Exception("DoorModule must contain a DoorState");
}
}
internal DoorState DoorState => DoorState.FromState(State);
internal DoorModule(Space space, DoorModuleDescription description) : base(space, description)
{
@@ -43,5 +33,15 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
{
return $"{(IsEntrance ? "Entrance" : IsExit ? "Exit" : "Unknown")} door";
}
public static DoorModule FromModule(Module module)
{
if (module is DoorModule doorModule)
{
return doorModule;
}
throw new WrongTypeException(typeof(DoorModule), module.GetType(), typeof(Module));
}
}
}

View File

@@ -41,14 +41,7 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
public override void SetModule(Module module)
{
if (module is DoorModule doorModule)
{
Module = doorModule;
}
else
{
throw new Exception($"Tried to set wrong type of module ({module.GetType()} instead of DoorModule)");
}
Module = DoorModule.FromModule(module);
}
[Button(enabledMode: EButtonEnableMode.Playmode)]
@@ -62,5 +55,15 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
{
Unlocked = false;
}
public static DoorState FromState(ModuleState state)
{
if (state is DoorState doorState)
{
return doorState;
}
throw new WrongTypeException(typeof(DoorState), state.GetType(), typeof(ModuleState));
}
}
}

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()

View File

@@ -1,23 +1,12 @@
using System;
using EscapeRoomEngine.Engine.Runtime.Utilities;
using UnityEngine;
namespace EscapeRoomEngine.Engine.Runtime.Modules
{
public class PuzzleModule : Module
{
internal PuzzleState PuzzleState
{
get
{
if (State is PuzzleState puzzleState)
{
return puzzleState;
}
internal PuzzleState PuzzleState => PuzzleState.FromState(State);
throw new Exception("PuzzleModule must contain a PuzzleState");
}
}
internal PuzzleModule(Space space, PuzzleModuleDescription description) : base(space, description)
{
srDimensions.Size = Vector2Int.one; // TODO: larger modules
@@ -29,5 +18,15 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
space.room.AddPuzzle(this);
}
public static PuzzleModule FromModule(Module module)
{
if (module is PuzzleModule puzzleModule)
{
return puzzleModule;
}
throw new WrongTypeException(typeof(PuzzleModule), module.GetType(), typeof(Module));
}
}
}

View File

@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;
using EscapeRoomEngine.Engine.Runtime.Utilities;
using NaughtyAttributes;
using Logger = EscapeRoomEngine.Engine.Runtime.Utilities.Logger;
@@ -58,14 +59,7 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
public override void SetModule(Module module)
{
if (module is PuzzleModule puzzleModule)
{
Module = puzzleModule;
}
else
{
throw new Exception($"Tried to set wrong type of module ({module.GetType()} instead of PuzzleModule)");
}
Module = PuzzleModule.FromModule(module);
}
[Button(enabledMode: EButtonEnableMode.Playmode)]
@@ -88,5 +82,15 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
OnPuzzleEvent(PuzzleEventType.WrongInput);
}
}
public static PuzzleState FromState(ModuleState state)
{
if (state is PuzzleState puzzleState)
{
return puzzleState;
}
throw new WrongTypeException(typeof(PuzzleState), state.GetType(), typeof(ModuleState));
}
}
}

View File

@@ -0,0 +1,21 @@
using System;
namespace EscapeRoomEngine.Engine.Runtime.Utilities
{
public class EngineException : Exception
{
public EngineException(string message) : base($"<b>[EngineException]</b> {message}") {}
}
public class WrongTypeException : EngineException
{
public WrongTypeException(Type expected, Type found, Type baseType) : base($"Wrong type of {baseType} ({found} instead of {expected})") {}
public WrongTypeException(string message) : base(message) {}
}
public class OptionException : Exception
{
public OptionException(string message) : base(message) {}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e84dc071915b429ba633c9541d3411ac
timeCreated: 1668964775

View File

@@ -49,8 +49,8 @@ namespace EscapeRoomEngine.Engine.Runtime.Utilities
{
public bool IsSome() => false;
public bool IsNone() => true;
public T Expect(string message) => throw new Exception(message);
public T Unwrap() => throw new Exception("Tried to unwrap None.");
public T Expect(string message) => throw new OptionException(message);
public T Unwrap() => throw new OptionException("Tried to unwrap None.");
public T UnwrapOr(T def) => def;
public T UnwrapOrElse(Func<T> f) => f();
public IOption<T> And(IOption<T> other) => this;