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

113 lines
2.6 KiB
C#
Raw Normal View History

2021-03-13 03:01:20 +01:00
using Ktyl.Util;
2021-03-05 17:24:50 +01:00
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;
2021-05-10 17:58:47 +02:00
2021-03-02 20:47:28 +01:00
// Time it takes for ogjecct to begin moving towards the ground.
public float fallAwayTime;
2021-05-10 17:58:47 +02:00
[SerializeField] private TrapSettings _settings;
[SerializeField] private GameObject _graphics;
2021-05-10 17:58:47 +02:00
private Vector3 initialPosition;
2021-05-10 17:58:47 +02:00
private Vector3 velocity;
2021-03-24 17:54:34 +01:00
public bool Falling => _triggered;
private bool _triggered = false;
private void Start()
{
initialPosition = transform.position;
}
private void LateUpdate()
{
2021-03-24 17:54:34 +01:00
if (!_triggered)
{
transform.position = initialPosition;
return;
}
2021-03-23 16:03:18 +01:00
if (_settings.SafeTime > _settings.FallawayFloor.SafeResetTime)
{
Reset();
2021-03-17 19:37:56 +01:00
// pop in animation
transform.localScale = Vector3.zero;
transform
.DOScale(Vector3.one, 0.5f)
.SetEase(_settings.FallawayFloor.PopInEase);
}
}
2021-05-10 17:58:47 +02:00
private void FixedUpdate()
{
transform.position += velocity * Time.fixedDeltaTime * _settings.ObjectTimeScale;
}
private void OnTriggerEnter(Collider other)
{
if (!_triggered && other.CompareTag("Player"))
{
2021-03-24 17:54:34 +01:00
Fall();
2021-03-13 03:01:20 +01:00
}
}
//The platform gets destroyed after the player resumes frozen time on a platform
private void OnTriggerStay(Collider other)
{
2021-03-24 13:45:19 +01:00
if (!_triggered && other.CompareTag("Player"))
2021-03-13 03:01:20 +01:00
{
2021-03-24 17:54:34 +01:00
Fall();
}
}
2021-03-24 17:54:34 +01:00
public void Fall()
{
2021-03-24 17:54:34 +01:00
// already falling
if (_triggered) return;
2021-05-10 17:58:47 +02:00
2021-03-24 17:54:34 +01:00
// time stop
2021-05-10 17:58:47 +02:00
if (_settings.ObjectTimeScale.AsBool) return;
2021-03-24 17:54:34 +01:00
StartCoroutine(FallCR());
}
2021-05-10 17:58:47 +02:00
2021-03-24 17:54:34 +01:00
private IEnumerator FallCR()
{
_triggered = true;
2021-05-10 17:58:47 +02:00
var shake = _graphics.transform.DOShakePosition(
fallAwayTime,
_settings.FallawayFloor.ShakeStrength);
FMODUnity.RuntimeManager.PlayOneShot(_settings.FallawayFloor.FMODEvent);
2021-05-10 17:58:47 +02:00
// wait a moment
2021-03-02 20:47:28 +01:00
yield return new WaitForSeconds(fallAwayTime);
2021-05-10 17:58:47 +02:00
float elapsed = 0f;
while (elapsed < fallAwayTime)
{
var dt = _settings.ObjectTimeScale;
elapsed += Time.deltaTime * dt;
yield return null;
}
// fall
2021-05-10 17:58:47 +02:00
velocity = Vector3.down * speed;
2021-03-10 18:41:09 +01:00
}
public void Reset()
{
_triggered = false;
transform.position = initialPosition;
2021-05-10 17:58:47 +02:00
velocity = Vector3.zero;
}
2021-05-10 17:58:47 +02:00
}