From 7284f52dcdea3cb4d994b35e06f88adf5c003882 Mon Sep 17 00:00:00 2001 From: John Pavek Date: Mon, 5 Feb 2024 00:57:11 -0600 Subject: [PATCH] Wow the cannon inventory works mostly --- Components/Singleton.cs | 2 + Entities/Archetypes/CannonBuilder.cs | 25 ++++- Entities/Archetypes/EnemyBuilder.cs | 2 +- Entities/Archetypes/PlayerBuilder.cs | 12 +- Scenes/Levels/Components/Ship.cs | 4 +- .../Systems/InventoryManagementSystem.cs | 105 ++++++++++++++---- Scenes/Levels/Systems/PlayerControlSystem.cs | 4 +- Scenes/Levels/Systems/UiSystem.cs | 2 +- Utilities/ContentData/DataExtensions.cs | 4 +- Utilities/ContentData/ShipData.cs | 29 ++--- Utilities/ContentData/TradeableGoodsData.cs | 8 +- Utilities/ShipSpriteBuilder.cs | 2 +- 12 files changed, 145 insertions(+), 54 deletions(-) diff --git a/Components/Singleton.cs b/Components/Singleton.cs index 2b53b59..3f6aa14 100644 --- a/Components/Singleton.cs +++ b/Components/Singleton.cs @@ -12,6 +12,8 @@ internal class Singleton public AudioKey Music; internal bool InventoryOpen; + + public bool EquipModeActive; } public enum DebugLevel diff --git a/Entities/Archetypes/CannonBuilder.cs b/Entities/Archetypes/CannonBuilder.cs index 2b9af25..5cf60eb 100644 --- a/Entities/Archetypes/CannonBuilder.cs +++ b/Entities/Archetypes/CannonBuilder.cs @@ -1,11 +1,13 @@ using NovemberPirates.Scenes.Levels.Components; +using NovemberPirates.Utilities; using NovemberPirates.Utilities.Data; +using System.Numerics; namespace NovemberPirates.Entities.Archetypes { internal static class CannonBuilder { - internal static Cannon Create(CannonType cannonType, BoatSide boatSide, int row) + internal static Cannon Create(HullType hull, CannonType cannonType, BoatSide boatSide, int row) { var cannon = new Cannon(); cannon.Placement = boatSide; @@ -24,6 +26,27 @@ internal static Cannon Create(CannonType cannonType, BoatSide boatSide, int row) cannon.BallDuration = ShipData.Instance.Data[$"{cannonType}{Stats.CannonballDuration}"]; cannon.ShotPer = ShipData.Instance.Data[$"{cannonType}{Stats.CannonsShotPer}"]; + + var cannonPosition = new Vector2( + ShipCannonCoords.Data[Enum.GetName(cannon.Placement)], + ShipCannonCoords.Data[$"{hull}{cannon.Row}"] + ); + + var cannonSprite = new Sprite(TextureKey.CannonLoose, "Assets/Art/cannonLoose") + { + Position = cannonPosition, + Rotation = cannon.Placement == BoatSide.Port ? 180 : 0, + }; + //cannons.Add(cannonSprite); + + //if (cannon.Position == Vector2.Zero) + // I have no idea why I needed these magic numbers, but they work. + cannon.Position = cannonPosition with + { + X = cannonPosition.X - cannonSprite.SpriteWidth - 10, + Y = cannonPosition.Y - cannonSprite.SpriteHeight - 25 + }; + return cannon; } } diff --git a/Entities/Archetypes/EnemyBuilder.cs b/Entities/Archetypes/EnemyBuilder.cs index 80c1cb1..f30bf9e 100644 --- a/Entities/Archetypes/EnemyBuilder.cs +++ b/Entities/Archetypes/EnemyBuilder.cs @@ -18,7 +18,7 @@ public static void CreateEnemyShip(World world, Vector2 position, Team team) ship.Team = team; ship.BoatColor = BoatColor.Yellow; - ship.BoatType = HullType.Large; + ship.HullType = HullType.Large; ship.Sail = SailStatus.Closed; ship.Crew = 10; diff --git a/Entities/Archetypes/PlayerBuilder.cs b/Entities/Archetypes/PlayerBuilder.cs index acfc0e6..fb3939b 100644 --- a/Entities/Archetypes/PlayerBuilder.cs +++ b/Entities/Archetypes/PlayerBuilder.cs @@ -18,12 +18,12 @@ internal static void Create(World world, Vector2 startPos) var ship = new Ship(HullType.Large, BoatColor.Dead, Team.November); ship.Sail = SailStatus.Closed; - ship.Cannons.Add(CannonBuilder.Create(Utilities.Data.CannonType.TrustyRusty, BoatSide.Starboard, 1)); - ship.Cannons.Add(CannonBuilder.Create(Utilities.Data.CannonType.TrustyRusty, BoatSide.Port, 1)); - ship.Cannons.Add(CannonBuilder.Create(Utilities.Data.CannonType.TrustyRusty, BoatSide.Starboard, 2)); - ship.Cannons.Add(CannonBuilder.Create(Utilities.Data.CannonType.TrustyRusty, BoatSide.Port, 2)); - ship.Cannons.Add(CannonBuilder.Create(Utilities.Data.CannonType.TrustyRusty, BoatSide.Starboard, 3)); - ship.Cannons.Add(CannonBuilder.Create(Utilities.Data.CannonType.TrustyRusty, BoatSide.Port, 3)); + ship.Cannons.Add(CannonBuilder.Create(ship.HullType, Utilities.Data.CannonType.TrustyRusty, BoatSide.Starboard, 1)); + ship.Cannons.Add(CannonBuilder.Create(ship.HullType, Utilities.Data.CannonType.TrustyRusty, BoatSide.Port, 1)); + ship.Cannons.Add(CannonBuilder.Create(ship.HullType, Utilities.Data.CannonType.TrustyRusty, BoatSide.Starboard, 2)); + ship.Cannons.Add(CannonBuilder.Create(ship.HullType, Utilities.Data.CannonType.TrustyRusty, BoatSide.Port, 2)); + ship.Cannons.Add(CannonBuilder.Create(ship.HullType, Utilities.Data.CannonType.TrustyRusty, BoatSide.Starboard, 3)); + ship.Cannons.Add(CannonBuilder.Create(ship.HullType, Utilities.Data.CannonType.TrustyRusty, BoatSide.Port, 3)); player.Set(ship); diff --git a/Scenes/Levels/Components/Ship.cs b/Scenes/Levels/Components/Ship.cs index 12a7cfd..49e2863 100644 --- a/Scenes/Levels/Components/Ship.cs +++ b/Scenes/Levels/Components/Ship.cs @@ -8,7 +8,7 @@ internal class Ship { public Ship(HullType hull, BoatColor color, Team team) { - BoatType = hull; + HullType = hull; BoatColor = color; Team = team; @@ -25,7 +25,7 @@ public Ship(HullType hull, BoatColor color, Team team) public BoatCondition BoatCondition = BoatCondition.Good; public BoatColor BoatColor; - public HullType BoatType; + public HullType HullType; public Team Team; internal SailStatus Sail = SailStatus.Closed; diff --git a/Scenes/Levels/Systems/InventoryManagementSystem.cs b/Scenes/Levels/Systems/InventoryManagementSystem.cs index dff7a4d..d2d09c9 100644 --- a/Scenes/Levels/Systems/InventoryManagementSystem.cs +++ b/Scenes/Levels/Systems/InventoryManagementSystem.cs @@ -1,6 +1,7 @@ using Arch.Core; using Arch.Core.Extensions; using NovemberPirates.Components; +using NovemberPirates.Entities.Archetypes; using NovemberPirates.Extensions; using NovemberPirates.Scenes.Levels.Components; using NovemberPirates.Scenes.Levels.DataManagers; @@ -57,40 +58,81 @@ internal override void UpdateNoCamera(World world) var shipChanged = false; - for (int i = 0; i < playerShip.Cannons.Count; i++) + if (singleton.EquipModeActive) { - var cannon = playerShip.Cannons[i]; + var maxRow = ShipData.Instance.Data[$"{playerShip.HullType}{Stats.MaxCannons}"] / 2; - var rect = new Rectangle( - (int)(leftCenter.X + (cannon.Placement == BoatSide.Port ? 40 : -120)), - (int)(leftCenter.Y + (cannon.Position.Y * 4) - 130), - 70, 60); - - Raylib.DrawRectangleLines((int)rect.X, (int)rect.Y, (int)rect.width, (int)rect.height, Raylib.BLACK); - - if (Raylib.IsMouseButtonPressed(MouseButton.MOUSE_BUTTON_LEFT) && Raylib.CheckCollisionPointRec(Raylib.GetMousePosition(), rect)) + for (int i = 1; i <= maxRow; i++) { - playerShip.Cannons.Remove(cannon); - InventoryManager.Instance.Inventory.Add(TradeableGoodsData.GetGood((TradeableGoodsNames)cannon.CannonType)); - shipChanged = true || shipChanged; + var cannon = CannonBuilder.Create(playerShip.HullType, CannonType.TrustyRusty, BoatSide.Port, i); + + var rect = new Rectangle( + (int)(leftCenter.X + (cannon.Placement == BoatSide.Port ? 40 : -120)), + (int)(leftCenter.Y + (cannon.Position.Y * 4) - 130), + 70, 60); + + Raylib.DrawRectangleLines((int)rect.X, (int)rect.Y, (int)rect.width, (int)rect.height, Raylib.BLACK); + + if (Raylib.IsMouseButtonPressed(MouseButton.MOUSE_BUTTON_LEFT) && Raylib.CheckCollisionPointRec(Raylib.GetMousePosition(), rect)) + { + singleton.EquipModeActive = false; + playerShip.Cannons.Add(cannon); + InventoryManager.Instance.Inventory.Remove(InventoryManager.Instance.Inventory.First(x => x.EquipModeActive)); + shipChanged = true || shipChanged; + break; + } + + var cannon2 = CannonBuilder.Create(playerShip.HullType, CannonType.TrustyRusty, BoatSide.Starboard, i); + var rect2 = new Rectangle( + (int)(leftCenter.X + (cannon2.Placement == BoatSide.Port ? 40 : -120)), + (int)(leftCenter.Y + (cannon2.Position.Y * 4) - 130), + 70, 60); + + Raylib.DrawRectangleLines((int)rect2.X, (int)rect2.Y, (int)rect2.width, (int)rect2.height, Raylib.BLACK); + + if (Raylib.IsMouseButtonPressed(MouseButton.MOUSE_BUTTON_LEFT) && Raylib.CheckCollisionPointRec(Raylib.GetMousePosition(), rect2)) + { + singleton.EquipModeActive = false; + playerShip.Cannons.Add(cannon2); + InventoryManager.Instance.Inventory.Remove(InventoryManager.Instance.Inventory.First(x => x.EquipModeActive)); + shipChanged = true || shipChanged; + break; + } } + } - if (shipChanged) + else { - Console.WriteLine("Ship Changed"); - var newboat = ShipSpriteBuilder.GenerateBoat(new BoatOptions(playerShip)); - newboat.Position = realShipSprite.Position; - newboat.Rotation = realShipSprite.Rotation; - playerEntity.Set(newboat); + for (int i = 0; i < playerShip.Cannons.Count; i++) + { + var cannon = playerShip.Cannons[i]; + + var rect = new Rectangle( + (int)(leftCenter.X + (cannon.Placement == BoatSide.Port ? 40 : -120)), + (int)(leftCenter.Y + (cannon.Position.Y * 4) - 130), + 70, 60); + + Raylib.DrawRectangleLines((int)rect.X, (int)rect.Y, (int)rect.width, (int)rect.height, Raylib.BLACK); + + if (Raylib.IsMouseButtonPressed(MouseButton.MOUSE_BUTTON_LEFT) && Raylib.CheckCollisionPointRec(Raylib.GetMousePosition(), rect)) + { + playerShip.Cannons.Remove(cannon); + InventoryManager.Instance.Inventory.Add(TradeableGoodsData.GetGood((TradeableGoodsNames)cannon.CannonType)); + shipChanged = true || shipChanged; + } + } } - Raylib.DrawText($"Crew: {playerShip.Crew} / {ShipData.Instance.Data[$"{playerShip.BoatType}{Stats.InitialCrew}"]}", rightAlignment, Raylib.GetScreenHeight() / 2 - heightOffset, 24, Raylib.WHITE); + + // Right Side + + Raylib.DrawText($"Crew: {playerShip.Crew} / {ShipData.Instance.Data[$"{playerShip.HullType}{Stats.InitialCrew}"]}", rightAlignment, Raylib.GetScreenHeight() / 2 - heightOffset, 24, Raylib.WHITE); heightOffset -= lineOffset; - Raylib.DrawText($"Hull: {playerShip.HullHealth} / {ShipData.Instance.Data[$"{playerShip.BoatType}{Stats.HullHealth}"]} ", rightAlignment, Raylib.GetScreenHeight() / 2 - heightOffset, 24, Raylib.WHITE); + Raylib.DrawText($"Hull: {playerShip.HullHealth} / {ShipData.Instance.Data[$"{playerShip.HullType}{Stats.HullHealth}"]} ", rightAlignment, Raylib.GetScreenHeight() / 2 - heightOffset, 24, Raylib.WHITE); heightOffset -= lineOffset; - Raylib.DrawText($"Sails: {playerShip.SailHealth} / {ShipData.Instance.Data[$"{playerShip.BoatType}{Stats.SailHealth}"]} ", rightAlignment, Raylib.GetScreenHeight() / 2 - heightOffset, 24, Raylib.WHITE); + Raylib.DrawText($"Sails: {playerShip.SailHealth} / {ShipData.Instance.Data[$"{playerShip.HullType}{Stats.SailHealth}"]} ", rightAlignment, Raylib.GetScreenHeight() / 2 - heightOffset, 24, Raylib.WHITE); heightOffset -= lineOffset; Raylib.DrawText($"Gold: {InventoryManager.Instance.Gold} ", rightAlignment, Raylib.GetScreenHeight() / 2 - heightOffset, 24, Raylib.WHITE); @@ -100,8 +142,27 @@ internal override void UpdateNoCamera(World world) foreach (var invGroup in InventoryManager.Instance.Inventory.GroupBy(x => x.Name)) { Raylib.DrawText($"{invGroup.Key}: {invGroup.Count()}", rightAlignment, Raylib.GetScreenHeight() / 2 - heightOffset, 24, Raylib.WHITE); + if (invGroup.Any(x => x.IsEquipable)) + { + if (RayGui.GuiButton(new Rectangle(rightAlignment + 100, Raylib.GetScreenHeight() / 2 - heightOffset, 100, 30), "Equip")) + { + singleton.EquipModeActive = true; + invGroup.First().EquipModeActive = true; + //InventoryManager.Instance.Inventory.Remove(invGroup.First()); + }; + } + + heightOffset -= lineOffset; } + + if (shipChanged) + { + var newboat = ShipSpriteBuilder.GenerateBoat(new BoatOptions(playerShip)); + newboat.Position = realShipSprite.Position; + newboat.Rotation = realShipSprite.Rotation; + playerEntity.Set(newboat); + } } } } diff --git a/Scenes/Levels/Systems/PlayerControlSystem.cs b/Scenes/Levels/Systems/PlayerControlSystem.cs index 8f713c3..909ff5e 100644 --- a/Scenes/Levels/Systems/PlayerControlSystem.cs +++ b/Scenes/Levels/Systems/PlayerControlSystem.cs @@ -54,13 +54,13 @@ internal override void Update(World world) { if (Raylib.IsKeyPressed(KeyboardKey.KEY_PAGE_UP)) { - playerShip.BoatType = (HullType)Math.Min(Enum.GetValues().Length - 1, (int)playerShip.BoatType + 1); + playerShip.HullType = (HullType)Math.Min(Enum.GetValues().Length - 1, (int)playerShip.HullType + 1); boatChanged = true; } if (Raylib.IsKeyPressed(KeyboardKey.KEY_PAGE_DOWN)) { - playerShip.BoatType = (HullType)Math.Max(0, (int)playerShip.BoatType - 1); + playerShip.HullType = (HullType)Math.Max(0, (int)playerShip.HullType - 1); boatChanged = true; } } diff --git a/Scenes/Levels/Systems/UiSystem.cs b/Scenes/Levels/Systems/UiSystem.cs index 5420956..6629c81 100644 --- a/Scenes/Levels/Systems/UiSystem.cs +++ b/Scenes/Levels/Systems/UiSystem.cs @@ -27,7 +27,7 @@ internal override void UpdateNoCamera(World world) if (singleton.Debug > DebugLevel.None) { - RayGui.GuiLabel(topleft, Enum.GetName(ship.Sail) + " " + ship.BoatType); + RayGui.GuiLabel(topleft, Enum.GetName(ship.Sail) + " " + ship.HullType); Raylib.DrawText(Raylib.GetFrameTime().ToString(), 10, 70, 20, Raylib.RED); Raylib.DrawFPS(10, 90); } diff --git a/Utilities/ContentData/DataExtensions.cs b/Utilities/ContentData/DataExtensions.cs index e36acf3..b7aaa72 100644 --- a/Utilities/ContentData/DataExtensions.cs +++ b/Utilities/ContentData/DataExtensions.cs @@ -6,11 +6,11 @@ internal static class DataExtensions { internal static bool CanDo(this Ship ship, ShipAbilities ability) { - return ship.Crew >= ShipData.Instance.Data[$"{ship.BoatType}{ability}"]; + return ship.Crew >= ShipData.Instance.Data[$"{ship.HullType}{ability}"]; } internal static float GetStat(this Ship ship, Stats stat) { - return ShipData.Instance.Data[$"{ship.BoatType}{stat}"]; + return ShipData.Instance.Data[$"{ship.HullType}{stat}"]; } } } diff --git a/Utilities/ContentData/ShipData.cs b/Utilities/ContentData/ShipData.cs index 0e456c2..7de2d6a 100644 --- a/Utilities/ContentData/ShipData.cs +++ b/Utilities/ContentData/ShipData.cs @@ -51,8 +51,8 @@ internal void SaveToJson() { $"{HullType.Small}{ShipAbilities.Rowing}", 2 }, { $"{HullType.Small}{ShipAbilities.HalfSail}", 3 }, { $"{HullType.Small}{ShipAbilities.FullSail}", 10 }, - { $"{HullType.Small}{ShipAbilities.OneCannon}", 5 }, - { $"{HullType.Small}{ShipAbilities.TwoCannon}", 7 }, + { $"{HullType.Small}{ShipAbilities.FireOneCannon}", 5 }, + { $"{HullType.Small}{ShipAbilities.FireTwoCannon}", 7 }, { $"{HullType.Small}{Stats.RowingSpeed}", 100}, { $"{HullType.Small}{Stats.TurningSpeed}", 100}, @@ -64,6 +64,7 @@ internal void SaveToJson() { $"{HullType.Small}{Stats.MaxCrew}" , 15}, { $"{HullType.Small}{Stats.HullHealth}" , 100}, { $"{HullType.Small}{Stats.SailHealth}" , 100}, + { $"{HullType.Small}{Stats.MaxCannons}" , 4}, // Medium Ships @@ -71,8 +72,8 @@ internal void SaveToJson() { $"{HullType.Medium}{ShipAbilities.Rowing}", 2 }, { $"{HullType.Medium}{ShipAbilities.HalfSail}", 3 }, { $"{HullType.Medium}{ShipAbilities.FullSail}", 10 }, - { $"{HullType.Medium}{ShipAbilities.OneCannon}", 5 }, - { $"{HullType.Medium}{ShipAbilities.TwoCannon}", 7 }, + { $"{HullType.Medium}{ShipAbilities.FireOneCannon}", 5 }, + { $"{HullType.Medium}{ShipAbilities.FireTwoCannon}", 7 }, { $"{HullType.Medium}{Stats.RowingSpeed}", 100}, @@ -85,14 +86,15 @@ internal void SaveToJson() { $"{HullType.Medium}{Stats.MaxCrew}" , 20}, { $"{HullType.Medium}{Stats.HullHealth}" , 100}, { $"{HullType.Medium}{Stats.SailHealth}" , 100}, + { $"{HullType.Medium}{Stats.MaxCannons}" , 4}, // Large Ships { $"{HullType.Large}{ShipAbilities.Steering}", 1 }, { $"{HullType.Large}{ShipAbilities.Rowing}", 2 }, { $"{HullType.Large}{ShipAbilities.HalfSail}", 3 }, { $"{HullType.Large}{ShipAbilities.FullSail}", 10 }, - { $"{HullType.Large}{ShipAbilities.OneCannon}", 5 }, - { $"{HullType.Large}{ShipAbilities.TwoCannon}", 7 }, + { $"{HullType.Large}{ShipAbilities.FireOneCannon}", 5 }, + { $"{HullType.Large}{ShipAbilities.FireTwoCannon}", 7 }, { $"{HullType.Large}{Stats.RowingSpeed}", 100}, { $"{HullType.Large}{Stats.TurningSpeed}", 100}, @@ -104,7 +106,7 @@ internal void SaveToJson() { $"{HullType.Large}{Stats.MaxCrew}" , 25}, { $"{HullType.Large}{Stats.HullHealth}" , 100}, { $"{HullType.Large}{Stats.SailHealth}" , 100}, - + { $"{HullType.Large}{Stats.MaxCannons}" , 6}, // Cannon types { $"{CannonType.TrustyRusty}{Stats.CannonReloadTime}", 0.5f }, @@ -154,12 +156,12 @@ internal enum ShipAbilities HalfSail, FullSail, - OneCannon, - TwoCannon, - ThreeCannon, - FourCannon, - FiveCannon, - SixCannon, + FireOneCannon, + FireTwoCannon, + FireThreeCannon, + FireFourCannon, + FireFiveCannon, + FireSixCannon, } internal enum CannonType @@ -196,5 +198,6 @@ internal enum Stats MaxCrew, HullHealth, SailHealth, + MaxCannons, } } diff --git a/Utilities/ContentData/TradeableGoodsData.cs b/Utilities/ContentData/TradeableGoodsData.cs index a68b0c6..bb60ca9 100644 --- a/Utilities/ContentData/TradeableGoodsData.cs +++ b/Utilities/ContentData/TradeableGoodsData.cs @@ -23,9 +23,9 @@ private void LoadPrices() Goods.Add(TradeableGoodsNames.Gems, new TradeableGood { Name = "Gems", Price = 80 }); //Cannon Prices - Goods.Add(TradeableGoodsNames.TrustyRusty, new TradeableGood { Name = "Trusty Rusty", Price = 100 }); - Goods.Add(TradeableGoodsNames.PvtPepper, new TradeableGood { Name = "Pvt Pepper", Price = 200 }); - Goods.Add(TradeableGoodsNames.BFC1700, new TradeableGood { Name = "BFC 1700", Price = 400 }); + Goods.Add(TradeableGoodsNames.TrustyRusty, new TradeableGood { Name = "Trusty Rusty", Price = 100, IsEquipable = true }); + Goods.Add(TradeableGoodsNames.PvtPepper, new TradeableGood { Name = "Pvt Pepper", Price = 200, IsEquipable = true }); + Goods.Add(TradeableGoodsNames.BFC1700, new TradeableGood { Name = "BFC 1700", Price = 400, IsEquipable = true }); } static internal TradeableGood GetGood(TradeableGoodsNames name) @@ -38,6 +38,8 @@ internal class TradeableGood { internal string Name; internal int Price; + internal bool IsEquipable = false; + internal bool EquipModeActive; } internal enum TradeableGoodsNames diff --git a/Utilities/ShipSpriteBuilder.cs b/Utilities/ShipSpriteBuilder.cs index 1bb1ea1..2559073 100644 --- a/Utilities/ShipSpriteBuilder.cs +++ b/Utilities/ShipSpriteBuilder.cs @@ -142,7 +142,7 @@ internal static Sprite GenerateBoat(BoatOptions options) } internal record BoatOptions(HullType Hull, BoatColor Color, SailStatus Sails, List Cannons, BoatCondition Condition = BoatCondition.Good) { - internal BoatOptions(Ship ship) : this(ship.BoatType, ship.BoatColor, ship.Sail, ship.Cannons, ship.BoatCondition) { } + internal BoatOptions(Ship ship) : this(ship.HullType, ship.BoatColor, ship.Sail, ship.Cannons, ship.BoatCondition) { } internal string ToKey() {