private Direction FindClosestDirection() { Automate closest = FindNearestNeighbor(); if (pos.x == closest.pos.x && (pos.y == closest.pos.y)) { return(RandomDirection()); } Direction tmpDirx = (pos.x < closest.pos.x) ? Direction.W : Direction.E; Direction tmpDiry = (pos.y < closest.pos.y) ? Direction.N : Direction.S; return((Math.Abs(closest.pos.x - pos.x) < Math.Abs(closest.pos.y - pos.y)) ? tmpDirx : tmpDiry); }
private Automate FindNearestNeighbor() { float distance; float closestDistance = float.MaxValue; Automate found = null; foreach (var bot in world.bots) { //Math.Sqsrt isn't required if we don't really need the actual distance distance = (bot.pos.x - pos.x) * (bot.pos.x - pos.x) + (bot.pos.y - pos.y) * (bot.pos.y - pos.y); //We need to ignore "self" otherwise the closest distance is always 0.0 ;) if (distance < closestDistance && distance != 0.0) { closestDistance = distance; found = bot; } } return(found); }