private IntrestingObj FindClosestObj(IInheritanceTreeCollection <Geometry> MapObjects) { var objects = MapObjects.GetAll <IntrestingObj>(); IntrestingObj closest = objects[0]; foreach (IntrestingObj obj in objects) { if (obj.Distance(this) < closest.Distance(this)) { closest = obj; } } return(closest); }
public void MoveToObj(IInheritanceTreeCollection <Geometry> MapObjects) { IntrestingObj closestObj = FindClosestObj(MapObjects); if (IsStuck == 1) { Console.WriteLine("Я застрял, пытаюсь найти край рифа"); IsStuck = 2; Coordinate reefDownLeftCoordinate = StuckedReef.Coordinates[1]; if (FindRelativeCoords(reefDownLeftCoordinate).Y < 0) { Y = StuckedReef.Coordinates[0].Y + 50000; } else { Y = reefDownLeftCoordinate.Y - 50000; } } else if (IsStuck == 2) { Console.WriteLine("Край нашёл, теперь обхожу"); IsStuck = 0; Coordinate reefDownLeftCoordinate = StuckedReef.Coordinates[1]; if (FindRelativeCoords(reefDownLeftCoordinate).X < 0) { X = StuckedReef.Coordinates[0].X - 22000; } else { X = reefDownLeftCoordinate.X + 22000; } StuckedReef = null; } else { if (closestObj.Distance(this) <= Speed / 5) { if (WaitingTime == 0) { X = closestObj.Coordinate.X; Y = closestObj.Coordinate.Y; } else if (WaitingTime == 10) { MapObjects.Remove(closestObj); WaitingTime = 0; } WaitingTime++; } Coordinate relativeCoord = FindRelativeCoords(closestObj.Coordinate); double curSpeedX = Math.Abs(relativeCoord.X) > Speed ? Speed : Speed / 5; double curSpeedY = Math.Abs(relativeCoord.Y) > Speed ? Speed : Speed / 5; double nextX = X + curSpeedX * Math.Sign(relativeCoord.X); double nextY = Y + curSpeedY * Math.Sign(relativeCoord.Y); var nextCoord = new Coordinate(nextX, nextY); Reef stuckedReef = CheckReefs(relativeCoord, MapObjects, nextCoord); if (stuckedReef == null) { X += curSpeedX * Math.Sign(relativeCoord.X); Y += curSpeedY * Math.Sign(relativeCoord.Y); } else { IsStuck = 1; StuckedReef = stuckedReef; } } }