Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/UpdatedMusic'
Browse files Browse the repository at this point in the history
# Conflicts:
#	Assets/HandMadeGame/Code/GameFlow.cs
  • Loading branch information
PathogenDavid committed Sep 18, 2024
2 parents ae1cccf + 43bcc23 commit dc259b4
Show file tree
Hide file tree
Showing 16 changed files with 3,687 additions and 22 deletions.
Binary file modified Assets/HandMadeGame/Audio/MUS_Decorate_LOOP.ogg
Binary file not shown.
Binary file modified Assets/HandMadeGame/Audio/MUS_Explore_Buzzard_LOOP.ogg
Binary file not shown.
Binary file modified Assets/HandMadeGame/Audio/MUS_Explore_Crow_LOOP.ogg
Binary file not shown.
Binary file modified Assets/HandMadeGame/Audio/MUS_Explore_LOOP.ogg
Binary file not shown.
Binary file modified Assets/HandMadeGame/Audio/MUS_Explore_Partridge_LOOP.ogg
Binary file not shown.
Binary file modified Assets/HandMadeGame/Audio/MUS_Menu_IN.ogg
Binary file not shown.
Binary file modified Assets/HandMadeGame/Audio/MUS_Menu_LOOP.ogg
Binary file not shown.
96 changes: 75 additions & 21 deletions Assets/HandMadeGame/Code/Audio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
public class Audio : MonoBehaviour
{
public static Audio Singleton;
private float AudioVolume;
private float MusicVolume = 0;
private float SFXVolume = 0;
private AudioSource[] MusicSources;
private AudioSource SfxSource, SfxSourcePitched;
private int ActiveMusicSourceIndex;
private bool MusicCrossfadeActive;
private bool QueueVolumeLock = false;
private float QueueCrossoverTime;

private void Awake()
{
Expand Down Expand Up @@ -37,42 +40,84 @@ private void Awake()
NewSfxSourcePitched.transform.parent = transform;
}

// This will force stop all sound effects
public void StopAllSFX() => SfxSource.Stop();
// This will force stop all sound effects
public void StopAllSFX() => SfxSource.Stop();

public void StopMusic(float _fade = 0)
{
ActiveMusicSourceIndex = 1 - ActiveMusicSourceIndex;
StartCoroutine(AnimateMusicCrossfade(_fade));
}

public static void SetMusicVolume(int Vol) => Audio.Singleton.SetMusicVolumeSingle(Vol);
public void SetMusicVolumeSingle(int Vol)
{
Vol = Mathf.Clamp(Vol, 0, 10);
MusicVolume = (float)Vol / 10;
if (!MusicCrossfadeActive)
{
MusicSources[ActiveMusicSourceIndex].volume = MusicVolume;
MusicSources[1 - ActiveMusicSourceIndex].volume = 0;
}

public static void SetAudioVolume(int Vol) => Audio.Singleton.SetAudioVolumeSingle(Vol);
public void SetAudioVolumeSingle(int Vol)
}

public static void SetSFXVolume(int Vol) => Audio.Singleton.SetSFXVolumeSingle(Vol);
public void SetSFXVolumeSingle(int Vol)
{
Vol = Mathf.Clamp(Vol, 0, 10);
MusicVolume = (float)Vol / 10;
if (!MusicCrossfadeActive)
{
Vol = Mathf.Clamp(Vol, 0, 10);
AudioVolume = (float)Vol / 10;
if (!MusicCrossfadeActive)
{
MusicSources[ActiveMusicSourceIndex].volume = AudioVolume;
MusicSources[1 - ActiveMusicSourceIndex].volume = 0;
}
MusicSources[ActiveMusicSourceIndex].volume = MusicVolume;
MusicSources[1 - ActiveMusicSourceIndex].volume = 0;
}
}

// Returns the currently playing song
public static AudioClip CurrentSong() { return Audio.Singleton.CurrentSongSingle(); }
// Returns the currently playing song
public static AudioClip CurrentSong() { return Audio.Singleton.CurrentSongSingle(); }
public AudioClip CurrentSongSingle() { return MusicSources[ActiveMusicSourceIndex].clip; }
public float CurrentSongPosition() { return MusicSources[ActiveMusicSourceIndex].time; }
public void PauseCurrentSong() { MusicSources[ActiveMusicSourceIndex].Pause(); }
public void UnpauseCurrentSong() { MusicSources[ActiveMusicSourceIndex].UnPause(); }

public static void QueueNextSong(AudioClip _clip) => Audio.Singleton.QueueNextSongSingle(_clip);
public void QueueNextSongSingle(AudioClip _clip)
{
QueueVolumeLock = true;
MusicSources[ActiveMusicSourceIndex].loop = false;
QueueCrossoverTime = Time.time + MusicSources[ActiveMusicSourceIndex].clip.length;
MusicSources[1 - ActiveMusicSourceIndex].clip = _clip;
MusicSources[1 - ActiveMusicSourceIndex].PlayDelayed(MusicSources[ActiveMusicSourceIndex].clip.length - 0.06f); // Hard coded time to take into account song loading? It works OK enough
MusicSources[1 - ActiveMusicSourceIndex].volume = MusicVolume;
}

// This will load the referenced AudioClip and crossfade between them.
// This also starts the music at the same timestamp as the currently playing song.
// Great for music that has different layers!
public static void PlayMusic(AudioClip _clip, float _fade = 1) => Audio.Singleton.PlayMusicSingle(_clip, _fade);
public void PlayMusicSingle(AudioClip _clip, float _fade = 1)
{
// If the requested audio is different from the currently playing audio, start fade
if (MusicSources[ActiveMusicSourceIndex].clip != _clip)

// This generates two new gameobjects and attaches an AudioSource component to it
for (int i = 0; i < 2; i++)
{
MusicSources[i].loop = true;
}
if (QueueVolumeLock)
{
QueueVolumeLock = false;
if (QueueCrossoverTime < Time.time)
{
ActiveMusicSourceIndex = 1 - ActiveMusicSourceIndex;
MusicSources[ActiveMusicSourceIndex].volume = 0;
} else
{
MusicSources[1-ActiveMusicSourceIndex].volume = 0;
}
}

// If the requested audio is different from the currently playing audio, start fade
if (MusicSources[ActiveMusicSourceIndex].clip != _clip)
{
ActiveMusicSourceIndex = 1 - ActiveMusicSourceIndex;
MusicSources[ActiveMusicSourceIndex].clip = _clip;
Expand All @@ -95,13 +140,13 @@ public void PlayMusicSingle(AudioClip _clip, float _fade = 1)

// Plays a one-shot sound
public static void PlaySound(AudioClip _clip) { Audio.Singleton.PlaySoundSingle(_clip); }
public void PlaySoundSingle(AudioClip _clip) { SfxSource.PlayOneShot(_clip, AudioVolume); }
public void PlaySoundSingle(AudioClip _clip) { SfxSource.PlayOneShot(_clip, SFXVolume); }

public static void PlaySoundPitched(AudioClip _clip, float PitchMod) { Audio.Singleton.PlaySoundPitchedSingle(_clip, PitchMod); }
public void PlaySoundPitchedSingle(AudioClip _clip, float PitchMod)
{
SfxSourcePitched.pitch = PitchMod;
SfxSourcePitched.PlayOneShot(_clip, AudioVolume);
SfxSourcePitched.PlayOneShot(_clip, SFXVolume);
}

public static void StopSFX() { Audio.Singleton.StopSFXSingle(); }
Expand All @@ -115,11 +160,20 @@ private IEnumerator AnimateMusicCrossfade(float _duration)
while (Percent < 1)
{
Percent += Time.deltaTime / _duration;
MusicSources[ActiveMusicSourceIndex].volume = Mathf.Lerp(0, AudioVolume, Percent);
MusicSources[1 - ActiveMusicSourceIndex].volume = Mathf.Lerp(AudioVolume, 0, Percent);
MusicSources[ActiveMusicSourceIndex].volume = Mathf.Lerp(0, MusicVolume, Percent);
if (!QueueVolumeLock)
{
MusicSources[1 - ActiveMusicSourceIndex].volume = Mathf.Lerp(MusicVolume, 0, Percent);
} else
{
MusicSources[1 - ActiveMusicSourceIndex].volume = MusicVolume;
}
yield return new WaitForFixedUpdate();
}
MusicCrossfadeActive = false;
if (!QueueVolumeLock)
{
MusicSources[1 - ActiveMusicSourceIndex].Stop();
}
}
}
7 changes: 6 additions & 1 deletion Assets/HandMadeGame/Code/GameFlow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public static GameFlow Instance
public InfoPopupController InfoPopupController;
public ArrangementModeControllerBase ArrangementModeController;

public AudioClip ExplorationLoop;

private Quest CurrentQuest;

public bool ShowDebugger;
Expand All @@ -40,7 +42,10 @@ private void Awake()
Destroy(this);
return;
}
}

