Audio Integration

Associated equippable items with audio triggers and individual game objects
Added event triggers for stopping sounds when entities are destroyed
This commit is contained in:
Reinier Vens 2021-03-16 16:35:23 +00:00
commit d3a03bec6c
24 changed files with 591 additions and 283 deletions

View file

@ -4,7 +4,16 @@ using UnityEngine;
public class ShipInstance : EntityInstance
{
public (Thruster effect, ParticleSystem system, float baseEmission)[] Particles;
private class ThrusterInstance
{
public Thruster Thruster;
public GameObject SfxSource;
public ParticleSystem System;
public float BaseEmission;
public int MaxParticleCount;
}
private ThrusterInstance[] _thrusters;
public Ship Ship { get; private set; }
@ -18,40 +27,52 @@ public class ShipInstance : EntityInstance
return;
}
Ship = ship;
Particles = ship.GetBehaviors<Thruster>()
.Select<Thruster, (Thruster effect, ParticleSystem system, float baseEmission)>(x =>
_thrusters = ship.GetBehaviors<Thruster>().Select(thruster =>
{
var effectData = (ThrusterData) x.Data;
var effectData = (ThrusterData) thruster.Data;
var particles = Instantiate(UnityHelpers.LoadAsset<ParticleSystem>(effectData.ParticlesPrefab), transform, false);
var particlesShape = particles.shape;
particlesShape.meshRenderer = ThrusterHardpoints
.FirstOrDefault(t => t.name == x.Entity.Hardpoints[x.Item.Position.x, x.Item.Position.y].Transform)
?.Emitter;
return (x, particles, particles.emission.rateOverTimeMultiplier);
var thrusterHardpoint = ThrusterHardpoints
.FirstOrDefault(t => t.name == thruster.Entity.Hardpoints[thruster.Item.Position.x, thruster.Item.Position.y].Transform);
particlesShape.meshRenderer = thrusterHardpoint?.Emitter;
if (!string.IsNullOrEmpty(thruster.Item.Data.SoundEffectTrigger) && thrusterHardpoint != null)
{
AkSoundEngine.RegisterGameObj(thrusterHardpoint.gameObject);
AkSoundEngine.PostEvent(thruster.Item.Data.SoundEffectTrigger, thrusterHardpoint.gameObject);
}
return new ThrusterInstance
{
Thruster = thruster,
SfxSource = thrusterHardpoint?.gameObject,
System = particles,
BaseEmission = particles.emission.rateOverTimeMultiplier,
MaxParticleCount = 0
};
})
.ToArray();
foreach (var particle in Particles)
foreach (var particle in _thrusters)
{
particle.system.gameObject.SetActive(false);
particle.System.gameObject.SetActive(false);
}
}
protected override void ShowUnfadedElements()
{
base.ShowUnfadedElements();
foreach (var particle in Particles)
foreach (var particle in _thrusters)
{
particle.system.gameObject.SetActive(true);
particle.System.gameObject.SetActive(true);
}
}
protected override void HideUnfadedElements()
{
base.HideUnfadedElements();
foreach (var particle in Particles)
foreach (var particle in _thrusters)
{
particle.system.gameObject.SetActive(false);
particle.System.gameObject.SetActive(false);
}
}
@ -59,14 +80,38 @@ public class ShipInstance : EntityInstance
{
base.Update();
foreach (var (effect, system, baseEmission) in Particles)
foreach (var thrusterInstance in _thrusters)
{
var emissionModule = system.emission;
var item = effect.Item.EquippableItem;
var emissionModule = thrusterInstance.System.emission;
var item = thrusterInstance.Thruster.Item.EquippableItem;
var data = Entity.ItemManager.GetData(item);
emissionModule.rateOverTimeMultiplier = baseEmission * effect.Axis * (item.Durability / data.Durability);
thrusterInstance.MaxParticleCount = thrusterInstance.System.particleCount;
emissionModule.rateOverTimeMultiplier = thrusterInstance.BaseEmission * thrusterInstance.Thruster.Axis * (item.Durability / data.Durability);
if (thrusterInstance.SfxSource)
{
var throttle = 0f;
if (thrusterInstance.System.particleCount > 0)
throttle = (float) thrusterInstance.System.particleCount / thrusterInstance.MaxParticleCount;
AkSoundEngine.SetRTPCValue("thruster_throttle", throttle, thrusterInstance.SfxSource);
AkSoundEngine.SetRTPCValue("performance_durability", thrusterInstance.Thruster.Item.DurabilityPerformance, thrusterInstance.SfxSource);
AkSoundEngine.SetRTPCValue("performance_thermal", thrusterInstance.Thruster.Item.ThermalPerformance, thrusterInstance.SfxSource);
AkSoundEngine.SetRTPCValue("performance_quality",
thrusterInstance.Thruster.Item.ItemManager.CompoundQuality(thrusterInstance.Thruster.Item.EquippableItem), thrusterInstance.SfxSource);
}
}
transform.rotation = Ship.Rotation;
}
public override void OnDestroy()
{
foreach (var thrusterInstance in _thrusters)
{
if (thrusterInstance.SfxSource)
{
AkSoundEngine.PostEvent(thrusterInstance.Thruster.Item.Data.SoundEffectTrigger + "_stop", thrusterInstance.SfxSource);
AkSoundEngine.UnregisterGameObj(thrusterInstance.SfxSource);
}
}
}
}