/** * Set the act order of objects in the world. Act order is specified by class: objects of one * class will always act before objects of some other class. The order of objects of the same * class cannot be specified. * * <p>Objects of classes listed first in the parameter list will act before any objects of classes * listed later. * * <p>Objects of a class not explicitly specified inherit the act order from their superclass. * * <p>Objects of classes not listed will act after all objects whose classes have been specified. * * @param classes The classes in desired act order */ public void setActOrder(Class... classes) { if (classes == null) { // Allow null as an argument, to specify no paint order if (objectsInActOrder == objectsDisordered) { if (objectsInPaintOrder == null) { classes = new Class[0]; objectsDisordered.setClassOrder(false, classes); } else { objectsDisordered = objectsInPaintOrder; } } objectsInActOrder = null; return; } if (objectsInActOrder != null) { // Just reuse existing set } else if (objectsInPaintOrder == objectsDisordered) { // Use new set because existing disordered set is in use // already. objectsInActOrder = new TreeActorSet(); objectsInActOrder.setClassOrder(false, classes); objectsInActOrder.addAll(objectsDisordered); return; } else { // Reuse disordered set, since it is not already in use by the // paint ordering objectsInActOrder = objectsDisordered; } objectsInActOrder.setClassOrder(false, classes); }
/** * Remove an object from the world. * * @param object the object to remove */ public void removeObject(Actor object) { if (object == null || object.world != this) { return; } objectsDisordered.remove(object); collisionChecker.removeObject(object); if (objectsDisordered != objectsInActOrder && objectsInActOrder != null) { objectsInActOrder.remove(object); } else if (objectsDisordered != objectsInPaintOrder && objectsInPaintOrder != null) { objectsInPaintOrder.remove(object); } object.setWorld(null); }
/** * Get all the objects in the world, or all the objects of a particular class. * * <p>If a class is specified as a parameter, only objects of that class (or its subclasses) will * be returned. * * <p> * * @param cls Class of objects to look for ('null' will find all objects). * @return A list of objects. */ public List getObjects(Class cls) { List<Actor> result = new ArrayList<Actor>(); Iterator<Actor> i = objectsDisordered.iterator(); while (i.hasNext()) { Actor actor = i.next(); if (cls == null || cls.isInstance(actor)) { result.add(actor); } } return result; }
/** * Add an Actor to the world. * * @param object The new object to add. * @param x The x coordinate of the location where the object is added. * @param y The y coordinate of the location where the object is added. */ public void addObject(Actor object, int x, int y) { if (object.world != null) { if (object.world == this) { return; // Actor is already in the world } object.world.removeObject(object); } objectsDisordered.add(object); addInPaintOrder(object); addInActOrder(object); // Note we must call this before adding the object to the collision checker, // so that the cached bounds are cleared: object.addToWorld(x, y, this); collisionChecker.addObject(object); object.addedToWorld(this); WorldHandler whInstance = WorldHandler.getInstance(); if (whInstance != null) { WorldHandler.getInstance().objectAddedToWorld(object); } }
private void addInPaintOrder(Actor object) { if (objectsInPaintOrder != null) { objectsInPaintOrder.add(object); } }
/** * Get the number of actors currently in the world. * * @return The number of actors */ public int numberOfObjects() { return objectsDisordered.size(); }