Skip to content

Commit

Permalink
Wow the cannon inventory works mostly
Browse files Browse the repository at this point in the history
  • Loading branch information
Nhawdge committed Feb 5, 2024
1 parent bab652c commit 7284f52
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 54 deletions.
2 changes: 2 additions & 0 deletions Components/Singleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ internal class Singleton

public AudioKey Music;
internal bool InventoryOpen;

public bool EquipModeActive;
}

public enum DebugLevel
Expand Down
25 changes: 24 additions & 1 deletion Entities/Archetypes/CannonBuilder.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)],

Check warning on line 31 in Entities/Archetypes/CannonBuilder.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'key' in 'int Dictionary<string, int>.this[string key]'.
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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Entities/Archetypes/EnemyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
12 changes: 6 additions & 6 deletions Entities/Archetypes/PlayerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions Scenes/Levels/Components/Ship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal class Ship
{
public Ship(HullType hull, BoatColor color, Team team)
{
BoatType = hull;
HullType = hull;
BoatColor = color;
Team = team;

Expand All @@ -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;

Expand Down
105 changes: 83 additions & 22 deletions Scenes/Levels/Systems/InventoryManagementSystem.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Scenes/Levels/Systems/PlayerControlSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ internal override void Update(World world)
{
if (Raylib.IsKeyPressed(KeyboardKey.KEY_PAGE_UP))
{
playerShip.BoatType = (HullType)Math.Min(Enum.GetValues<HullType>().Length - 1, (int)playerShip.BoatType + 1);
playerShip.HullType = (HullType)Math.Min(Enum.GetValues<HullType>().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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Scenes/Levels/Systems/UiSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal override void UpdateNoCamera(World world)

if (singleton.Debug > DebugLevel.None)
{
RayGui.GuiLabel(topleft, Enum.GetName<SailStatus>(ship.Sail) + " " + ship.BoatType);
RayGui.GuiLabel(topleft, Enum.GetName<SailStatus>(ship.Sail) + " " + ship.HullType);
Raylib.DrawText(Raylib.GetFrameTime().ToString(), 10, 70, 20, Raylib.RED);
Raylib.DrawFPS(10, 90);
}
Expand Down
4 changes: 2 additions & 2 deletions Utilities/ContentData/DataExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"];
}
}
}
29 changes: 16 additions & 13 deletions Utilities/ContentData/ShipData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -64,15 +64,16 @@ 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
{ $"{HullType.Medium}{ShipAbilities.Steering}", 1 },
{ $"{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},
Expand All @@ -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},
Expand All @@ -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 },
Expand Down Expand Up @@ -154,12 +156,12 @@ internal enum ShipAbilities
HalfSail,
FullSail,

OneCannon,
TwoCannon,
ThreeCannon,
FourCannon,
FiveCannon,
SixCannon,
FireOneCannon,
FireTwoCannon,
FireThreeCannon,
FireFourCannon,
FireFiveCannon,
FireSixCannon,
}

internal enum CannonType
Expand Down Expand Up @@ -196,5 +198,6 @@ internal enum Stats
MaxCrew,
HullHealth,
SailHealth,
MaxCannons,
}
}
8 changes: 5 additions & 3 deletions Utilities/ContentData/TradeableGoodsData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -38,6 +38,8 @@ internal class TradeableGood
{
internal string Name;
internal int Price;
internal bool IsEquipable = false;
internal bool EquipModeActive;
}

internal enum TradeableGoodsNames
Expand Down
2 changes: 1 addition & 1 deletion Utilities/ShipSpriteBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ internal static Sprite GenerateBoat(BoatOptions options)
}
internal record BoatOptions(HullType Hull, BoatColor Color, SailStatus Sails, List<Cannon> 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()
{
Expand Down

0 comments on commit 7284f52

Please sign in to comment.