revival/game/Assets/Scripts/Artefacts/PickUpDisplay.cs

204 lines
4.8 KiB
C#
Raw Normal View History

using System;
2021-03-11 20:34:32 +01:00
using System.Collections;
2021-03-01 19:09:21 +01:00
using Ktyl.Util;
using System.Collections.Generic;
2021-03-11 20:34:32 +01:00
using FMODUnity;
using TMPro;
2021-03-01 19:09:21 +01:00
using UnityEngine;
2021-03-02 17:33:31 +01:00
using UnityEngine.InputSystem;
2021-03-11 20:34:32 +01:00
using UnityEngine.Serialization;
2021-03-02 17:33:31 +01:00
using UnityEngine.UI;
2021-03-01 19:09:21 +01:00
public class PickUpDisplay : MonoBehaviour
{
2021-03-11 20:34:32 +01:00
[System.Serializable]
struct FadeElement
{
public float Delay;
public float Duration;
public CanvasGroup Renderer;
public float GetAlpha( float t )
=> Mathf.Clamp01( ( t - Delay ) / Duration );
public float GetAlphaInverse( float t )
=> 1f - GetAlpha( t );
}
[FormerlySerializedAs( "_elements" )] [SerializeField]
private FadeElement[] _fadeInElements;
[SerializeField]
private FadeElement[] _fadeOutElements;
2021-03-01 19:09:21 +01:00
2021-03-11 20:34:32 +01:00
[SerializeField]
private float _dialogueDelay;
[SerializeField]
[EventRef]
private string _unlockedSfx;
[SerializeField]
private float _unlockedSfxDelay;
2021-03-01 19:09:21 +01:00
[SerializeField] private GameObject artefactUI;
2021-03-11 20:34:32 +01:00
[SerializeField] private TMP_Text artefactText;
[SerializeField] private DialogueSystem dialogue;
[SerializeField] private ArtefactSystem artefacts;
2021-03-11 14:31:35 +01:00
[SerializeField] private ArtefactPreview _preview;
2021-03-01 19:09:21 +01:00
2021-03-11 14:12:19 +01:00
[SerializeField]
private GameEvent _uiOpen;
[SerializeField]
private GameEvent _uiClose;
2021-03-11 20:34:32 +01:00
private Artefact _chosenArtefact;
private Coroutine _currentFade;
2021-03-02 17:33:31 +01:00
private void Start()
2021-03-01 19:09:21 +01:00
{
EventHandler.current.onArtefactUI += PopUpOn;
}
2021-03-02 17:33:31 +01:00
2021-03-11 20:34:32 +01:00
private IEnumerator AnimateIn()
{
_uiOpen.Raise();
artefactText.text = _chosenArtefact.Name;
// inputSettings.updateMode = (InputSettings.UpdateMode)1;
_preview.Preview( _chosenArtefact.Prefab );
bool isComplete = false;
bool hasSpoken = false;
bool hasClinged = false;
foreach ( var fader in _fadeInElements )
{
fader.Renderer.alpha = 0f;
}
artefactUI.SetActive(true);
yield return null;
float t = 0;
while ( true )
{
t += Time.deltaTime;
isComplete = true;
foreach ( var fader in _fadeInElements )
{
float alpha = fader.GetAlpha( t );
float oneMinusAlpha = 1f - alpha;
fader.Renderer.alpha = 1f - oneMinusAlpha * oneMinusAlpha;
if ( alpha < 1f )
{
isComplete = false;
}
}
if ( t < _dialogueDelay )
{
isComplete = false;
}
else if (!hasSpoken)
{
hasSpoken = true;
dialogue.PlayLine( _chosenArtefact.dialogueKey );
}
if ( t < _unlockedSfxDelay )
{
isComplete = false;
}
else if ( !hasClinged )
{
hasClinged = true;
RuntimeManager.PlayOneShot( _unlockedSfx );
}
if ( isComplete )
break;
yield return null;
}
}
private IEnumerator AnimateOut()
{
yield return null;
bool isComplete = false;
foreach ( var fader in _fadeInElements )
{
fader.Renderer.alpha = 1f;
}
yield return null;
_preview.Dismiss();
_uiClose.Raise();
float t = 0;
while ( true )
{
t += Time.deltaTime;
isComplete = true;
foreach ( var fader in _fadeInElements )
{
float alpha = fader.GetAlphaInverse( t );
float oneMinusAlpha = 1f - alpha;
fader.Renderer.alpha = 1f - oneMinusAlpha * oneMinusAlpha;
if ( alpha > 0f )
{
isComplete = false;
}
}
yield return null;
if ( isComplete )
break;
}
artefactUI.SetActive( false );
_chosenArtefact = null;
yield return null;
}
private void Update()
2021-03-02 17:33:31 +01:00
{
var artefact = artefacts.GetNearbyArtefact();
if (!artefact) return;
2021-03-11 20:34:32 +01:00
_chosenArtefact = artefact;
2021-03-02 17:33:31 +01:00
}
2021-03-01 19:09:21 +01:00
public void PopUpOn()
{
2021-03-11 20:34:32 +01:00
if (_currentFade != null)
StopCoroutine( _currentFade );
2021-03-11 14:12:19 +01:00
2021-03-11 20:34:32 +01:00
_currentFade = StartCoroutine( AnimateIn() );
2021-03-01 19:09:21 +01:00
}
public void PopUpOff()
{
2021-03-11 20:34:32 +01:00
if (_currentFade != null)
StopCoroutine( _currentFade );
2021-03-11 14:12:19 +01:00
2021-03-11 20:34:32 +01:00
_currentFade = StartCoroutine( AnimateOut() );
// inputSettings.updateMode = (InputSettings.UpdateMode)2;
2021-03-01 19:09:21 +01:00
}
}