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

129 lines
3.0 KiB
C#
Raw Normal View History

using Ktyl.Util;
using System.Collections;
2021-05-16 18:08:35 +02:00
using PathCreation;
using UnityEngine;
2021-05-17 00:03:42 +02:00
using UnityEngine.Serialization;
2021-05-16 18:08:35 +02:00
#if UNITY_EDITOR
using UnityEditor;
#endif
public class RollingBoulder : MonoBehaviour
{
2021-05-16 18:08:35 +02:00
// how long it takes to travel the length of the path
[SerializeField] private float speed;
2021-05-17 00:03:42 +02:00
[FormerlySerializedAs("boulder")] [SerializeField] private Transform boulderMove;
[SerializeField] private Transform boulderSpin;
2021-05-16 18:08:35 +02:00
[SerializeField] private SerialFloat objectTimeScale;
[SerializeField] private PathCreator path;
[SerializeField] private Killbox _killbox;
[SerializeField] private string _fmodKey;
2021-05-16 21:00:39 +02:00
[SerializeField] private float _resetTime;
2021-05-17 00:03:42 +02:00
[SerializeField] private float _rotationSpeed;
2021-05-16 18:08:35 +02:00
private bool _triggered = false;
2021-05-17 00:03:42 +02:00
private bool _stopped;
2021-05-16 18:08:35 +02:00
private void Update()
{
2021-05-17 00:03:42 +02:00
_killbox.gameObject.SetActive(!_stopped && objectTimeScale > 0.5f);
if (!_stopped)
{
var dt = Time.deltaTime * objectTimeScale;
boulderSpin.transform.Rotate(new Vector3(0,0,_rotationSpeed) * dt);
}
}
private void OnTriggerEnter(Collider other)
{
// Checks to make sure other collider is the Player using tag.
if (other.gameObject.CompareTag("Player"))
{
2021-05-16 18:08:35 +02:00
Trigger();
}
}
2021-05-16 18:08:35 +02:00
public void Trigger()
{
2021-05-16 18:08:35 +02:00
if (_triggered) return;
FMODUnity.RuntimeManager.PlayOneShot(_fmodKey);
StartCoroutine(RollingStone());
}
2021-05-17 00:03:42 +02:00
2021-05-16 18:08:35 +02:00
private IEnumerator RollingStone()
{
_triggered = true;
var d = 0f;
var pathLength = path.path.length;
while (_triggered && d < pathLength)
{
2021-05-16 18:08:35 +02:00
d += Time.deltaTime * objectTimeScale * speed;
var pos = path.path.GetPointAtDistance(d);
2021-05-17 00:03:42 +02:00
boulderMove.transform.position = pos;
2021-05-16 18:08:35 +02:00
yield return null;
}
2021-05-16 18:08:35 +02:00
if (_triggered)
{
2021-05-17 00:03:42 +02:00
_stopped = true;
boulderMove.position = path.path.GetPointAtTime(0.999f);
}
2021-05-16 21:00:39 +02:00
var resetElapsed = 0f;
while (resetElapsed < _resetTime)
{
resetElapsed += Time.deltaTime * objectTimeScale;
yield return null;
}
Reset();
}
2021-05-16 18:08:35 +02:00
public void Reset()
{
2021-05-17 00:03:42 +02:00
boulderMove.transform.position = path.path.GetPointAtTime(0);
2021-05-16 18:08:35 +02:00
_triggered = false;
2021-05-17 00:03:42 +02:00
_killbox.enabled = true;
_stopped = false;
}
}
2021-05-16 18:08:35 +02:00
#if UNITY_EDITOR
[CustomEditor(typeof(RollingBoulder))]
public class RollingBoulderEditor : Editor
{
private RollingBoulder _data;
private void OnEnable()
{
_data = target as RollingBoulder;
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (!Application.isPlaying) return;
if (GUILayout.Button("Trigger"))
{
_data.Trigger();
}
if (GUILayout.Button("Reset"))
{
_data.Reset();
}
}
}
#endif