public void Update(GameTime gameTime, List <Controller> AllControllers, SpawnRateController.DataPoint Statistics) { List <Unit_KDTree> EnemyTrees = new List <Unit_KDTree>(); for (int i = 0; i < AllControllers.Count; i++) { if (AllControllers[i].Allegiance != Allegiance) { EnemyTrees.Add(AllControllers[i].SpatialTree); } } for (int i = 0; i < units.Count; i++) { units[i].Update(gameTime, EnemyTrees, SpatialTree); if (units[i].HitPoints <= 0.0) { units.RemoveAt(i); } } spawnTimer -= gameTime.ElapsedGameTime.TotalSeconds; if (spawnTimer <= 0.0) { spawnTimer = Manager.GeneralSpawnTimer; units.Add(new Unit(Manager.terrain.GetRandomPosition() , (Unit.StatDistribution)MyGame.random.Next((int)Unit.StatDistribution.End) , (Unit.BehaviorType)MyGame.random.Next((int)Unit.BehaviorType.End))); SpatialTree = new Unit_KDTree(-1, units.ToArray()); } }
/// <summary> /// Organizes Units by X Y Z coordinates /// More efficient to recreate a tree rather than update it /// </summary> /// <param name="ParentSplitDimensionIndex">Outside of this constructor, inputs should be -1</param> /// <param name="UnitList">Should be converted from a List</param> public Unit_KDTree(int ParentSplitDimensionIndex, Unit[] UnitList) { SplitDimensionIndex = (ParentSplitDimensionIndex + 1) % 3; if (UnitList.Length > 2) { int MedianIndex = UnitList.Length / 2; PartialSortListAt(MedianIndex, SplitDimensionIndex, ref UnitList); SplitPosition = UnitList[MedianIndex].IndexablePosition[SplitDimensionIndex]; Unit[] LowerList = new Unit[MedianIndex + 1]; Unit[] UpperList = new Unit[UnitList.Length - MedianIndex - 1]; Array.Copy(UnitList, 0, LowerList, 0, LowerList.Length); Array.Copy(UnitList, MedianIndex + 1, UpperList, 0, UpperList.Length); LessEqualNode = new Unit_KDTree(SplitDimensionIndex, LowerList); GreaterNode = new Unit_KDTree(SplitDimensionIndex, UpperList); } else { Constituents = UnitList; isLeafNode = true; } }
public void Update(GameTime gameTime, List<Controller> AllControllers, SpawnRateController.DataPoint Statistics) { List<Unit_KDTree> EnemyTrees = new List<Unit_KDTree>(); for (int i = 0; i < AllControllers.Count; i++) { if (AllControllers[i].Allegiance != Allegiance) { EnemyTrees.Add(AllControllers[i].SpatialTree); } } for (int i = 0; i < units.Count; i++) { units[i].Update(gameTime, EnemyTrees, SpatialTree); if (units[i].HitPoints <= 0.0) { units.RemoveAt(i); } } spawnTimer -= gameTime.ElapsedGameTime.TotalSeconds; if (spawnTimer <= 0.0) { spawnTimer = Manager.GeneralSpawnTimer; units.Add(new Unit(Manager.terrain.GetRandomPosition() , (Unit.StatDistribution)MyGame.random.Next((int)Unit.StatDistribution.End) , (Unit.BehaviorType)MyGame.random.Next((int)Unit.BehaviorType.End))); SpatialTree = new Unit_KDTree(-1, units.ToArray()); } }
/// <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> /// By default, the Unit moves randomly and targets the closest enemy Unit /// </summary> private void Behavior_Default(GameTime gameTime, List <Unit_KDTree> EnemyTrees, Unit_KDTree AllyTree) { DirectionTimer -= gameTime.ElapsedGameTime.TotalSeconds; if (DirectionTimer <= 0.0) { DirectionTimer = 5.0; TargetPosition = Manager.terrain.GetRandomPosition(); } Move(gameTime); Attack(gameTime, EnemyTrees); }
/// <summary> /// By default, the Unit moves randomly and targets the closest enemy Unit /// </summary> private void Behavior_Default(GameTime gameTime, List<Unit_KDTree> EnemyTrees, Unit_KDTree AllyTree) { DirectionTimer -= gameTime.ElapsedGameTime.TotalSeconds; if (DirectionTimer <= 0.0) { DirectionTimer = 5.0; TargetPosition = Manager.terrain.GetRandomPosition(); } 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); }