From d5ad458482e50f937d53084ceba84008c3fedd1f Mon Sep 17 00:00:00 2001 From: John Pavek Date: Mon, 13 Nov 2023 00:05:10 -0600 Subject: [PATCH] Fixed the cache.... --- Entities/Archetypes/EnemyBuilder.cs | 2 +- Entities/Archetypes/PlayerBuilder.cs | 4 ++ Scenes/Levels/Systems/CannonBallSystem.cs | 1 - Scenes/Levels/Systems/EnemyControlSystem.cs | 47 ++++++++++++++---- Utilities/Data/ShipData.cs | 53 +++++++++++++++++++++ Utilities/ShipSpriteBuilder.cs | 32 ++++++++++--- Utilities/TextureManager.cs | 8 ++-- 7 files changed, 126 insertions(+), 21 deletions(-) create mode 100644 Utilities/Data/ShipData.cs diff --git a/Entities/Archetypes/EnemyBuilder.cs b/Entities/Archetypes/EnemyBuilder.cs index 2c9ac89..97ec6cd 100644 --- a/Entities/Archetypes/EnemyBuilder.cs +++ b/Entities/Archetypes/EnemyBuilder.cs @@ -15,7 +15,7 @@ public static void CreateEnemyShip(World world, Vector2 position, Team team) ship.Team = team; ship.BoatColor = BoatColor.Yellow; ship.BoatType = BoatType.HullLarge; - ship.Sail = SailStatus.Full; + ship.Sail = SailStatus.Closed; ship.Crew = 10; diff --git a/Entities/Archetypes/PlayerBuilder.cs b/Entities/Archetypes/PlayerBuilder.cs index 2e8e941..079ca97 100644 --- a/Entities/Archetypes/PlayerBuilder.cs +++ b/Entities/Archetypes/PlayerBuilder.cs @@ -20,9 +20,13 @@ internal static void Create(World world) ship.BoatType = BoatType.HullMedium; ship.Sail = SailStatus.Closed; ship.Crew = 10; + ship.RowingPower = 150f; ship.Cannons.Add(new Cannon { Placement = BoatSide.Port, Row = 1 }); ship.Cannons.Add(new Cannon { Placement = BoatSide.Starboard, Row = 1 }); ship.Cannons.Add(new Cannon { Placement = BoatSide.Port, Row = 2 }); + ship.Cannons.Add(new Cannon { Placement = BoatSide.Starboard, Row = 2 }); + ship.Cannons.Add(new Cannon { Placement = BoatSide.Port, Row = 3 }); + ship.Cannons.Add(new Cannon { Placement = BoatSide.Starboard, Row = 3 }); player.Set(ship); diff --git a/Scenes/Levels/Systems/CannonBallSystem.cs b/Scenes/Levels/Systems/CannonBallSystem.cs index 0084f8c..62151be 100644 --- a/Scenes/Levels/Systems/CannonBallSystem.cs +++ b/Scenes/Levels/Systems/CannonBallSystem.cs @@ -51,7 +51,6 @@ internal override void Update(World world) < 75 => BoatCondition.Good, _ => BoatCondition.Good }; - ship.Sail = SailStatus.Full; shipSprite.Texture = ShipSpriteBuilder.GenerateBoat(new BoatOptions(ship)).Texture; } diff --git a/Scenes/Levels/Systems/EnemyControlSystem.cs b/Scenes/Levels/Systems/EnemyControlSystem.cs index 5066844..a4816ad 100644 --- a/Scenes/Levels/Systems/EnemyControlSystem.cs +++ b/Scenes/Levels/Systems/EnemyControlSystem.cs @@ -5,6 +5,7 @@ using NovemberPirates.Extensions; using NovemberPirates.Systems; using NovemberPirates.Utilities; +using NovemberPirates.Utilities.Data; using NovemberPirates.Utilities.Maps; using Raylib_CsLo; using System.Numerics; @@ -44,6 +45,12 @@ internal override void Update(World world) { sprite.Position += wind.WindDirection * Raylib.GetFrameTime() * wind.WindStrength * .1f; } + if (ship.HullHealth < 0) + { + ship.Crew = 0; + var percent = (100 + ship.HullHealth) / 100; + sprite.Color.a = (byte)(255 * percent); + } if (ship.HullHealth <= -100) { world.Destroy(entity); @@ -139,20 +146,44 @@ internal override void Update(World world) if (singleton.Debug >= DebugLevel.Low) Raylib.DrawLine((int)sailTarget.X, (int)sailTarget.Y, (int)sprite.Position.X, (int)sprite.Position.Y, Raylib.RED); - var targetDirection = Vector2.Normalize(sprite.Position - sailTarget); + if (ship.CanDo(ShipAbilities.Steering)) + { + var targetDirection = Vector2.Normalize(sprite.Position - sailTarget); - var rotationInDegrees = Math.Atan2(targetDirection.Y, targetDirection.X) * (180 / Math.PI); - if (sprite.Rotation > rotationInDegrees) + var rotationInDegrees = Math.Atan2(targetDirection.Y, targetDirection.X) * (180 / Math.PI); + if (sprite.Rotation > rotationInDegrees) + { + var rotationNeeded = (float)Math.Min(sprite.Rotation - rotationInDegrees, ship.RotationSpeed * Raylib.GetFrameTime()); + sprite.Rotation -= rotationNeeded; + } + else + { + var rotationNeeded = (float)Math.Min(rotationInDegrees - sprite.Rotation, ship.RotationSpeed * Raylib.GetFrameTime()); + sprite.Rotation += ship.RotationSpeed * Raylib.GetFrameTime(); + } + } + + if (ship.CanDo(ShipAbilities.FullSail)) + { + ship.Sail = SailStatus.Full; + sprite.Texture = ShipSpriteBuilder.GenerateBoat(new BoatOptions(ship)).Texture; + } + else if (ship.CanDo(ShipAbilities.HalfSail)) + { + ship.Sail = SailStatus.Half; + sprite.Texture = ShipSpriteBuilder.GenerateBoat(new BoatOptions(ship)).Texture; + } + else if (ship.CanDo(ShipAbilities.Rowing)) { - var rotationNeeded = (float)Math.Min(sprite.Rotation - rotationInDegrees, ship.RotationSpeed * Raylib.GetFrameTime()); - sprite.Rotation -= rotationNeeded; + ship.Sail = SailStatus.Rowing; + sprite.Texture = ShipSpriteBuilder.GenerateBoat(new BoatOptions(ship)).Texture; } else { - var rotationNeeded = (float)Math.Min(rotationInDegrees - sprite.Rotation, ship.RotationSpeed * Raylib.GetFrameTime()); - sprite.Rotation += ship.RotationSpeed * Raylib.GetFrameTime(); + ship.Sail = SailStatus.Closed; + sprite.Texture = ShipSpriteBuilder.GenerateBoat(new BoatOptions(ship)).Texture; } - ship.Sail = SailStatus.Full; + Console.WriteLine($"{new BoatOptions(ship).ToString()} {ship.Crew} {ship.HullHealth} "); }); } } diff --git a/Utilities/Data/ShipData.cs b/Utilities/Data/ShipData.cs new file mode 100644 index 0000000..54620bd --- /dev/null +++ b/Utilities/Data/ShipData.cs @@ -0,0 +1,53 @@ +using NovemberPirates.Components; + +namespace NovemberPirates.Utilities.Data +{ + internal static class ShipData + { + internal static bool CanDo(this Ship ship, ShipAbilities ability) + { + return ship.Crew >= Data[$"{ship.BoatType}{ability}"]; + } + + static readonly Dictionary Data = new() + { + // Small Ships + { $"{BoatType.HullSmall}{ShipAbilities.Steering}", 1 }, + { $"{BoatType.HullSmall}{ShipAbilities.Rowing}", 2 }, + { $"{BoatType.HullSmall}{ShipAbilities.HalfSail}", 3 }, + { $"{BoatType.HullSmall}{ShipAbilities.FullSail}", 10 }, + { $"{BoatType.HullSmall}{ShipAbilities.OneCannon}", 5 }, + { $"{BoatType.HullSmall}{ShipAbilities.TwoCannon}", 7 }, + + // Medium Ships + { $"{BoatType.HullMedium}{ShipAbilities.Steering}", 1 }, + { $"{BoatType.HullMedium}{ShipAbilities.Rowing}", 2 }, + { $"{BoatType.HullMedium}{ShipAbilities.HalfSail}", 3 }, + { $"{BoatType.HullMedium}{ShipAbilities.FullSail}", 10 }, + { $"{BoatType.HullMedium}{ShipAbilities.OneCannon}", 5 }, + { $"{BoatType.HullMedium}{ShipAbilities.TwoCannon}", 7 }, + + // Large Ships + { $"{BoatType.HullLarge}{ShipAbilities.Steering}", 1 }, + { $"{BoatType.HullLarge}{ShipAbilities.Rowing}", 2 }, + { $"{BoatType.HullLarge}{ShipAbilities.HalfSail}", 3 }, + { $"{BoatType.HullLarge}{ShipAbilities.FullSail}", 10 }, + { $"{BoatType.HullLarge}{ShipAbilities.OneCannon}", 5 }, + { $"{BoatType.HullLarge}{ShipAbilities.TwoCannon}", 7 }, + }; + } + internal enum ShipAbilities + { + Steering, + Rowing, + HalfSail, + FullSail, + + OneCannon, + TwoCannon, + ThreeCannon, + FourCannon, + FiveCannon, + SixCannon, + } +} diff --git a/Utilities/ShipSpriteBuilder.cs b/Utilities/ShipSpriteBuilder.cs index eced4ae..6737b88 100644 --- a/Utilities/ShipSpriteBuilder.cs +++ b/Utilities/ShipSpriteBuilder.cs @@ -10,18 +10,34 @@ internal static class ShipSpriteBuilder { internal static Sprite GenerateBoat(BoatOptions options) { - - var inCache = TextureManager.Instance.GetCachedSprite(options.ToKey()); - if (inCache is not null) - { - return inCache; - } var spriteHeight = options.Hull switch { BoatType.HullLarge => 128, _ => 108, }; var shipSize = new Vector2(66, spriteHeight); + var inCache = TextureManager.Instance.GetCachedTexture(options.ToKey()); + if (inCache.HasValue) + { + var cachedSprite = new Sprite() + { + Texture = inCache.Value, + Animations = new Dictionary() + { + { + "idle", new AnimationSets("idle", 0,0, Direction.forward, new List + { + new Frame(0, 0, (int)shipSize.X, (int)shipSize.Y, 100f) + }) + } + }, + Column = 0, + Row = 0, + Color = Raylib.WHITE, + }; + cachedSprite.Play("idle"); + return cachedSprite; + } var baseHullSprite = options.Hull switch { @@ -118,7 +134,9 @@ internal static Sprite GenerateBoat(BoatOptions options) }; sprite.Play("idle"); - TextureManager.Instance.SpriteCache.Add(options.ToKey(), sprite); + //Raylib.ExportImage(Raylib.LoadImageFromTexture(sprite.Texture), options.ToKey() + ".png"); + + //TextureManager.Instance.TextureCache.Add(options.ToKey(), renderTexture.texture); return sprite; } diff --git a/Utilities/TextureManager.cs b/Utilities/TextureManager.cs index e2ae717..cff18db 100644 --- a/Utilities/TextureManager.cs +++ b/Utilities/TextureManager.cs @@ -7,7 +7,7 @@ internal class TextureManager { internal static TextureManager Instance { get; } = new(); internal Dictionary TextureStore { get; set; } = new(); - internal Dictionary SpriteCache { get; set; } = new(); + internal Dictionary TextureCache { get; set; } = new(); private TextureManager() { @@ -43,10 +43,10 @@ internal Texture GetTexture(TextureKey key) } return TextureStore[key]; } - internal Sprite GetCachedSprite(string key) + internal Texture? GetCachedTexture(string key) { - if (SpriteCache.TryGetValue(key, out Sprite sprite)) - return sprite; + if (TextureCache.TryGetValue(key, out var texture)) + return texture; return null; } }