diff --git a/Assets/Engine/Runtime/Engine.cs b/Assets/Engine/Runtime/Engine.cs
index 8ecd41e..22f5468 100644
--- a/Assets/Engine/Runtime/Engine.cs
+++ b/Assets/Engine/Runtime/Engine.cs
@@ -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
diff --git a/Assets/Engine/Runtime/Modules/DoorModule.cs b/Assets/Engine/Runtime/Modules/DoorModule.cs
index bd4b6bd..7b6cc8d 100644
--- a/Assets/Engine/Runtime/Modules/DoorModule.cs
+++ b/Assets/Engine/Runtime/Modules/DoorModule.cs
@@ -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));
+ }
}
}
\ No newline at end of file
diff --git a/Assets/Engine/Runtime/Modules/DoorState.cs b/Assets/Engine/Runtime/Modules/DoorState.cs
index 3719582..01cae05 100644
--- a/Assets/Engine/Runtime/Modules/DoorState.cs
+++ b/Assets/Engine/Runtime/Modules/DoorState.cs
@@ -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));
+ }
}
}
\ No newline at end of file
diff --git a/Assets/Engine/Runtime/Modules/Module.cs b/Assets/Engine/Runtime/Modules/Module.cs
index ca45966..2abcda3 100644
--- a/Assets/Engine/Runtime/Modules/Module.cs
+++ b/Assets/Engine/Runtime/Modules/Module.cs
@@ -56,17 +56,18 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
/// Place this module with a position relative to the room.
///
/// The room relative (RR) position of this module. Must be inside the space dimensions.
- /// If the position is not inside the space dimensions.
+ /// If the position is not inside the space dimensions.
internal void PlaceRoomRelative(Vector2Int rrPosition) => Place(space.ToSpaceRelative(rrPosition));
+
///
/// Place this module with a position relative to the space it is in.
///
/// The space relative (SR) position of this module. Must be inside the space dimensions.
- /// If the position is not inside the space dimensions.
+ /// If the position is not inside the space dimensions.
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);
}
+ ///
+ /// Creates a module of the correct type given a module description.
+ ///
+ /// If the module description shows "Puzzle" as a type, this will try to create a puzzle module.
+ ///
+ /// The space to create the module for.
+ /// The module description to create the module from.
+ /// A or .
+ /// If there is no module type fitting the description.
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()
diff --git a/Assets/Engine/Runtime/Modules/PuzzleModule.cs b/Assets/Engine/Runtime/Modules/PuzzleModule.cs
index cd34d41..7b9087d 100644
--- a/Assets/Engine/Runtime/Modules/PuzzleModule.cs
+++ b/Assets/Engine/Runtime/Modules/PuzzleModule.cs
@@ -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));
+ }
}
}
\ No newline at end of file
diff --git a/Assets/Engine/Runtime/Modules/PuzzleState.cs b/Assets/Engine/Runtime/Modules/PuzzleState.cs
index c32c31b..f5702cb 100644
--- a/Assets/Engine/Runtime/Modules/PuzzleState.cs
+++ b/Assets/Engine/Runtime/Modules/PuzzleState.cs
@@ -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));
+ }
}
}
\ No newline at end of file
diff --git a/Assets/Engine/Runtime/Utilities/Exceptions.cs b/Assets/Engine/Runtime/Utilities/Exceptions.cs
new file mode 100644
index 0000000..3d80bc5
--- /dev/null
+++ b/Assets/Engine/Runtime/Utilities/Exceptions.cs
@@ -0,0 +1,21 @@
+using System;
+
+namespace EscapeRoomEngine.Engine.Runtime.Utilities
+{
+ public class EngineException : Exception
+ {
+ public EngineException(string message) : base($"[EngineException] {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) {}
+ }
+}
\ No newline at end of file
diff --git a/Assets/Engine/Runtime/Utilities/Exceptions.cs.meta b/Assets/Engine/Runtime/Utilities/Exceptions.cs.meta
new file mode 100644
index 0000000..1acf4c2
--- /dev/null
+++ b/Assets/Engine/Runtime/Utilities/Exceptions.cs.meta
@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: e84dc071915b429ba633c9541d3411ac
+timeCreated: 1668964775
\ No newline at end of file
diff --git a/Assets/Engine/Runtime/Utilities/Option.cs b/Assets/Engine/Runtime/Utilities/Option.cs
index 8ad9b43..be50083 100644
--- a/Assets/Engine/Runtime/Utilities/Option.cs
+++ b/Assets/Engine/Runtime/Utilities/Option.cs
@@ -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 f) => f();
public IOption And(IOption other) => this;