Skip to content

Commit

Permalink
Remove the consumer event by a proper listener.
Browse files Browse the repository at this point in the history
  • Loading branch information
Steffen Wilke committed Jan 11, 2020
1 parent 70c598a commit 4204d8a
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/de/gurkenlabs/litiengine/graphics/emitters/Emitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.EventListener;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import de.gurkenlabs.litiengine.Align;
Expand All @@ -37,7 +38,7 @@ public abstract class Emitter extends Entity implements IUpdateable, ITimeToLive
public static final int DEFAULT_SPAWNAMOUNT = 1;
public static final int DEFAULT_MAXPARTICLES = 100;

private final List<Consumer<Emitter>> finishedConsumer;
private final Collection<EmitterFinishedListener> finishedListeners;
private final CopyOnWriteArrayList<Particle> particles;
private final List<Color> colors;

Expand Down Expand Up @@ -65,7 +66,7 @@ public abstract class Emitter extends Entity implements IUpdateable, ITimeToLive

public Emitter() {
this.colors = new ArrayList<>();
this.finishedConsumer = new CopyOnWriteArrayList<>();
this.finishedListeners = ConcurrentHashMap.newKeySet();
this.particles = new CopyOnWriteArrayList<>();
this.renderables = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -275,8 +276,12 @@ public boolean isStopped() {
return this.stopped;
}

public void onFinished(Consumer<Emitter> cons) {
this.finishedConsumer.add(cons);
public void onFinished(EmitterFinishedListener listener) {
this.finishedListeners.add(listener);
}

public void removeFinishedListener(EmitterFinishedListener listener) {
this.finishedListeners.remove(listener);
}

@Override
Expand Down Expand Up @@ -369,8 +374,8 @@ public void update() {

// clear particles if the effect time to life is reached
if (this.isFinished()) {
for (Consumer<Emitter> cons : this.finishedConsumer) {
cons.accept(this);
for (EmitterFinishedListener listener : this.finishedListeners) {
listener.finished(this);
}

this.delete();
Expand Down Expand Up @@ -502,4 +507,9 @@ private void renderParticles(final Graphics2D g, final RenderType renderType) {
}
}
}

@FunctionalInterface
public interface EmitterFinishedListener extends EventListener {
void finished(Emitter emitter);
}
}

0 comments on commit 4204d8a

Please sign in to comment.