Skip to content

Commit

Permalink
Added a loader, and finished the refactoring for pathing
Browse files Browse the repository at this point in the history
  • Loading branch information
Nhawdge committed Dec 31, 2023
1 parent 1fe74a3 commit 725abee
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 35 deletions.
11 changes: 8 additions & 3 deletions NovemberPiratesEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,17 @@ public void GameLoop()
Raylib.BeginMode2D(Camera);

Raylib.ClearBackground(Raylib.BLACK);

ActiveScene.Systems.ForEach(system => system.Update(ActiveScene.World));
for (int i = 0; i < ActiveScene.Systems.Count; i++)
{
ActiveScene.Systems[i].Update(ActiveScene.World);
}

Raylib.EndMode2D();

ActiveScene.Systems.ForEach(system => system.UpdateNoCamera(ActiveScene.World));
for (int i = 0; i < ActiveScene.Systems.Count; i++)
{
ActiveScene.Systems[i].UpdateNoCamera(ActiveScene.World);
}
Raylib.EndDrawing();
}
}
Expand Down
4 changes: 4 additions & 0 deletions Scenes/BaseScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ internal abstract class BaseScene
public BaseScene()
{
AudioManager.Instance.StopAllSounds();
Systems.Add(new LoadingSystem(this));
}

internal Dictionary<string, Action> LoadingTasks = new();

internal World World = World.Create();

internal List<GameSystem> Systems = new();


public Vector2 MapEdge;
public int TileSize;
}
Expand Down
75 changes: 46 additions & 29 deletions Scenes/Levels/OceanScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,65 @@
using NovemberPirates.Scenes.Levels.Systems;
using NovemberPirates.Utilities;
using NovemberPirates.Utilities.Maps;
using Raylib_CsLo;

namespace NovemberPirates.Scenes.Levels
{
internal class OceanScene : BaseScene
{
internal OceanScene()
{
//var mapTilesArchetype = new ComponentType[] { typeof(MapTile), typeof(Render) };
//var entity = World.Create(mapTilesArchetype);
var singleton = World.Create<Singleton, Wind>();
singleton.Set(new Wind());
singleton.Set(new Singleton() { Music = AudioKey.Drifting });

Systems.Add(new RenderSystem());
Systems.Add(new ShipControlSystem());
Systems.Add(new CameraSystem());
Systems.Add(new WindSystem());
Systems.Add(new DebugSystem());
Systems.Add(new CannonBallSystem());
Systems.Add(new EnemyControlSystem());
Systems.Add(new EffectsSystem());
Systems.Add(new SpawningSystem());
Systems.Add(new PickupSystem());
Systems.Add(new AudioSystem());
Systems.Add(new NavigationSystem());
Systems.Add(new PlayerControlSystem());
Systems.Add(new PortSystem());
Systems.Add(new UiSystem());
LoadingTasks.Add("Map From File", () =>
{
var mapDetails = MapManager.Instance.LoadMap("Level_0", World);
MapEdge = mapDetails.MapEdge;
TileSize = mapDetails.TileSize;
});

var mapDetails = MapManager.Instance.LoadMap("Level_0", World);
MapEdge = mapDetails.MapEdge;
TileSize = mapDetails.TileSize;
LoadingTasks.Add("Player", () =>
{
PlayerBuilder.Create(World);
NovemberPiratesEngine.Instance.Camera.target.X = 3000;
});

PlayerBuilder.Create(World);
LoadingTasks.Add("World", () =>
{
NavigationUtilities.BuildMap(World);
});

var singleton = World.Create<Singleton, Wind>();
singleton.Set(new Wind());
singleton.Set(new Singleton() { Music = AudioKey.Drifting });
LoadingTasks.Add("Trade Routes", () =>
{
NavigationUtilities.AddTradeRoutes(World);
});

NavigationUtilities.BuildMap(World);
NavigationUtilities.AddTradeRoutes(World);
LoadingTasks.Add("Audio", () =>
{
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 });
});

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 });
LoadingTasks.Add("Systems", () =>
{
Systems.RemoveAll(x => true);
Systems.Add(new RenderSystem());
Systems.Add(new ShipControlSystem());
Systems.Add(new CameraSystem());
Systems.Add(new WindSystem());
Systems.Add(new DebugSystem());
Systems.Add(new CannonBallSystem());
Systems.Add(new EnemyControlSystem());
Systems.Add(new EffectsSystem());
Systems.Add(new SpawningSystem());
Systems.Add(new PickupSystem());
Systems.Add(new AudioSystem());
Systems.Add(new NavigationSystem());
Systems.Add(new PlayerControlSystem());
Systems.Add(new PortSystem());
Systems.Add(new UiSystem());
});
}
}
}
2 changes: 1 addition & 1 deletion Scenes/Levels/Systems/EnemyControlSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ internal override void Update(World world)
}
}

Console.WriteLine($"Target {ship.Target}");
//Console.WriteLine($"Target {ship.Target}");

if (singleton.Debug > DebugLevel.None)
{
Expand Down
61 changes: 61 additions & 0 deletions Scenes/LoadingSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Arch.Core;
using NovemberPirates.Systems;
using Raylib_CsLo;
using System.Diagnostics;
using System.Numerics;

namespace NovemberPirates.Scenes
{
internal class LoadingSystem : GameSystem
{
private BaseScene Scene;
private int CurrentLoading;
private bool LoadingComplete;
private Stopwatch Stopwatch = new();
private LoadingPhase Phase = LoadingPhase.Text;
public LoadingSystem(BaseScene baseScene)
{
this.Scene = baseScene;
}

internal override void Update(World world)
{
}

internal override void UpdateNoCamera(World world)
{
if (LoadingComplete || Scene.LoadingTasks.Count() == 0)
return;
var next = Scene.LoadingTasks.ElementAtOrDefault(CurrentLoading);
if (next.Value is not null)
{
var center = new Vector2(Raylib.GetScreenWidth() / 2, Raylib.GetScreenHeight() / 2);
var text = $"Loading {next.Key} ({CurrentLoading + 1} of {Scene.LoadingTasks.Count()})";
var size = 48;
var width = Raylib.MeasureText(text, size);
if (Phase == LoadingPhase.Text)
{
Raylib.DrawText(text, center.X - width / 2, center.Y, size, Raylib.BLUE);
Console.WriteLine(text);
Phase = LoadingPhase.Load;
}
else if (Phase == LoadingPhase.Load)
{
Stopwatch = Stopwatch.StartNew();
next.Value();
Phase = LoadingPhase.Text;
CurrentLoading++;
Console.WriteLine($"Done - {Stopwatch.ElapsedMilliseconds}ms ");
}
}
else
LoadingComplete = true;

}
private enum LoadingPhase
{
Text,
Load
}
}
}
1 change: 0 additions & 1 deletion Scenes/Menus/MainMenu/MainMenuScene.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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 Down
3 changes: 2 additions & 1 deletion Utilities/MapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ public MapDetails LoadMap(string key, World world)

mapTileEntity.Set(mapTile);
}

foreach (var entity in layer.EntityInstances)
{
Console.WriteLine($"entity: {entity.Identifier}");
//Console.WriteLine($"entity: {entity.Identifier}");
if (entity.Identifier == "Spawn_Point")
{
EnemyBuilder.CreateSpawnPoint(world, entity.Px.ToVector2(), Team.Red);
Expand Down

0 comments on commit 725abee

Please sign in to comment.