Skip to content

Commit

Permalink
Accidentally rewrote the whole ship nav system
Browse files Browse the repository at this point in the history
  • Loading branch information
Nhawdge committed Feb 12, 2024
1 parent 7284f52 commit 8f58ac7
Show file tree
Hide file tree
Showing 13 changed files with 320 additions and 149 deletions.
27 changes: 17 additions & 10 deletions Entities/Archetypes/EnemyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,22 @@ namespace NovemberPirates.Entities.Archetypes
{
internal static class EnemyBuilder
{
public static void CreateEnemyShip(World world, Vector2 position, Team team)
public static void CreateEnemyShip(World world, Vector2 position, Team team, NpcPurpose purpose)
{
var enemyArchetype = new ComponentType[] { typeof(Ship), typeof(Sprite), typeof(Npc) };

var entity = world.Create(enemyArchetype);
var ship = new Ship(HullType.Small, BoatColor.Yellow, Team.Yellow);
var color = team switch
{
Team.Red => BoatColor.Red,
Team.Blue => BoatColor.Blue,
Team.Yellow => BoatColor.Yellow,
_ => BoatColor.Yellow,
};
var ship = new Ship(HullType.Small, color, Team.Yellow);

ship.Team = team;
ship.BoatColor = BoatColor.Yellow;
ship.HullType = HullType.Large;

ship.Sail = SailStatus.Closed;
ship.Crew = 10;

Expand All @@ -29,7 +35,8 @@ public static void CreateEnemyShip(World world, Vector2 position, Team team)
entity.Set(ship);
entity.Set(sprite);

entity.Set(new Npc() );
entity.Set(new Npc(purpose));

//if (purpose == Purpose.Trade)
//{
// ship.SailingRoute = RouteDataStore.Instance.GetRandomRoute();
Expand All @@ -44,11 +51,11 @@ internal static void CreatePatrolPoint(World world, Vector2 vector2, Team team,
patrolEntity.Set(new PatrolPoint() { Position = vector2, Team = team, Order = order });
}

internal static void CreateSpawnPoint(World world, Vector2 pos, Team team)
{
var spawnerEntity = world.Create<Spawner>();
//internal static void CreateSpawnPoint(World world, Vector2 pos, Team team)
//{
// var spawnerEntity = world.Create<Spawner>();

spawnerEntity.Set(new Spawner() { Team = team, Position = pos, SpawnTime = 100, Elapsed = 90 });
}
// spawnerEntity.Set(new Spawner() { Team = team, SpawnTime = 100, Elapsed = 90 });
//}
}
}
7 changes: 7 additions & 0 deletions Scenes/Levels/Components/MapTile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ namespace NovemberPirates.Scenes.Levels.Components
{
internal class MapTile
{
public MapTile(Vector2 coords, long gridSize, float cost = 1)
{
Coordinates = coords;
MovementCost = cost;
Position = new Vector2(coords.X * gridSize, coords.Y * gridSize);
}
internal Vector2 Coordinates;
internal float MovementCost;
internal Vector2 Position;
}
}
26 changes: 23 additions & 3 deletions Scenes/Levels/Components/Npc.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
namespace NovemberPirates.Scenes.Levels.Components
using System.Numerics;

namespace NovemberPirates.Scenes.Levels.Components
{
internal class Npc
{
public Npc(NpcPurpose purpose)
{
Purpose = purpose;
}

internal NpcGoals Goal;
internal float TimeSinceLastGoalChange;
internal NpcPurpose Purpose;
internal float TimeSinceLastGoalChange = 100;
internal NpcGoals CurrentAction;
internal float VisionRange = 50;
internal Vector2 TargetPosition;
internal float TargetOffsetInDegrees;
}

public enum NpcPurpose
{
Merchant,
Patrol,
Escort,
PirateHunter,
}

public enum NpcGoals
Expand All @@ -13,7 +33,7 @@ public enum NpcGoals
Repairing,
Escaping,
GetReinforcements,
Fighting
Attacking
}

}
1 change: 0 additions & 1 deletion Scenes/Levels/Components/Ship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public Ship(HullType hull, BoatColor color, Team team)
internal List<Cannon> Cannons = new List<Cannon>();

