Skip to content

Commit

Permalink
Prefer lazy access to origin over default init.
Browse files Browse the repository at this point in the history
  • Loading branch information
steffen-wilke committed Dec 15, 2021
1 parent 7a557e2 commit 0ee92fa
Showing 1 changed file with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ public Emitter() {
this.emitterData.setOriginValign(info.originValign());
this.activateOnInit = info.activateOnInit();
}

this.updateOrigin();
}

public Emitter(EmitterData emitterData) {
Expand Down Expand Up @@ -147,7 +145,6 @@ public void addParticle(final Particle particle) {
this.particles.add(particle);
}

/** Deactivate. */
public void deactivate() {
if (!this.activated) {
return;
Expand All @@ -170,6 +167,7 @@ public void delete() {

@FunctionalInterface
public interface EmitterFinishedListener extends EventListener {

void finished(Emitter emitter);
}

Expand All @@ -188,10 +186,14 @@ public EmitterData data() {
}

public Point2D getOrigin() {
if (this.origin == null) {
this.updateOrigin();

}
return this.origin;
}

protected void updateOrigin(){
protected void updateOrigin() {
this.origin = new Point2D.Double(
this.getX() + this.data().getOriginAlign().getValue(this.getWidth()),
this.getY() + this.data().getOriginValign().getValue(this.getHeight()));
Expand Down Expand Up @@ -302,6 +304,11 @@ public void toggleStopped() {
this.stopped = !this.stopped;
}

@Override
public int getTimeToLive() {
return this.data().getEmitterDuration();
}

@Override
public void update() {
if (this.isPaused()) {
Expand Down Expand Up @@ -410,15 +417,7 @@ protected boolean particleCanBeRemoved(final Particle particle) {
return particle.timeToLiveReached();
}

/**
* Render particles of this effect. The particles are always rendered relatively to this effects
* render location. A particle doesn't have an own map location. It is always relative to the
* effect it is assigned to.
*
* @param g the g
* @param p the p
*/
/** Spawn particle. */

protected void spawnParticle() {
for (short i = 0; i < this.data().getSpawnAmount(); i++) {
if (!this.canTakeNewParticles()) {
Expand All @@ -432,6 +431,14 @@ protected void spawnParticle() {
}
}

/**
* Render particles of this effect. The particles are always rendered relatively to this effects
* render location. A particle doesn't have an own map location. It is always relative to the
* effect it is assigned to.
*
* @param g The graphics object to draw on.
* @param renderType The render type.
*/
private void renderParticles(final Graphics2D g, final RenderType renderType) {
if (Game.config().graphics().getGraphicQuality().getValue()
< this.data().getRequiredQuality().getValue()) {
Expand All @@ -445,16 +452,11 @@ private void renderParticles(final Graphics2D g, final RenderType renderType) {
: null;
for (Particle particle : this.particles) {
if (((!particle.usesCustomRenderType() && renderType == RenderType.NONE)
|| (particle.usesCustomRenderType() && particle.getCustomRenderType() == renderType))
|| (particle.usesCustomRenderType() && particle.getCustomRenderType() == renderType))
&& viewport != null
&& viewport.intersects(particle.getBoundingBox(origin))) {
particle.render(g, origin);
}
}
}

@Override
public int getTimeToLive() {
return this.data().getEmitterDuration();
}
}

0 comments on commit 0ee92fa

Please sign in to comment.