96 lines
3.8 KiB
C#
96 lines
3.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
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 Object = UnityEngine.Object;
|
|
|
|
namespace Escape_Room_Engine.Engine.Scripts.Modules
|
|
{
|
|
public enum Orientation
|
|
{
|
|
North = 0, East = 90, South = 180, West = 270
|
|
}
|
|
|
|
public class Module
|
|
{
|
|
public static HashSet<Orientation> EveryOrientation => new(new[]
|
|
{
|
|
Orientation.North, Orientation.East, Orientation.South, Orientation.West
|
|
});
|
|
|
|
/// <summary>
|
|
/// Get the space relative (<i>SR</i>) position of this module.
|
|
/// </summary>
|
|
internal Vector2Int SrPosition => srDimensions.Position;
|
|
/// <summary>
|
|
/// Get the room relative (<i>RR</i>) position of this module.
|
|
/// </summary>
|
|
internal Vector2Int RrPosition => _space.ToRoomRelative(SrPosition);
|
|
|
|
internal readonly ModuleDescription description;
|
|
internal Orientation orientation;
|
|
|
|
/// <summary>
|
|
/// The space relative (<i>SR</i>) dimensions of this module.
|
|
/// </summary>
|
|
protected Dimensions srDimensions;
|
|
|
|
private GameObject _moduleObject, _orientationObject;
|
|
private readonly Space _space;
|
|
|
|
internal Module(Space space, ModuleDescription description)
|
|
{
|
|
_space = space;
|
|
this.description = description;
|
|
}
|
|
|
|
internal bool IsType(ModuleType type)
|
|
{
|
|
return description.types.Contains(type);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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>
|
|
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))
|
|
{
|
|
throw new Exception($"Trying to place {this} at {srPosition}, which is outside space dimensions {_space.rrDimensions}.");
|
|
}
|
|
|
|
srDimensions.Position = srPosition;
|
|
|
|
Logger.Log($"{this} has been placed at {srPosition} (SR)", LogType.ModulePlacement);
|
|
}
|
|
|
|
internal 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);
|
|
|
|
_orientationObject = new GameObject("Orientation");
|
|
_orientationObject.transform.SetParent(_moduleObject.transform, false);
|
|
_orientationObject.transform.Rotate(Vector3.up, (float)orientation);
|
|
|
|
Object.Instantiate(description.modulePrefab, _orientationObject.transform, false);
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"Module ({string.Join(", ", description.types.ToList().ConvertAll(type => type.ToString()))})";
|
|
}
|
|
}
|
|
}
|