public void Update(IMessage message) { IThreatMessage threatMessage = message as IThreatMessage; if (message != null) { if (!GaveUp) { if (threatMessage.WindowWidth < X || threatMessage.WindowHeight < Y) { throw new ArgumentOutOfRangeException("Point can't be outside the window area!"); } if (Math.Abs(threatMessage.X - X) <= threatMessage.ActionRadius && Math.Abs(threatMessage.Y - Y) <= threatMessage.ActionRadius) { GaveUp = true; } else if (Math.Abs(threatMessage.X - X) <= ThreatRadius && Math.Abs(threatMessage.Y - Y) <= ThreatRadius) { RunAway(message); } } } }
private void RunAway(IThreatMessage message) { List <ShiftCoordinateDelegate> optimalShifts = new List <ShiftCoordinateDelegate>(); ShiftCoordinateDelegate shiftCoordinate; if (Y - Step >= 0) { if (Math.Abs((Y - Step) - message.Y) > Math.Abs(Y - message.Y)) { optimalShifts.Add(shiftCoordinate = (() => Y = Y - Step)); } } if (Y + Step <= message.WindowHeight) { if (Math.Abs((Y + Step) - message.Y) > Math.Abs(Y - message.Y)) { optimalShifts.Add(shiftCoordinate = (() => Y = Y + Step)); } } if (X + Step <= message.WindowWidth) { if (Math.Abs((X + Step) - message.X) > Math.Abs(X - message.X)) { optimalShifts.Add(shiftCoordinate = (() => X = X + Step)); } } if (X - Step >= 0) { shiftCoordinate = () => X = X - Step; } if (Math.Abs((X - Step) - message.X) > Math.Abs(X - message.X)) { optimalShifts.Add(shiftCoordinate = (() => X = X - Step)); } if (optimalShifts.Count != 0) { Random random = new Random(); int randomIndex = random.Next(optimalShifts.Count); optimalShifts[randomIndex](); } }
private void RunAway(IMessage message) { IThreatMessage threatMessage = (IThreatMessage)message; int moveX, moveY, preciseMoveX; double tgA; tgA = (threatMessage.Y - Y) / (threatMessage.X - X); moveX = (int)Math.Sqrt((Step * Step) / (tgA * tgA + 1)); preciseMoveX = moveX; moveY = (int)Math.Abs(tgA) * moveX; if (threatMessage.X < X && threatMessage.Y > Y) { if (X + moveX < threatMessage.WindowWidth) { X = X + moveX; } else { moveX = threatMessage.WindowWidth - X; X = threatMessage.WindowWidth; moveY = (int)Math.Sqrt(Step * Step - moveX * moveX); } if (Y - moveY > 0) { Y = Y - moveY; } else { moveY = Y; Y = 0; moveX = (int)Math.Sqrt(Step * Step - moveY * moveY); X = (X + (moveX - preciseMoveX) < threatMessage.WindowWidth) ? X + (moveX - preciseMoveX) : threatMessage.WindowWidth; } } if (threatMessage.X > X && threatMessage.Y > Y) { if (X - moveX > 0) { X = X - moveX; } else { moveX = X; X = 0; moveY = (int)Math.Sqrt(Step * Step - moveX * moveX); } if (Y - moveY > 0) { Y = Y - moveY; } else { moveY = Y; Y = 0; moveX = (int)Math.Sqrt(Step * Step - moveY * moveY); X = (X - (moveX - preciseMoveX) > 0) ? X - (moveX - preciseMoveX) : 0; } } if (threatMessage.X > X && threatMessage.Y < Y) { if (X - moveX > 0) { X = X - moveX; } else { moveX = X; X = 0; moveY = (int)Math.Sqrt(Step * Step - moveX * moveX); } if (Y + moveY < threatMessage.WindowHeight) { Y = Y + moveY; } else { moveY = threatMessage.WindowHeight - Y; Y = threatMessage.WindowHeight; moveX = (int)Math.Sqrt(Step * Step - moveY * moveY); X = (X - (moveX - preciseMoveX) > 0) ? X - (moveX - preciseMoveX) : 0; } } if (threatMessage.X < X && threatMessage.Y < Y) { if (X + moveX < threatMessage.WindowWidth) { X = X + moveX; } else { X = threatMessage.WindowWidth; moveX = threatMessage.WindowWidth - X; moveY = (int)Math.Sqrt(Step * Step - moveX * moveX); } if (Y + moveY < threatMessage.WindowHeight) { Y = Y + moveY; } else { moveY = threatMessage.WindowHeight - Y; Y = threatMessage.WindowHeight; moveX = (int)Math.Sqrt(Step * Step - moveY * moveY); X = (X + (moveX - preciseMoveX) > 0) ? X + (moveX - preciseMoveX) : 0; } } }