internal int NextPatrolPoint = 1;
internal Vector2 Target;
internal List<Vector2> Route = new();
internal float WindInSail;

Expand Down
5 changes: 1 addition & 4 deletions Scenes/Levels/Components/Spawner.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System.Numerics;

namespace NovemberPirates.Scenes.Levels.Components
namespace NovemberPirates.Scenes.Levels.Components
{
internal class Spawner
{
internal Team Team;

public float Elapsed;
public float SpawnTime;
internal Vector2 Position;
}
}
8 changes: 4 additions & 4 deletions Scenes/Levels/OceanScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ internal OceanScene()
{
NavigationUtilities.BuildMap(World);
});
LoadingTasks.Add("Trade Routes", () =>
{
NavigationUtilities.AddTradeRoutes(World);
});
//LoadingTasks.Add("Trade Routes", () =>
//{
// NavigationUtilities.AddTradeRoutes(World);
//});

LoadingTasks.Add("Audio", () =>
{
Expand Down
113 changes: 111 additions & 2 deletions Scenes/Levels/Systems/EnemyAISystem.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using Arch.Core;
using Arch.Core.Extensions;
using NovemberPirates.Extensions;
using NovemberPirates.Scenes.Levels.Components;
using NovemberPirates.Systems;
using Raylib_CsLo;
using System.Formats.Asn1;
using System.Numerics;

namespace NovemberPirates.Scenes.Levels.Systems
{
Expand All @@ -16,13 +20,118 @@ internal override void Update(World world)
var npc = entity.Get<Npc>();
var ship = entity.Get<Ship>();
var sprite = entity.Get<Sprite>();


npc.TimeSinceLastGoalChange += Raylib.GetFrameTime();

if (npc.TimeSinceLastGoalChange < 10)
{
return;
}
npc.TimeSinceLastGoalChange = 0;

var goalWeights = new Dictionary<NpcGoals, float> {
{ NpcGoals.Sailing, 0f },
{ NpcGoals.Attacking, 0f },
{ NpcGoals.Escaping, 0f },
{ NpcGoals.Trading, 0f },
{ NpcGoals.Repairing, 0f },
};

if (npc.Purpose == NpcPurpose.Merchant)
{
goalWeights[NpcGoals.Trading] += 10f;
}

npc.CurrentAction = goalWeights.OrderByDescending(x => x.Value).First().Key;

// End Goal weighing

if (npc.CurrentAction == NpcGoals.Trading)
{
if (npc.TargetPosition == Vector2.Zero)
{
Console.WriteLine("fresh ship");
npc.TargetPosition = GetNearestPort(world, sprite.Position);
}

if (npc.TargetPosition.DistanceTo(sprite.Position) < 200)
{
Console.WriteLine("Port Hit!");
npc.TargetPosition = GetSecondNearestPort(world, sprite.Position);
}
}
});
}

#region Weighting helpers
private float GetRecentDamage()
{
throw new NotImplementedException();
}
}

private float DistanceToPlayer()
{
throw new NotImplementedException();
}

private float DistanceToAlliedShip()
{
throw new NotImplementedException();
}

private float DistanceToSafePort()
{
throw new NotImplementedException();
}

private float GetCurrentGoldValue()
{
throw new NotImplementedException();
}

#endregion

#region Action helpers

private Vector2 GetNearestPort(World world, Vector2 fromPosition)
{
var coords = new Vector2();

var query = new QueryDescription().WithAll<Port>();
var shortestDistance = float.MaxValue;

world.Query(query, (entity) =>
{
var port = entity.Get<Port>();
var distance = fromPosition.DistanceTo(port.Position);

if (distance < shortestDistance)
{
coords = port.Position;
shortestDistance = distance;
}
});

return coords;
}

private Vector2 GetSecondNearestPort(World world, Vector2 fromPosition)
{
var query = new QueryDescription().WithAll<Port>();
var shortestDistance = float.MaxValue;
var targets = new List<(float distance, Vector2 position)>();
world.Query(query, (entity) =>
{
var port = entity.Get<Port>();
var distance = fromPosition.DistanceTo(port.Position);

targets.Add((distance, port.Position));
});

return targets.OrderBy(targets => targets.distance).Skip(1).First().position;
}

#endregion
}

}
Loading

0 comments on commit 8f58ac7

Please sign in to comment.