Skip to content

Commit

Permalink
Fixed the cache....
Browse files Browse the repository at this point in the history
  • Loading branch information
Nhawdge committed Nov 13, 2023
1 parent 14ff7fd commit d5ad458
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Entities/Archetypes/EnemyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;


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

Expand Down
1 change: 0 additions & 1 deletion Scenes/Levels/Systems/CannonBallSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
47 changes: 39 additions & 8 deletions Scenes/Levels/Systems/EnemyControlSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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} ");
});
}
}
Expand Down
53 changes: 53 additions & 0 deletions Utilities/Data/ShipData.cs
Original file line number Diff line number Diff line change
@@ -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<string, int> 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,
}
}
32 changes: 25 additions & 7 deletions Utilities/ShipSpriteBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, AnimationSets>()
{
{
"idle", new AnimationSets("idle", 0,0, Direction.forward, new List<Frame>
{
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
{
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions Utilities/TextureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal class TextureManager
{
internal static TextureManager Instance { get; } = new();
internal Dictionary<TextureKey, Texture> TextureStore { get; set; } = new();
internal Dictionary<string, Sprite> SpriteCache { get; set; } = new();
internal Dictionary<string, Texture> TextureCache { get; set; } = new();

private TextureManager()
{
Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit d5ad458

Please sign in to comment.