Skip to content

Commit

Permalink
WIP Enemy AI
Browse files Browse the repository at this point in the history
  • Loading branch information
Nhawdge committed Feb 4, 2024
1 parent ca16e6e commit 98d3bac
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 100 deletions.
1 change: 1 addition & 0 deletions Scenes/Levels/OceanScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ internal OceanScene()
Systems.Add(new NavigationSystem());
Systems.Add(new PlayerControlSystem());
Systems.Add(new PortSystem());
Systems.Add(new EnemyAISystem());
Systems.Add(new UiSystem());
});
}
Expand Down
202 changes: 102 additions & 100 deletions Scenes/Levels/Systems/EnemyControlSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,122 +43,124 @@ internal override void Update(World world)
}
}

if (ship.BoatCondition == BoatCondition.Empty)
{
sprite.Position += wind.WindDirection * Raylib.GetFrameTime() * wind.WindStrength * .1f;
}
//if (ship.BoatCondition == BoatCondition.Empty)
//{
// 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);
ship.HullHealth -= Raylib.GetFrameTime();
}

if (ship.HullHealth <= -100)
{
world.Destroy(entity);
return;
}

var maxPatrolPoint = 0;
//var maxPatrolPoint = 0;

var npc = entity.Get<Npc>();
if (npc.Purpose == Purpose.Patrol)
{
if (ship.Goal == Vector2.Zero)
{
world.Query(in patrolQuery, (patrolEntity) =>
{
var point = patrolEntity.Get<PatrolPoint>();
maxPatrolPoint = Math.Max(maxPatrolPoint, point.Order);
if (point.Order == ship.NextPatrolPoint)
{
ship.Goal = point.Position;
}
});
}
}
if (npc.Purpose == Purpose.Trade)
{
if (ship.Route.Count == 0)
{
ship.Route = ship.SailingRoute.First().RoutePoints;
ship.SailingRoute.Add(ship.SailingRoute.First());
ship.SailingRoute.RemoveAt(0);
}
}
else if (npc.Purpose == Purpose.Patrol)
{
if ((ship.Route.Count < 10))
//var npc = entity.Get<Npc>();
//if (npc.Purpose == Purpose.Patrol)
//{
// if (ship.Goal == Vector2.Zero)
// {
// world.Query(in patrolQuery, (patrolEntity) =>
// {
// var point = patrolEntity.Get<PatrolPoint>();
// maxPatrolPoint = Math.Max(maxPatrolPoint, point.Order);
// if (point.Order == ship.NextPatrolPoint)
// {
// ship.Goal = point.Position;
// }
// });
// }
//}
//if (npc.Purpose == Purpose.Trade)
//{
// if (ship.Route.Count == 0)
// {
// ship.Route = ship.SailingRoute.First().RoutePoints;
// ship.SailingRoute.Add(ship.SailingRoute.First());
// ship.SailingRoute.RemoveAt(0);
// }
//}
//else if (npc.Purpose == Purpose.Patrol)
//{
// if ((ship.Route.Count < 10))

{
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();
if (sailTargetVec is not null)
{
ship.Target = sailTargetVec.Value;
if (sprite.Position.DistanceTo(ship.Target) < 300)
{
if (ship.Route?.Count > 0)
ship.Route?.RemoveAt(0);

if (ship.Route?.Count == 0 && npc.Purpose == Purpose.Patrol)
{
ship.NextPatrolPoint += 1;
if (ship.NextPatrolPoint > maxPatrolPoint)
ship.NextPatrolPoint = 1;
}
if (ship.Route?.Count == 0 && npc.Purpose == Purpose.Trade)
{
ship.Goal = Vector2.Zero;
var availableMoney = 10;
//ship.TargetPort.Currency -= availableMoney;
ship.Currency += availableMoney;
}
}
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.NavTask == null)
// ship.NavTask = new Task<List<Vector2>>(() => NavigationUtilities.CalculateRouteFromShip(world, entity).ToList());

if (ship.Target != Vector2.Zero)
{
if (ship.CanDo(ShipAbilities.Steering))
{
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();
}
}
}
}
// 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();
//if (sailTargetVec is not null)
//{
// ship.Target = sailTargetVec.Value;
// if (sprite.Position.DistanceTo(ship.Target) < 300)
// {
// if (ship.Route?.Count > 0)
// ship.Route?.RemoveAt(0);

// if (ship.Route?.Count == 0 && npc.Purpose == Purpose.Patrol)
// {
// ship.NextPatrolPoint += 1;
// if (ship.NextPatrolPoint > maxPatrolPoint)
// ship.NextPatrolPoint = 1;
// }
// if (ship.Route?.Count == 0 && npc.Purpose == Purpose.Trade)
// {
// ship.Goal = Vector2.Zero;
// var availableMoney = 10;
// //ship.TargetPort.Currency -= availableMoney;
// ship.Currency += availableMoney;
// }
// }
// 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)
// {
// if (ship.CanDo(ShipAbilities.Steering))
// {
// 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();
// }
// }
// }
//}

//Console.WriteLine($"Target {ship.Target}");

Expand Down

0 comments on commit 98d3bac

Please sign in to comment.