/// <summary> /// /// </summary> /// <param name="Searcher"></param> /// <param name="Closest">Can be a null input</param> /// <param name="Distance">Can be up to float.PositiveInfinity</param> public void FindNearestUnit(Unit Searcher, ref Unit Closest, ref float Distance) { if (isLeafNode) { for (int i = 0; i < Constituents.Length; i++) { if (Constituents[i].HitPoints > 0.0) { float tempDistance = (Constituents[i].Position - Searcher.Position).Length(); if (tempDistance < Distance) { Closest = Constituents[i]; Distance = tempDistance; } } } } else { if (Searcher.IndexablePosition[SplitDimensionIndex] > SplitPosition) { GreaterNode.FindNearestUnit(Searcher, ref Closest, ref Distance); if (Searcher.IndexablePosition[SplitDimensionIndex] - Distance < SplitPosition) { LessEqualNode.FindNearestUnit(Searcher, ref Closest, ref Distance); } } else { LessEqualNode.FindNearestUnit(Searcher, ref Closest, ref Distance); if (Searcher.IndexablePosition[SplitDimensionIndex] + Distance > SplitPosition) { GreaterNode.FindNearestUnit(Searcher, ref Closest, ref Distance); } } } }
/// <summary> /// With BuddyUp, the Unit moves (with a bias) towards the first ally Unit it encounters /// The unit still targets the closest enemy Unit /// </summary> private void Behavior_BuddyUp(GameTime gameTime, List <Unit_KDTree> EnemyTrees, Unit_KDTree AllyTree) { DirectionTimer -= gameTime.ElapsedGameTime.TotalSeconds; if (DirectionTimer <= 0.0) { DirectionTimer = 3.0; TargetPosition = Manager.terrain.GetRandomPosition(); } if (OtherTarget == null || OtherTarget.HitPoints <= 0.0) { OtherTarget = null; Distance = float.PositiveInfinity; AllyTree.FindNearestUnit(this, ref OtherTarget, ref Distance); } else { TargetPosition = Vector3.Lerp(TargetPosition, OtherTarget.Position, (float)gameTime.ElapsedGameTime.TotalSeconds); } Move(gameTime); Attack(gameTime, EnemyTrees); }
/// <summary> /// With BuddyUp, the Unit moves (with a bias) towards the first ally Unit it encounters /// The unit still targets the closest enemy Unit /// </summary> private void Behavior_BuddyUp(GameTime gameTime, List<Unit_KDTree> EnemyTrees, Unit_KDTree AllyTree) { DirectionTimer -= gameTime.ElapsedGameTime.TotalSeconds; if (DirectionTimer <= 0.0) { DirectionTimer = 3.0; TargetPosition = Manager.terrain.GetRandomPosition(); } if (OtherTarget == null || OtherTarget.HitPoints <= 0.0) { OtherTarget = null; Distance = float.PositiveInfinity; AllyTree.FindNearestUnit(this, ref OtherTarget, ref Distance); } else { TargetPosition = Vector3.Lerp(TargetPosition, OtherTarget.Position, (float)gameTime.ElapsedGameTime.TotalSeconds); } Move(gameTime); Attack(gameTime, EnemyTrees); }