private BattleMoveOutputSingle[] MakeTeamMove( string teamName, UnitData[] allUnits, ProjectileData[] allProjs, ITeamBattleMoveMaker tb) { UnitData[] friendlyUnits = allUnits.Where(x => x.Team == teamName).ToArray(); UnitData[] enemyUnits = allUnits.Where(x => x.Team != teamName).ToArray(); ProjectileData[] friendlyProj = allProjs.Where(x => x.Team == teamName).ToArray(); ProjectileData[] enemyProj = allProjs.Where(x => x.Team != teamName).ToArray(); BattleMoveOutputSingle[] result = tb.MakeMove(new BattleMoveInputWholeTeam { FriendlyUnits = friendlyUnits, EnemyUnits = enemyUnits, FriendlyProjs = friendlyProj, EnemyProjs = enemyProj }); if (friendlyUnits.Length != result.Length) { throw new Exception("The results are not the same length as the friendly units passed!"); } for (int i = 0; i < result.Length; i++) { BattleMoveOutputSingle data = result[i]; UnitData unit = friendlyUnits[i]; data.ID = unit.ID; } return(result); }
public BattleMoveOutputSingle[] MakeMove(BattleMoveInputWholeTeam input) { var returnVal = new BattleMoveOutputSingle[input.FriendlyUnits.Length]; MoveHelpersDeterministic help = new MoveHelpersDeterministic(); BattleMoveOutputSingle[] result = input.FriendlyUnits.Select(x => new BattleMoveOutputSingle()).ToArray(); for (int i = 0; i < input.FriendlyUnits.Length; i++) { UnitData myUnit = input.FriendlyUnits[i]; Fix64Vector2 myLocation = input.FriendlyUnits[i].Position; SpellCooldown fireball = myUnit.SpellCooldowns.FirstOrDefault(x => x.Spell == "fireball"); result[i].ProjVelosity = null; if (fireball != null && input.EnemyUnits.Length > 0 && fireball.CurrentIteration <= 0) { result[i].ProjVelosity = input.EnemyUnits[0].Position - myLocation; } var allHits = input.EnemyProjs .Select(x => new { one = help.DistanceToHit(x.Position, x.Direction, myLocation), two = x }) .Where(x => x.one != null) .Where(x => x.one.HitVector.Magnitude <= (Fix64)1.5f) .ToArray(); result[i].NewLocation = null; if (allHits.Length != 0) { var thing = allHits.First(x => x.one.DistanceToProj == allHits.Min(y => y.one.DistanceToProj)); Fix64Vector2 hitVector = thing.one.HitVector; Fix64Vector2 projVel = thing.two.Direction; if (hitVector.Magnitude == (Fix64)0) { result[i].NewLocation = (projVel.Negative()).Rotate((Fix64)90); } else { result[i].NewLocation = (hitVector.Negative()).Normalized; } } } return(result); }
private void CheckCollisionsAndMove(ref List <UnitData> units, ref List <ProjectileData> projectiles, ref List <BattleMoveOutputSingle> results) { List <int> removedUnitsIndecies = new List <int>(); List <int> removedProjsIndecies = new List <int>(); units = units.OrderBy(x => x.ID).ToList(); results = results.OrderBy(x => x.ID).ToList(); for (int i = units.Count - 1; i >= 0; i--) { UnitData unit = units[i]; for (int j = projectiles.Count - 1; j >= 0; j--) { ProjectileData proj = projectiles[j]; if ((unit.Position - proj.Position).Magnitude < (Fix64)1f && unit.Team != proj.Team) { unit.Health -= proj.Demage; if (unit.Health <= 0) { removedUnitsIndecies.Add(i); } removedProjsIndecies.Add(j); } } BattleMoveOutputSingle result = results[i]; if (result.NewLocation == null) { continue; } Fix64Vector2 currentPos = unit.Position; Fix64Vector2 nextPos = unit.Position + result.NewLocation.Value.Normalized * this.unitSpeed; List <UnitData> overlapers = this.units .Where(x => x.ID != unit.ID) .Where(x => (x.Position - currentPos).Magnitude < (Fix64)1f) .Where(x => (x.Position - currentPos).Magnitude > (x.Position - nextPos).Magnitude) .ToList(); if (overlapers.Any()) { ///No move is made! } else { unit.Position = nextPos; } } foreach (var ind in removedUnitsIndecies.OrderByDescending(x => x)) { units.RemoveAt(ind); } foreach (var ind in removedProjsIndecies.OrderByDescending(x => x)) { projectiles.RemoveAt(ind); } }