This commit is contained in:
Regalis
2015-11-15 19:18:02 +02:00
11 changed files with 199 additions and 107 deletions

View File

@@ -10,7 +10,7 @@ namespace Barotrauma
public enum AiState { None, Attack, GoTo, Escape }
public enum SteeringState { Wander, Seek, Escape }
public Character Character;
public readonly Character Character;
protected AiState state;
@@ -45,10 +45,7 @@ namespace Barotrauma
steeringManager = new SteeringManager(this);
}
public virtual void DebugDraw(SpriteBatch spriteBatch)
{
}
public virtual void DebugDraw(SpriteBatch spriteBatch) { }
public virtual void OnAttacked(IDamageable attacker, float amount) { }

View File

@@ -11,7 +11,7 @@ namespace Barotrauma
public virtual bool IsCompleted()
{
return true;
return false;
}
public AIObjective()

View File

@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using FarseerPhysics;
namespace Barotrauma
{
class PathSteeringManager : SteeringManager
{
private PathFinder pathFinder;
private SteeringPath currentPath;
private Vector2 currentTarget;
private float findPathTimer;
public PathSteeringManager(ISteerable host)
: base(host)
{}
public override void Update(float speed = 1)
{
base.Update(speed);
findPathTimer -= 1.0f / 60.0f;
}
protected override Vector2 DoSteeringSeek(Vector2 target, float speed = 1)
{
//find a new path if one hasn't been found yet or the target is different from the current target
if (currentPath == null || Vector2.DistanceSquared(target, currentTarget)>10.0f)
{
if (findPathTimer > 0.0f) return Vector2.Zero;
currentTarget = target;
currentPath = pathFinder.FindPath(ConvertUnits.ToDisplayUnits(host.SimPosition), target);
findPathTimer = 1.0f;
return DiffToCurrentNode();
}
Vector2 diff = DiffToCurrentNode();
return (diff == Vector2.Zero) ? Vector2.Zero : Vector2.Normalize(diff)*speed;
}
private Vector2 DiffToCurrentNode()
{
if (currentPath == null) return Vector2.Zero;
currentPath.CheckProgress(host.SimPosition, 0.1f);
if (currentPath.CurrentNode == null) return Vector2.Zero;
return currentPath.CurrentNode.SimPosition - host.SimPosition;
}
}
}

View File

@@ -6,12 +6,12 @@ namespace Barotrauma
{
class SteeringManager
{
private const float CircleDistance = 2.5f;
private const float CircleRadius = 0.3f;
protected const float CircleDistance = 2.5f;
protected const float CircleRadius = 0.3f;
private const float RayCastInterval = 0.5f;
protected const float RayCastInterval = 0.5f;
private ISteerable host;
protected ISteerable host;
private Vector2 steering;
@@ -50,7 +50,7 @@ namespace Barotrauma
steering += DoSteeringAvoid(deltaTime, speed);
}
public void Update(float speed = 1.0f)
public virtual void Update(float speed = 1.0f)
{
float steeringSpeed = steering.Length();
if (steeringSpeed>speed)
@@ -61,7 +61,7 @@ namespace Barotrauma
host.Steering = steering;
}
private Vector2 DoSteeringSeek(Vector2 target, float speed = 1.0f)
protected virtual Vector2 DoSteeringSeek(Vector2 target, float speed = 1.0f)
{
Vector2 targetVel = target - host.SimPosition;
@@ -81,7 +81,7 @@ namespace Barotrauma
return newSteering;
}
private Vector2 DoSteeringWander(float speed = 1.0f)
protected virtual Vector2 DoSteeringWander(float speed = 1.0f)
{
Vector2 circleCenter = (host.Velocity == Vector2.Zero) ? new Vector2(speed, 0.0f) : host.Velocity;
circleCenter = Vector2.Normalize(circleCenter) * CircleDistance;
@@ -105,7 +105,7 @@ namespace Barotrauma
return newSteering;
}
private Vector2 DoSteeringAvoid(float deltaTime, float speed = 1.0f)
protected virtual Vector2 DoSteeringAvoid(float deltaTime, float speed = 1.0f)
{
if (steering == Vector2.Zero || host.Steering == Vector2.Zero) return Vector2.Zero;

View File

@@ -48,10 +48,10 @@ namespace Barotrauma
currentIndex++;
}
public WayPoint CheckProgress(Vector2 pos, float minSimDistance = 0.1f)
public WayPoint CheckProgress(Vector2 simPosition, float minSimDistance = 0.1f)
{
if (nodes.Count == 0 || currentIndex>nodes.Count-1) return null;
if (Vector2.Distance(pos, nodes[currentIndex].SimPosition) < minSimDistance) currentIndex++;
if (Vector2.Distance(simPosition, nodes[currentIndex].SimPosition) < minSimDistance) currentIndex++;
return CurrentNode;
}