79 lines
3.1 KiB
C#
79 lines
3.1 KiB
C#
using System;
|
|
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 class Module
|
|
{
|
|
/// <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;
|
|
protected GameObject _moduleObject;
|
|
/// <summary>
|
|
/// The space relative (<i>SR</i>) dimensions of this module.
|
|
/// </summary>
|
|
protected Dimensions srDimensions;
|
|
|
|
private readonly Space _space;
|
|
|
|
internal Module(Space space, ModuleDescription description)
|
|
{
|
|
_space = space;
|
|
_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, 0, srDimensions.z);
|
|
|
|
Object.Instantiate(_description.modulePrefab, _moduleObject.transform, false);
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"Module ({string.Join(',', _description.types.ToList().ConvertAll(type => type.ToString()))})";
|
|
}
|
|
}
|
|
}
|