module state game objects

This commit is contained in:
2022-11-13 20:49:32 +01:00
parent 14ab0374cc
commit 5f19adcb43
13 changed files with 251 additions and 84 deletions

View File

@@ -1,6 +1,5 @@
using System;
using UnityEngine;
using LogType = Escape_Room_Engine.Engine.Scripts.Utilities.LogType;
namespace Escape_Room_Engine.Engine.Scripts.Modules
{
@@ -14,19 +13,24 @@ namespace Escape_Room_Engine.Engine.Scripts.Modules
{
public bool IsEntrance => IsType((ModuleType)DoorType.Entrance);
public bool IsExit => IsType((ModuleType)DoorType.Exit);
private bool _unlocked;
internal new DoorState State { get; private set; }
internal DoorModule(Space space, DoorModuleDescription description) : base(space, description)
{
srDimensions.Size = Vector2Int.one; // door always has size 1x1
}
internal void Unlock()
internal override void InstantiateModule(Transform parent)
{
_unlocked = true;
base.InstantiateModule(parent);
Utilities.Logger.Log($"{this} has been unlocked", LogType.PuzzleFlow);
space.room.AddDoor(this);
}
protected override void AddStateComponent()
{
State = moduleObject.AddComponent<DoorState>();
State.SetModule(this);
}
public override string ToString()

View File

@@ -0,0 +1,66 @@
using System;
using Escape_Room_Engine.Engine.Scripts.Utilities;
using NaughtyAttributes;
namespace Escape_Room_Engine.Engine.Scripts.Modules
{
public enum DoorEventType
{
Locked, Unlocked
}
public delegate void DoorEventHandler(DoorModule source, DoorEventType e);
public class DoorState : ModuleState
{
public event DoorEventHandler DoorEvent;
private new DoorModule Module { get; set; }
public bool Unlocked
{
get => _unlocked;
private set
{
var type =
!_unlocked && value ? Some<DoorEventType>.Of(DoorEventType.Unlocked)
: _unlocked && !value ? Some<DoorEventType>.Of(DoorEventType.Locked)
: None<DoorEventType>.New();
_unlocked = value;
type.Match(some: OnDoorEvent);
}
}
private bool _unlocked;
private void OnDoorEvent(DoorEventType type)
{
Logger.Log($"{Module} has been {type}", LogType.PuzzleFlow);
DoorEvent?.Invoke(Module, type);
}
public override void SetModule(Module module)
{
if (module is DoorModule doorModule)
{
Module = doorModule;
}
else
{
throw new Exception("Tried to set wrong type of module.");
}
}
[Button(enabledMode: EButtonEnableMode.Playmode)]
internal void Unlock()
{
Unlocked = true;
}
[Button(enabledMode: EButtonEnableMode.Playmode)]
internal void Lock()
{
Unlocked = false;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 696181e3eda449d49d4c1c88b07d7b05
timeCreated: 1668337769

View File

@@ -28,7 +28,8 @@ namespace Escape_Room_Engine.Engine.Scripts.Modules
/// <summary>
/// Get the room relative (<i>RR</i>) position of this module.
/// </summary>
internal Vector2Int RrPosition => _space.ToRoomRelative(SrPosition);
internal Vector2Int RrPosition => space.ToRoomRelative(SrPosition);
internal ModuleState State { get; private set; }
internal readonly ModuleDescription description;
internal Orientation orientation;
@@ -38,12 +39,13 @@ namespace Escape_Room_Engine.Engine.Scripts.Modules
/// </summary>
protected Dimensions srDimensions;
private GameObject _moduleObject, _orientationObject;
private readonly Space _space;
protected GameObject moduleObject;
protected readonly Space space;
private GameObject _orientationObject;
internal Module(Space space, ModuleDescription description)
{
_space = space;
this.space = space;
this.description = description;
}
@@ -57,16 +59,16 @@ namespace Escape_Room_Engine.Engine.Scripts.Modules
/// </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>
internal void PlaceRoomRelative(Vector2Int rrPosition) => Place(_space.ToSpaceRelative(rrPosition));
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>
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 Exception($"Trying to place {this} at {srPosition}, which is outside space dimensions {space.rrDimensions}.");
}
srDimensions.Position = srPosition;
@@ -92,19 +94,26 @@ namespace Escape_Room_Engine.Engine.Scripts.Modules
};
}
internal void InstantiateModule(Transform parent)
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);
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.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>();
State.SetModule(this);
}
public override string ToString()
{
return $"Module ({string.Join(", ", description.types.ToList().ConvertAll(type => type.ToString()))})";

View File

@@ -0,0 +1,11 @@
using UnityEngine;
namespace Escape_Room_Engine.Engine.Scripts.Modules
{
public class ModuleState : MonoBehaviour
{
public Module Module { get; protected set; }
public virtual void SetModule(Module module) => Module = module;
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: efdc32c450f7411385748449459a17b4
timeCreated: 1668180361

View File

@@ -1,57 +1,27 @@
using Escape_Room_Engine.Engine.Scripts.Utilities;
using UnityEngine;
using Logger = Escape_Room_Engine.Engine.Scripts.Utilities.Logger;
using LogType = Escape_Room_Engine.Engine.Scripts.Utilities.LogType;
using UnityEngine;
namespace Escape_Room_Engine.Engine.Scripts.Modules
{
public enum PuzzleEventType
{
Reset, Solved
}
public delegate void PuzzleEventHandler(PuzzleModule source, PuzzleEventType e);
public class PuzzleModule : Module
{
internal event PuzzleEventHandler PuzzleEvent;
internal bool Solved
{
get => _solved;
private set
{
var type =
!_solved && value ? Some<PuzzleEventType>.Of(PuzzleEventType.Solved)
: _solved && value ? Some<PuzzleEventType>.Of(PuzzleEventType.Reset)
: None<PuzzleEventType>.New();
_solved = value;
type.Match(some: OnPuzzleEvent);
}
}
private bool _solved;
internal new PuzzleState State { get; private set; }
internal PuzzleModule(Space space, PuzzleModuleDescription description) : base(space, description)
{
srDimensions.Size = Vector2Int.one; // TODO: larger modules
}
protected virtual void OnPuzzleEvent(PuzzleEventType type)
internal override void InstantiateModule(Transform parent)
{
Logger.Log($"{this} has been {type}", LogType.PuzzleFlow);
base.InstantiateModule(parent);
PuzzleEvent?.Invoke(this, type);
space.room.AddPuzzle(this);
}
public void Solve()
protected override void AddStateComponent()
{
Solved = true;
}
public override string ToString()
{
return $"Puzzle module ({(Solved ? "" : "not ")}solved)";
State = moduleObject.AddComponent<PuzzleState>();
State.SetModule(this);
}
}
}

View File

@@ -0,0 +1,66 @@
using System;
using Escape_Room_Engine.Engine.Scripts.Utilities;
using NaughtyAttributes;
namespace Escape_Room_Engine.Engine.Scripts.Modules
{
public enum PuzzleEventType
{
Restarted, Solved
}
public delegate void PuzzleEventHandler(PuzzleModule source, PuzzleEventType e);
public class PuzzleState : ModuleState
{
public event PuzzleEventHandler PuzzleEvent;
private new PuzzleModule Module { get; set; }
public bool Solved
{
get => _solved;
private set
{
var type =
!_solved && value ? Some<PuzzleEventType>.Of(PuzzleEventType.Solved)
: _solved && !value ? Some<PuzzleEventType>.Of(PuzzleEventType.Restarted)
: None<PuzzleEventType>.New();
_solved = value;
type.Match(some: OnPuzzleEvent);
}
}
private bool _solved;
private void OnPuzzleEvent(PuzzleEventType type)
{
Logger.Log($"{Module} has been {type}", LogType.PuzzleFlow);
PuzzleEvent?.Invoke(Module, type);
}
public override void SetModule(Module module)
{
if (module is PuzzleModule puzzleModule)
{
Module = puzzleModule;
}
else
{
throw new Exception("Tried to set wrong type of module.");
}
}
[Button(enabledMode: EButtonEnableMode.Playmode)]
public void Solve()
{
Solved = true;
}
[Button(enabledMode: EButtonEnableMode.Playmode)]
public void Restart()
{
Solved = false;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 265ea1efb38042b282ea67c50ac3e878
timeCreated: 1668180832