Audio.SetMusicVolume(3);
Audio.PlayMusic(ExplorationLoop);
}

public void HandleInteraction(Quest quest)
{
Expand Down
7 changes: 7 additions & 0 deletions Assets/HandMadeGame/Code/Quest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ public sealed class Quest : MonoBehaviour
{
public Sprite CharacterPortrait;
public int RequiredReputation;
public AudioClip CharacterMusic;
public AudioClip[] CharacterTalkSounds;

[Header("Quest Logistics")]
[Multiline] public string StartDialogue = "HELLO YES I WOULD LIKE SOME DECORATION";
Expand Down Expand Up @@ -54,4 +56,9 @@ private void Awake()
foreach (GameObject gameObject in SpawnOnAccept)
gameObject.SetActive(false);
}

public AudioClip GetRandomTalkSound()
{
return CharacterTalkSounds[UnityEngine.Random.Range(0, CharacterTalkSounds.Length)];
}
}
26 changes: 26 additions & 0 deletions Assets/MainMenuAudioController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MainMenuAudioController : MonoBehaviour
{
public AudioClip MusicIN;
public AudioClip MusicLOOP;
public AudioClip MusicEXPLORE;

public void Start()
{
Audio.SetMusicVolume(4);
Audio.PlayMusic(MusicIN);
Audio.QueueNextSong(MusicLOOP);
}

public void Update()
{
if (Input.anyKey)
{
Debug.Log("NEW SONG");
Audio.PlayMusic(MusicEXPLORE);
}
}
}
11 changes: 11 additions & 0 deletions Assets/MainMenuAudioController.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit dc259b4

Please sign in to comment.