Skip to content

Commit

Permalink
Look-- Listen! Sounds!
Browse files Browse the repository at this point in the history
  • Loading branch information
Nhawdge committed Nov 11, 2023
1 parent b27e2c9 commit 996c36b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Components/AudioEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ internal class AudioEvent
{
internal AudioKey Key;
internal Vector2 Position;
internal bool Replay = false;
internal bool AllowMultiple = true;
}
}
2 changes: 1 addition & 1 deletion Components/Cannonball.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Scenes/Levels/OceanScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ internal OceanScene()
singleton.Set(new Singleton());

NavigationUtilities.BuildMap(World);

World.Create<AudioEvent>().Set(new AudioEvent() { Key = AudioKey.ShipSailingWater, Replay = true, AllowMultiple = false });
World.Create<AudioEvent>().Set(new AudioEvent() { Key = AudioKey.Wind, Replay = true, AllowMultiple = false });
}
}
}
30 changes: 28 additions & 2 deletions Scenes/Levels/Systems/AudioSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using NovemberPirates.Systems;
using NovemberPirates.Utilities;
using Raylib_CsLo;
using System.Numerics;

namespace NovemberPirates.Scenes.Levels.Systems
{
Expand All @@ -18,8 +19,33 @@ internal override void Update(World world)
world.Query(in audioQuery, (entity) =>
{
var audioEvent = entity.Get<AudioEvent>();
Raylib.PlaySound(AudioManager.Instance.GetSound(audioEvent.Key));
world.Destroy(entity);
var sound = AudioManager.Instance.GetSound(audioEvent.Key);

var playerEntity = world.QueryFirst<Player>();
var playerSprite = playerEntity.Get<Sprite>();

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

});

Expand Down
2 changes: 2 additions & 0 deletions Scenes/Levels/Systems/CannonBallSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ internal override void Update(World world)
cannonball.Elapsed += Raylib.GetFrameTime();
if (cannonball.Elapsed > cannonball.Duration)
{
var sound = world.Create<AudioEvent>();
sound.Set(new AudioEvent() { Key = AudioKey.CannonHitWater, Position = sprite.Position });
world.Destroy(entity);
}

Expand Down
7 changes: 5 additions & 2 deletions Scenes/Levels/Systems/EnemyControlSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,17 +28,19 @@ internal override void Update(World world)
var ship = entity.Get<Ship>();
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<AudioEvent>();
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;
}
Expand Down

0 comments on commit 996c36b

Please sign in to comment.