237 lines
11 KiB
C#
237 lines
11 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace BigShipTycoon
|
|
{
|
|
internal enum DeckSound { Deck, Crowd, Pool, Dining, Entertainment, Machinery, Galley, Laundry, Bridge, Retail }
|
|
|
|
internal sealed class DeckAmbience : MonoBehaviour
|
|
{
|
|
private static readonly DeckSound[] Sounds = (DeckSound[])Enum.GetValues(typeof(DeckSound));
|
|
private readonly AudioSource[] sources = new AudioSource[Sounds.Length];
|
|
private readonly AudioLowPassFilter[] filters = new AudioLowPassFilter[Sounds.Length];
|
|
private readonly bool[] started = new bool[Sounds.Length];
|
|
private readonly float[] targets = new float[Sounds.Length];
|
|
private readonly float[] pans = new float[Sounds.Length];
|
|
private Harbor harbor;
|
|
|
|
internal float Focus { get; private set; }
|
|
internal bool Ready { get; private set; }
|
|
internal float Level(DeckSound sound) => sources[(int)sound].volume;
|
|
|
|
internal void Initialize(Harbor scene)
|
|
{
|
|
harbor = scene;
|
|
Ready = true;
|
|
foreach (DeckSound sound in Sounds)
|
|
{
|
|
int i = (int)sound;
|
|
var layer = new GameObject($"Deck ambience · {sound}");
|
|
layer.transform.SetParent(transform, false);
|
|
AudioSource source = sources[i] = layer.AddComponent<AudioSource>();
|
|
source.playOnAwake = false;
|
|
source.loop = true;
|
|
source.spatialBlend = 0;
|
|
source.volume = 0;
|
|
source.priority = 164;
|
|
source.clip = Resources.Load<AudioClip>($"Audio/Deck/{sound}");
|
|
Ready &= source.clip != null;
|
|
AudioLowPassFilter filter = filters[i] = layer.AddComponent<AudioLowPassFilter>();
|
|
filter.cutoffFrequency = 2100;
|
|
filter.lowpassResonanceQ = 1;
|
|
}
|
|
}
|
|
|
|
internal void Mix(float delta, bool active, OnboardSession session, bool addressPlaying)
|
|
{
|
|
Array.Clear(targets, 0, targets.Length);
|
|
float focus = active ? harbor.ReadDeckAmbience(this, session) : 0;
|
|
float ease = 1 - Mathf.Exp(-Mathf.Max(0, delta) * 4.3f);
|
|
Focus = Mathf.Lerp(Focus, focus, ease);
|
|
float announcementGain = addressPlaying ? .44f : 1;
|
|
foreach (DeckSound sound in Sounds)
|
|
{
|
|
int i = (int)sound;
|
|
AudioSource source = sources[i];
|
|
float target = targets[i] * announcementGain;
|
|
source.volume = Mathf.Lerp(source.volume, target, ease);
|
|
source.panStereo = Mathf.Lerp(source.panStereo, target > 0 ? pans[i] : 0, ease);
|
|
if (source.clip == null)
|
|
continue;
|
|
|
|
if (target > .002f && !source.isPlaying)
|
|
{
|
|
if (started[i])
|
|
source.UnPause();
|
|
else
|
|
{
|
|
source.Play();
|
|
started[i] = true;
|
|
}
|
|
}
|
|
else if (target == 0 && source.volume < .001f && source.isPlaying)
|
|
source.Pause();
|
|
|
|
filters[i].cutoffFrequency = Mathf.Lerp(1450, sound == DeckSound.Dining ? 7200 : 5400, Focus);
|
|
}
|
|
}
|
|
|
|
internal void Listen(DeckSound sound, float level, float pan)
|
|
{
|
|
int i = (int)sound;
|
|
float combined = targets[i] + level;
|
|
if (combined <= 0)
|
|
return;
|
|
pans[i] = (pans[i] * targets[i] + pan * level) / combined;
|
|
targets[i] = Mathf.Min(Mathf.Max(level, targets[i]) + Mathf.Min(level, targets[i]) * .16f, .56f);
|
|
}
|
|
}
|
|
|
|
public sealed partial class Harbor
|
|
{
|
|
internal float SunRidePhase(SunAttraction kind) => decks?.SunRidePhase(kind) ?? -1;
|
|
|
|
internal float ReadDeckAmbience(DeckAmbience mix, OnboardSession session)
|
|
{
|
|
if (viewCamera == null || !viewCamera.enabled || Frame == null || vessel == null || displayedShip == null || PortCinematicActive)
|
|
return 0;
|
|
|
|
bool onFoot = cameraMode != CameraMode.Orbit;
|
|
Vector3 listener = onFoot ? freePosition : OverviewFocus() + orbitOffset;
|
|
Vector3 right = vessel.InverseTransformDirection(viewCamera.transform.right);
|
|
float span = onFoot ? 5 : OrbitVerticalSpan;
|
|
float overviewSpan = 2 * OverviewHalfHeight(ViewAspect) * OverviewZoom;
|
|
float nearZoom = Mathf.Min(.24f, 8.4f / overviewSpan);
|
|
float focus = onFoot ? 1 : Mathf.SmoothStep(0, 1, Mathf.InverseLerp(.9f, nearZoom, zoom / OverviewZoom));
|
|
float radius = Mathf.Lerp(7.2f, 27f, Mathf.InverseLerp(6, 44, span));
|
|
if (focus < .001f)
|
|
return 0;
|
|
|
|
if (cameraMode == CameraMode.Bridge)
|
|
{
|
|
mix.Listen(DeckSound.Bridge, focus * .47f, 0);
|
|
return focus;
|
|
}
|
|
|
|
if (cameraMode == CameraMode.Service)
|
|
{
|
|
// Listen from the actual walking position as the player moves through the crew corridor.
|
|
mix.Listen(DeckSound.Machinery, focus * .10f, 0);
|
|
for (int area = 0; area < ServiceDeckModel.Areas.Length; area++)
|
|
{
|
|
DeckSound sound = area switch { 1 => DeckSound.Galley, 6 => DeckSound.Laundry, _ => DeckSound.Machinery };
|
|
Heard(sound, serviceDeck.Entry(area) + new Vector3(0, -.65f, 3.6f),
|
|
area is 0 or 1 or 6 ? .46f : .20f, 1);
|
|
}
|
|
return focus;
|
|
}
|
|
|
|
float passengers = session == null ? .18f : Mathf.Clamp01(.30f + session.Quote.Guests * .0035f);
|
|
mix.Listen(DeckSound.Deck, focus * (deckIndex < 0 ? .27f : .07f), 0);
|
|
if (deckIndex < 0)
|
|
{
|
|
// The orbit point projects onto the sun deck rather than listening through the hull.
|
|
if (decks == null)
|
|
return focus;
|
|
int deckCount = displayedShip.Decks.Count;
|
|
float roof = MegaDeckGeometry.RoofHeight(deckCount, SunDeckZone.Sky);
|
|
if (!onFoot)
|
|
listener.y = roof;
|
|
float outdoors = session?.Quote.Weather == WeatherCondition.Storm ? .16f : 1f;
|
|
HearOutside(decks.SunDeck, roof);
|
|
if (displayedShip.HasAftTerrace)
|
|
HearOutside(decks.AftTerrace, roof - VesselExterior.DeckSpacing);
|
|
|
|
void HearOutside(VesselExterior.SunDeckLayout plan, float elevation)
|
|
{
|
|
if (!plan.Custom && !plan.HasPool)
|
|
Heard(DeckSound.Crowd, plan.Pool.Position(new Vector3(-2.2f, elevation, 0)), passengers * outdoors * .29f, 1.1f);
|
|
foreach (VesselExterior.SunDeckVenue venue in plan.Venues)
|
|
{
|
|
Vector3 center = venue.Position(new Vector3(0, elevation, 0));
|
|
switch (venue.Kind)
|
|
{
|
|
case SunAttraction.Pool:
|
|
Heard(DeckSound.Crowd, venue.Position(new Vector3(-2.2f, elevation, 0)), passengers * outdoors * .29f, 1.1f);
|
|
Heard(DeckSound.Pool, center, .11f + .28f * Service(OnboardService.Amenities) * outdoors, 1.0f);
|
|
break;
|
|
case SunAttraction.Theater:
|
|
Heard(DeckSound.Entertainment, venue.Position(new Vector3(venue.StageX, elevation, 0)),
|
|
.36f * Service(OnboardService.Shows) * outdoors, 1.18f);
|
|
break;
|
|
default:
|
|
Heard(DeckSound.Crowd, center, passengers * outdoors * Service(OnboardService.Amenities) * .28f, 1.1f);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return focus;
|
|
}
|
|
|
|
mix.Listen(DeckSound.Crowd, focus * passengers * .075f, 0);
|
|
if (decks == null)
|
|
return focus;
|
|
|
|
var stations = decks.ActiveStations;
|
|
for (int i = 0; i < stations.Count; i++)
|
|
{
|
|
ShipStation station = stations[i];
|
|
switch (station.Kind)
|
|
{
|
|
case StationKind.Dining:
|
|
case StationKind.Bar:
|
|
float diners = Service(station.Kind == StationKind.Dining ? OnboardService.Meals : OnboardService.Drinks);
|
|
Heard(DeckSound.Dining, station.Position, diners * .44f, 1);
|
|
Heard(DeckSound.Crowd, station.Position, passengers * diners * .34f, 1.1f);
|
|
break;
|
|
case StationKind.Stage:
|
|
Heard(DeckSound.Entertainment, station.Position, Service(OnboardService.Shows) * .49f, 1.15f);
|
|
break;
|
|
case StationKind.Pool:
|
|
Heard(DeckSound.Pool, station.Position, .12f + Service(OnboardService.Amenities) * .29f, 1.10f);
|
|
Heard(DeckSound.Crowd, station.Position, passengers * Service(OnboardService.Amenities) * .26f, 1);
|
|
break;
|
|
case StationKind.Galley:
|
|
Heard(DeckSound.Galley, station.Position, .16f + Service(OnboardService.Meals) * .36f, .86f);
|
|
break;
|
|
case StationKind.Engine:
|
|
case StationKind.Stores:
|
|
case StationKind.Maintenance:
|
|
case StationKind.Water:
|
|
case StationKind.Waste:
|
|
Heard(DeckSound.Machinery, station.Position, .29f, .86f);
|
|
break;
|
|
case StationKind.Retail:
|
|
Heard(DeckSound.Retail, station.Position, .30f * Service(OnboardService.Retail), .92f);
|
|
Heard(DeckSound.Crowd, station.Position, passengers * Service(OnboardService.Retail) * .25f, 1);
|
|
break;
|
|
case StationKind.Casino:
|
|
Heard(DeckSound.Dining, station.Position, Service(OnboardService.Casino) * .21f, .98f);
|
|
Heard(DeckSound.Crowd, station.Position, passengers * Service(OnboardService.Casino) * .30f, 1);
|
|
break;
|
|
}
|
|
}
|
|
return focus;
|
|
|
|
float Service(OnboardService kind)
|
|
{
|
|
if (session == null)
|
|
return .13f;
|
|
OnboardSample service = session.Get(kind);
|
|
return Mathf.Clamp01(service.Served * .012f);
|
|
}
|
|
|
|
void Heard(DeckSound sound, Vector3 source, float level, float reach)
|
|
{
|
|
Vector3 offset = source - listener;
|
|
float distance = offset.sqrMagnitude / (radius * radius * reach * reach);
|
|
float gain = focus * level / (1 + distance * (1 + distance));
|
|
if (onFoot && walkSpace != null && !walkSpace.HasSightLine(listener, source))
|
|
gain *= .47f;
|
|
float pan = Mathf.Clamp(Vector3.Dot(offset, right) / Mathf.Lerp(4.5f, 16f, Mathf.InverseLerp(6, 38, span)), -.62f, .62f);
|
|
mix.Listen(sound, gain, pan);
|
|
}
|
|
}
|
|
}
|
|
}
|