Skip to content

Commit

Permalink
Merchants can do their thing!!
Browse files Browse the repository at this point in the history
  • Loading branch information
Nhawdge committed Jan 1, 2024
1 parent 8e027ff commit 22e5c9b
Show file tree
Hide file tree
Showing 12 changed files with 660 additions and 102 deletions.
604 changes: 561 additions & 43 deletions Assets/Maps/Pirates.ldtk

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion Components/Port.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ namespace NovemberPirates.Components
{
internal class Port
{
public Port()
{
Guid = Guid.NewGuid();
}
public float Currency = 0f;
public float Population = 100f;
internal Vector2 Position = Vector2.Zero;
internal Guid Guid = Guid.NewGuid();
internal Guid Guid;
internal string ShortId() => Guid.ToString().Substring(0, 4);


public Team Team = Team.None;
Expand Down
1 change: 1 addition & 0 deletions Components/Render.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,6 @@ public enum CollisionType
None,
Slow,
Solid,
Padding,
}
}
1 change: 1 addition & 0 deletions Components/Ship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public Ship(HullType hull, BoatColor color, Team team)
internal Vector2 Goal;
internal float Currency;
internal Port? TargetPort;
internal List<Route> SailingRoute;

public Task<List<Vector2>> NavTask { get; set; }
}
Expand Down
12 changes: 9 additions & 3 deletions Entities/Archetypes/EnemyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
using Arch.Core.Extensions;
using NovemberPirates.Components;
using NovemberPirates.Utilities;
using NovemberPirates.Utilities.Data;
using System.Numerics;

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, Purpose purpose = Purpose.None)
{
var entity = world.Create<Ship, Sprite, Npc>();
var ship = new Ship(HullType.Small, BoatColor.Yellow, Team.Yellow);
Expand All @@ -25,7 +26,12 @@ public static void CreateEnemyShip(World world, Vector2 position, Team team)
entity.Set(ship);
entity.Set(sprite);

entity.Set(new Npc() { Purpose = Purpose.Trade });
entity.Set(new Npc() { Purpose = purpose });
if (purpose == Purpose.Trade)
{
ship.SailingRoute = RouteDataStore.Instance.GetRandomRoute();
sprite.Position = ship.SailingRoute[0].RoutePoints[0];
}
}

internal static void CreatePatrolPoint(World world, Vector2 vector2, Team team, int order)
Expand All @@ -39,7 +45,7 @@ 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 = 100 });
spawnerEntity.Set(new Spawner() { Team = team, Position = pos, SpawnTime = 100, Elapsed = 90 });
}
}
}
2 changes: 1 addition & 1 deletion Scenes/Levels/Systems/CameraSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ internal override void Update(World world)
var scroll = Raylib.GetMouseWheelMove();
if (scroll < 0)
{
NovemberPiratesEngine.Instance.Camera.zoom -= 0.1f;
NovemberPiratesEngine.Instance.Camera.zoom = Math.Max(NovemberPiratesEngine.Instance.Camera.zoom - 0.1f, 0.1f);
}
else if (scroll > 0)
{
Expand Down
47 changes: 21 additions & 26 deletions Scenes/Levels/Systems/EnemyControlSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,36 +78,31 @@ internal override void Update(World world)
}
}
if (npc.Purpose == Purpose.Trade)
{
if (ship.Goal == Vector2.Zero)
{
if (ship.Route.Count == 0)
{
var portQuery = new QueryDescription().WithAll<Port>();
var ports = new List<(Port? port, float distance)>();
world.Query(portQuery, (portEntity) =>
{
var port = portEntity.Get<Port>();
ports.Add(new(port, sprite.Position.DistanceTo(port.Position)));
});
var port = ports.OrderBy(port => port.port.Currency).ThenBy(port => port.distance).First();
ship.TargetPort = port.port;
ship.Goal = port.port.Position;
ship.Route = ship.SailingRoute.First().RoutePoints;
ship.SailingRoute.Add(ship.SailingRoute.First());
ship.SailingRoute.RemoveAt(0);
}
}

if ((ship.Route.Count < 10 && npc.Purpose == Purpose.Patrol)
|| npc.Purpose == Purpose.Trade && ship.Route.Count == 0)
else if (npc.Purpose == Purpose.Patrol)
{
if (ship.NavTask == null)
ship.NavTask = new Task<List<Vector2>>(() => NavigationUtilities.CalculateRouteFromShip(world, entity).ToList());
if ((ship.Route.Count < 10))

if (ship.NavTask.IsCompleted)
{
ship.Route = ship.NavTask.Result;
ship.NavTask = null;
}
else if (ship.NavTask.Status == TaskStatus.Created)
{
ship.NavTask.Start();
if (ship.NavTask == null)
ship.NavTask = new Task<List<Vector2>>(() => NavigationUtilities.CalculateRouteFromShip(world, entity).ToList());

if (ship.NavTask.IsCompleted)
{
ship.Route = ship.NavTask.Result;
ship.NavTask = null;
}
else if (ship.NavTask.Status == TaskStatus.Created)
{
ship.NavTask.Start();
}
}
}
var sailTargetVec = ship.Route?.FirstOrDefault();
Expand All @@ -128,8 +123,8 @@ internal override void Update(World world)
if (ship.Route?.Count == 0 && npc.Purpose == Purpose.Trade)
{
ship.Goal = Vector2.Zero;
var availableMoney = Math.Min(10, ship.TargetPort.Currency);
ship.TargetPort.Currency -= availableMoney;
var availableMoney = 10;
//ship.TargetPort.Currency -= availableMoney;
ship.Currency += availableMoney;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Scenes/Levels/Systems/SpawningSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal override void Update(World world)
if (spawner.Elapsed > spawner.SpawnTime)
{
spawner.Elapsed = 0;
EnemyBuilder.CreateEnemyShip(world, spawner.Position, spawner.Team);
EnemyBuilder.CreateEnemyShip(world, spawner.Position, spawner.Team, Purpose.Trade);
}
});
}
Expand Down
19 changes: 11 additions & 8 deletions Scenes/Levels/Systems/WindSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using NovemberPirates.Extensions;
using NovemberPirates.Systems;
using Raylib_CsLo;
using System.Numerics;

