Skip to content

Commit

Permalink
Some settings and controls rework
Browse files Browse the repository at this point in the history
  • Loading branch information
Nhawdge committed Feb 4, 2024
1 parent 224c89f commit 814ceac
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 49 deletions.
1 change: 1 addition & 0 deletions Components/Singleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ internal class Singleton
internal Map Map;

public AudioKey Music;
internal bool InventoryOpen;
}

public enum DebugLevel
Expand Down
2 changes: 0 additions & 2 deletions NovemberPiratesEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public void StartGame()

ActiveScene = new MainMenuScene();

var data = ShipData.Instance;

while (!Raylib.WindowShouldClose())
{
GameLoop();
Expand Down
1 change: 1 addition & 0 deletions Scenes/Levels/OceanScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ internal OceanScene()
Systems.Add(new CannonBallSystem());
Systems.Add(new EnemyControlSystem());
Systems.Add(new EffectsSystem());
Systems.Add(new InventorySystem());
Systems.Add(new SpawningSystem());
Systems.Add(new PickupSystem());
Systems.Add(new AudioSystem());
Expand Down
27 changes: 27 additions & 0 deletions Scenes/Levels/Systems/InventorySystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Arch.Core;
using Arch.Core.Extensions;
using NovemberPirates.Components;
using NovemberPirates.Extensions;
using NovemberPirates.Systems;
using Raylib_CsLo;

namespace NovemberPirates.Scenes.Levels.Systems
{
internal class InventorySystem : GameSystem
{
internal override void Update(World world) { }

internal override void UpdateNoCamera(World world)
{
var singletonEntity = world.QueryFirst<Singleton>();
var singleton = singletonEntity.Get<Singleton>();

if (singleton.InventoryOpen)
{
Console.WriteLine("Inventory Open");
Raylib.DrawText("Inventory", 0, 0, 24, Raylib.BLUE);
Raylib.DrawRectangle(Raylib.GetScreenWidth() / 2 - 200, Raylib.GetScreenHeight() / 2 - 200, 400, 400, Raylib.Fade(Raylib.BLACK, 0.8f));
}
}
}
}
237 changes: 193 additions & 44 deletions Scenes/Levels/Systems/PlayerControlSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,52 +12,159 @@ namespace NovemberPirates.Scenes.Levels.Systems
{
internal class PlayerControlSystem : GameSystem
{
private Dictionary<ControlModes, List<Control>> Controls = new();
internal override void Update(World world)
{
if (Controls.Count == 0)
{
LoadControls(world);
}

var singletonEntity = world.QueryFirst<Singleton>();
var singleton = singletonEntity.Get<Singleton>();

var controlMode = singleton.InventoryOpen ? ControlModes.Inventory : ControlModes.Standard;

var playerEntity = world.QueryFirst<Player>();
//var player = playerEntity.Get<Player>();

var sprite = playerEntity.Get<Sprite>();
var playerShip = playerEntity.Get<Ship>();

var boatChanged = false;

if (Raylib.IsKeyPressed(KeyboardKey.KEY_W))
foreach (var control in Controls[controlMode])
{
var priorSail = playerShip.Sail;
playerShip.Sail = (SailStatus)Math.Min(Enum.GetValues<SailStatus>().Length - 1, (int)playerShip.Sail + 1);
if (control.RepeatPress)
{
if (Raylib.IsKeyDown(control.Key))
{
boatChanged = control.Action() || boatChanged;
}
}
else
{
if (Raylib.IsKeyPressed(control.Key))
{
boatChanged = control.Action() || boatChanged;
}
}
}

if (priorSail != playerShip.Sail && playerShip.Sail >= SailStatus.Half)
world.Create<AudioEvent>().Set(new AudioEvent() { Key = AudioKey.SailOpen, Position = sprite.Position });
if (singleton.Debug > DebugLevel.None)
{
if (Raylib.IsKeyPressed(KeyboardKey.KEY_PAGE_UP))
{
playerShip.BoatType = (HullType)Math.Min(Enum.GetValues<HullType>().Length - 1, (int)playerShip.BoatType + 1);
boatChanged = true;

boatChanged = true;
}
if (Raylib.IsKeyPressed(KeyboardKey.KEY_PAGE_DOWN))
{
playerShip.BoatType = (HullType)Math.Max(0, (int)playerShip.BoatType - 1);
boatChanged = true;
}
}
if (Raylib.IsKeyPressed(KeyboardKey.KEY_S))
{
var priorSail = playerShip.Sail;
playerShip.Sail = (SailStatus)Math.Max(0, (int)playerShip.Sail - 1);

if (priorSail != playerShip.Sail && playerShip.Sail >= SailStatus.Rowing)
world.Create<AudioEvent>().Set(new AudioEvent() { Key = AudioKey.SailClose, Position = sprite.Position });
if (boatChanged)
{
var newboat = ShipSpriteBuilder.GenerateBoat(new BoatOptions(playerShip));
newboat.Position = sprite.Position;
newboat.Rotation = sprite.Rotation;
playerEntity.Set(newboat);

boatChanged = true;
//sprite.Texture = newboat.Texture;
}
if (Raylib.IsKeyDown(KeyboardKey.KEY_A))
}
public void LoadControls(World world)
{
var singletonEntity = world.QueryFirst<Singleton>();
var singleton = singletonEntity.Get<Singleton>();

Controls.Add(ControlModes.Standard, new List<Control>());
Controls.Add(ControlModes.Inventory, new List<Control>());

Controls[ControlModes.Standard].Add(new Control()
{
sprite.Rotation -= playerShip.RotationSpeed * Raylib.GetFrameTime();
}
if (Raylib.IsKeyDown(KeyboardKey.KEY_D))
Key = KeyboardKey.KEY_W,
RepeatPress = false,
Action = () =>
{
var playerEntity = world.QueryFirst<Player>();
var sprite = playerEntity.Get<Sprite>();
var playerShip = playerEntity.Get<Ship>();

var priorSail = playerShip.Sail;
playerShip.Sail = (SailStatus)Math.Min(Enum.GetValues<SailStatus>().Length - 1, (int)playerShip.Sail + 1);

if (priorSail != playerShip.Sail && playerShip.Sail >= SailStatus.Half)
{
world.Create<AudioEvent>().Set(new AudioEvent() { Key = AudioKey.SailOpen, Position = sprite.Position });
return true;
}

return false;
}
});

Controls[ControlModes.Standard].Add(new Control()
{
sprite.Rotation += playerShip.RotationSpeed * Raylib.GetFrameTime();
}
Key = KeyboardKey.KEY_S,
RepeatPress = false,
Action = () =>
{
var playerEntity = world.QueryFirst<Player>();
var sprite = playerEntity.Get<Sprite>();
var playerShip = playerEntity.Get<Ship>();

var priorSail = playerShip.Sail;
playerShip.Sail = (SailStatus)Math.Max(0, (int)playerShip.Sail - 1);

if (priorSail != playerShip.Sail && playerShip.Sail >= SailStatus.Rowing)
{
world.Create<AudioEvent>().Set(new AudioEvent() { Key = AudioKey.SailClose, Position = sprite.Position });
return true;
}
return false;
}
});

Controls[ControlModes.Standard].Add(new Control()
{
Key = KeyboardKey.KEY_A,
RepeatPress = true,
Action = () =>
{
var playerEntity = world.QueryFirst<Player>();
var sprite = playerEntity.Get<Sprite>();
var playerShip = playerEntity.Get<Ship>();

sprite.Rotation -= playerShip.RotationSpeed * Raylib.GetFrameTime();
return false;
}
});

Controls[ControlModes.Standard].Add(new Control()
{
Key = KeyboardKey.KEY_D,
RepeatPress = true,
Action = () =>
{
var playerEntity = world.QueryFirst<Player>();
var sprite = playerEntity.Get<Sprite>();
var playerShip = playerEntity.Get<Ship>();

sprite.Rotation += playerShip.RotationSpeed * Raylib.GetFrameTime();
return false;
}
});


if (Raylib.IsKeyPressed(KeyboardKey.KEY_Q) || Raylib.IsKeyDown(KeyboardKey.KEY_LEFT))
var fireLeftCannon = () =>
{
var playerEntity = world.QueryFirst<Player>();
var sprite = playerEntity.Get<Sprite>();
var playerShip = playerEntity.Get<Ship>();

// fire cannon Port
var nextCannon = playerShip.Cannons.FirstOrDefault(x => x.ReloadElapsed >= x.ReloadTime && x.Placement == BoatSide.Port);

Expand All @@ -72,10 +179,14 @@ internal override void Update(World world)
var sound = world.Create<AudioEvent>();
sound.Set(new AudioEvent() { Position = cannonPos, Key = AudioKey.CannonFire });
}
}
if (Raylib.IsKeyPressed(KeyboardKey.KEY_E) || Raylib.IsKeyDown(KeyboardKey.KEY_RIGHT))
return false;
};

var fireRightCannon = () =>
{
// fire cannon Starboard
var playerEntity = world.QueryFirst<Player>();
var sprite = playerEntity.Get<Sprite>();
var playerShip = playerEntity.Get<Ship>();

var nextCannon = playerShip.Cannons.FirstOrDefault(x => x.ReloadElapsed >= x.ReloadTime && x.Placement == BoatSide.Starboard);

Expand All @@ -90,32 +201,70 @@ internal override void Update(World world)
var sound = world.Create<AudioEvent>();
sound.Set(new AudioEvent() { Position = cannonPos, Key = AudioKey.CannonFire });
}
}
return false;
};

