Skip to content

Commit

Permalink
Merge pull request #2 from richardTingle/AllowNonIntegerEmisionsPerSe…
Browse files Browse the repository at this point in the history
…cond

Allow the emitter to have non integer emissions per second
  • Loading branch information
Jeddic authored Dec 13, 2021
2 parents 1eae147 + 7766d9d commit 277eaf6
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/main/java/com/epaga/particles/Emitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ public class Emitter extends Node {
// Emitter info
private int nextIndex = 0;
private float targetInterval = .00015f, currentInterval = 0;
private int emissionsPerSecond, totalParticlesThisEmission, particlesPerEmission;
private int particlesPerEmission;
private float emissionsPerSecond;
private boolean useStaticParticles = false;
private boolean useRandomEmissionPoint = false;
private boolean particlesFollowEmitter = true;
Expand Down Expand Up @@ -253,18 +254,28 @@ public EmitterShape getShape() {

/**
* Specifies the number of times the particle particles will emit particles over
* the course of one second
* the course of one second (May be a non-whole number)
*
* @param emissionsPerSecond The number of particle emissions per second, must be at least 1
* @param emissionsPerSecond The number of particle emissions per second, must be at least 0
*/
public void setEmissionsPerSecond(int emissionsPerSecond) {
if (emissionsPerSecond > 0) {
this.emissionsPerSecond = emissionsPerSecond;
targetInterval = 1f / emissionsPerSecond;
public void setEmissionsPerSecond(float emissionsPerSecond) {
assert emissionsPerSecond >= 0 : "emissionsPerSecond must be positive or zero";

if (emissionsPerSecond >= 0) {
this.emissionsPerSecond = emissionsPerSecond;
targetInterval = 1f / emissionsPerSecond;
}

/*
* If the emissions per second is changed suddenly from zero to a small number it might try to emit
* all that currentInterval suddenly (outputting a huge number of partials).
* Equally the emissionsPerSecond might be being updated every tick so we don't want to just reset
* currentInterval to zero when targetInterval is updated. This clamps it to "just about to emit".
*/
this.currentInterval = Math.min(targetInterval, this.currentInterval );
}

public int getEmissionsPerSecond() {
public float getEmissionsPerSecond() {
return emissionsPerSecond;
}

Expand Down Expand Up @@ -666,7 +677,7 @@ public void updateEmitter(float tpf) {
if (currentDuration <= duration) {
// check for particle emission
if (currentInterval >= targetInterval) {
totalParticlesThisEmission = calcParticlesPerEmission();
int totalParticlesThisEmission = calcParticlesPerEmission();
for (int i = 0; i < totalParticlesThisEmission; i++) {
emitNextParticle();
}
Expand Down Expand Up @@ -840,7 +851,7 @@ public void read(JmeImporter im) throws IOException {
maxParticles = ic.readInt("maxParticles", 30);
targetInterval = ic.readFloat("targetInterval", .00015f);
currentInterval = ic.readFloat("currentInterval", 0f);
emissionsPerSecond = ic.readInt("emissionsPerSecond", 20);
emissionsPerSecond = ic.readFloat("emissionsPerSecond", 20);
particlesPerEmission = ic.readInt("particlesPerEmission", 0);
useStaticParticles = ic.readBoolean("useStaticParticles", false);
useRandomEmissionPoint = ic.readBoolean("useRandomEmissionPoint", false);
Expand Down

0 comments on commit 277eaf6

Please sign in to comment.