Move distance calculations into the Region object

This commit is contained in:
Trever Fischer 2012-04-25 16:28:49 -04:00
parent 3c10f7a379
commit 9eb41a1b5a
2 changed files with 8 additions and 8 deletions

View File

@ -40,4 +40,11 @@ public class Region {
public String name() { public String name() {
return m_name; return m_name;
} }
/**
* An alternative to Location.distance() which doesn't use floating point math.
*/
public int distanceTo(Location loc) {
return Math.abs((m_location.getBlockX()-loc.getBlockX())+Math.abs(m_location.getBlockZ()-loc.getBlockZ()));
}
} }

View File

@ -116,7 +116,7 @@ public class RegionManager {
Region nearest = null; Region nearest = null;
int minDistance = -1; int minDistance = -1;
for(Region r : regions) { for(Region r : regions) {
int check = distance(loc, r.location()); int check = r.distanceTo(loc);
if (minDistance == -1 || check < minDistance) { if (minDistance == -1 || check < minDistance) {
nearest = r; nearest = r;
minDistance = check; minDistance = check;
@ -186,11 +186,4 @@ public class RegionManager {
} }
} }
} }
/**
* An alternative to Location.distance() which doesn't use floating point math.
*/
private static int distance(Location l1, Location l2) {
return Math.abs((l1.getBlockX()-l2.getBlockX())+Math.abs(l1.getBlockZ()-l2.getBlockZ()));
}
} }