import com.dnsalias.java.gage.sprite.Sprite; import java.util.ArrayList; import java.util.Iterator; /** *

This class provides a convinient way of sorting sprites in back to * front order. Sorting is based on the Y position of the sprite. Depending * on what mode you select it either sorts on the top-Y coordinate or the * bottom-Y coordinate. This implementation is unsynchronized. Invoking * methods from different threads could result in strange behaviour. *

* Class requires the Gage package. *

* * @author Vincent van Beveren **/ public class SortedSprites { /** * Order by sprite top. **/ public final static int TOP = 0; /** * Order by sprite bottom. **/ public final static int BOTTOM = 1; private long autoSortDelay = 200; // sort only max 5 times a second private int width; // viewport width private int height; // viewport height private int mode; // y check mode private long nextSort = System.currentTimeMillis()+autoSortDelay; private ArrayList sprites = new ArrayList(); private boolean autoSort = true; // whether it should autosort private boolean checkBounds = true; // whether it should check bounds /** * Create a new list for sorted sprites. * * @param mode TOP or BOTTOM * @param width The width of the viewport * @param height The hieght of the viewport **/ public SortedSprites(int mode, int width, int height) { this.mode = mode; this.width = width; this.height= height; } /** * Sort the array automatically. If set * the array of sprites is sorted periodically. * If you do not want to sort, you can invoke checkOrder to sort the array. * * @param autoSort true if you want to autosort * false if you do not want to autosort **/ public void setAutoSort(boolean autoSort) { this.autoSort = autoSort; } /** * Sets the autoSort delay. By default this is 200ms. If your graphics * need faster or slower updating, it can be set with this. * * @param delay Delay in ms. **/ public void setAutoSortDelay(long delay) { autoSortDelay = delay; } /** * See whether the array is sorted automatically. **/ public boolean getAutoSort() { return autoSort; } /** * Check the bounds of sprite before drawing. **/ public void setCheckBounds(boolean checkBounds) { this.checkBounds = checkBounds; } /** * Check the bounds of sprite before drawing. **/ public boolean getCheckBounds() { return checkBounds; } /** * Add a sprite from the list of SortedSprites. * @param sp The sprite to add **/ public void add(Sprite sp) { if (!sprites.contains(sp)) { insert(sp); } } /** * Remove a sprite from the list of SortedSprites. * @param sp The sprite to remove. **/ public void remove(Sprite sp) { if (sprites.contains(sp)) { sprites.remove(sp); } } // return actual Y pos of sprite (in given mode) private int getY(Sprite sp) { if (mode==TOP) { return sp.getY(); } else { return sp.getY()+sp.getHeight(); } }; // Inserts sorted into array private void insert(Sprite sp) { sprites.add(sp); if (sprites.size()>1) { int y = getY(sp); int pos = sprites.size()-1; Object tmp; while (pos>0 && getY((Sprite)sprites.get(pos-1))>y) { tmp = sprites.get(pos-1); sprites.set(pos-1, sprites.get(pos)); sprites.set(pos, tmp); pos --; } } } /** * Resort the array of sprite positions. **/ public void checkOrder() { if (sprites.size()<=1) return; int oyPos = getY((Sprite)sprites.get(0)); // old Y int nyPos = 0; // new Y int n = 0; Object tmp; for (int i=1;i0 && getY((Sprite)sprites.get(n-1))>nyPos) { tmp = sprites.get(n-1); sprites.set(n-1, sprites.get(n)); sprites.set(n, tmp); n --; } } else oyPos = nyPos; } } /** * Render to Graphics object. **/ public void render(java.awt.Graphics g, int offsetx, int offsety) { if (autoSort) { if (nextSort=offsetx && sp.getY()>=offsety && sp.getX()+sp.getWidth()