Skip to content

Commit

Permalink
Wow I multithreaded a game
Browse files Browse the repository at this point in the history
  • Loading branch information
Nhawdge committed Nov 20, 2023
1 parent 8dc9190 commit 116cb51
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 94 deletions.
8 changes: 4 additions & 4 deletions Assets/Maps/Pirates.ldtk
Original file line number Diff line number Diff line change
Expand Up @@ -2428,18 +2428,18 @@
},
{
"__identifier": "Patrol_Point",
"__grid": [169,199],
"__grid": [177,209],
"__pivot": [0,0],
"__tags": [],
"__tile": null,
"__smartColor": "#BE4A2F",
"__worldX": 2704,
"__worldY": 3184,
"__worldX": 2832,
"__worldY": 3344,
"iid": "5f66e180-6280-11ee-8bdd-0182c99f7ed8",
"width": 16,
"height": 16,
"defUid": 427,
"px": [2704,3184],
"px": [2832,3344],
"fieldInstances": [{ "__identifier": "Order", "__type": "Int", "__value": 1, "__tile": null, "defUid": 430, "realEditorValues": [{ "id": "V_Int", "params": [1] }] }]
},
{
Expand Down
203 changes: 113 additions & 90 deletions Scenes/Levels/Systems/EnemyControlSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace NovemberPirates.Scenes.Levels.Systems
{
internal class EnemyControlSystem : GameSystem
{
private Task<List<Vector2>> NavTask { get; set; }
internal override void Update(World world)
{
var singletonEntity = world.QueryFirst<Singleton>();
Expand Down Expand Up @@ -58,8 +59,6 @@ internal override void Update(World world)
return;
}

ship.Target = Vector2.Zero;

var nextPoint = Vector2.Zero;

var maxPatrolPoint = 0;
Expand All @@ -75,113 +74,137 @@ internal override void Update(World world)
}
});
}
Console.WriteLine($"Next: {ship.NextPatrolPoint}");

if (ship.Route.Count == 0)
{
ship.Route = new List<Vector2>();

var shipTile = singleton.Map.GetTileFromPosition(sprite.Position);

MapPath pathToTarget = null;
var targetTile = singleton.Map.GetTileFromPosition(nextPoint);

var last = new MapPath(
shipTile.Coordinates,
shipTile.Coordinates.DistanceTo(nextPoint),
shipTile.Coordinates.DistanceTo(shipTile.Coordinates),
shipTile.MovementCost);

var openTiles = new List<MapPath>();
var closedTiles = new List<Vector2>();

var neighbors = singleton.Map.GetTileNeighborsForTile(shipTile).Select(neighbor =>
new MapPath(
neighbor.Coordinates,
neighbor.Coordinates.DistanceTo(nextPoint),
neighbor.Coordinates.DistanceTo(shipTile.Coordinates),
neighbor.MovementCost,
last)
);
openTiles.AddRange(neighbors);

while (pathToTarget is null)
{
var openTile = openTiles.OrderBy(tile => tile.TotalCost).ThenBy(tile => tile.DistanceTo).First();
if (openTile.Coords == targetTile.Coordinates)
if (NavTask == null)
NavTask = new Task<List<Vector2>>(() =>
{
pathToTarget = openTile;
break;
}
closedTiles.Add(openTile.Coords);

neighbors = singleton.Map.GetTileNeighborsForCoords(openTile.Coords)
.Where(x => !closedTiles.Contains(x.Coordinates))
.Select(neighbor =>
new MapPath(
neighbor.Coordinates,
neighbor.Coordinates.DistanceTo(targetTile.Coordinates),
neighbor.Coordinates.DistanceTo(shipTile.Coordinates),
neighbor.MovementCost,
openTile)
);

openTiles.AddRange(neighbors);
openTiles.RemoveAll(x => x.Coords == openTile.Coords);

//Console.WriteLine($"Adding {openTile.Coords}");

Console.WriteLine($"Open Count: {openTiles.Count()}\t Closed Count: {closedTiles.Count()}\t{openTile.DistanceFrom} => {openTile.DistanceTo}");
ship.Route = new List<Vector2>();

var shipTile = singleton.Map.GetTileFromPosition(sprite.Position);

MapPath pathToTarget = null;
var targetTile = singleton.Map.GetTileFromPosition(nextPoint);

var last = new MapPath(
shipTile.Coordinates,
shipTile.Coordinates.DistanceTo(nextPoint),
shipTile.Coordinates.DistanceTo(shipTile.Coordinates),
shipTile.MovementCost);

var openTiles = new List<MapPath>();
var closedTiles = new List<Vector2>();

var neighbors = singleton.Map.GetTileNeighborsForTile(shipTile).Select(neighbor =>
new MapPath(
neighbor.Coordinates,
neighbor.Coordinates.DistanceTo(nextPoint),
neighbor.Coordinates.DistanceTo(shipTile.Coordinates),
neighbor.MovementCost,
last)
);
openTiles.AddRange(neighbors);

while (pathToTarget is null)
{
var openTile = openTiles.OrderBy(tile => tile.TotalCost).ThenBy(tile => tile.DistanceTo).First();
if (openTile.Coords == targetTile.Coordinates)
{
pathToTarget = openTile;
break;
}
closedTiles.Add(openTile.Coords);

neighbors = singleton.Map.GetTileNeighborsForCoords(openTile.Coords)
.Where(x => !closedTiles.Contains(x.Coordinates))
.Select(neighbor =>
new MapPath(
neighbor.Coordinates,
neighbor.Coordinates.DistanceTo(targetTile.Coordinates),
neighbor.Coordinates.DistanceTo(shipTile.Coordinates),
neighbor.MovementCost,
openTile)
);

openTiles.AddRange(neighbors);
openTiles.RemoveAll(x => x.Coords == openTile.Coords);

//Console.WriteLine($"Adding {openTile.Coords}");

Console.WriteLine($"Open Count: {openTiles.Count()}\t Closed Count: {closedTiles.Count()}\t{openTile.DistanceFrom} => {openTile.DistanceTo}");
}
//var lastStep = Vector2.Zero;
var route = new List<Vector2>();
while (pathToTarget.Parent is not null)
{
//if (last.Coords.X != pathToTarget.Coords.X && last.Coords.Y != pathToTarget.Coords.Y)
//{
route.Insert(0, pathToTarget.Coords.ToPixels());
//}
//lastStep = pathToTarget.Coords;
pathToTarget = pathToTarget.Parent;
}
return route;
});
if (NavTask.IsCompleted)
{
ship.Route = NavTask.Result;
NavTask = null;
}
//var lastStep = Vector2.Zero;
while (pathToTarget.Parent is not null)
else if (NavTask.Status == TaskStatus.Created)
{
//if (last.Coords.X != pathToTarget.Coords.X && last.Coords.Y != pathToTarget.Coords.Y)
//{
ship.Route.Insert(0, pathToTarget.Coords.ToPixels());
//}
//lastStep = pathToTarget.Coords;
pathToTarget = pathToTarget.Parent;
NavTask.Start();
}
}