if (singleton.Debug > DebugLevel.None)
Controls[ControlModes.Standard].Add(new Control()
{
if (Raylib.IsKeyPressed(KeyboardKey.KEY_PAGE_UP))
{
playerShip.BoatType = (HullType)Math.Min(Enum.GetValues<HullType>().Length - 1, (int)playerShip.BoatType + 1);
boatChanged = true;
Key = KeyboardKey.KEY_Q,
RepeatPress = false,
Action = fireLeftCannon
});

}
if (Raylib.IsKeyPressed(KeyboardKey.KEY_PAGE_DOWN))
{
playerShip.BoatType = (HullType)Math.Max(0, (int)playerShip.BoatType - 1);
boatChanged = true;
Controls[ControlModes.Standard].Add(new Control()
{
Key = KeyboardKey.KEY_LEFT,
RepeatPress = true,
Action = fireLeftCannon
});

}
}
if (boatChanged)

Controls[ControlModes.Standard].Add(new Control()
{
var newboat = ShipSpriteBuilder.GenerateBoat(new BoatOptions(playerShip));
newboat.Position = sprite.Position;
newboat.Rotation = sprite.Rotation;
playerEntity.Set(newboat);
Key = KeyboardKey.KEY_E,
RepeatPress = false,
Action = fireRightCannon
});

