Code:
/**
* An object
* @author Fritz
*
*/
public class Object {
/**
* Object's id
*/
private int primaryId;
/**
* Object id that will replace the object after clicked
*/
private int secondaryId;
/**
* Duration the object will be transformed
*/
private int transformDuration;
/**
* Location of the object
*/
private Location loc;
/**
* Actions when the object is clicked
*/
private Interaction[] actions;
/**
* Delay between interactions
*/
private final static long ACTION_DELAY = 1000;
/**
* Last interaction
*/
private static long lastAction;
/**
* If the object event has been triggered
*/
private static boolean triggered;
/**
* Creates an object
* @param objectId - Object ID
* @param actions - Actions to be performed when the object is clicked
*/
public Object(int objectId, Location loc, Interaction ... actions) {
this.primaryId = objectId;
this.loc = loc;
this.actions = actions;
}
/**
* Creates an object that will be replaced by the secondary object
* and then reverted after the tick
* @param primaryId - Object ID
* @param secondaryId - Object to turn into
* @param tick - Duration the object is transformed
* @param actions - What happens when clicked on
*/
public Object(int primaryId, int secondaryId, int transformDuration,
Location loc, Interaction ... actions) {
this.primaryId = primaryId;
this.secondaryId = secondaryId;
this.transformDuration = transformDuration;
this.loc = loc;
this.actions = actions;
}
/**
* Spawns the object for a player
* @param c - Player shown the object
*/
public void show(Client c) {
c.getPA().checkObjectSpawn(primaryId, loc.getX(), loc.getY(), 0, 10);
}
/**
* Called when the object is clicked on
* If the object has a transformation, it will transform if
* the interacction was successful
* @param index - Option clicked (ex first = 1, second = 2 ...)
*/
public boolean interactWith(final Client c, int index) {
if (System.currentTimeMillis() - ACTION_DELAY <= lastAction
|| triggered)
return false;
index--;
lastAction = System.currentTimeMillis();
if (actions[index].interact(c)) {
triggered = true;
c.getPA().checkObjectSpawn(secondaryId, loc.getX(), loc.getY(), 0, 10);
if (transformDuration > 0) {
EventManager.getSingleton().addEvent(new Event() {
@Override
public void execute(EventContainer container) {
show(c);
triggered = false;
container.stop();
}
}, transformDuration);
} else
triggered = false;
return true;
}
return false;
}
/**
* Gets the object id
* @return
*/
public int getObjectId() {
return primaryId;
}
/**
* Generates a hash
* @return - Object hash
*/
public static int getHash(int id, Location loc) {
return loc.getX() + loc.getY() << 4 + id << 8;
}
/**
* Gets the object's location
* @return - Object's location
*/
public Location getLoc() {
return loc;
}
/**
* An interaction with the object
* @author Fritz
*
*/
public interface Interaction {
/**
* Action on object click
* @param c - Player interacting
* @return - Whether the interaction was successful
*/
public abstract boolean interact(Client c);
}
}
Code:
/**
* Creates the area and initializes the objects
*/
public Area() {
initObjects();
}
/**
* List of objects in the area
*/
public HashMap<Integer, Object> objects =
new HashMap<Integer, Object>();
/**
* Adds an object to the objects HashMap
* @param object - Object to add to the map
*/
public void addObject(Object object) {
objects.put(Object.getHash(object.getObjectId(),
object.getLoc()), object);
}
/**
* Generates a hash for the inputs and attempts to locate an object
* in the hashmap with the requested hash
* @param objectId - ID of the object
* @param loc - Location of the object
* @return - Object corresponding to the hash
*/
public Object getObject(int objectId, Location loc) {
return objects.get(Object.getHash(objectId, loc));
}
Code:
for (Area area : AreaHandler.areas)
if (area.getObject(objectType, new Location(obX, obY, c.heightLevel))
.interactWith(c, 1)) // 2 for 2nd click and 3 for 3rd click
return;
Code:
for(Area area : AreaHandler.areas)
for (Object object : area.objects.values())
object.show(c);
Usage:
Code:
public void initObjects() {
addObject(new Object(2929, -1, 5000, new Location(3096, 9881, 0),
new Interaction() {
@Override
public boolean interact(Client c) {
c.sendMessage("You picked up the bones");
c.getItems().addItem(526, 1);
return true;
}
}
));
}