var sailTarget = ship.Route.First();
if (sprite.Position.DistanceTo(sailTarget) < 300)
var sailTargetVec = ship.Route?.FirstOrDefault();
if (sailTargetVec is not null)
{
ship.Route.RemoveAt(0);
ship.NextPatrolPoint += 1;
if (ship.NextPatrolPoint > maxPatrolPoint)
ship.NextPatrolPoint = 1;
}

if (singleton.Debug >= DebugLevel.Low)
Raylib.DrawLine((int)sailTarget.X, (int)sailTarget.Y, (int)sprite.Position.X, (int)sprite.Position.Y, Raylib.RED);

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)
ship.Target = sailTargetVec.Value;
if (sprite.Position.DistanceTo(ship.Target) < 300)
{
var rotationNeeded = (float)Math.Min(sprite.Rotation - rotationInDegrees, ship.RotationSpeed * Raylib.GetFrameTime());
if (Math.Abs(rotationInDegrees) > 1)
ship.Route?.RemoveAt(0);
if (ship.Route?.Count == 0)
{
sprite.Rotation -= rotationNeeded;
ship.NextPatrolPoint += 1;
if (ship.NextPatrolPoint > maxPatrolPoint)
ship.NextPatrolPoint = 1;
}
//Console.WriteLine($" Spin left: {rotationInDegrees}");
}
else
if (singleton.Debug >= DebugLevel.Low)
Raylib.DrawLine((int)ship.Target.X, (int)ship.Target.Y, (int)sprite.Position.X, (int)sprite.Position.Y, Raylib.RED);


if (ship.Target != Vector2.Zero)
{
var rotationNeeded = (float)Math.Min(rotationInDegrees - sprite.Rotation, ship.RotationSpeed * Raylib.GetFrameTime());
if (Math.Abs(rotationInDegrees) > 1)
if (ship.CanDo(ShipAbilities.Steering))
{
sprite.Rotation += rotationNeeded;
var targetDirection = Vector2.Normalize(sprite.Position - ship.Target);

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());
if (Math.Abs(rotationInDegrees) > 1)
{
sprite.Rotation -= rotationNeeded;
}
}
else
{
var rotationNeeded = (float)Math.Min(rotationInDegrees - sprite.Rotation, ship.RotationSpeed * Raylib.GetFrameTime());
if (Math.Abs(rotationInDegrees) > 1)
{
sprite.Rotation += rotationNeeded;
}
sprite.Rotation += ship.RotationSpeed * Raylib.GetFrameTime();
}
}
sprite.Rotation += ship.RotationSpeed * Raylib.GetFrameTime();
//Console.WriteLine($"Spin Right: {rotationInDegrees}");
}
}


if (ship.CanDo(ShipAbilities.FullSail))
{
ship.Sail = SailStatus.Full;
Expand Down

0 comments on commit 116cb51

Please sign in to comment.