HeavenStudioPlus/Assets/Scripts/Games/CallAndResponseHandler.cs
minenice55 afc665ed61 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

178 lines
6.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Starpelly;
namespace HeavenStudio.Games
{
public class CallAndResponseHandler
{
public struct CallAndResponseEventParam
{
public string propertyName;
public dynamic value;
public CallAndResponseEventParam(string propertyName, dynamic value)
{
this.propertyName = propertyName;
this.value = value;
}
}
public class CallAndResponseEvent
{
public double beat;
public float length;
public double relativeBeat; // this beat is relative to the intervalStartBeat
public Dictionary<string, dynamic> DynamicData; //if you need more properties for your queued event
public string tag;
public CallAndResponseEvent(double beat, double relativeBeat, string tag, float length = 0)
{
this.beat = beat;
this.length = length;
this.relativeBeat = relativeBeat;
DynamicData = new Dictionary<string, dynamic>();
this.tag = tag;
this.length = length;
}
public void CreateProperty(string name, dynamic defaultValue)
{
if (!DynamicData.ContainsKey(name))
{
DynamicData.Add(name, defaultValue);
}
}
public dynamic this[string propertyName]
{
get
{
if (DynamicData.ContainsKey(propertyName))
{
return DynamicData[propertyName];
}
else
{
Debug.LogWarning("This property does not exist on this callAndResponse event.");
return null;
}
}
set
{
if (DynamicData.ContainsKey(propertyName))
{
DynamicData[propertyName] = value;
}
else
{
Debug.LogError($"This callAndRespone event does not have a property named {propertyName}! Attempted to insert value of type {value.GetType()}");
}
}
}
}
public double intervalStartBeat = -1; // the first beat of the interval
public float intervalLength = -1; // the duration of the interval in beats
public float defaultIntervalLength; // when an event is queued and the interval has not started yet, it will use this as the interval length.
public CallAndResponseHandler(float defaultIntervalLength)
{
this.defaultIntervalLength = defaultIntervalLength;
}
public List<CallAndResponseEvent> queuedEvents = new List<CallAndResponseEvent>();
/// <summary>
/// Returns the normalized progress of the interval
/// </summary>
public float GetIntervalProgress(float lengthOffset = 0)
{
return Conductor.instance.GetPositionFromBeat(intervalStartBeat, intervalLength - lengthOffset);
}
public float GetIntervalProgressFromBeat(double beat, float lengthOffset = 0)
{
return (float)((beat - intervalStartBeat) / ((intervalStartBeat + intervalLength - lengthOffset) - intervalStartBeat));
}
/// <summary>
/// Is the interval currently on-going?
/// </summary>
public bool IntervalIsActive()
{
float progress = GetIntervalProgress();
return progress >= 0 && progress <= 1;
}
/// <summary>
/// Starts the interval.
/// </summary>
/// <param name="beat">The interval start beat.</param>
/// <param name="length">The length of the interval.</param>
public void StartInterval(double beat, float length)
{
if (!IntervalIsActive())
{
if (queuedEvents.Count > 0) queuedEvents.Clear();
}
intervalStartBeat = beat;
intervalLength = length;
defaultIntervalLength = length;
}
/// <summary>
/// Adds an event to the queued events list.
/// </summary>
/// <param name="beat">The current beat.</param>
/// <param name="crParams">Extra properties to add to the event.</param>
/// <param name="ignoreInterval">If true, this function will not start a new interval if the interval isn't active.</param>
/// <param name="overrideInterval">If true, overrides the current interval.</param>
public void AddEvent(double beat, float length = 0, string tag = "", List<CallAndResponseEventParam> crParams = null, bool ignoreInterval = false, bool overrideInterval = false)
{
if ((!IntervalIsActive() && !ignoreInterval) || overrideInterval)
{
StartInterval(beat, defaultIntervalLength);
}
CallAndResponseEvent addedEvent = new CallAndResponseEvent(beat, beat - intervalStartBeat, tag, length);
if (crParams != null && crParams.Count > 0)
{
foreach (var param in crParams)
{
addedEvent.CreateProperty(param.propertyName, param.value);
}
}
queuedEvents.Add(addedEvent);
}
/// <summary>
/// Check if an event exists at beat.
/// </summary>
/// <param name="beat">The beat to check.</param>
public bool EventExistsAtBeat(double beat)
{
if (queuedEvents.Count == 0)
{
return false;
}
CallAndResponseEvent foundEvent = queuedEvents.Find(x => x.beat == beat);
return foundEvent != null;
}
/// <summary>
/// Check if an event exists at relativeBeat.
/// </summary>
/// <param name="beat">The beat to check.</param>
public bool EventExistsAtRelativetBeat(double relativeBeat)
{
if (queuedEvents.Count == 0)
{
return false;
}
CallAndResponseEvent foundEvent = queuedEvents.Find(x => x.relativeBeat == relativeBeat);
return foundEvent != null;
}
}
}