/** * Get all the entities who are touching this entity * * @param dest A list to store the entities in, or null, to construct a new one * @return dest, or a new List if dest was null */ public final List<Entity> checkCollisions(List<Entity> dest) { if (node != null) { return node.checkCollisions(this, dest); } else { // Haven't got a node in the quadtree. Use the root assert false : this + " is not in the quadtree!"; return COLLISIONMANAGER.checkCollisions(this, dest); } }
/** Add this entity to the collision manager */ protected final void addToCollisionManager() { if (node != null) { if (!node.remove(this)) { assert false : "Entity " + this + " was not found!"; } } node = COLLISIONMANAGER.add(this); if (node == null) { node = COLLISIONMANAGER; COLLISIONMANAGER.store(this); } }
/** Tick. Called every frame if this entity is alive. */ @Override public final void tick() { try { doTick(); float newR = getRadius(); if (node != null) { if (canCollide() && isActive()) { // If we've changed radius, remove and re-add to the quadtree if (oldR != newR) { oldR = newR; addToCollisionManager(); } } else { // Can't collide any more - remove from quadtree node.remove(this); node = null; } } else { if (canCollide() && isActive()) { // Add us in to the quadtree addToCollisionManager(); } } } catch (Exception e) { System.err.println("Error ticking " + this); e.printStackTrace(System.err); remove(); } }
/** Remove the entity */ @Override public final void remove() { removeSprites(); if (!active) { // Already removed return; } active = false; Worm.getGameState().removeEntity(this); if (node != null) { node.remove(this); node = null; } doRemove(); }
@Override public void actionPerformed(ActionEvent arg0) { LevelScreen screen = levelScreenManager.getScreen(); BallShooter shooter = screen.getShooter(); BallSequence sequence = screen.getSequence(); Hole hole = screen.getHole(); sequence.slide(); if (shooter.getShooting()) { shooter.shoot(); Ball current = shooter.getCurrent(); if (sequence.contains(current.getPoint())) { collisionManager.updateSequence(current, sequence); shooter.switchShooting(); } } if (hole.containsBall(sequence)) finishLevel(0); // lost if (sequence.getSize() == 0) finishLevel(1); // won }
/** * Find out which entities are touching the specified rectangle * * @param rect * @param dest * @return */ public static List<Entity> getCollisions(ReadableRectangle rect, List<Entity> dest) { return COLLISIONMANAGER.checkCollisions(rect, dest); }
public static void reset() { COLLISIONMANAGER.clear(); }
/** Process collisions */ public static void checkCollisions() { COLLISIONMANAGER.checkCollisions(); }