HeavenStudio/Assets/Scripts/Games/Global/Flash.cs
minenice55 b1fab52ad9
Integration of Jukebox Library (#451) (#457)
* add Jukebox library

todo:
- saving / loading of new format
- inferrence of unknown data like past versions
- move the temporary float casts to proper use of double
- make sound related functions take double for timing
- inform people that the Jukebox sound player was renamed to SoundByte lol

* make sound, input scheduling, and super curve use double precision

* successfully load charts

* editor works again

v1 riqs can be saved and loaded

* first tempo and volume markers are unmovable

fix loading of charts' easing values

* use gsync / freesync

* update Jukebox refs to SoundByte

* game events use double part 1

Air Rally - Glee Club converted

* don't load song if chart load fails

* finish conversion of all minigames

* remove editor waveform toggle

* timeline now respects added song offset length

clear cache files on app close
prepped notes for dsp sync

* update timeline length when offset changed

* update to latest Jukebox

* make error panel object in global game manager

* improve conductor music scheduling

* added error message box

fix first game events sometimes not playing
2023-06-10 19:17:06 +00:00

111 lines
3.9 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using HeavenStudio.Util;
using System.Linq;
using Jukebox;
using Jukebox.Legacy;
namespace HeavenStudio.Games.Global
{
public class Flash : MonoBehaviour
{
[NonSerialized] public double startBeat;
[NonSerialized] public float length;
[NonSerialized] public Color startColor;
[NonSerialized] public Color endColor;
[NonSerialized] public Util.EasingFunction.Ease ease;
[NonSerialized] private Util.EasingFunction.Function func;
[NonSerialized] private Image spriteRenderer;
[SerializeField] private Color currentCol;
[NonSerialized] private List<RiqEntity> allFadeEvents = new List<RiqEntity>();
private void Awake()
{
spriteRenderer = GetComponent<Image>();
spriteRenderer.color = currentCol;
func = Util.EasingFunction.GetEasingFunction(Util.EasingFunction.Ease.Linear);
GameManager.instance.onBeatChanged += OnBeatChanged;
}
public void OnBeatChanged(double beat)
{
allFadeEvents = EventCaller.GetAllInGameManagerList("vfx", new string[] { "flash" });
// backwards-compatibility baybee
allFadeEvents.AddRange(EventCaller.GetAllInGameManagerList("gameManager", new string[] { "flash" }));
allFadeEvents.Sort((x, y) => x.beat.CompareTo(y.beat));
FindFadeFromBeat(beat);
}
private void FindFadeFromBeat(double beat)
{
Color startCol = Color.white;
Color endCol = Color.white;
bool override_ = false;
if (allFadeEvents.Count > 0)
{
RiqEntity startEntity = default(RiqEntity);
for (int i = 0; i < allFadeEvents.Count; i++)
{
if (allFadeEvents[i].beat <= beat)
{
startEntity = allFadeEvents[i];
}
else if (i == 0 && allFadeEvents[i].beat > beat)
{
startEntity = allFadeEvents[i];
override_ = true;
startCol = new Color(1, 1, 1, 0);
endCol = new Color(1, 1, 1, 0);
}
}
if (!string.IsNullOrEmpty(startEntity.datamodel))
{
if (!override_)
{
Color colA = startEntity["colorA"];
Color colB = startEntity["colorB"];
startCol = new Color(colA.r, colA.g, colA.b, startEntity["valA"]);
endCol = new Color(colB.r, colB.g, colB.b, startEntity["valB"]);
}
SetFade(startEntity.beat, startEntity.length, startCol, endCol, (Util.EasingFunction.Ease) startEntity["ease"]);
}
}
}
public void SetFade(double beat, float length, Color startCol, Color endCol, Util.EasingFunction.Ease ease)
{
this.startBeat = beat;
this.length = length;
this.startColor = startCol;
this.endColor = endCol;
this.ease = ease;
func = Util.EasingFunction.GetEasingFunction(ease);
}
private void Update()
{
FindFadeFromBeat(Conductor.instance.songPositionInBeatsAsDouble);
float normalizedBeat = Conductor.instance.GetPositionFromBeat(startBeat, length);
// normalizedBeat = Mathf.Clamp01(normalizedBeat);
currentCol = new Color(func(startColor.r, endColor.r, normalizedBeat), func(startColor.g, endColor.g, normalizedBeat), func(startColor.b, endColor.b, normalizedBeat), func(startColor.a, endColor.a, normalizedBeat));
spriteRenderer.color = currentCol;
}
}
}