Files
HauntedBloodlines/Assets/Scripts/Scripted Events/EnemyAIDoorTrigger.cs
2025-05-29 22:31:40 +03:00

93 lines
2.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using InfallibleCode;
public class EnemyAIDoorTrigger : MonoBehaviour
{
public DoorBehaviour door;
public bool LockDoor;
public bool UnlockDoor;
public bool OpenDoor;
public bool CloseDoor;
[Range(0, -90)]
[Tooltip("The lerp angle of the door")]
public float DoorOpenAngle = 0f;
[Header("Door Speed")]
[Range(0, 0.9f)]
public float DoorSpeed = 0.5f;
private bool ActionPerformed;
private void Awake()
{
if(door == null)
{
door = GetComponent<DoorBehaviour>();
}
//door._doorLerpOpenAngle = DoorOpenAngle;
}
private void OnTriggerStay(Collider col)
{
if (col.gameObject.tag == "EnemyAI" && !ActionPerformed)
{
if (door.CanOpenDoor && !door.DoorIsOpen)
{
if (IsFacingTarget())
{
door.StopDoorCoroutines();
door.enemyIsTryingToOpenDoor = true;
Debug.Log("se sixainomai");
door.OpenDoor(DoorSpeed);
//door.UnlockDoor();
ActionPerformed = true;
}
else
{
door.StopDoorCoroutines();
door.enemyIsTryingToOpenDoor = true;
Debug.Log("se sixainomai alla se 90 moires <3");
door.OpenDoor90Degrees(DoorSpeed);
//door.UnlockDoor();
ActionPerformed = true;
}
}
}
}
private void OnTriggerExit(Collider col)
{
if (col.gameObject.tag == "EnemyAI")
{
if (door.CanOpenDoor && door.DoorIsOpen)
{
door.StopDoorCoroutines();
door.enemyIsTryingToOpenDoor = false;
door.CloseDoor(DoorSpeed);
ActionPerformed = false;
}
}
}
bool IsFacingTarget()
{
if (door.transform == null)
{
Debug.LogWarning("Target not assigned!");
return false;
}
Vector3 toTarget = door.transform.position - MedeaDemonicEnemyBehaviour.GetInstance()._medea.position;
toTarget.y = 0; // Optional: If you want to ignore the vertical difference.
// Check if the angle between the forward vector of this GameObject and the direction to the target is within a certain threshold.
float angle = Vector3.Angle(MedeaDemonicEnemyBehaviour.GetInstance()._medea.forward, toTarget);
// You can adjust the angle threshold according to your needs.
float angleThreshold = 30f; // For example, 30 degrees.
return angle < angleThreshold;
}
}