puzzle module framework, puzzle a ball module
This commit is contained in:
@@ -13,8 +13,20 @@ namespace Escape_Room_Engine.Engine.Scripts.Modules
|
||||
{
|
||||
public bool IsEntrance => IsType((ModuleType)DoorType.Entrance);
|
||||
public bool IsExit => IsType((ModuleType)DoorType.Exit);
|
||||
internal new DoorState State { get; private set; }
|
||||
|
||||
|
||||
internal DoorState DoorState
|
||||
{
|
||||
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)
|
||||
{
|
||||
srDimensions.Size = Vector2Int.one; // door always has size 1x1
|
||||
@@ -27,12 +39,6 @@ namespace Escape_Room_Engine.Engine.Scripts.Modules
|
||||
space.room.AddDoor(this);
|
||||
}
|
||||
|
||||
protected override void AddStateComponent()
|
||||
{
|
||||
State = moduleObject.AddComponent<DoorState>();
|
||||
State.SetModule(this);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{(IsEntrance ? "Entrance" : IsExit ? "Exit" : "Unknown")} door";
|
||||
|
||||
@@ -39,9 +39,7 @@ namespace Escape_Room_Engine.Engine.Scripts.Modules
|
||||
/// </summary>
|
||||
protected Dimensions srDimensions;
|
||||
|
||||
protected GameObject moduleObject;
|
||||
protected readonly Space space;
|
||||
private GameObject _orientationObject;
|
||||
|
||||
internal Module(Space space, ModuleDescription description)
|
||||
{
|
||||
@@ -96,21 +94,12 @@ namespace Escape_Room_Engine.Engine.Scripts.Modules
|
||||
|
||||
internal virtual void InstantiateModule(Transform parent)
|
||||
{
|
||||
moduleObject = new GameObject(ToString());
|
||||
moduleObject.transform.SetParent(parent, false);
|
||||
moduleObject.transform.localPosition = new Vector3(srDimensions.x + .5f, 0, srDimensions.z + .5f);
|
||||
AddStateComponent();
|
||||
|
||||
_orientationObject = new GameObject("Orientation");
|
||||
_orientationObject.transform.SetParent(moduleObject.transform, false);
|
||||
_orientationObject.transform.Rotate(Vector3.up, (float)orientation);
|
||||
|
||||
Object.Instantiate(description.modulePrefab, _orientationObject.transform, false);
|
||||
}
|
||||
|
||||
protected virtual void AddStateComponent()
|
||||
{
|
||||
State = moduleObject.AddComponent<ModuleState>();
|
||||
Logger.Log($"Instantiating {this}", LogType.RoomGeneration);
|
||||
|
||||
State = Object.Instantiate(description.modulePrefab, parent, false);
|
||||
State.transform.localPosition = new Vector3(srDimensions.x + .5f, 0, srDimensions.z + .5f);
|
||||
State.transform.Rotate(Vector3.up, (float)orientation);
|
||||
State.name = ToString();
|
||||
State.SetModule(this);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace Escape_Room_Engine.Engine.Scripts.Modules
|
||||
public class ModuleDescription : ScriptableObject
|
||||
{
|
||||
public List<ModuleType> types = new();
|
||||
public GameObject modulePrefab;
|
||||
public ModuleState modulePrefab;
|
||||
public List<PlacementRequirement> placementRequirements = new();
|
||||
public List<OrientationRequirement> orientationRequirements = new();
|
||||
}
|
||||
|
||||
@@ -1,10 +1,22 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Escape_Room_Engine.Engine.Scripts.Modules
|
||||
{
|
||||
public class PuzzleModule : Module
|
||||
{
|
||||
internal new PuzzleState State { get; private set; }
|
||||
internal PuzzleState PuzzleState
|
||||
{
|
||||
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)
|
||||
{
|
||||
@@ -17,11 +29,5 @@ namespace Escape_Room_Engine.Engine.Scripts.Modules
|
||||
|
||||
space.room.AddPuzzle(this);
|
||||
}
|
||||
|
||||
protected override void AddStateComponent()
|
||||
{
|
||||
State = moduleObject.AddComponent<PuzzleState>();
|
||||
State.SetModule(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,36 @@
|
||||
using System;
|
||||
using Escape_Room_Engine.Engine.Scripts.Utilities;
|
||||
using NaughtyAttributes;
|
||||
using Logger = Escape_Room_Engine.Engine.Scripts.Utilities.Logger;
|
||||
using LogType = Escape_Room_Engine.Engine.Scripts.Utilities.LogType;
|
||||
|
||||
namespace Escape_Room_Engine.Engine.Scripts.Modules
|
||||
{
|
||||
public enum PuzzleEventType
|
||||
{
|
||||
Restarted, Solved
|
||||
Restarted, Solved, WrongInput
|
||||
}
|
||||
|
||||
|
||||
public static class PuzzleEventExtensions
|
||||
{
|
||||
public static string Description(this PuzzleEventType type, PuzzleModule module)
|
||||
{
|
||||
return type switch
|
||||
{
|
||||
PuzzleEventType.Restarted => $"{module} has been restarted",
|
||||
PuzzleEventType.Solved => $"{module} has been solved",
|
||||
PuzzleEventType.WrongInput => $"Wrong input for {module}",
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public delegate void PuzzleEventHandler(PuzzleModule source, PuzzleEventType e);
|
||||
|
||||
public class PuzzleState : ModuleState
|
||||
{
|
||||
public event PuzzleEventHandler PuzzleEvent;
|
||||
public EngineTheme theme;
|
||||
|
||||
private new PuzzleModule Module { get; set; }
|
||||
public bool Solved
|
||||
@@ -34,7 +51,7 @@ namespace Escape_Room_Engine.Engine.Scripts.Modules
|
||||
|
||||
private void OnPuzzleEvent(PuzzleEventType type)
|
||||
{
|
||||
Logger.Log($"{Module} has been {type}", LogType.PuzzleFlow);
|
||||
Logger.Log(type.Description(Module), LogType.PuzzleFlow);
|
||||
|
||||
PuzzleEvent?.Invoke(Module, type);
|
||||
}
|
||||
@@ -62,5 +79,11 @@ namespace Escape_Room_Engine.Engine.Scripts.Modules
|
||||
{
|
||||
Solved = false;
|
||||
}
|
||||
|
||||
[Button("Trigger Wrong Input", EButtonEnableMode.Playmode)]
|
||||
public void WrongInput()
|
||||
{
|
||||
OnPuzzleEvent(PuzzleEventType.WrongInput);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user