PDA

View Full Version : (317-377) Player's map area



Shaun
July 4th, 2010, 11:16
So I saw some stupid tutorial on how to get player's map areas which just had tons of if statements and location checks, so I wrote my own. It is simple so I won't really be explaining anything.


public class MapSquare
{
private final int
MIN_X, // min x bound of the square
MAX_Y, // max y bound of the square
MAX_X, // max x bound of the square
MIN_Y, // min y bound of the square
HEIGHT; // height of the square

/**
* Initialize the MapSquare
* @param MIN_X min x bound of the square
* @param MAX_Y max y bound of the square
* @param MAX_X max x bound of the square
* @param MIN_Y min y bound of the square
* @param HEIGHT height of the square
*/
MapSquare(int MIN_X, int MAX_Y, int MAX_X, int MIN_Y, int HEIGHT)
{
this.MIN_X = MIN_X;
this.MAX_Y = MAX_Y;
this.MAX_X = MAX_X;
this.MIN_Y = MIN_Y;
this.HEIGHT = HEIGHT;
}

/**
* @param p player to check if is in the square
* @return if player is in this map square
*/
public boolean isPlayerInSquare(Player p)
{
return (p.absX >= MIN_X && p.absY <= MAX_Y && p.absX <= MAX_X && p.absY >= MIN_Y && p.heightLevel == HEIGHT);
}
}


public enum MapArea
{
EDGEVILLE(new MapSquare(3074, 3514, 3101, 3486, 0), "Edgeville"),
VARROCK(new MapSquare(3150, 3509, 3291, 3375, 0), "Varrock"),
MAGE_BANK(new MapSquare(2528, 4726, 2548, 4709, 0), "Mage Bank"),
CAMELOT(new MapSquare(2743, 3520, 3774, 3482, 0), "Camelot Castle"),
ETC(null, "keep putting the areas in here"),
DEFAULT_NOT_FOUND(null, "not found");

private final MapSquare SQUARE; // square area of this map area
private final String DESCRIPTION; // description of the area

/**
* Initialize the map area
* @param SQUARE squae area of this map area
* @param DESCRIPTION description of the area
*/
MapArea(MapSquare SQUARE, String DESCRIPTION)
{
this.SQUARE = SQUARE;
this.DESCRIPTION = DESCRIPTION;
}

/**
* @return square area of this map area
*/
public MapArea getArea()
{ return SQUARE; }

/**
* @return description string of this map area
*/
public String toString()
{ return DESCRIPTION; }

/**
* Find what area the given player is in
* @param p player to find area of
* @return MapArea for the given
*/
public static MapArea getPlayersMapArea(Player p)
{
// loop through all map areas to check
for (MapArea area : MapArea.values())
{
if (area.getArea().isPlayerInSquare(p))
return area;
}

// area not created, so not found
return DEFAULT_NOT_FOUND;
}
}


sendMessage(p.playerName+"'s location is " + MapArea.getPlayersMapArea(this).toString() + ".");