namespace NovemberPirates.Scenes.Levels.Systems
{
Expand All @@ -15,17 +14,21 @@ internal override void Update(World world) { }

internal override void UpdateNoCamera(World world)
{
var singletonEntity = world.QueryFirst<Singleton>();
var singleton = singletonEntity.Get<Singleton>();
var singletonEntity = world.QueryFirst<Singleton>();

var wind = singletonEntity.Get<Wind>();
wind.LastWindChange += Raylib.GetFrameTime();
if (wind.LastWindChange > 500)
var windOffset = wind.LastWindChange - 75;
if (windOffset > 0)
{
wind.LastWindChange = 0;
wind.WindDirection = RayMath.Vector2Rotate(wind.WindDirection, Random.Shared.Next(0, 10));
wind.WindStrength = Random.Shared.Next(200, 400);
EffectsBuilder.CreateWindEffect(world);
var shouldChange = Random.Shared.Next(0, Math.Max(90 - (int)windOffset, 1)) <= 1;
if (shouldChange)
{
wind.LastWindChange = 0;
wind.WindDirection = RayMath.Vector2Rotate(wind.WindDirection, Random.Shared.Next(0, 10));
wind.WindStrength = Random.Shared.Next(200, 400);
EffectsBuilder.CreateWindEffect(world);
}
}

Raylib.DrawLine(50, 50, (int)(50 + wind.WindDirection.X * 30), (int)(50 + wind.WindDirection.Y * 30), Raylib.ORANGE);
Expand Down
9 changes: 9 additions & 0 deletions Utilities/Data/RouteDataStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ internal class RouteDataStore
internal Dictionary<string, List<Route>> Routes = new();
private RouteDataStore() { }

internal List<Route> GetRandomRoute()
{
if (Routes.TryGetValue(Routes.Keys.ElementAtOrDefault(Random.Shared.Next(Routes.Count())), out var route))
{
return route;
}
return null;
}

}
internal class Route
{
Expand Down
9 changes: 7 additions & 2 deletions Utilities/MapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public MapDetails LoadMap(string key, World world)
{
tileSprite.Collision = CollisionType.Slow;
}
else if (layer.Identifier == "IslandProxy")
{
tileSprite.Collision = CollisionType.Padding;
}
else if (layer.Identifier == "Islands")
{
tileSprite.Collision = CollisionType.Solid;
Expand All @@ -66,13 +70,14 @@ public MapDetails LoadMap(string key, World world)
mapTile.MovementCost = tileSprite.Collision switch
{
CollisionType.Solid => 9999,
CollisionType.Slow => 2,
CollisionType.Padding => 2,
CollisionType.Slow => 3,
_ => 1,
};

mapTileEntity.Set(mapTile);
}

foreach (var entity in layer.EntityInstances)
{
//Console.WriteLine($"entity: {entity.Identifier}");
Expand Down
49 changes: 32 additions & 17 deletions Utilities/Maps/NavigationUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,44 @@ internal static void AddTradeRoutes(World world)
});


foreach (var port in allPorts)
for (var i = 0; i < allPorts.Count; i++)
{
var routePorts = allPorts.OrderBy(x => Random.Shared.Next()).Take(3);
var routePorts = allPorts.OrderBy(x => Random.Shared.Next()).Take(3).ToList();
var routes = new List<Route>();

var key = $"Route-{i}";
if (RouteDataStore.Instance.Routes.ContainsKey(key))
{
continue;
}
var index = 0;
foreach (var destinationPort in routePorts)
var calcTasks = new List<Task>();
foreach (var port in routePorts)
{
index++;
var secondPort = routePorts.ElementAtOrDefault(index);
if (secondPort is null)
calcTasks.Add(new Task(() =>
{
index = 0;
secondPort = routePorts.ElementAt(index);
}
var route = new Route();
route.FromPort = destinationPort;
route.ToPort = secondPort;

route.RoutePoints = CalculateRoute(world, port.Position, secondPort.Position).ToList();
routes.Add(route);
index++;
var secondPort = routePorts.ElementAtOrDefault(index);
if (secondPort is null)
{
index = 0;
secondPort = routePorts.ElementAt(index);
}
var route = new Route();

route.FromPort = port;
route.ToPort = secondPort;
Console.WriteLine($"{key} - {route.FromPort.ShortId()} {route.ToPort.ShortId()}");
route.RoutePoints = CalculateRoute(world, route.FromPort.Position, route.ToPort.Position).ToList();
routes.Add(route);
}));
}
foreach (var task in calcTasks)
{
task.Start();
}
RouteDataStore.Instance.Routes.Add($"Route-{port.Position.X}-{port.Position.Y}", routes);
Task.WaitAll(calcTasks.ToArray());

RouteDataStore.Instance.Routes.Add(key, routes);
}
}

Expand Down

0 comments on commit 22e5c9b

Please sign in to comment.