custom exceptions, methods to create modules and states of specific type from generic ones
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user