custom exceptions, methods to create modules and states of specific type from generic ones
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using EscapeRoomEngine.Engine.Runtime.Modules;
|
using EscapeRoomEngine.Engine.Runtime.Modules;
|
||||||
@@ -77,7 +76,7 @@ namespace EscapeRoomEngine.Engine.Runtime
|
|||||||
// add exit
|
// add exit
|
||||||
var exitDoor = new DoorModule(space, theme.exitDoorTypes.RandomElement());
|
var exitDoor = new DoorModule(space, theme.exitDoorTypes.RandomElement());
|
||||||
if (!space.AddModuleWithRequirements(exitDoor))
|
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);
|
var exit = new Passage(exitDoor);
|
||||||
|
|
||||||
// add puzzles
|
// add puzzles
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using EscapeRoomEngine.Engine.Runtime.Utilities;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace EscapeRoomEngine.Engine.Runtime.Modules
|
namespace EscapeRoomEngine.Engine.Runtime.Modules
|
||||||
@@ -14,18 +15,7 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
|
|||||||
public bool IsEntrance => IsType((ModuleType)DoorType.Entrance);
|
public bool IsEntrance => IsType((ModuleType)DoorType.Entrance);
|
||||||
public bool IsExit => IsType((ModuleType)DoorType.Exit);
|
public bool IsExit => IsType((ModuleType)DoorType.Exit);
|
||||||
|
|
||||||
internal DoorState DoorState
|
internal DoorState DoorState => DoorState.FromState(State);
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
if (State is DoorState doorState)
|
|
||||||
{
|
|
||||||
return doorState;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Exception("DoorModule must contain a DoorState");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal DoorModule(Space space, DoorModuleDescription description) : base(space, description)
|
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";
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -41,14 +41,7 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
|
|||||||
|
|
||||||
public override void SetModule(Module module)
|
public override void SetModule(Module module)
|
||||||
{
|
{
|
||||||
if (module is DoorModule doorModule)
|
Module = DoorModule.FromModule(module);
|
||||||
{
|
|
||||||
Module = doorModule;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new Exception($"Tried to set wrong type of module ({module.GetType()} instead of DoorModule)");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Button(enabledMode: EButtonEnableMode.Playmode)]
|
[Button(enabledMode: EButtonEnableMode.Playmode)]
|
||||||
@@ -62,5 +55,15 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
|
|||||||
{
|
{
|
||||||
Unlocked = false;
|
Unlocked = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static DoorState FromState(ModuleState state)
|
||||||
|
{
|
||||||
|
if (state is DoorState doorState)
|
||||||
|
{
|
||||||
|
return doorState;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new WrongTypeException(typeof(DoorState), state.GetType(), typeof(ModuleState));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -56,17 +56,18 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
|
|||||||
/// Place this module with a position relative to the room.
|
/// Place this module with a position relative to the room.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="rrPosition">The room relative (<i>RR</i>) position of this module. Must be inside the space dimensions.</param>
|
/// <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));
|
internal void PlaceRoomRelative(Vector2Int rrPosition) => Place(space.ToSpaceRelative(rrPosition));
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Place this module with a position relative to the space it is in.
|
/// Place this module with a position relative to the space it is in.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="srPosition">The space relative (<i>SR</i>) position of this module. Must be inside the space dimensions.</param>
|
/// <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) {
|
internal void Place(Vector2Int srPosition) {
|
||||||
if (space != null && !srPosition.IsInsideRelative(space.rrDimensions))
|
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;
|
srDimensions.Position = srPosition;
|
||||||
@@ -103,6 +104,15 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
|
|||||||
State.SetModule(this);
|
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)
|
internal static Module CreateModuleByType(Space space, ModuleDescription description)
|
||||||
{
|
{
|
||||||
if (description.HasType(ModuleType.Puzzle) &&
|
if (description.HasType(ModuleType.Puzzle) &&
|
||||||
@@ -110,15 +120,14 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
|
|||||||
{
|
{
|
||||||
return new PuzzleModule(space, puzzleModuleDescription);
|
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);
|
return new DoorModule(space, doorModuleDescription);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
throw new WrongTypeException("There is no module type fitting this description.");
|
||||||
throw new Exception("Module description does not have fitting type.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
|
|||||||
@@ -1,22 +1,11 @@
|
|||||||
using System;
|
using EscapeRoomEngine.Engine.Runtime.Utilities;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace EscapeRoomEngine.Engine.Runtime.Modules
|
namespace EscapeRoomEngine.Engine.Runtime.Modules
|
||||||
{
|
{
|
||||||
public class PuzzleModule : Module
|
public class PuzzleModule : Module
|
||||||
{
|
{
|
||||||
internal PuzzleState PuzzleState
|
internal PuzzleState PuzzleState => PuzzleState.FromState(State);
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
if (State is PuzzleState puzzleState)
|
|
||||||
{
|
|
||||||
return puzzleState;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Exception("PuzzleModule must contain a PuzzleState");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal PuzzleModule(Space space, PuzzleModuleDescription description) : base(space, description)
|
internal PuzzleModule(Space space, PuzzleModuleDescription description) : base(space, description)
|
||||||
{
|
{
|
||||||
@@ -29,5 +18,15 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
|
|||||||
|
|
||||||
space.room.AddPuzzle(this);
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
using EscapeRoomEngine.Engine.Runtime.Utilities;
|
using EscapeRoomEngine.Engine.Runtime.Utilities;
|
||||||
using NaughtyAttributes;
|
using NaughtyAttributes;
|
||||||
using Logger = EscapeRoomEngine.Engine.Runtime.Utilities.Logger;
|
using Logger = EscapeRoomEngine.Engine.Runtime.Utilities.Logger;
|
||||||
@@ -58,14 +59,7 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
|
|||||||
|
|
||||||
public override void SetModule(Module module)
|
public override void SetModule(Module module)
|
||||||
{
|
{
|
||||||
if (module is PuzzleModule puzzleModule)
|
Module = PuzzleModule.FromModule(module);
|
||||||
{
|
|
||||||
Module = puzzleModule;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new Exception($"Tried to set wrong type of module ({module.GetType()} instead of PuzzleModule)");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Button(enabledMode: EButtonEnableMode.Playmode)]
|
[Button(enabledMode: EButtonEnableMode.Playmode)]
|
||||||
@@ -88,5 +82,15 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
|
|||||||
OnPuzzleEvent(PuzzleEventType.WrongInput);
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
21
Assets/Engine/Runtime/Utilities/Exceptions.cs
Normal file
21
Assets/Engine/Runtime/Utilities/Exceptions.cs
Normal 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) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
3
Assets/Engine/Runtime/Utilities/Exceptions.cs.meta
Normal file
3
Assets/Engine/Runtime/Utilities/Exceptions.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: e84dc071915b429ba633c9541d3411ac
|
||||||
|
timeCreated: 1668964775
|
||||||
@@ -49,8 +49,8 @@ namespace EscapeRoomEngine.Engine.Runtime.Utilities
|
|||||||
{
|
{
|
||||||
public bool IsSome() => false;
|
public bool IsSome() => false;
|
||||||
public bool IsNone() => true;
|
public bool IsNone() => true;
|
||||||
public T Expect(string message) => throw new Exception(message);
|
public T Expect(string message) => throw new OptionException(message);
|
||||||
public T Unwrap() => throw new Exception("Tried to unwrap None.");
|
public T Unwrap() => throw new OptionException("Tried to unwrap None.");
|
||||||
public T UnwrapOr(T def) => def;
|
public T UnwrapOr(T def) => def;
|
||||||
public T UnwrapOrElse(Func<T> f) => f();
|
public T UnwrapOrElse(Func<T> f) => f();
|
||||||
public IOption<T> And(IOption<T> other) => this;
|
public IOption<T> And(IOption<T> other) => this;
|
||||||
|
|||||||
Reference in New Issue
Block a user