/** 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(); } }
/** 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); } }
/** 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(); }