diff --git a/Components/AudioEvent.cs b/Components/AudioEvent.cs index 6e3cfce..1363b19 100644 --- a/Components/AudioEvent.cs +++ b/Components/AudioEvent.cs @@ -7,5 +7,7 @@ internal class AudioEvent { internal AudioKey Key; internal Vector2 Position; + internal bool Replay = false; + internal bool AllowMultiple = true; } } diff --git a/Components/Cannonball.cs b/Components/Cannonball.cs index 2f36868..65f75c1 100644 --- a/Components/Cannonball.cs +++ b/Components/Cannonball.cs @@ -6,7 +6,7 @@ internal class Cannonball { internal Vector2 Motion; internal Team FiredBy; - internal float Duration = 8f; + internal float Duration = 0.75f; internal float Elapsed = 0f; } internal enum Team diff --git a/Scenes/Levels/OceanScene.cs b/Scenes/Levels/OceanScene.cs index 1000381..0f46b23 100644 --- a/Scenes/Levels/OceanScene.cs +++ b/Scenes/Levels/OceanScene.cs @@ -41,6 +41,9 @@ internal OceanScene() singleton.Set(new Singleton()); NavigationUtilities.BuildMap(World); + + World.Create().Set(new AudioEvent() { Key = AudioKey.ShipSailingWater, Replay = true, AllowMultiple = false }); + World.Create().Set(new AudioEvent() { Key = AudioKey.Wind, Replay = true, AllowMultiple = false }); } } } diff --git a/Scenes/Levels/Systems/AudioSystem.cs b/Scenes/Levels/Systems/AudioSystem.cs index 0cb30d3..42c8634 100644 --- a/Scenes/Levels/Systems/AudioSystem.cs +++ b/Scenes/Levels/Systems/AudioSystem.cs @@ -5,6 +5,7 @@ using NovemberPirates.Systems; using NovemberPirates.Utilities; using Raylib_CsLo; +using System.Numerics; namespace NovemberPirates.Scenes.Levels.Systems { @@ -18,8 +19,33 @@ internal override void Update(World world) world.Query(in audioQuery, (entity) => { var audioEvent = entity.Get(); - Raylib.PlaySound(AudioManager.Instance.GetSound(audioEvent.Key)); - world.Destroy(entity); + var sound = AudioManager.Instance.GetSound(audioEvent.Key); + + var playerEntity = world.QueryFirst(); + var playerSprite = playerEntity.Get(); + + var distance = Vector2.Distance(playerSprite.Position, audioEvent.Position); + var maxDistance = 1280f; // TODO Get dynamically later + if (distance < maxDistance || audioEvent.Position == Vector2.Zero) + { + var volume = 1 - (distance / maxDistance); + var range = maxDistance * 2; + var pan = ((audioEvent.Position.X - playerSprite.Position.X) + maxDistance) / range; + + if (audioEvent.Key == AudioKey.CannonHitWater) + { + Console.WriteLine($"Distance: {distance}\tvol: {volume}\tpan: {pan}"); + } + Raylib.SetSoundPan(sound, pan); + + Raylib.SetSoundVolume(sound, volume); + + if (audioEvent.AllowMultiple || !Raylib.IsSoundPlaying(sound)) + Raylib.PlaySound(sound); + } + + if (!audioEvent.Replay) + world.Destroy(entity); }); diff --git a/Scenes/Levels/Systems/CannonBallSystem.cs b/Scenes/Levels/Systems/CannonBallSystem.cs index c6eb223..47a85af 100644 --- a/Scenes/Levels/Systems/CannonBallSystem.cs +++ b/Scenes/Levels/Systems/CannonBallSystem.cs @@ -26,6 +26,8 @@ internal override void Update(World world) cannonball.Elapsed += Raylib.GetFrameTime(); if (cannonball.Elapsed > cannonball.Duration) { + var sound = world.Create(); + sound.Set(new AudioEvent() { Key = AudioKey.CannonHitWater, Position = sprite.Position }); world.Destroy(entity); } diff --git a/Scenes/Levels/Systems/EnemyControlSystem.cs b/Scenes/Levels/Systems/EnemyControlSystem.cs index 47b3672..e4c3748 100644 --- a/Scenes/Levels/Systems/EnemyControlSystem.cs +++ b/Scenes/Levels/Systems/EnemyControlSystem.cs @@ -4,6 +4,7 @@ using NovemberPirates.Entities.Archetypes; using NovemberPirates.Extensions; using NovemberPirates.Systems; +using NovemberPirates.Utilities; using NovemberPirates.Utilities.Maps; using Raylib_CsLo; using System.Numerics; @@ -27,17 +28,19 @@ internal override void Update(World world) var ship = entity.Get(); var shouldMakeNewFire = Random.Shared.Next(0, 100) < 5; - if (ship.BoatCondition == Utilities.BoatCondition.Broken && shouldMakeNewFire) + if (ship.BoatCondition == BoatCondition.Broken && shouldMakeNewFire) { EffectsBuilder.CreateFire(world, sprite.Position + new Vector2(Random.Shared.Next(-30, 30), Random.Shared.Next(-50, 30))); if (ship.Crew > 0 && Random.Shared.Next(0, 100) < 5) { ship.Crew -= 1; ship.HullHealth += 1; + var sound = world.Create(); + sound.Set(new AudioEvent() { Key = AudioKey.CrewHitWater }); PickupBuilder.CreateCrewMember(world, sprite.Position); } } - if (ship.BoatCondition == Utilities.BoatCondition.Empty) + if (ship.BoatCondition == BoatCondition.Empty) { sprite.Position += wind.WindDirection * Raylib.GetFrameTime() * wind.WindStrength * .1f; }