revival/game/Assets/Scripts/Traps/FallawayFloor.cs

113 lines
2.6 KiB
C#

using Ktyl.Util;
using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
public class FallawayFloor : MonoBehaviour
{
// // Speed at which the object moves towards the ground.
public float speed;
// Time it takes for ogjecct to begin moving towards the ground.
public float fallAwayTime;
[SerializeField] private TrapSettings _settings;
[SerializeField] private GameObject _graphics;
private Vector3 initialPosition;
private Vector3 velocity;
public bool Falling => _triggered;
private bool _triggered = false;
private void Start()
{
initialPosition = transform.position;
}
private void LateUpdate()
{
if (!_triggered)
{
transform.position = initialPosition;
return;
}
if (_settings.SafeTime > _settings.FallawayFloor.SafeResetTime)
{
Reset();
// pop in animation
transform.localScale = Vector3.zero;
transform
.DOScale(Vector3.one, 0.5f)
.SetEase(_settings.FallawayFloor.PopInEase);
}
}
private void FixedUpdate()
{
transform.position += velocity * Time.fixedDeltaTime * _settings.ObjectTimeScale;
}
private void OnTriggerEnter(Collider other)
{
if (!_triggered && other.CompareTag("Player"))
{
Fall();
}
}
//The platform gets destroyed after the player resumes frozen time on a platform
private void OnTriggerStay(Collider other)
{
if (!_triggered && other.CompareTag("Player"))
{
Fall();
}
}
public void Fall()
{
// already falling
if (_triggered) return;
// time stop
if (_settings.ObjectTimeScale.AsBool) return;
StartCoroutine(FallCR());
}
private IEnumerator FallCR()
{
_triggered = true;
var shake = _graphics.transform.DOShakePosition(
fallAwayTime,
_settings.FallawayFloor.ShakeStrength);
FMODUnity.RuntimeManager.PlayOneShot(_settings.FallawayFloor.FMODEvent);
// wait a moment
yield return new WaitForSeconds(fallAwayTime);
float elapsed = 0f;
while (elapsed < fallAwayTime)
{
var dt = _settings.ObjectTimeScale;
elapsed += Time.deltaTime * dt;
yield return null;
}
// fall
velocity = Vector3.down * speed;
}
public void Reset()
{
_triggered = false;
transform.position = initialPosition;
velocity = Vector3.zero;
}
}