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

@ -257,11 +257,12 @@ public class ActionGameManager : MonoBehaviour
{
if (length(wormhole.Position - CurrentEntity.Position.xz) < Settings.GameplaySettings.WormholeExitRadius)
{
var oldZone = Zone;
ship.EnterWormhole(wormhole.Position);
ship.OnEnteredWormhole += () =>
{
GenerateLevel(wormhole.Target);
ship.ExitWormhole(ZoneRenderer.WormholeInstances.Keys.First().Position,
ship.ExitWormhole(ZoneRenderer.WormholeInstances.Keys.First(w=>w.Target==Zone.SectorZone).Position,
Settings.GameplaySettings.WormholeExitVelocity * ItemManager.Random.NextFloat2Direction());
CurrentEntity.Zone = Zone;
};
@ -477,10 +478,7 @@ public class ActionGameManager : MonoBehaviour
if (CurrentEntity != null)
{
CurrentEntity.Deactivate();
if (CurrentEntity.Zone != null)
{
CurrentEntity.Zone.Entities.Remove(CurrentEntity);
}
CurrentEntity.Zone?.Entities.Remove(CurrentEntity);
CurrentEntity.Zone = Zone;
Zone.Entities.Add(CurrentEntity);
CurrentEntity.Activate();

View file

@ -15,6 +15,7 @@ public class EntityInstance : MonoBehaviour
public ShieldManager Shield;
public HullCollider[] HullColliders;
public Transform[] EquipmentHardpoints;
public RadiatorHardpoint[] RadiatorHardpoints;
public ThrusterHardpoint[] ThrusterHardpoints;
public WeaponHardpoint[] WeaponHardpoints;
@ -36,6 +37,9 @@ public class EntityInstance : MonoBehaviour
private bool _unfadedElementsVisible = false;
private float _fadeTime;
private (Reactor reactor, GameObject sfxSource) _reactor;
private Dictionary<Radiator, GameObject> _radiatorSfx = new Dictionary<Radiator, GameObject>();
private static Dictionary<InstantWeaponData, InstantWeaponEffectManager> _instantWeaponManagers = new Dictionary<InstantWeaponData, InstantWeaponEffectManager>();
private static Dictionary<ConstantWeaponData, ConstantWeaponEffectManager> _constantWeaponManagers = new Dictionary<ConstantWeaponData, ConstantWeaponEffectManager>();
@ -195,6 +199,39 @@ public class EntityInstance : MonoBehaviour
_constantWeaponManagers[data].StopFiring(item);
}
}
var hp = Entity.Hardpoints[item.Position.x, item.Position.y];
if (hp != null && !string.IsNullOrEmpty(item.Data.SoundEffectTrigger))
{
if(hp.Type == HardpointType.Radiator)
{
var radiatorHardpoint = RadiatorHardpoints.FirstOrDefault(x => x.name == hp.Transform);
if (radiatorHardpoint)
{
var rad = item.GetBehavior<Radiator>();
if(rad != null && !string.IsNullOrEmpty(item.Data.SoundEffectTrigger))
{
AkSoundEngine.RegisterGameObj(radiatorHardpoint.gameObject);
AkSoundEngine.PostEvent(item.Data.SoundEffectTrigger, radiatorHardpoint.gameObject);
_radiatorSfx[rad] = radiatorHardpoint.gameObject;
}
}
}
if(hp.Type == HardpointType.Reactor)
{
var reactorHardpoint = EquipmentHardpoints.FirstOrDefault(x => x.name == hp.Transform);
if (reactorHardpoint)
{
var reactor = item.GetBehavior<Reactor>();
if( reactor != null && !string.IsNullOrEmpty(item.Data.SoundEffectTrigger) )
{
AkSoundEngine.RegisterGameObj(reactorHardpoint.gameObject);
AkSoundEngine.PostEvent(item.Data.SoundEffectTrigger, reactorHardpoint.gameObject);
_reactor = (reactor, reactorHardpoint.gameObject);
}
}
}
}
}
RadiatorMeshes = new Dictionary<Radiator, MeshRenderer>();
Barrels = new Dictionary<HardpointData, Transform[]>();
@ -423,6 +460,23 @@ public class EntityInstance : MonoBehaviour
foreach(var material in _fadeMaterials) material.SetFloat("_Fade", _fade);
}
}
foreach (var r in _radiatorSfx)
{
r.Value.SetActive(r.Key.Item.Online.Value);
AkSoundEngine.SetRTPCValue("performance_durability", r.Key.Item.DurabilityPerformance, r.Value);
AkSoundEngine.SetRTPCValue("performance_thermal", r.Key.Item.ThermalPerformance, r.Value);
AkSoundEngine.SetRTPCValue("performance_quality", r.Key.Item.ItemManager.CompoundQuality(r.Key.Item.EquippableItem), r.Value);
}
if (_reactor.reactor != null)
{
_reactor.sfxSource.SetActive(_reactor.reactor.Item.Online.Value);
AkSoundEngine.SetRTPCValue("reactor_load", _reactor.reactor.CurrentLoadRatio, _reactor.sfxSource);
AkSoundEngine.SetRTPCValue("performance_durability", _reactor.reactor.Item.DurabilityPerformance, _reactor.sfxSource);
AkSoundEngine.SetRTPCValue("performance_thermal", _reactor.reactor.Item.ThermalPerformance, _reactor.sfxSource);
AkSoundEngine.SetRTPCValue("performance_quality", _reactor.reactor.Item.ItemManager.CompoundQuality(_reactor.reactor.Item.EquippableItem), _reactor.sfxSource);
}
foreach (var x in RadiatorMeshes)
{
@ -439,8 +493,19 @@ public class EntityInstance : MonoBehaviour
transform.position = Entity.Position;
}
private void OnDestroy()
public virtual void OnDestroy()
{
foreach (var r in _radiatorSfx)
{
AkSoundEngine.PostEvent(r.Key.Item.Data.SoundEffectTrigger + "_stop", r.Value);
AkSoundEngine.UnregisterGameObj(r.Value);
}
if (_reactor.reactor != null)
{
AkSoundEngine.PostEvent(_reactor.reactor.Item.Data.SoundEffectTrigger + "_stop", _reactor.sfxSource);
AkSoundEngine.UnregisterGameObj(_reactor.sfxSource);
}
foreach(var x in _subscriptions)
x.Dispose();
}

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);
}
}
}
}

