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

102 lines
2.7 KiB
C#

using Ktyl.Util;
using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using Ktyl.Util;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
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;
public Material dissolve;
[SerializeField] private Renderer _renderer;
[SerializeField] private SerialFloat objectTimeScale;
[SerializeField] private TrapSettings _settings;
[SerializeField] private GameObject _graphics;
private Rigidbody rb;
private Vector3 initialPosition;
private bool _triggered = false;
private void Start()
{
initialPosition = transform.position;
// Get Rigidbody component.
rb = GetComponent<Rigidbody>();
}
private void LateUpdate()
{
if (!_triggered) return;
if (_settings.FallawayFloor.CanRespawn)
{
Reset();
// pop in animation
transform.localScale = Vector3.zero;
transform
.DOScale(Vector3.one, 0.5f)
.SetEase(_settings.FallawayFloor.PopInEase);
}
}
private void OnTriggerEnter(Collider other)
{
if (!_triggered && other.CompareTag("Player"))
{
// Start the Destroy floor coroutine and switch to the dissolve material.
if (objectTimeScale != 0)
{
StartCoroutine(Fall());
_renderer.material = dissolve;
}
}
}
//The platform gets destroyed after the player resumes frozen time on a platform
private void OnTriggerStay(Collider other)
{
if (!_triggered && other.CompareTag("Player"))
{
// Start the Destroy floor coroutine and switch to the dissolve material.
if (objectTimeScale != 0)
{
StartCoroutine(Fall());
_renderer.material = dissolve;
}
}
}
private IEnumerator Fall()
{
_graphics.transform.DOShakePosition(
fallAwayTime,
_settings.FallawayFloor.ShakeStrength);
FMODUnity.RuntimeManager.PlayOneShot(_settings.FallawayFloor.FMODEvent);
_triggered = true;
// wait a moment
yield return new WaitForSeconds(fallAwayTime);
// fall
rb.velocity = Vector3.down * speed;
}
public void Reset()
{
_triggered = false;
transform.position = initialPosition;
rb.velocity = Vector3.zero;
}
}