Skip to content

Instantly share code, notes, and snippets.

@lances101
Created June 3, 2016 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lances101/496e8c0145eaccb3bf06da3a1fd53a55 to your computer and use it in GitHub Desktop.
Save lances101/496e8c0145eaccb3bf06da3a1fd53a55 to your computer and use it in GitHub Desktop.
A location calculator for Exploration 2016
public class LocationUtility {
public static boolean areColliding(Location lvalue, Location rvalue) {
return lvalue.getX() == rvalue.getX() && lvalue.getY() == rvalue.getY();
}
private static void applyDirection(Location loc, int modX, int modY, int sizeX, int sizeY){
Location newLoc = new Location();
if(loc.getX() + modX > sizeX)
newLoc.setX(1);
else if(loc.getX() + modX < 1)
newLoc.setX(sizeX);
else
newLoc.setX(loc.getX()+modX);
if(loc.getY() + modY > sizeY)
newLoc.setY(loc.getX() % 2 == 0? 1 : 2);
else if(loc.getY() + modY < 1)
newLoc.setY(loc.getX() % 2 == 0? sizeY - 1 : sizeY);
else
newLoc.setY(loc.getY()+modY);
loc.setX(newLoc.getX());
loc.setY(newLoc.getY());
}
public static Location calculateNewLocation(Location location, int dir, int sizeX, int sizeY) {
Location newLocation = new Location();
newLocation.setX(location.getX());
newLocation.setY(location.getY());
switch(dir) {
case 1:
applyDirection(newLocation, 0, -2, sizeX, sizeY);
break;
case 2:
applyDirection(newLocation, 1, -1, sizeX, sizeY );
break;
case 3:
applyDirection(newLocation, 1, 1, sizeX, sizeY);
break;
case 4:
applyDirection(newLocation, 0, 2, sizeX, sizeY);
break;
case 5:
applyDirection(newLocation, -1, 1, sizeX, sizeY);
break;
case 6:
applyDirection(newLocation, -1, -1, sizeX, sizeY);
break;
}
return newLocation;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment