Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TelepadRegistry {
- private HashMap<Player, ArrayList<Telepad>> registeredPads;
- public void register(Player p, Telepad tp) {
- if(!registeredPads.containsKey(p)){
- //to prevent possible npe's if the HashMap doesn't have the player registered, add the player to the Map,
- //and add a new arraylist to that key index before putting the pad in the player's list.
- registeredPads.put(p, new ArrayList<Telepad>());
- }
- registeredPads.get(p).add(tp);
- }
- public Telepad getLastBuiltInRange(Telepad pad) {
- Location padLoc = pad.getLocation();
- Telepad currentChoice = pad;
- ArrayList<Telepad> inRange = getInRange(pad);
- if (inRange.isEmpty()) {
- // nothing found do nothing
- }
- else {
- currentChoice = inRange.get(0);
- double currentDiffTime = pad.getBuildTime() - currentChoice.getBuildTime();
- for (Telepad tp : inRange) {
- if(pad.getBuildTime() - tp.getBuildTime() < currentDiffTime) {
- currentChoice = tp;
- }
- }
- }
- return currentChoice;
- }
- private ArrayList<Telepad> getInRange(Telepad source) {
- ArrayList<Telepad> retVal = new ArrayList<Telepad>();
- Location sourceLoc = source.getLocation();
- for (Telepad tp : registeredPads.get(source.getPlayerOwner())) {
- Location destLoc = tp.getLocation();
- double dx = sourceLoc.getX() - destLoc.getX();
- double dy = sourceLoc.getY() - destLoc.getY();
- double distance = Math.abs(Math.sqrt((dx * dx) + (dy * dy)));
- if (distance < source.getRange()) {
- retVal.add(tp);
- }
- }
- return retVal;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement