ZeroVR/ZeroPacientVR/Assets/Scripts/DoorFrame.cs

101 lines
2.5 KiB
C#

using System.Collections.Generic;
using System.Linq;
using FMOD.Studio;
using FMODUnity;
using UnityEngine;
using STOP_MODE = FMOD.Studio.STOP_MODE;
public class DoorFrame : MonoBehaviour
{
[SerializeField] private float timeScale = 1f;
[SerializeField] private Animator animator;
[SerializeField] private EventReference doorClip;
[SerializeField] private string paramName;
[SerializeField] private string labelToOpening;
[SerializeField] private string labelToFinish;
[SerializeField] private List<ParticleSystem> particles;
private EventInstance eventInstance;
private State state;
private readonly int open = Animator.StringToHash("Open");
private float time;
private enum State
{
Idle = 0,
Open = 1,
Close = 2,
}
private void Awake()
{
eventInstance = RuntimeManager.CreateInstance(doorClip);
eventInstance.set3DAttributes(transform.position.To3DAttributes());
}
private void Update()
{
var deltaTime = Time.deltaTime * timeScale;
switch(state)
{
case State.Idle:
animator.Play(open, 0, time);
return;
case State.Open:
time += deltaTime;
break;
case State.Close:
time -= deltaTime;
break;
}
time = Mathf.Clamp01(time);
animator.Play(open, 0, time);
if(time is <= 0 or >= 1f)
{
if(state != State.Idle)
{
eventInstance.setParameterByNameWithLabel(paramName, labelToFinish);
PlayParticles();
}
state = State.Idle;
}
}
public void Enter()
{
eventInstance.setParameterByNameWithLabel(paramName, labelToOpening);
if(state == State.Idle)
{
eventInstance.stop(STOP_MODE.IMMEDIATE);
eventInstance.start();
PlayParticles();
}
state = State.Open;
}
public void Exit()
{
eventInstance.setParameterByNameWithLabel(paramName, labelToOpening);
if(state == State.Idle)
{
eventInstance.stop(STOP_MODE.IMMEDIATE);
eventInstance.start();
PlayParticles();
}
state = State.Close;
}
private void PlayParticles()
{
foreach(var particle in particles.Where(particle => particle))
{
particle.Stop();
particle.Play();
}
}
}