2 using Microsoft.Xna.Framework;
4 using System.Collections.Generic;
13 private readonly
float minDistFromClosest;
14 private readonly
float maxDistFromCenter;
15 private readonly
float cohesion;
18 public List<AICharacter>
Members {
get;
private set; } =
new List<AICharacter>();
19 public HashSet<AICharacter>
ActiveMembers {
get;
private set; } =
new HashSet<AICharacter>();
30 minDistFromClosest = ConvertUnits.ToSimUnits(element.GetAttributeFloat(nameof(minDistFromClosest), 10.0f));
31 maxDistFromCenter = ConvertUnits.ToSimUnits(element.GetAttributeFloat(nameof(maxDistFromCenter), 1000.0f));
32 cohesion = element.GetAttributeFloat(nameof(cohesion), 1) / 10;
36 public static void CreateSwarm(IEnumerable<AICharacter> swarm)
38 var aiControllers =
new List<EnemyAIController>();
43 aiControllers.Add(enemyAI);
46 var filteredMembers = aiControllers.Select(m => m.Character as
AICharacter).Where(m => m !=
null);
74 float closestDistSqr =
float.MaxValue;
75 Vector2 center = Vector2.Zero;
82 if (distSqr < closestDistSqr)
84 closestDistSqr = distSqr;
90 if (closest ==
null) {
return; }
93 float closestDist = (float)Math.Sqrt(closestDistSqr);
94 if (closestDist < minDistFromClosest)
96 Vector2 diff = closest.SimPosition - ai.
SimPosition;
97 if (diff.LengthSquared() < 0.0001f)
104 else if (Vector2.DistanceSquared(center, ai.
SimPosition) > maxDistFromCenter * maxDistFromCenter)
106 float distFromCenter = Vector2.Distance(center, ai.
SimPosition);
113 Vector2 avgVel = Vector2.Zero;
override AIController AIController
readonly Character Character
SteeringManager SteeringManager
override Vector2? SimPosition
static Character? Controlled
readonly AnimController AnimController
SwarmBehavior SwarmBehavior
void SteeringManual(float deltaTime, Vector2 velocity)
void SteeringSeek(Vector2 targetSimPos, float weight=1)
void UpdateSteering(float deltaTime)
HashSet< AICharacter > ActiveMembers
static void CreateSwarm(IEnumerable< AICharacter > swarm)
SwarmBehavior(XElement element, EnemyAIController ai)
List< AICharacter > Members