From 1bb319425b3499b4a31c62d6925397d1a4e5f08c Mon Sep 17 00:00:00 2001 From: abcdefg30 Date: Sun, 7 Apr 2019 23:20:20 +0200 Subject: [PATCH] Add Sound.Play overloads that play a random sound from a list --- OpenRA.Game/Sound/Sound.cs | 10 ++++++++++ OpenRA.Mods.Cnc/Projectiles/IonCannon.cs | 2 +- OpenRA.Mods.Common/Activities/Air/Fly.cs | 2 +- OpenRA.Mods.Common/Activities/Air/HeliFly.cs | 2 +- OpenRA.Mods.Common/Activities/Air/HeliLand.cs | 2 +- OpenRA.Mods.Common/Activities/Air/Land.cs | 2 +- OpenRA.Mods.Common/Projectiles/NukeLaunch.cs | 2 +- OpenRA.Mods.Common/Traits/AcceptsDeliveredCash.cs | 2 +- OpenRA.Mods.Common/Traits/Armament.cs | 11 +++-------- .../Traits/Conditions/GrantConditionOnDeploy.cs | 4 ++-- OpenRA.Mods.Common/Traits/DeliversCash.cs | 2 +- OpenRA.Mods.Common/Traits/Explodes.cs | 2 +- OpenRA.Mods.Common/Traits/Sound/AttackSounds.cs | 2 +- OpenRA.Mods.Common/Traits/ThrowsShrapnel.cs | 2 +- OpenRA.Mods.D2k/Traits/SpiceBloom.cs | 2 +- 15 files changed, 27 insertions(+), 22 deletions(-) diff --git a/OpenRA.Game/Sound/Sound.cs b/OpenRA.Game/Sound/Sound.cs index 1f1b523230..d5188fbd99 100644 --- a/OpenRA.Game/Sound/Sound.cs +++ b/OpenRA.Game/Sound/Sound.cs @@ -147,6 +147,16 @@ namespace OpenRA public ISound PlayLooped(SoundType type, string name) { return Play(type, null, name, true, WPos.Zero, 1f, true); } public ISound PlayLooped(SoundType type, string name, WPos pos) { return Play(type, null, name, false, pos, 1f, true); } + public ISound Play(SoundType type, string[] names, World world, Player player = null, float volumeModifier = 1f) + { + return Play(type, player, names.Random(world.LocalRandom), true, WPos.Zero, volumeModifier); + } + + public ISound Play(SoundType type, string[] names, World world, WPos pos, Player player = null, float volumeModifier = 1f) + { + return Play(type, player, names.Random(world.LocalRandom), false, pos, volumeModifier); + } + public void PlayVideo(byte[] raw, int channels, int sampleBits, int sampleRate) { StopVideo(); diff --git a/OpenRA.Mods.Cnc/Projectiles/IonCannon.cs b/OpenRA.Mods.Cnc/Projectiles/IonCannon.cs index f26480cb19..0cabd96d2d 100644 --- a/OpenRA.Mods.Cnc/Projectiles/IonCannon.cs +++ b/OpenRA.Mods.Cnc/Projectiles/IonCannon.cs @@ -39,7 +39,7 @@ namespace OpenRA.Mods.Cnc.Effects anim.PlayThen(sequence, () => Finish(world)); if (weapon.Report != null && weapon.Report.Any()) - Game.Sound.Play(SoundType.World, weapon.Report.Random(firedBy.World.SharedRandom), launchPos); + Game.Sound.Play(SoundType.World, weapon.Report, world, launchPos); } public void Tick(World world) diff --git a/OpenRA.Mods.Common/Activities/Air/Fly.cs b/OpenRA.Mods.Common/Activities/Air/Fly.cs index 6e41c4aded..615b51f738 100644 --- a/OpenRA.Mods.Common/Activities/Air/Fly.cs +++ b/OpenRA.Mods.Common/Activities/Air/Fly.cs @@ -101,7 +101,7 @@ namespace OpenRA.Mods.Common.Activities if (!soundPlayed && aircraft.Info.TakeoffSounds.Length > 0 && self.IsAtGroundLevel()) { - Game.Sound.Play(SoundType.World, aircraft.Info.TakeoffSounds.Random(self.World.SharedRandom), aircraft.CenterPosition); + Game.Sound.Play(SoundType.World, aircraft.Info.TakeoffSounds, self.World, aircraft.CenterPosition); soundPlayed = true; } diff --git a/OpenRA.Mods.Common/Activities/Air/HeliFly.cs b/OpenRA.Mods.Common/Activities/Air/HeliFly.cs index 95b554a9db..2128310c2f 100644 --- a/OpenRA.Mods.Common/Activities/Air/HeliFly.cs +++ b/OpenRA.Mods.Common/Activities/Air/HeliFly.cs @@ -94,7 +94,7 @@ namespace OpenRA.Mods.Common.Activities if (!soundPlayed && aircraft.Info.TakeoffSounds.Length > 0 && self.IsAtGroundLevel()) { - Game.Sound.Play(SoundType.World, aircraft.Info.TakeoffSounds.Random(self.World.SharedRandom), aircraft.CenterPosition); + Game.Sound.Play(SoundType.World, aircraft.Info.TakeoffSounds, self.World, aircraft.CenterPosition); soundPlayed = true; } diff --git a/OpenRA.Mods.Common/Activities/Air/HeliLand.cs b/OpenRA.Mods.Common/Activities/Air/HeliLand.cs index 7edb0d88e4..a9116e3d86 100644 --- a/OpenRA.Mods.Common/Activities/Air/HeliLand.cs +++ b/OpenRA.Mods.Common/Activities/Air/HeliLand.cs @@ -42,7 +42,7 @@ namespace OpenRA.Mods.Common.Activities if (!soundPlayed && aircraft.Info.LandingSounds.Length > 0 && !self.IsAtGroundLevel()) { - Game.Sound.Play(SoundType.World, aircraft.Info.LandingSounds.Random(self.World.SharedRandom), aircraft.CenterPosition); + Game.Sound.Play(SoundType.World, aircraft.Info.LandingSounds, self.World, aircraft.CenterPosition); soundPlayed = true; } diff --git a/OpenRA.Mods.Common/Activities/Air/Land.cs b/OpenRA.Mods.Common/Activities/Air/Land.cs index 4b0e88fb2b..95641f8b03 100644 --- a/OpenRA.Mods.Common/Activities/Air/Land.cs +++ b/OpenRA.Mods.Common/Activities/Air/Land.cs @@ -38,7 +38,7 @@ namespace OpenRA.Mods.Common.Activities if (!soundPlayed && aircraft.Info.LandingSounds.Length > 0 && !self.IsAtGroundLevel()) { - Game.Sound.Play(SoundType.World, aircraft.Info.LandingSounds.Random(self.World.SharedRandom), aircraft.CenterPosition); + Game.Sound.Play(SoundType.World, aircraft.Info.LandingSounds, self.World, aircraft.CenterPosition); soundPlayed = true; } diff --git a/OpenRA.Mods.Common/Projectiles/NukeLaunch.cs b/OpenRA.Mods.Common/Projectiles/NukeLaunch.cs index 636766e5aa..7b2a7efd19 100644 --- a/OpenRA.Mods.Common/Projectiles/NukeLaunch.cs +++ b/OpenRA.Mods.Common/Projectiles/NukeLaunch.cs @@ -74,7 +74,7 @@ namespace OpenRA.Mods.Common.Effects { anim.PlayRepeating(upSequence); if (weapon.Report != null && weapon.Report.Any()) - Game.Sound.Play(SoundType.World, weapon.Report.Random(firedBy.World.SharedRandom), pos); + Game.Sound.Play(SoundType.World, weapon.Report, world, pos); world.ScreenMap.Add(this, pos, anim.Image); isLaunched = true; diff --git a/OpenRA.Mods.Common/Traits/AcceptsDeliveredCash.cs b/OpenRA.Mods.Common/Traits/AcceptsDeliveredCash.cs index b6af512d8b..2fd3d2a83d 100644 --- a/OpenRA.Mods.Common/Traits/AcceptsDeliveredCash.cs +++ b/OpenRA.Mods.Common/Traits/AcceptsDeliveredCash.cs @@ -41,7 +41,7 @@ namespace OpenRA.Mods.Common.Traits void INotifyCashTransfer.OnAcceptingCash(Actor self, Actor donor) { if (info.Sounds.Length > 0) - Game.Sound.Play(SoundType.World, info.Sounds.Random(self.World.SharedRandom), self.CenterPosition); + Game.Sound.Play(SoundType.World, info.Sounds, self.World, self.CenterPosition); } void INotifyCashTransfer.OnDeliveringCash(Actor self, Actor acceptor) { } diff --git a/OpenRA.Mods.Common/Traits/Armament.cs b/OpenRA.Mods.Common/Traits/Armament.cs index 96a8682738..1bc9381e0a 100644 --- a/OpenRA.Mods.Common/Traits/Armament.cs +++ b/OpenRA.Mods.Common/Traits/Armament.cs @@ -327,10 +327,10 @@ namespace OpenRA.Mods.Common.Traits self.World.Add(projectile); if (args.Weapon.Report != null && args.Weapon.Report.Any()) - Game.Sound.Play(SoundType.World, args.Weapon.Report.Random(self.World.SharedRandom), self.CenterPosition); + Game.Sound.Play(SoundType.World, args.Weapon.Report, self.World, self.CenterPosition); if (Burst == args.Weapon.Burst && args.Weapon.StartBurstReport != null && args.Weapon.StartBurstReport.Any()) - Game.Sound.Play(SoundType.World, args.Weapon.StartBurstReport.Random(self.World.SharedRandom), self.CenterPosition); + Game.Sound.Play(SoundType.World, args.Weapon.StartBurstReport, self.World, self.CenterPosition); foreach (var na in notifyAttacks) na.Attacking(self, target, this, barrel); @@ -356,12 +356,7 @@ namespace OpenRA.Mods.Common.Traits Burst = Weapon.Burst; if (Weapon.AfterFireSound != null && Weapon.AfterFireSound.Any()) - { - ScheduleDelayedAction(Weapon.AfterFireSoundDelay, () => - { - Game.Sound.Play(SoundType.World, Weapon.AfterFireSound.Random(self.World.SharedRandom), self.CenterPosition); - }); - } + ScheduleDelayedAction(Weapon.AfterFireSoundDelay, () => Game.Sound.Play(SoundType.World, Weapon.AfterFireSound, self.World, self.CenterPosition)); foreach (var nbc in notifyBurstComplete) nbc.FiredBurst(self, target, this); diff --git a/OpenRA.Mods.Common/Traits/Conditions/GrantConditionOnDeploy.cs b/OpenRA.Mods.Common/Traits/Conditions/GrantConditionOnDeploy.cs index a8a39091b5..1874a3c522 100644 --- a/OpenRA.Mods.Common/Traits/Conditions/GrantConditionOnDeploy.cs +++ b/OpenRA.Mods.Common/Traits/Conditions/GrantConditionOnDeploy.cs @@ -222,7 +222,7 @@ namespace OpenRA.Mods.Common.Traits return; if (Info.DeploySounds != null && Info.DeploySounds.Any()) - Game.Sound.Play(SoundType.World, Info.DeploySounds.Random(self.World.LocalRandom), self.CenterPosition); + Game.Sound.Play(SoundType.World, Info.DeploySounds, self.World, self.CenterPosition); // Revoke condition that is applied while undeployed. if (!init) @@ -246,7 +246,7 @@ namespace OpenRA.Mods.Common.Traits return; if (Info.UndeploySounds != null && Info.UndeploySounds.Any()) - Game.Sound.Play(SoundType.World, Info.UndeploySounds.Random(self.World.LocalRandom), self.CenterPosition); + Game.Sound.Play(SoundType.World, Info.UndeploySounds, self.World, self.CenterPosition); if (!init) OnUndeployStarted(); diff --git a/OpenRA.Mods.Common/Traits/DeliversCash.cs b/OpenRA.Mods.Common/Traits/DeliversCash.cs index af45eb7008..4420cb26b6 100644 --- a/OpenRA.Mods.Common/Traits/DeliversCash.cs +++ b/OpenRA.Mods.Common/Traits/DeliversCash.cs @@ -81,7 +81,7 @@ namespace OpenRA.Mods.Common.Traits void INotifyCashTransfer.OnDeliveringCash(Actor self, Actor acceptor) { if (info.Sounds.Length > 0) - Game.Sound.Play(SoundType.World, info.Sounds.Random(self.World.SharedRandom), self.CenterPosition); + Game.Sound.Play(SoundType.World, info.Sounds, self.World, self.CenterPosition); } public class DeliversCashOrderTargeter : UnitOrderTargeter diff --git a/OpenRA.Mods.Common/Traits/Explodes.cs b/OpenRA.Mods.Common/Traits/Explodes.cs index af249204b3..72fe656433 100644 --- a/OpenRA.Mods.Common/Traits/Explodes.cs +++ b/OpenRA.Mods.Common/Traits/Explodes.cs @@ -110,7 +110,7 @@ namespace OpenRA.Mods.Common.Traits var source = Info.DamageSource == DamageSource.Self ? self : e.Attacker; if (weapon.Report != null && weapon.Report.Any()) - Game.Sound.Play(SoundType.World, weapon.Report.Random(source.World.SharedRandom), self.CenterPosition); + Game.Sound.Play(SoundType.World, weapon.Report, self.World, self.CenterPosition); if (Info.Type == ExplosionType.Footprint && buildingInfo != null) { diff --git a/OpenRA.Mods.Common/Traits/Sound/AttackSounds.cs b/OpenRA.Mods.Common/Traits/Sound/AttackSounds.cs index 4879ab2c94..3ce8b00e67 100644 --- a/OpenRA.Mods.Common/Traits/Sound/AttackSounds.cs +++ b/OpenRA.Mods.Common/Traits/Sound/AttackSounds.cs @@ -43,7 +43,7 @@ namespace OpenRA.Mods.Common.Traits.Sound void PlaySound(Actor self) { if (info.Sounds.Any()) - Game.Sound.Play(SoundType.World, info.Sounds.Random(self.World.SharedRandom), self.CenterPosition); + Game.Sound.Play(SoundType.World, info.Sounds, self.World, self.CenterPosition); } void INotifyAttack.Attacking(Actor self, Target target, Armament a, Barrel barrel) diff --git a/OpenRA.Mods.Common/Traits/ThrowsShrapnel.cs b/OpenRA.Mods.Common/Traits/ThrowsShrapnel.cs index 679cf91044..7085717983 100644 --- a/OpenRA.Mods.Common/Traits/ThrowsShrapnel.cs +++ b/OpenRA.Mods.Common/Traits/ThrowsShrapnel.cs @@ -94,7 +94,7 @@ namespace OpenRA.Mods.Common.Traits self.World.Add(projectile); if (args.Weapon.Report != null && args.Weapon.Report.Any()) - Game.Sound.Play(SoundType.World, args.Weapon.Report.Random(self.World.SharedRandom), self.CenterPosition); + Game.Sound.Play(SoundType.World, args.Weapon.Report, self.World, self.CenterPosition); } }); } diff --git a/OpenRA.Mods.D2k/Traits/SpiceBloom.cs b/OpenRA.Mods.D2k/Traits/SpiceBloom.cs index 4bac555ac6..b237944102 100644 --- a/OpenRA.Mods.D2k/Traits/SpiceBloom.cs +++ b/OpenRA.Mods.D2k/Traits/SpiceBloom.cs @@ -157,7 +157,7 @@ namespace OpenRA.Mods.D2k.Traits self.World.Add(projectile); if (args.Weapon.Report != null && args.Weapon.Report.Any()) - Game.Sound.Play(SoundType.World, args.Weapon.Report.Random(self.World.SharedRandom), self.CenterPosition); + Game.Sound.Play(SoundType.World, args.Weapon.Report, self.World, self.CenterPosition); } }); }