//sprite.Texture = newboat.Texture;
}
Controls[ControlModes.Standard].Add(new Control()
{
Key = KeyboardKey.KEY_RIGHT,
RepeatPress = true,
Action = fireRightCannon
});

var openInventory = () =>
{
singleton.InventoryOpen = !singleton.InventoryOpen;
return false;
};

Controls[ControlModes.Standard].Add(new Control()
{
Key = KeyboardKey.KEY_TAB,
RepeatPress = false,
Action = openInventory
});

Controls[ControlModes.Inventory].Add(new Control()
{
Key = KeyboardKey.KEY_TAB,
RepeatPress = false,
Action = openInventory
});
}
}

public class Control
{
public KeyboardKey Key;
public Func<bool> Action = () => { return false; };
public bool RepeatPress;
}

public enum ControlModes
{
Standard,
Inventory,
}
}
1 change: 0 additions & 1 deletion Scenes/Menus/MainMenu/PauseScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using NovemberPirates.Extensions;
using NovemberPirates.Scenes.Menus.Components;
using NovemberPirates.Scenes.Menus.Systems;
using System.ComponentModel.Design;

namespace NovemberPirates.Scenes.Menus.MainMenu
{
Expand Down
11 changes: 11 additions & 0 deletions Scenes/Menus/MainMenu/SettingsScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ public SettingsScene(Singleton singleton, BaseScene lastscene)
SettingKey = SettingsManager.SettingKeys.SfxVolume,
});

World.Create(new UiButton
{
Text = "Full Screen",
Action = () =>
{
Raylib.ToggleFullscreen();
SettingsManager.Instance.Settings[SettingsManager.SettingKeys.Fullscreen] = Raylib.IsWindowFullscreen() ? 1 : 0;
},
Order = index++
});

World.Create(new UiButton
{
Text = "Save",
Expand Down
5 changes: 3 additions & 2 deletions Utilities/SettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal void LoadSettings()
Settings.Add(SettingKeys.MainVolume, 50f);
Settings.Add(SettingKeys.MusicVolume, 50f);
Settings.Add(SettingKeys.SfxVolume, 50f);

Settings.Add(SettingKeys.Fullscreen, 0);
}
}

Expand All @@ -51,7 +51,8 @@ internal enum SettingKeys
MainVolume,
MusicVolume,
SfxVolume,
Language
Language,
Fullscreen
}
}
}

0 comments on commit 814ceac

Please sign in to comment.