| 34 | /// the contents change) and applies a camera offset that scrolls the whole scene. |
| 35 | /// `#update(double)` advances every sprite (driving `AnimatedSprite` playback). |
| 36 | public class Scene { |
| 37 | private final List sprites = new ArrayList(); |
| 38 | private boolean sortDirty; |
| 39 | private int cameraX; |
| 40 | private int cameraY; |
| 41 | |
| 42 | private static final Comparator Z_ORDER = new ZComparator(); |
| 43 | |
| 44 | private static final class ZComparator implements Comparator { |
| 45 | @Override |
| 46 | public int compare(Object a, Object b) { |
| 47 | int za = ((Sprite) a).getZOrder(); |
| 48 | int zb = ((Sprite) b).getZOrder(); |
| 49 | return za < zb ? -1 : (za > zb ? 1 : 0); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /// Adds a sprite to the scene. |
| 54 | public void add(Sprite s) { |
| 55 | sprites.add(s); |
| 56 | sortDirty = true; |
| 57 | } |
| 58 | |
| 59 | /// Removes a sprite from the scene. |
| 60 | public void remove(Sprite s) { |
| 61 | sprites.remove(s); |
| 62 | } |
| 63 | |
| 64 | /// Removes all sprites. |
| 65 | public void clear() { |
| 66 | sprites.clear(); |
| 67 | } |
| 68 | |
| 69 | public int size() { |
| 70 | return sprites.size(); |
| 71 | } |
| 72 | |
| 73 | public Sprite get(int index) { |
| 74 | return (Sprite) sprites.get(index); |
| 75 | } |
| 76 | |
| 77 | /// Advances every sprite in the scene by calling `Sprite#onUpdate(double)`. Do |
| 78 | /// not add or remove sprites from within `onUpdate`; defer that to the next frame. |
| 79 | public void update(double deltaSeconds) { |
| 80 | for (Object sprite : sprites) { |
| 81 | ((Sprite) sprite).onUpdate(deltaSeconds); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /// Sorts the sprites by z-order if the contents changed. Called by the renderer |
| 86 | /// before drawing. |
| 87 | void ensureSorted() { |
| 88 | if (sortDirty) { |
| 89 | Collections.sort(sprites, Z_ORDER); |
| 90 | sortDirty = false; |
| 91 | } |
| 92 | } |
| 93 |
nothing calls this directly
no outgoing calls
no test coverage detected