Skip to content

Commit

Permalink
feat(FireParticle): Implement Fire particle behavior
Browse files Browse the repository at this point in the history
- Add `FireParticleBehavior` 
- fix some behavior processing.

fix #84
  • Loading branch information
mcgivrer committed Nov 19, 2021
1 parent 625af14 commit 366cdd0
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,49 @@
public class BasicParticleBehavior implements Behavior<Particle> {

private ParticleSystem parent;
private int defaultLifeTime = 1000;
private Color defaultColor = Color.WHITE;

public BasicParticleBehavior(ParticleSystem ps) {
public BasicParticleBehavior(ParticleSystem ps, int defaultLifeTime) {
super();
this.parent = ps;
this.defaultLifeTime = defaultLifeTime;
}

@Override
public void onCreate(Particle p) {
Behavior.super.onCreate(p);
p.alive = true;
p.life = 1000;
p.life = defaultLifeTime;
p.color = Color.WHITE;
p.setSize(1 + (int) (Math.random() * 5));
p.setSize(2 + (int) (Math.random() * 5));
p.setPosition(parent.position);
p.setAcceleration(Utils.randV2d(-1, 1, -1, 1));

}

/**
* Define the default Color for the new particle
*
* @param c
* @return
*/
public BasicParticleBehavior setColor(Color c) {
this.defaultColor = c;
return this;
}

/**
* Set the default lifetime t for the new particle.
*
* @param t
* @return
*/
public BasicParticleBehavior setLifeTime(int t) {
this.defaultLifeTime = t;
return this;
}

@Override
public void onInput(Particle go, ActionHandler ih) {

Expand All @@ -40,10 +65,8 @@ public void onUpdate(Particle go, long dt) {
if (go.alive) {
go.life = go.life - 1;
go.setPosition(parent.position);
}
if (go.life < 0) {
go.alive = false;
go.life = 0;
} else if (go.life < 0) {
this.onCreate(go);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package fr.snapgames.fromclasstogame.core.behaviors.particle;

import fr.snapgames.fromclasstogame.core.behaviors.Behavior;
import fr.snapgames.fromclasstogame.core.entity.particles.Particle;
import fr.snapgames.fromclasstogame.core.gfx.Render;
import fr.snapgames.fromclasstogame.core.io.ActionHandler;

public class FireParticleBehavior implements Behavior<Particle> {

@Override
public void onInput(Particle go, ActionHandler ih) {

}

@Override
public void onUpdate(Particle go, long dt) {
go.velocity = go.acceleration.multiply(dt).multiply(0.5);
go.position = go.velocity.multiply(dt);
}

@Override
public void onRender(Particle go, Render r) {

}

@Override
public void onAction(Particle go, ActionHandler.ACTIONS action) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class ParticleSystem extends GameObject {

public ParticleSystem(String objectName, Vector2d pos) {
super(objectName, pos);
this.life = 10000;
}


Expand Down Expand Up @@ -48,7 +49,7 @@ public ParticleSystem addParticleBehavior(Behavior<Particle> bp) {
@Override
public List<String> getDebugInfo() {
List<String> ls = super.getDebugInfo();
ls.add(String.format("NbPtl:"+this.nbMaxParticle));
ls.add(String.format("NbPtl:" + this.nbMaxParticle));
return ls;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ private void update(GameObject go, long dt) {
go.position.x += ceilMinMaxValue(go.velocity.x * dtCorrected, 0.1, world.maxVelocity);
go.position.y += ceilMinMaxValue(go.velocity.y * dtCorrected, 0.1, world.maxVelocity);

// apply Object behaviors computations
if (go.behaviors.size() > 0) {
go.behaviors.forEach(b -> b.onUpdate(go, dt));
}


// test World space constrained
verifyGameConstraint(go);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,15 @@ public void create(Game g) throws UnknownResource {
add(bckG);

// add a ParticleSystem
ParticleSystem ps = new ParticleSystem("PS_test", player.position).create(100);
ps.addParticleBehavior(new BasicParticleBehavior(ps));
ParticleSystem ps = new ParticleSystem("PS_test", player.position);
ps.addParticleBehavior(
new BasicParticleBehavior(ps, 5000)
.setColor(Color.RED));
ps.add(new CopyObjectPosition(player));
ps.setLayer(1);
ps.setPriority(1);
ps.setDebug(3);
ps.create(10);
add(ps);

// add score display.
Expand Down

0 comments on commit 366cdd0

Please sign in to comment.