Implement more QoL features and rebalances
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
using System.Collections.Generic;
|
||||
using KernelPanic.Combat;
|
||||
using KernelPanic.Core;
|
||||
using UnityEngine;
|
||||
|
||||
namespace KernelPanic.UI
|
||||
{
|
||||
/// <summary>Short synthesized terminal cues. Observes gameplay; never delays it or consumes RNG.</summary>
|
||||
[DisallowMultipleComponent]
|
||||
public sealed class CombatAudio : MonoBehaviour
|
||||
{
|
||||
private AudioSource _source;
|
||||
private readonly Dictionary<string, AudioClip> _clips = new();
|
||||
private readonly Dictionary<string, float> _lastPlayed = new();
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_source = gameObject.AddComponent<AudioSource>();
|
||||
_source.playOnAwake = false;
|
||||
_source.spatialBlend = 0;
|
||||
if (FindFirstObjectByType<AudioListener>() == null) gameObject.AddComponent<AudioListener>();
|
||||
CreateCue("dispatch", 420, 700, .07f);
|
||||
CreateCue("resolve", 660, 920, .10f);
|
||||
CreateCue("shield", 240, 70, .13f);
|
||||
CreateCue("hit", 120, 55, .08f);
|
||||
CreateCue("revive", 160, 540, .24f);
|
||||
CreateCue("clear", 520, 1040, .26f);
|
||||
CreateCue("victory", 660, 1320, .48f);
|
||||
CreateCue("defeat", 220, 55, .35f);
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
GameEvents.CardPlayed += OnPlayed;
|
||||
GameEvents.CardResolving += OnResolving;
|
||||
GameEvents.DamageDealt += OnDamage;
|
||||
GameEvents.EnemyRevived += OnRevived;
|
||||
GameEvents.WaveCleared += OnClear;
|
||||
GameEvents.RunEnded += OnEnded;
|
||||
UIPreferences.Changed += OnPreferences;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
GameEvents.CardPlayed -= OnPlayed;
|
||||
GameEvents.CardResolving -= OnResolving;
|
||||
GameEvents.DamageDealt -= OnDamage;
|
||||
GameEvents.EnemyRevived -= OnRevived;
|
||||
GameEvents.WaveCleared -= OnClear;
|
||||
GameEvents.RunEnded -= OnEnded;
|
||||
UIPreferences.Changed -= OnPreferences;
|
||||
if (_source != null) _source.Stop();
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
foreach (var clip in _clips.Values) Destroy(clip);
|
||||
if (_source != null) Destroy(_source);
|
||||
}
|
||||
|
||||
private void OnPreferences() { if (UIPreferences.SoundVolume == 0) _source?.Stop(); }
|
||||
private void OnPlayed(CardPlayedEvent _) => Play("dispatch");
|
||||
private void OnResolving(CardResolvedEvent e) { if (e.Track != ResolutionTrack.Native) Play("resolve"); }
|
||||
private void OnDamage(DamageDealtEvent e)
|
||||
{
|
||||
if (e.ShieldDamage > 0 && e.Target != null && e.Target.Shield == 0) Play("shield");
|
||||
else if (e.UptimeDamage > 0) Play("hit");
|
||||
}
|
||||
private void OnRevived(EnemyInstance _) => Play("revive");
|
||||
private void OnClear(WaveClearedEvent _) => Play("clear");
|
||||
private void OnEnded(RunEndedEvent e) => Play(e.PlayerDied ? "defeat" : "victory");
|
||||
|
||||
private void Play(string cue)
|
||||
{
|
||||
if (_source == null || UIPreferences.SoundVolume <= 0) return;
|
||||
// Bound dense multi-hit sequences without dropping different kinds of feedback.
|
||||
if (_lastPlayed.TryGetValue(cue, out float last) && Time.unscaledTime - last < .075f) return;
|
||||
_lastPlayed[cue] = Time.unscaledTime;
|
||||
_source.PlayOneShot(_clips[cue], UIPreferences.SoundVolume / 100f);
|
||||
}
|
||||
|
||||
private void CreateCue(string name, float from, float to, float duration)
|
||||
{
|
||||
const int sampleRate = 22050;
|
||||
float[] samples = new float[Mathf.CeilToInt(sampleRate * duration)];
|
||||
double phase = 0;
|
||||
for (int i = 0; i < samples.Length; i++)
|
||||
{
|
||||
float t = i / (float)samples.Length;
|
||||
phase += 2 * System.Math.PI * Mathf.Lerp(from, to, t) / sampleRate;
|
||||
float envelope = Mathf.Min(1, t * 40) * (1 - t) * (1 - t);
|
||||
samples[i] = .16f * envelope * (float)(System.Math.Sin(phase) + .2 * System.Math.Sin(phase * 2));
|
||||
}
|
||||
var clip = AudioClip.Create("terminal-" + name, samples.Length, 1, sampleRate, false);
|
||||
clip.SetData(samples, 0);
|
||||
_clips.Add(name, clip);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user