BITFALL/Assets/Artists/Scripts/Scenes/BasicDoor.cs

102 lines
3.0 KiB
C#
Raw Normal View History

2023-12-30 17:37:48 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using BITKit;
2024-02-21 01:40:53 +08:00
using BITKit.Entities;
2023-12-30 17:37:48 +08:00
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.AI;
2024-03-05 17:34:41 +08:00
using UnityEngine.Animations;
using UnityEngine.UI;
2023-12-30 17:37:48 +08:00
namespace BITFALL.Scene
{
public interface IDoorConfig
{
string Name { get; }
Transform Transform { get; }
2024-03-05 17:34:41 +08:00
Axis Axis { get; }
2023-12-30 17:37:48 +08:00
Vector3 OpenEuler { get; }
Vector3 CloseEuler { get; }
2024-03-31 23:34:22 +08:00
Vector3 OpenPosition { get; }
Vector3 ClosePosition { get; }
2023-12-30 17:37:48 +08:00
}
[Serializable]
public class BasicDoorConfig:IDoorConfig
{
[SerializeField] private string name;
[SerializeField] private Transform transform;
2024-03-05 17:34:41 +08:00
[SerializeField] private Axis axis;
2023-12-30 17:37:48 +08:00
[SerializeField] private Vector3 openEuler;
[SerializeField] private Vector3 closeEuler;
2024-03-31 23:34:22 +08:00
[SerializeField] private Vector3 openPosition;
[SerializeField] private Vector3 closePosition;
2023-12-30 17:37:48 +08:00
public string Name => name;
public Transform Transform => transform;
public Vector3 OpenEuler => openEuler;
public Vector3 CloseEuler => closeEuler;
2024-03-05 17:34:41 +08:00
public Axis Axis => axis;
2024-03-31 23:34:22 +08:00
public Vector3 OpenPosition => openPosition;
public Vector3 ClosePosition => closePosition;
2023-12-30 17:37:48 +08:00
}
2024-02-21 01:40:53 +08:00
public sealed class BasicDoor : MonoBehaviour,IAction,IDescription,ISceneBlockArea,IScenePlayerImpact
2023-12-30 17:37:48 +08:00
{
[SerializeReference,SubclassSelector] private IReference objName;
[SerializeField] private bool isOpened;
[SerializeField] private bool isLocked;
[SerializeField] private new Collider collider;
[SerializeField] private NavMeshObstacle obstacle;
[SerializeReference, SubclassSelector] private IDoorConfig[] doors;
2024-03-31 23:34:22 +08:00
private bool initialized;
2023-12-30 17:37:48 +08:00
public void Execute()
{
2024-03-31 23:34:22 +08:00
if (initialized is false)
{
initialized = true;
}
2023-12-30 17:37:48 +08:00
if(isLocked)return;
isOpened = !isOpened;
foreach (var door in doors)
{
door.Transform.localRotation = Quaternion.Euler(isOpened ? door.OpenEuler : door.CloseEuler);
2024-03-31 23:34:22 +08:00
door.Transform.localPosition = isOpened ? door.OpenPosition : door.ClosePosition;
2023-12-30 17:37:48 +08:00
}
if(obstacle)obstacle.enabled = !isOpened;
}
public string Name => objName.Value;
public bool IsBlocked =>isLocked || !isOpened;
public bool InRange(float3 position)=>collider.bounds.Contains(position);
public bool InRange(float3 position, float3 direction)
{
var ray = new Ray(position, ((Vector3)direction).normalized);
return collider.Raycast(ray, out _, 1f);
}
2024-02-21 01:40:53 +08:00
public void OnPlayerImpact(IEntity entity, float4x4 matrix)
{
2024-03-05 17:34:41 +08:00
if (isOpened) return;
isOpened = true;
if (obstacle) obstacle.enabled = false;
var playerPosition = ((Matrix4x4)matrix).GetPosition();
var dir = transform.InverseTransformPoint(playerPosition);
foreach (var door in doors)
{
2024-03-31 23:34:22 +08:00
if (!door.OpenEuler.IsDefault() || !door.CloseEuler.IsDefault()) continue;
2024-03-05 17:34:41 +08:00
var isForward = door.Axis switch
{
Axis.X => dir.x < 0,
Axis.Y => dir.y < 0,
Axis.Z => dir.z < 0,
_ => false,
};
var backEuler = door.OpenEuler;
backEuler.y *= -1;
door.Transform.localRotation = Quaternion.Euler(isForward ? door.OpenEuler : backEuler );
}
2024-02-21 01:40:53 +08:00
}
2023-12-30 17:37:48 +08:00
}
}