lucid-super-dream/Assets/TunnelController.cs

91 lines
2.9 KiB
C#
Raw Normal View History

2021-01-09 19:18:55 +01:00
using System;
using System.Collections;
using System.Collections.Generic;
using Ktyl.Util;
using UnityEngine;
2021-01-10 06:55:45 +01:00
public class TunnelController : MonoBehaviour
2021-01-09 19:18:55 +01:00
{
[SerializeField] private SerialFloat _intensity;
[SerializeField] private SerialFloat _duration;
[SerializeField] private AnimationCurve _anim;
2021-01-10 06:55:45 +01:00
[SerializeField] private SerialFloat _playerXPos;
[SerializeField] private SerialFloat _playerXMove;
[SerializeField] private SerialFloat _baseTubeRadius;
2021-01-09 19:18:55 +01:00
2021-01-10 18:54:04 +01:00
[SerializeField] private AccessibilityOptions _accessibility;
[SerializeField] private float _baseCameraShake;
[SerializeField] private SerialFloat _distanceToNextBeat;
[SerializeField] private Renderer _calm;
[SerializeField] private Renderer _vibe;
[SerializeField] private Renderer _party;
public Renderer Active { get; private set; }
2021-01-09 19:18:55 +01:00
private float _start = -1;
2021-01-11 11:01:41 +01:00
private static readonly int PlayerXMove = Shader.PropertyToID("_PlayerXMove");
private static readonly int PlayerXPos = Shader.PropertyToID("_PlayerXPos");
private static readonly int CameraShake = Shader.PropertyToID("_CameraShake");
2021-01-09 19:18:55 +01:00
private void Awake()
{
2021-01-11 11:01:41 +01:00
Shader.SetGlobalFloat(CameraShake, 0);
Shader.SetGlobalFloat("_BaseTubeRadius", _baseTubeRadius);
2021-01-10 18:54:04 +01:00
Debug.Log(_accessibility.Mode);
switch (_accessibility.Mode)
{
case AccessibilityMode.Calm:
_calm.enabled = true;
Active = _calm;
_vibe.enabled = false;
_party.enabled = false;
break;
case AccessibilityMode.Vibe:
_vibe.enabled = true;
Active = _vibe;
_calm.enabled = false;
_party.enabled = false;
break;
case AccessibilityMode.Party:
_party.enabled = true;
Active = _party;
_calm.enabled = false;
_vibe.enabled = false;
break;
default:
throw new ArgumentOutOfRangeException();
}
2021-01-09 19:18:55 +01:00
}
2021-01-11 11:01:41 +01:00
public void Reset()
{
Awake();
}
2021-01-09 19:18:55 +01:00
void LateUpdate()
{
2021-01-10 18:54:04 +01:00
var cameraShakeIntensity = _intensity * _accessibility.Intensity.Value;
2021-01-11 11:01:41 +01:00
Shader.SetGlobalFloat(PlayerXMove, _playerXMove);
Shader.SetGlobalFloat(PlayerXPos, _playerXPos);
Shader.SetGlobalFloat(CameraShake, _baseCameraShake * _distanceToNextBeat * cameraShakeIntensity);
2021-01-10 06:55:45 +01:00
2021-01-09 19:18:55 +01:00
var elapsed = Time.time - _start;
if (elapsed > _duration) return;
var normalisedElapsed = elapsed / _duration;
2021-01-10 18:54:04 +01:00
var shake = Mathf.Max(_anim.Evaluate(normalisedElapsed), _baseCameraShake) * _intensity;
2021-01-11 11:01:41 +01:00
Shader.SetGlobalFloat(CameraShake, shake);
2021-01-09 19:18:55 +01:00
}
2021-01-10 06:55:45 +01:00
public void LoseLifeShake()
2021-01-09 19:18:55 +01:00
{
_start = Time.time;
}
}