Unit GetNearestEnemy(Point2 from, Player enemyPlayer, double stage, out Point2 point) { Unit ret = null; point = new Point2(); double minDist = double.MaxValue; for (int i = 0; i < Const.NumberOfLines; i++) { Unit enemyUnit = enemyPlayer.Units[i]; if (enemyUnit == null || enemyUnit.AtBase() || enemyUnit.State == UnitState.Dead) { continue; } Point2 temp = new Point2(enemyUnit.GetNextCoordinateXWithStage(stage), Const.FieldOriginY + Const.TileHeight * (i + 0.5)); if (from.DistTo(temp) < minDist) { minDist = from.DistTo(temp); point = temp; ret = enemyUnit; } } return(ret); }
double?GetAngleToNearestEnemy(Unit unit, int line, Player enemyPlayer, double stage) { Point2 from = new Point2(unit.GetNextCoordinateXWithStage(stage), Const.FieldOriginY + Const.TileHeight * (line + 0.5)); Point2 to; Unit enemyUnit = GetNearestEnemy(from, enemyPlayer, stage, out to); if (enemyUnit == null) { return(null); } double time = from.DistTo(to) / (Const.ProjectileSpeed * Const.TileWidth); to.x = enemyUnit.GetNextCoordinateXWithStage(stage + time); double angle_to = from.angleTo(to); if ((unit.Owner == OwnerType.Player1 && from.x > to.x) || (unit.Owner == OwnerType.Player2 && from.x <= to.x)) { angle_to -= 180; } if (angle_to < -180) { angle_to += 360; } double ret_angle = (angle_to - unit.CurrAngle); ret_angle = Utility.LimitAngle(ret_angle); return(ret_angle); }
public static Point2 GetShellFinishPoint(Point2 startPoint, Point2 aimCurrentPoint, bool aimGoesToTheLeft) { double shellSpeed = Const.ProjectileSpeed * Const.TileWidth; double d = aimGoesToTheLeft ? -1 : 1; //подберем время, через которое юнит и снаряд окажутся в одной точке double l = 0, r = 1000; Point2 aimPos = new Point2(); while (r - l > 0.0001) { double mid = (r + l) / 2; aimPos = new Point2( aimCurrentPoint.x + d * mid, aimCurrentPoint.y ); double shellDistance = startPoint.DistTo(aimPos); double timeShellReachesAim = shellDistance / shellSpeed; if (mid < timeShellReachesAim) // т е цель доедет до места встречи раньше, чем снаряд долетит. Время нужно увеличить { l = mid; } else { r = mid; } } return(aimPos); }