View file

@ -40,6 +40,8 @@ public class Reactor : IBehavior, IOrderedBehavior
public ItemManager Context { get; }
public float Draw { get; private set; }
public float CurrentLoadRatio { get; private set; }
public int Order => 100;
@ -64,7 +66,6 @@ public class Reactor : IBehavior, IOrderedBehavior
var capacitor = onRemove.Value.GetBehavior<Capacitor>();
if (capacitor != null) _capacitors.Remove(capacitor);
});
}
public void ConsumeEnergy(float energy)
@ -78,7 +79,8 @@ public class Reactor : IBehavior, IOrderedBehavior
var efficiency = Item.Evaluate(_data.Efficiency);
// This behavior executes last, so any components drawing power have already done so
CurrentLoadRatio = Draw / max(charge, .01f);
// Subtract the baseline charge from draw
Draw -= charge;

View file

@ -907,7 +907,7 @@ public class EquippedItem
public ReadOnlyReactiveProperty<bool> Online { get; }
public ReactiveProperty<bool> Enabled { get; } = new ReactiveProperty<bool>(true);
public ReadOnlyReactiveProperty<bool> Active { get; }
public ItemManager ItemManager { get; }
public float Temperature
{
@ -919,20 +919,18 @@ public class EquippedItem
}
}
protected readonly ItemManager _itemManager;
//private bool _online;
public EquippedItem(ItemManager itemManager, EquippableItem item, int2 position, Entity entity)
{
_itemManager = itemManager;
Data = _itemManager.GetData(item);
ItemManager = itemManager;
Data = ItemManager.GetData(item);
Entity = entity;
EquippableItem = item;
Position = position;
Conductivity = Data.Conductivity;
ThermalExponent = _itemManager.Evaluate(Data.HeatExponent, EquippableItem);
DurabilityExponent = _itemManager.Evaluate(Data.DurabilityExponent, EquippableItem);
ThermalExponent = ItemManager.Evaluate(Data.HeatExponent, EquippableItem);
DurabilityExponent = ItemManager.Evaluate(Data.DurabilityExponent, EquippableItem);
var hullData = itemManager.GetData(entity.Hull);
InsetShape = hullData.Shape.Inset(Data.Shape, position, item.Rotation);
@ -971,7 +969,7 @@ public class EquippedItem
var durability = 1f;
if(stat.DurabilityDependent)
durability = pow(DurabilityPerformance, DurabilityExponent);
var quality = pow(_itemManager.Quality(stat, EquippableItem), stat.QualityExponent);
var quality = pow(ItemManager.Quality(stat, EquippableItem), stat.QualityExponent);
var scaleModifier = 1.0f;
foreach (var value in stat.GetScaleModifiers(Entity).Values) scaleModifier = scaleModifier * value;
@ -1001,8 +999,8 @@ public class EquippedItem
DurabilityPerformance = EquippableItem.Durability / Data.Durability;
var performanceThreshold = Entity.Settings.ShutdownPerformance;
Wear = (1 - pow(Data.Performance(temp),
(1 - pow(_itemManager.CompoundQuality(EquippableItem), _itemManager.GameplaySettings.QualityWearExponent)) *
_itemManager.GameplaySettings.ThermalWearExponent)
(1 - pow(ItemManager.CompoundQuality(EquippableItem), ItemManager.GameplaySettings.QualityWearExponent)) *
ItemManager.GameplaySettings.ThermalWearExponent)
) * Data.Durability / Data.ThermalResilience;
ThermalOnline.Value = ThermalPerformance > performanceThreshold || Entity.OverrideShutdown && EquippableItem.OverrideShutdown;
DurabilityOnline.Value = EquippableItem.Durability > .01f;
@ -1045,7 +1043,7 @@ public class EquippedCargoBay : EquippedItem
public EquippedCargoBay(ItemManager itemManager, EquippableItem item, int2 position, Entity entity, string name) : base(itemManager, item, position, entity)
{
Data = _itemManager.GetData(EquippableItem) as CargoBayData;
Data = ItemManager.GetData(EquippableItem) as CargoBayData;
Name = name;
Mass = Data.Mass;
@ -1057,7 +1055,7 @@ public class EquippedCargoBay : EquippedItem
// Check whether the given item will fit when its origin is placed at the given coordinate
public bool ItemFits(ItemInstance item, int2 cargoCoord)
{
var itemData = _itemManager.GetData(item);
var itemData = ItemManager.GetData(item);
// Check every cell of the item's shape
foreach (var i in itemData.Shape.Coordinates)
{
@ -1084,7 +1082,7 @@ public class EquippedCargoBay : EquippedItem
public bool TryFindSpace(SimpleCommodity item, out List<int2> positions)
{
positions = new List<int2>();
var itemData = _itemManager.GetData(item);
var itemData = ItemManager.GetData(item);
var remainingQuantity = item.Quantity;
// For simple commodities, search for existing item stacks to add to
@ -1170,7 +1168,7 @@ public class EquippedCargoBay : EquippedItem
// Returns true only when ALL of the items are successfully stored
public bool TryStore(SimpleCommodity item, int2 cargoCoord)
{
var itemData = _itemManager.GetData(item);
var itemData = ItemManager.GetData(item);
if (ItemFits(item, cargoCoord))
{
foreach (var p in itemData.Shape.Coordinates)
@ -1203,8 +1201,8 @@ public class EquippedCargoBay : EquippedItem
}
else return false;
Mass += _itemManager.GetMass(item);
ThermalMass += _itemManager.GetThermalMass(item);
Mass += ItemManager.GetMass(item);
ThermalMass += ItemManager.GetThermalMass(item);
return true;
}
@ -1216,7 +1214,7 @@ public class EquippedCargoBay : EquippedItem
{
if (!ItemFits(item, cargoCoord)) return false;
var itemData = _itemManager.GetData(item);
var itemData = ItemManager.GetData(item);
foreach (var p in itemData.Shape.Coordinates)
{
var pos = cargoCoord + itemData.Shape.Rotate(p, item.Rotation);
@ -1228,8 +1226,8 @@ public class EquippedCargoBay : EquippedItem
ItemsOfType[item.Data] = new List<ItemInstance>();
ItemsOfType[item.Data].Add(item);
Mass += _itemManager.GetMass(item);
ThermalMass += _itemManager.GetThermalMass(item);
Mass += ItemManager.GetMass(item);
ThermalMass += ItemManager.GetThermalMass(item);
return true;
}
@ -1238,10 +1236,10 @@ public class EquippedCargoBay : EquippedItem
{
if (!Cargo.ContainsKey(item))
{
_itemManager.Log("Attempted to remove item from a cargo bay that it wasn't even in! Something went wrong here!");
ItemManager.Log("Attempted to remove item from a cargo bay that it wasn't even in! Something went wrong here!");
return null;
}
var itemData = _itemManager.GetData(item);
var itemData = ItemManager.GetData(item);
if(quantity >= item.Quantity)
{
foreach(var v in Data.InteriorShape.Coordinates)
@ -1253,8 +1251,8 @@ public class EquippedCargoBay : EquippedItem
if (!ItemsOfType[item.Data].Any())
ItemsOfType.Remove(item.Data);
Mass -= _itemManager.GetMass(item);
ThermalMass -= _itemManager.GetThermalMass(item);
Mass -= ItemManager.GetMass(item);
ThermalMass -= ItemManager.GetThermalMass(item);
return item;
}
@ -1269,10 +1267,10 @@ public class EquippedCargoBay : EquippedItem
{
if (!Cargo.ContainsKey(item))
{
_itemManager.Log("Attempted to remove item from a cargo bay that it wasn't even in! Something went wrong here!");
ItemManager.Log("Attempted to remove item from a cargo bay that it wasn't even in! Something went wrong here!");
return;
}
var itemData = _itemManager.GetData(item);
var itemData = ItemManager.GetData(item);
foreach(var v in Data.InteriorShape.Coordinates)
if (Occupancy[v.x, v.y] == item)
Occupancy[v.x, v.y] = null;
@ -1282,8 +1280,8 @@ public class EquippedCargoBay : EquippedItem
if (!ItemsOfType[item.Data].Any())
ItemsOfType.Remove(item.Data);
Mass -= _itemManager.GetMass(item);
ThermalMass -= _itemManager.GetThermalMass(item);
Mass -= ItemManager.GetMass(item);
ThermalMass -= ItemManager.GetThermalMass(item);
}
public void Remove(ItemInstance item)
@ -1298,7 +1296,7 @@ public class EquippedCargoBay : EquippedItem
{
if (!Cargo.ContainsKey(item))
{
_itemManager.Log("Attempted to remove item from a cargo bay that it wasn't even in! Something went wrong here!");
ItemManager.Log("Attempted to remove item from a cargo bay that it wasn't even in! Something went wrong here!");
return false;
}
@ -1316,7 +1314,7 @@ public class EquippedCargoBay : EquippedItem
{
if (!Cargo.ContainsKey(item))
{
_itemManager.Log("Attempted to remove item from a cargo bay that it wasn't even in! Something went wrong here!");
ItemManager.Log("Attempted to remove item from a cargo bay that it wasn't even in! Something went wrong here!");
return false;
}
@ -1333,7 +1331,7 @@ public class EquippedDockingBay : EquippedCargoBay
private DockingBayData _data;
public EquippedDockingBay(ItemManager itemManager, EquippableItem item, int2 position, Entity entity, string name) : base(itemManager, item, position, entity, name)
{
_data = _itemManager.GetData(EquippableItem) as DockingBayData;
_data = ItemManager.GetData(EquippableItem) as DockingBayData;
}
}

View file

@ -300,6 +300,9 @@ public abstract class EquippableItemData : CraftedItemData
[InspectableField, JsonProperty("resilience"), Key(18)]
public float ThermalResilience = 1;
[InspectableField, JsonProperty("sfx"), Key(19)]
public string SoundEffectTrigger;
[IgnoreMember]
public abstract HardpointType HardpointType { get; }
@ -341,7 +344,7 @@ public abstract class EquippableItemData : CraftedItemData
[RethinkTable("Items"), Inspectable, MessagePackObject, JsonObject(MemberSerialization.OptIn)]
public class GearData : EquippableItemData
{
[InspectableField, JsonProperty("hardpointType"), Key(19)]
[InspectableField, JsonProperty("hardpointType"), Key(20)]
public HardpointType Hardpoint;
[IgnoreMember] public override HardpointType HardpointType => Hardpoint;
@ -350,7 +353,7 @@ public class GearData : EquippableItemData
[RethinkTable("Items"), Inspectable, MessagePackObject, JsonObject(MemberSerialization.OptIn)]
public class CargoBayData : EquippableItemData
{
[InspectableField, JsonProperty("interiorShape"), Key(19)]
[InspectableField, JsonProperty("interiorShape"), Key(20)]
public Shape InteriorShape;
[IgnoreMember] public override HardpointType HardpointType => HardpointType.Tool;
}
@ -358,7 +361,7 @@ public class CargoBayData : EquippableItemData
[RethinkTable("Items"), Inspectable, MessagePackObject, JsonObject(MemberSerialization.OptIn)]
public class DockingBayData : CargoBayData
{
[InspectableField, JsonProperty("maxSize"), Key(20)]
[InspectableField, JsonProperty("maxSize"), Key(21)]
public int2 MaxSize;
[IgnoreMember] public override HardpointType HardpointType => HardpointType.Tool;
}
@ -366,19 +369,19 @@ public class DockingBayData : CargoBayData
[RethinkTable("Items"), Inspectable, MessagePackObject, JsonObject(MemberSerialization.OptIn)]
public class HullData : EquippableItemData
{
[InspectableField, JsonProperty("hardpoints"), Key(19)]
[InspectableField, JsonProperty("hardpoints"), Key(20)]
public List<HardpointData> Hardpoints = new List<HardpointData>();
[InspectablePrefab, JsonProperty("prefab"), Key(20)]
[InspectablePrefab, JsonProperty("prefab"), Key(21)]
public string Prefab;
[InspectableField, JsonProperty("hullType"), Key(21)]
[InspectableField, JsonProperty("hullType"), Key(22)]
public HullType HullType;
[InspectableField, JsonProperty("gridOffset"), Key(22)]
[InspectableField, JsonProperty("gridOffset"), Key(23)]
public float GridOffset;
[InspectableField, JsonProperty("armor"), Key(23)]
[InspectableField, JsonProperty("armor"), Key(24)]
public float Armor;
[IgnoreMember]

View file

@ -233,9 +233,9 @@ public class Ship : Entity
public override void Update(float delta)
{
foreach (var thruster in _allThrusters) thruster.Axis = 0;
if (_active && !_exitingWormhole && !_enteringWormhole)
{
foreach (var thruster in _allThrusters) thruster.Axis = 0;
var rightThrusterTorqueCompensation = abs(RightStrafeTotalTorque) / RightStrafeTorqueThrusters.Length;
foreach (var thruster in _rightThrusters)
{