Skip to content

Commit

Permalink
Adds music and makes AI pathfinding a lot better.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nhawdge committed Nov 21, 2023
1 parent 116cb51 commit 0ac260d
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 8 deletions.
Binary file added Assets/Audio/Dreaming-of-Treasure.wav
Binary file not shown.
5 changes: 4 additions & 1 deletion Components/Singleton.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NovemberPirates.Utilities.Maps;
using NovemberPirates.Utilities;
using NovemberPirates.Utilities.Maps;

namespace NovemberPirates.Components
{
Expand All @@ -8,6 +9,8 @@ internal class Singleton
public string DebugText = "";

internal Map Map;

Check warning on line 11 in Components/Singleton.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'Map' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

public AudioKey Music;
}

public enum DebugLevel
Expand Down
6 changes: 6 additions & 0 deletions Scenes/BaseScene.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
using Arch.Core;
using NovemberPirates.Systems;
using NovemberPirates.Utilities;
using System.Numerics;

namespace NovemberPirates.Scenes
{
internal abstract class BaseScene
{
public BaseScene()
{
AudioManager.Instance.StopAllSounds();
}

internal World World = World.Create();

internal List<GameSystem> Systems = new();
Expand Down
12 changes: 5 additions & 7 deletions Scenes/Levels/Systems/EnemyControlSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ internal override void Update(World world)
}
});
}
Console.WriteLine($"Next: {ship.NextPatrolPoint}");

if (ship.Route.Count == 0)
if (ship.Route.Count < 10)
{
if (NavTask == null)
NavTask = new Task<List<Vector2>>(() =>
Expand Down Expand Up @@ -131,9 +130,7 @@ internal override void Update(World world)
openTiles.AddRange(neighbors);
openTiles.RemoveAll(x => x.Coords == openTile.Coords);

//Console.WriteLine($"Adding {openTile.Coords}");

Console.WriteLine($"Open Count: {openTiles.Count()}\t Closed Count: {closedTiles.Count()}\t{openTile.DistanceFrom} => {openTile.DistanceTo}");
//Console.WriteLine($"Open Count: {openTiles.Count()}\t Closed Count: {closedTiles.Count()}\t{openTile.DistanceFrom} => {openTile.DistanceTo}");
}
//var lastStep = Vector2.Zero;
var route = new List<Vector2>();
Expand Down Expand Up @@ -164,7 +161,9 @@ internal override void Update(World world)
ship.Target = sailTargetVec.Value;
if (sprite.Position.DistanceTo(ship.Target) < 300)
{
ship.Route?.RemoveAt(0);
if (ship.Route?.Count > 0)
ship.Route?.RemoveAt(0);

if (ship.Route?.Count == 0)
{
ship.NextPatrolPoint += 1;
Expand All @@ -175,7 +174,6 @@ internal override void Update(World world)
if (singleton.Debug >= DebugLevel.Low)
Raylib.DrawLine((int)ship.Target.X, (int)ship.Target.Y, (int)sprite.Position.X, (int)sprite.Position.Y, Raylib.RED);


if (ship.Target != Vector2.Zero)
{
if (ship.CanDo(ShipAbilities.Steering))
Expand Down
5 changes: 5 additions & 0 deletions Scenes/Menus/MainMenu/MainMenuScene.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NovemberPirates.Components;
using NovemberPirates.Scenes.Levels;
using NovemberPirates.Scenes.Levels.Systems;
using NovemberPirates.Scenes.Menus.Components;
using NovemberPirates.Scenes.Menus.Systems;
using NovemberPirates.Utilities;
Expand All @@ -13,6 +14,10 @@ internal class MainMenuScene : BaseScene
public MainMenuScene()
{
Systems.Add(new MenuSystem());
Systems.Add(new MenuMusicSystem());

World.Create(new Singleton() { Music = AudioKey.DreamingOfTreasure });


//var title = World.Create<UiTitle>();
//var uiTitle = new UiTitle() { Text = "November Pirates" };
Expand Down
30 changes: 30 additions & 0 deletions Scenes/Menus/Systems/MenuMusicSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Arch.Core;
using Arch.Core.Extensions;
using NovemberPirates.Components;
using NovemberPirates.Extensions;
using NovemberPirates.Systems;
using NovemberPirates.Utilities;
using Raylib_CsLo;

namespace NovemberPirates.Scenes.Menus.Systems
{
internal class MenuMusicSystem : GameSystem
{
internal override void Update(World world)
{
var singletonEntity = world.QueryFirst<Singleton>();
var singleton = singletonEntity.Get<Singleton>();


var music = AudioManager.Instance.GetMusic(singleton.Music);
if (Raylib.IsMusicStreamPlaying(music))
{
Raylib.UpdateMusicStream(music);
}
else
{
Raylib.PlayMusicStream(AudioManager.Instance.GetMusic(singleton.Music));
}
}
}
}
27 changes: 27 additions & 0 deletions Utilities/AudioManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ private AudioManager() { }
public static AudioManager Instance = new();

public Dictionary<AudioKey, Sound[]> AudioStore = new();
public Dictionary<AudioKey, Music> MusicStore = new();


internal void LoadAllAudio()
{
Expand Down Expand Up @@ -60,6 +62,8 @@ internal void LoadAllAudio()
{
Raylib.LoadSound("Assets/Audio/sfx_WindinSail.ogg"),
});

MusicStore.Add(AudioKey.DreamingOfTreasure, Raylib.LoadMusicStream("Assets/Audio/Dreaming-of-Treasure.wav"));
}
internal Sound GetSound(AudioKey key)
{
Expand All @@ -70,6 +74,28 @@ internal Sound GetSound(AudioKey key)
var group = AudioStore[key];
return group[Random.Shared.Next(0, group.Length)];
}

internal Music GetMusic(AudioKey key)
{
if (AudioStore.Count <= 0)
{
LoadAllAudio();
}
return MusicStore[key];
}

internal void StopAllSounds()
{
AudioStore.ToList().ForEach(x =>
{
x.Value.ToList().ForEach(s => Raylib.StopSound(s));
});

MusicStore.ToList().ForEach(x =>
{
Raylib.StopMusicStream(x.Value);
});
}
}
internal enum AudioKey
{
Expand All @@ -83,5 +109,6 @@ internal enum AudioKey
SailOpen,
WindInSail,
CannonHitShip,
DreamingOfTreasure,
}
}

0 comments on commit 0ac260d

Please sign in to comment.