74 lines
2.2 KiB
C#
74 lines
2.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
namespace BITKit
|
|
{
|
|
public class PhysicsDoor : MonoBehaviour, IAction,IDescription
|
|
{
|
|
public enum State
|
|
{
|
|
Close,
|
|
Open,
|
|
HalfOpen,
|
|
Locked,
|
|
}
|
|
[SerializeField] private string description;
|
|
[SerializeField] private bool allowPhysics = true;
|
|
[SerializeField] private Rigidbody root;
|
|
[SerializeField] private Vector3 openEuler;
|
|
[SerializeField] private Vector3 closeEuler;
|
|
[SerializeField] private State state;
|
|
[SerializeField] private Collider[] ignoreColliders;
|
|
private void Start()
|
|
{
|
|
var selfColliders = GetComponentsInChildren<Collider>(true);
|
|
var parentCollider = GetComponentInParent<Collider>(true);
|
|
foreach (var self in selfColliders)
|
|
{
|
|
foreach (var ignore in ignoreColliders)
|
|
{
|
|
Physics.IgnoreCollision(self, ignore, true);
|
|
}
|
|
|
|
if (parentCollider is not null)
|
|
Physics.IgnoreCollision(self, parentCollider, true);
|
|
}
|
|
Set();
|
|
}
|
|
public void Execute()
|
|
{
|
|
switch (state)
|
|
{
|
|
case State.Open:
|
|
state = State.Close;
|
|
Set();
|
|
break;
|
|
case State.Close:
|
|
state = State.Open;
|
|
Set();
|
|
break;
|
|
}
|
|
}
|
|
|
|
public void Set(bool isClosed)
|
|
{
|
|
state = isClosed ? State.Close : State.Open;
|
|
root.transform.localEulerAngles = isClosed ? closeEuler : openEuler;
|
|
if (allowPhysics)
|
|
root.isKinematic = isClosed;
|
|
}
|
|
|
|
private void Set()
|
|
{
|
|
var isClosed = state switch
|
|
{
|
|
State.Locked => true,
|
|
State.Close => true,
|
|
State.Open => false,
|
|
_ => true
|
|
};
|
|
Set(isClosed);
|
|
}
|
|
public string Name => description;
|
|
}
|
|
} |