environment management
This commit is contained in:
85
Assets/Engine/Runtime/Environment/AmbienceAudio.cs
Normal file
85
Assets/Engine/Runtime/Environment/AmbienceAudio.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using NaughtyAttributes;
|
||||
using UnityEngine;
|
||||
|
||||
namespace EscapeRoomEngine.Engine.Runtime.Environment
|
||||
{
|
||||
[RequireComponent(typeof(AudioSource))]
|
||||
public class AmbienceAudio : MonoBehaviour
|
||||
{
|
||||
public static AmbienceAudio Instance { get; private set; }
|
||||
|
||||
public float fadeDuration = 0.5f;
|
||||
public AmbienceClip initialAmbience;
|
||||
[BoxGroup("Internal")] [SerializeField] private AudioSource[] sources;
|
||||
|
||||
private AudioSource CurrentSource => sources[currentSource];
|
||||
private AudioSource OtherSource => sources[(currentSource + 1) % 2];
|
||||
|
||||
private int currentSource;
|
||||
private readonly Coroutine[] _activeFades = new Coroutine[2]; // the indices in these fades do not necessarily correspond to the sources
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Instance = this;
|
||||
|
||||
if (sources.Length != 2)
|
||||
{
|
||||
throw new Exception("There must be two sources.");
|
||||
}
|
||||
if (CurrentSource == OtherSource)
|
||||
{
|
||||
throw new Exception("Current source must be different from other source.");
|
||||
}
|
||||
|
||||
foreach (var source in sources)
|
||||
{
|
||||
source.loop = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void StartAmbience()
|
||||
{
|
||||
CrossFadeTo(initialAmbience);
|
||||
}
|
||||
|
||||
public void CrossFadeTo(AmbienceClip ambience)
|
||||
{
|
||||
// prepare fade source
|
||||
var fadeSource = OtherSource;
|
||||
fadeSource.clip = ambience.audio;
|
||||
fadeSource.Play();
|
||||
|
||||
// stop any running fades
|
||||
foreach (var fade in _activeFades)
|
||||
{
|
||||
if (fade != null)
|
||||
{
|
||||
StopCoroutine(fade);
|
||||
}
|
||||
}
|
||||
|
||||
// start new fades
|
||||
_activeFades[0] = StartCoroutine(Fade(CurrentSource, CurrentSource.volume, 0));
|
||||
_activeFades[1] = StartCoroutine(Fade(fadeSource, fadeSource.volume, ambience.volume));
|
||||
|
||||
// switch current source
|
||||
currentSource = (currentSource + 1) % 2;
|
||||
}
|
||||
|
||||
private IEnumerator Fade(AudioSource source, float from, float to)
|
||||
{
|
||||
var startTime = Time.time;
|
||||
|
||||
while (Math.Abs(source.volume - to) > 0.0001f)
|
||||
{
|
||||
source.volume = Mathf.Clamp01(Mathf.Lerp(
|
||||
from,
|
||||
to,
|
||||
(Time.time - startTime) / fadeDuration));
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Engine/Runtime/Environment/AmbienceAudio.cs.meta
Normal file
3
Assets/Engine/Runtime/Environment/AmbienceAudio.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: affaa904605c46dca7162e99fd363b5a
|
||||
timeCreated: 1684143627
|
||||
12
Assets/Engine/Runtime/Environment/AmbienceClip.cs
Normal file
12
Assets/Engine/Runtime/Environment/AmbienceClip.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using NaughtyAttributes;
|
||||
using UnityEngine;
|
||||
|
||||
namespace EscapeRoomEngine.Engine.Runtime.Environment
|
||||
{
|
||||
[CreateAssetMenu(menuName = "Environment/Ambience Clip")]
|
||||
public class AmbienceClip : ScriptableObject
|
||||
{
|
||||
[Required] public AudioClip audio;
|
||||
public float volume = 1;
|
||||
}
|
||||
}
|
||||
3
Assets/Engine/Runtime/Environment/AmbienceClip.cs.meta
Normal file
3
Assets/Engine/Runtime/Environment/AmbienceClip.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3d36d46846cd41019647bae00e4b0f17
|
||||
timeCreated: 1684145618
|
||||
12
Assets/Engine/Runtime/Environment/RoomEnvironment.cs
Normal file
12
Assets/Engine/Runtime/Environment/RoomEnvironment.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using NaughtyAttributes;
|
||||
using UnityEngine;
|
||||
|
||||
namespace EscapeRoomEngine.Engine.Runtime.Environment
|
||||
{
|
||||
[CreateAssetMenu(menuName = "Environment/Room", order = 0)]
|
||||
public class RoomEnvironment : ScriptableObject
|
||||
{
|
||||
[Required] public AmbienceClip ambience;
|
||||
[Required] public GameObject environment;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3b7d4fea984e4fff841ac107b6896270
|
||||
timeCreated: 1684146291
|
||||
Reference in New Issue
Block a user