2024-02-19 21:00:36 +03:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using Mirror;
|
|
|
|
|
2024-03-16 23:54:50 +03:00
|
|
|
[RequireComponent(typeof(AudioSource))]
|
2024-02-19 21:00:36 +03:00
|
|
|
public class SciFiDoor : NetworkBehaviour, IInteractable
|
|
|
|
{
|
2024-03-17 01:01:01 +03:00
|
|
|
[SyncVar(hook = nameof(OnStateChanged))]
|
2024-02-19 21:00:36 +03:00
|
|
|
public bool state = false;
|
|
|
|
|
2024-03-16 23:54:50 +03:00
|
|
|
public AudioClip openClip;
|
|
|
|
public AudioClip closeClip;
|
|
|
|
|
2024-02-19 21:00:36 +03:00
|
|
|
public float duration = 2f, offset;
|
|
|
|
public AnimationCurve oCurva;
|
|
|
|
public List<Transform> doorList;
|
|
|
|
|
|
|
|
private float _posX, _originX;
|
|
|
|
|
|
|
|
private float _timeElapsed;
|
2024-03-16 23:54:50 +03:00
|
|
|
private AudioSource _audioSource;
|
2024-02-19 21:00:36 +03:00
|
|
|
|
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
_originX = doorList[0].localPosition.x;
|
2024-03-16 23:54:50 +03:00
|
|
|
_audioSource = GetComponent<AudioSource>();
|
2024-02-19 21:00:36 +03:00
|
|
|
}
|
|
|
|
public void Interact()
|
|
|
|
{
|
|
|
|
CmdInteract();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Command(requiresAuthority = false)]
|
|
|
|
private void CmdInteract(NetworkConnectionToClient sender = null)
|
|
|
|
{
|
|
|
|
state = !state;
|
2024-03-17 01:01:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnStateChanged(bool _, bool value)
|
|
|
|
{
|
|
|
|
_audioSource.clip = value ? openClip : closeClip;
|
2024-03-16 23:54:50 +03:00
|
|
|
_audioSource.pitch = duration / _audioSource.clip.length;
|
|
|
|
_audioSource.Play();
|
2024-02-19 21:00:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
_timeElapsed += Time.deltaTime * (state ? 1 : -1);
|
|
|
|
_timeElapsed = Mathf.Clamp(_timeElapsed, 0, duration);
|
|
|
|
|
|
|
|
float progress = _timeElapsed / duration;
|
|
|
|
|
2024-03-16 23:54:50 +03:00
|
|
|
if (progress >= 1f)
|
|
|
|
{
|
|
|
|
_audioSource.Stop();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var t = oCurva.Evaluate(progress);
|
|
|
|
|
|
|
|
_posX = Mathf.Lerp(_originX, _originX + offset, t);
|
2024-02-19 21:00:36 +03:00
|
|
|
// _posX = Mathf.Lerp(_originX, _originX + offset, Mathf.SmoothStep(0, 1, progress));
|
2024-03-16 23:54:50 +03:00
|
|
|
|
|
|
|
if (_audioSource.clip)
|
|
|
|
{
|
|
|
|
_audioSource.timeSamples = Mathf.FloorToInt(Mathf.Abs(_audioSource.clip.samples * t));
|
|
|
|
}
|
2024-02-19 21:00:36 +03:00
|
|
|
|
|
|
|
for (int i = 1; i <= doorList.Count; i++)
|
|
|
|
{
|
|
|
|
float side = i % 2 == 0 ? -1 : 1;
|
|
|
|
var pos = doorList[i - 1].localPosition;
|
|
|
|
|
|
|
|
pos.x = _posX * side;
|
|
|
|
doorList[i - 1].localPosition = pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|