Files
modular-vr/Assets/Portal/Runtime/Portal.cs
2022-11-27 12:12:02 +01:00

119 lines
4.0 KiB
C#

using System;
using System.Collections.Generic;
using EscapeRoomEngine.Engine.Runtime.Modules;
using EscapeRoomEngine.Engine.Runtime.Utilities;
using NaughtyAttributes;
using UnityEngine;
namespace EscapeRoomEngine.Portal.Runtime
{
[RequireComponent(typeof(Collider))]
public class Portal : DoorState
{
public static readonly Matrix4x4 HalfRotation = Matrix4x4.Rotate(Quaternion.Euler(0, 180, 0));
/// <summary>
/// The portal that is connected with this one.
/// </summary>
public Portal linkedPortal;
/// <summary>
/// The camera that will draw the view for this portal.
/// </summary>
[BoxGroup("Internal")] public PortalCamera portalCamera;
/// <summary>
/// The transform marking the edge of the portal plane.
/// </summary>
[BoxGroup("Internal")] public Transform portalTransform;
private bool Connected => linkedPortal != null;
private readonly List<PortalDriver> _closePortalDrivers = new();
private void Awake()
{
// check whether the collider is set up correctly
if (!GetComponent<Collider>().isTrigger) throw new Exception("Collider must be a trigger.");
DoorEvent += (_, type) =>
{
if (type == DoorEventType.Connected)
{
linkedPortal = FromDoorState(Module.ConnectedDoorState);
portalCamera.screen.gameObject.SetActive(true);
portalCamera.enabled = true;
}
};
}
private void FixedUpdate()
{
if (Connected)
{
for (var i = 0; i < _closePortalDrivers.Count; i++)
{
var portalDriver = _closePortalDrivers[i];
if (portalDriver.entrySide < 0 && CalculateSide(portalDriver.transform) >= 0) // must have entered from the front and exited the back
{
StopTrackingDriver(portalDriver);
linkedPortal.StartTrackingDriver(portalDriver, -1);
portalDriver.Teleport(this, linkedPortal);
i--; // decrease the loop counter because the list is one element smaller now
}
}
}
}
private void StartTrackingDriver(PortalDriver portalDriver, int entrySide)
{
_closePortalDrivers.Add(portalDriver);
portalDriver.EnableClone(linkedPortal);
portalDriver.entrySide = entrySide;
}
private void StopTrackingDriver(PortalDriver portalDriver)
{
_closePortalDrivers.Remove(portalDriver);
portalDriver.DisableClone(linkedPortal);
}
private void OnTriggerEnter(Collider other)
{
if (Connected)
{
var portalDriver = other.GetComponent<PortalDriver>();
if (portalDriver && !_closePortalDrivers.Contains(portalDriver))
{
StartTrackingDriver(portalDriver, CalculateSide(portalDriver.transform));
}
}
}
private void OnTriggerExit(Collider other)
{
if (Connected)
{
var portalDriver = other.GetComponent<PortalDriver>();
if (portalDriver)
{
StopTrackingDriver(portalDriver);
}
}
}
private int CalculateSide(Transform portalDriverTransform)
{
return Math.Sign(Vector3.Dot(portalTransform.forward, portalDriverTransform.position - portalTransform.position));
}
private static Portal FromDoorState(DoorState state)
{
if (state is Portal portal)
{
return portal;
}
throw new WrongTypeException(typeof(Portal), state.GetType(), typeof(DoorState));
}
}
}