mirror of
https://github.com/jneug/zeichenmaschine.git
synced 2026-10-12 01:45:18 +02:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3ab9701629 | ||
|
|
3196914564 |
@@ -191,6 +191,22 @@ public class Vector extends Point2D.Double {
|
|||||||
return new Vector(x, y);
|
return new Vector(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getIntX() {
|
||||||
|
return (int)this.x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getRoundedX() {
|
||||||
|
return (int)Math.round(this.x);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getIntY() {
|
||||||
|
return (int)this.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getRoundedY() {
|
||||||
|
return (int)Math.round(this.y);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setzt die Komponenten dieses Vektors neu.
|
* Setzt die Komponenten dieses Vektors neu.
|
||||||
*
|
*
|
||||||
|
|||||||
77
src/main/java/schule/ngb/zm/particles/BasicParticle.java
Normal file
77
src/main/java/schule/ngb/zm/particles/BasicParticle.java
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
package schule.ngb.zm.particles;
|
||||||
|
|
||||||
|
import schule.ngb.zm.Color;
|
||||||
|
import schule.ngb.zm.Vector;
|
||||||
|
|
||||||
|
import java.awt.Graphics2D;
|
||||||
|
|
||||||
|
public class BasicParticle extends Particle {
|
||||||
|
|
||||||
|
protected Color color, startColor, finalColor;
|
||||||
|
|
||||||
|
|
||||||
|
public BasicParticle() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public BasicParticle( Color startColor ) {
|
||||||
|
this(startColor, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BasicParticle( Color startColor, Color finalColor ) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.color = startColor;
|
||||||
|
this.startColor = startColor;
|
||||||
|
this.finalColor = finalColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Color getColor() {
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setColor( Color pColor ) {
|
||||||
|
this.color = pColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Color getStartColor() {
|
||||||
|
return startColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStartColor( Color pStartColor ) {
|
||||||
|
this.startColor = pStartColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Color getFinalColor() {
|
||||||
|
return finalColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFinalColor( Color pFinalColor ) {
|
||||||
|
this.finalColor = pFinalColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void spawn( int pLifetime, Vector pPosition, Vector pVelocity ) {
|
||||||
|
super.spawn(pLifetime, pPosition, pVelocity);
|
||||||
|
this.color = this.startColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update( double delta ) {
|
||||||
|
super.update(delta);
|
||||||
|
|
||||||
|
if( isActive() && startColor != null && finalColor != null ) {
|
||||||
|
double t = 1.0 - lifetime / maxLifetime;
|
||||||
|
this.color = Color.interpolate(startColor, finalColor, t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void draw( Graphics2D graphics ) {
|
||||||
|
if( isActive() && this.color != null ) {
|
||||||
|
graphics.setColor(this.color.getJavaColor());
|
||||||
|
graphics.fillOval(position.getIntX() - 3, position.getIntY() - 3, 6, 6);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package schule.ngb.zm.particles;
|
||||||
|
|
||||||
|
import schule.ngb.zm.Color;
|
||||||
|
|
||||||
|
public class BasicParticleFactory implements ParticleFactory {
|
||||||
|
|
||||||
|
private final Color startColor;
|
||||||
|
|
||||||
|
private final Color finalColor;
|
||||||
|
|
||||||
|
private boolean fadeOut = true;
|
||||||
|
|
||||||
|
public BasicParticleFactory() {
|
||||||
|
this(null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BasicParticleFactory( Color startColor ) {
|
||||||
|
this(startColor, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BasicParticleFactory( Color startColor, Color finalColor ) {
|
||||||
|
this.startColor = startColor;
|
||||||
|
this.finalColor = finalColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFadeOut( boolean pFadeOut ) {
|
||||||
|
this.fadeOut = pFadeOut;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Particle createParticle() {
|
||||||
|
Color finalClr = finalColor;
|
||||||
|
if( fadeOut ) {
|
||||||
|
if( finalColor != null ) {
|
||||||
|
finalClr = new Color(finalColor, 0);
|
||||||
|
} else if( startColor != null ) {
|
||||||
|
finalClr = new Color(startColor, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new BasicParticle(startColor, finalClr);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package schule.ngb.zm.particles;
|
||||||
|
|
||||||
|
import schule.ngb.zm.util.Log;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
public class GenericParticleFactory<T extends Particle> implements ParticleFactory {
|
||||||
|
|
||||||
|
private final Class<T> type;
|
||||||
|
|
||||||
|
private final Supplier<T> supplier;
|
||||||
|
|
||||||
|
public GenericParticleFactory( Class<T> type, Object... params ) {
|
||||||
|
this.type = type;
|
||||||
|
|
||||||
|
// Create paramTypes array once
|
||||||
|
Class<?>[] paramTypes = new Class<?>[params.length];
|
||||||
|
for( int i = 0; i < params.length; i++ ) {
|
||||||
|
paramTypes[i] = params[i].getClass();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.supplier = () -> {
|
||||||
|
T p = null;
|
||||||
|
try {
|
||||||
|
p = GenericParticleFactory.this.type.getDeclaredConstructor(paramTypes).newInstance(params);
|
||||||
|
} catch( Exception ex ) {
|
||||||
|
LOG.error( ex,
|
||||||
|
"Unable to create new Particle of type %s",
|
||||||
|
GenericParticleFactory.this.type.getCanonicalName()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return p;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public GenericParticleFactory( Supplier<T> supplier ) {
|
||||||
|
this.supplier = supplier;
|
||||||
|
this.type = (Class<T>)supplier.get().getClass();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Particle createParticle() {
|
||||||
|
return this.supplier.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final Log LOG = Log.getLogger(GenericParticleFactory.class);
|
||||||
|
|
||||||
|
}
|
||||||
60
src/main/java/schule/ngb/zm/particles/Particle.java
Normal file
60
src/main/java/schule/ngb/zm/particles/Particle.java
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
package schule.ngb.zm.particles;
|
||||||
|
|
||||||
|
import schule.ngb.zm.Drawable;
|
||||||
|
import schule.ngb.zm.Updatable;
|
||||||
|
import schule.ngb.zm.Vector;
|
||||||
|
|
||||||
|
|
||||||
|
public abstract class Particle extends PhysicsObject implements Updatable, Drawable {
|
||||||
|
|
||||||
|
protected double maxLifetime = 0, lifetime = 0;
|
||||||
|
|
||||||
|
|
||||||
|
public Particle() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void spawn( int pLifetime, Vector pPosition, Vector pVelocity ) {
|
||||||
|
this.maxLifetime = pLifetime;
|
||||||
|
this.lifetime = pLifetime;
|
||||||
|
this.position = pPosition.copy();
|
||||||
|
this.velocity = pVelocity.copy();
|
||||||
|
this.acceleration = new Vector();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isActive() {
|
||||||
|
return lifetime > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isVisible() {
|
||||||
|
return isActive();
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getLifetime() {
|
||||||
|
return lifetime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLifetime( double pLifetime ) {
|
||||||
|
this.lifetime = pLifetime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getMaxLifetime() {
|
||||||
|
return maxLifetime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMaxLifetime( double pMaxLifetime ) {
|
||||||
|
this.maxLifetime = pMaxLifetime;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update( double delta ) {
|
||||||
|
super.update(delta);
|
||||||
|
// lifetime -= delta;
|
||||||
|
lifetime -= 1;
|
||||||
|
|
||||||
|
// TODO: (ngb) calculate delta based on lifetime?
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
174
src/main/java/schule/ngb/zm/particles/ParticleEmitter.java
Normal file
174
src/main/java/schule/ngb/zm/particles/ParticleEmitter.java
Normal file
@@ -0,0 +1,174 @@
|
|||||||
|
package schule.ngb.zm.particles;
|
||||||
|
|
||||||
|
|
||||||
|
import schule.ngb.zm.Drawable;
|
||||||
|
import schule.ngb.zm.Updatable;
|
||||||
|
import schule.ngb.zm.Vector;
|
||||||
|
|
||||||
|
import java.awt.Graphics2D;
|
||||||
|
|
||||||
|
public class ParticleEmitter implements Updatable, Drawable {
|
||||||
|
|
||||||
|
protected ParticleFactory particleFactory;
|
||||||
|
|
||||||
|
private int particlesPerFrame;
|
||||||
|
|
||||||
|
private int particleLifetime = 180;
|
||||||
|
|
||||||
|
private Particle[] particles;
|
||||||
|
|
||||||
|
private boolean active = false;
|
||||||
|
|
||||||
|
private Particle nextParticle;
|
||||||
|
|
||||||
|
public Vector position;
|
||||||
|
|
||||||
|
public Vector direction = new Vector();
|
||||||
|
|
||||||
|
public double strength = 100.0;
|
||||||
|
|
||||||
|
public int angle = 0;
|
||||||
|
|
||||||
|
public double randomness = 0.0;
|
||||||
|
|
||||||
|
// private Vortex vortex = null;
|
||||||
|
|
||||||
|
public ParticleEmitter( double pX, double pY, int pParticleLifetime, int pParticlesPerFrame, ParticleFactory pFactory ) {
|
||||||
|
this.position = new Vector(pX, pY);
|
||||||
|
this.particlesPerFrame = pParticlesPerFrame;
|
||||||
|
this.particleLifetime = pParticleLifetime;
|
||||||
|
this.particleFactory = pFactory;
|
||||||
|
|
||||||
|
// Create particle pool
|
||||||
|
this.particles = new Particle[particlesPerFrame * pParticleLifetime];
|
||||||
|
this.direction = Vector.random(8, 16).normalize();
|
||||||
|
|
||||||
|
// vortex = new Vortex(position.copy().add(-10, -10), -.2, 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isActive() {
|
||||||
|
return active;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isVisible() {
|
||||||
|
return active;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start() {
|
||||||
|
this.direction.normalize();
|
||||||
|
|
||||||
|
// Partikel initialisieren
|
||||||
|
for( int i = 0; i < particles.length; i++ ) {
|
||||||
|
particles[i] = particleFactory.createParticle();
|
||||||
|
}
|
||||||
|
|
||||||
|
active = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop() {
|
||||||
|
for( int i = 0; i < particles.length; i++ ) {
|
||||||
|
particles[i].setLifetime(0);
|
||||||
|
particles[i] = null;
|
||||||
|
}
|
||||||
|
active = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Particle getNextParticle() {
|
||||||
|
// TODO: improve by caching next particle
|
||||||
|
for( Particle p : particles ) {
|
||||||
|
if( p != null && !p.isActive() ) {
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void emitParticle() {
|
||||||
|
int ppf = particlesPerFrame;
|
||||||
|
Particle nextParticle = getNextParticle();
|
||||||
|
while( ppf > 0 && nextParticle != null ) {
|
||||||
|
int lifetime = (int) random(particleLifetime);
|
||||||
|
|
||||||
|
double rotation = (angle / 2.0) - (int) (Math.random() * angle);
|
||||||
|
Vector velocity = direction.copy().scale(strength).rotate(rotation);
|
||||||
|
velocity.scale(random());
|
||||||
|
|
||||||
|
nextParticle.spawn(lifetime, this.position, velocity);
|
||||||
|
nextParticle = getNextParticle();
|
||||||
|
ppf -= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update( double delta ) {
|
||||||
|
emitParticle();
|
||||||
|
|
||||||
|
boolean _active = false;
|
||||||
|
for( Particle particle : particles ) {
|
||||||
|
if( particle != null ) {
|
||||||
|
if( particle.isActive() ) {
|
||||||
|
// if( vortex != null ) {
|
||||||
|
// vortex.attract(particle);
|
||||||
|
// }
|
||||||
|
particle.update(delta);
|
||||||
|
_active = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.active = _active;
|
||||||
|
}
|
||||||
|
|
||||||
|
private double random() {
|
||||||
|
return 1.0 - (Math.random() * randomness);
|
||||||
|
}
|
||||||
|
|
||||||
|
private double random( double pZahl ) {
|
||||||
|
return pZahl * random();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void draw( Graphics2D graphics ) {
|
||||||
|
java.awt.Color current = graphics.getColor();
|
||||||
|
for( Particle particle : particles ) {
|
||||||
|
if( particle != null && particle.isVisible() ) {
|
||||||
|
particle.draw(graphics);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if( vortex != null ) {
|
||||||
|
// graphics.setColor(java.awt.Color.BLACK);
|
||||||
|
// double vscale = (4 * vortex.scale);
|
||||||
|
// graphics.fillOval((int) (vortex.position.x - vscale * .5), (int) (vortex.position.y - vscale * .5), (int) vscale, (int) vscale);
|
||||||
|
// }
|
||||||
|
graphics.setColor(current);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class Vortex {
|
||||||
|
|
||||||
|
Vector position;
|
||||||
|
|
||||||
|
double speed = 1.0, scale = 1.0;
|
||||||
|
|
||||||
|
public Vortex( Vector pPosition, double pSpeed, double pScale ) {
|
||||||
|
this.position = pPosition.copy();
|
||||||
|
this.scale = pScale;
|
||||||
|
this.speed = pSpeed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void attract( Particle pPartikel ) {
|
||||||
|
Vector diff = Vector.sub(pPartikel.position, this.position);
|
||||||
|
double dx = -diff.y * this.speed;
|
||||||
|
double dy = diff.x * this.speed;
|
||||||
|
double f = 1.0 / (1.0 + (dx * dx + dy * dy) / scale);
|
||||||
|
|
||||||
|
pPartikel.position.x += (diff.x - pPartikel.velocity.x) * f;
|
||||||
|
pPartikel.position.y += (diff.y - pPartikel.velocity.y) * f;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package schule.ngb.zm.particles;
|
||||||
|
|
||||||
|
public interface ParticleFactory {
|
||||||
|
|
||||||
|
Particle createParticle();
|
||||||
|
|
||||||
|
}
|
||||||
69
src/main/java/schule/ngb/zm/particles/PhysicsObject.java
Normal file
69
src/main/java/schule/ngb/zm/particles/PhysicsObject.java
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
package schule.ngb.zm.particles;
|
||||||
|
|
||||||
|
import schule.ngb.zm.Updatable;
|
||||||
|
import schule.ngb.zm.Vector;
|
||||||
|
|
||||||
|
|
||||||
|
public abstract class PhysicsObject implements Updatable {
|
||||||
|
|
||||||
|
protected Vector position, velocity, acceleration;
|
||||||
|
|
||||||
|
protected double mass = 1.0;
|
||||||
|
|
||||||
|
|
||||||
|
public PhysicsObject() {
|
||||||
|
position = new Vector();
|
||||||
|
velocity = new Vector();
|
||||||
|
acceleration = new Vector();
|
||||||
|
}
|
||||||
|
|
||||||
|
public PhysicsObject( Vector pPosition ) {
|
||||||
|
position = pPosition.copy();
|
||||||
|
velocity = new Vector();
|
||||||
|
acceleration = new Vector();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vector getAcceleration() {
|
||||||
|
return acceleration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAcceleration( Vector pAcceleration ) {
|
||||||
|
this.acceleration = pAcceleration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getMass() {
|
||||||
|
return mass;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMass( double pMass ) {
|
||||||
|
this.mass = pMass;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vector getPosition() {
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPosition( Vector pPosition ) {
|
||||||
|
this.position = pPosition;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vector getVelocity() {
|
||||||
|
return velocity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setVelocity( Vector pVelocity ) {
|
||||||
|
this.velocity = pVelocity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void accelerate( Vector pAcceleration ) {
|
||||||
|
acceleration.add(Vector.div(pAcceleration, mass));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update( double delta ) {
|
||||||
|
velocity.add(acceleration);
|
||||||
|
position.add(Vector.scale(velocity, delta));
|
||||||
|
acceleration.scale(0.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
35
src/main/java/schule/ngb/zm/particles/StarParticle.java
Normal file
35
src/main/java/schule/ngb/zm/particles/StarParticle.java
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package schule.ngb.zm.particles;
|
||||||
|
|
||||||
|
import schule.ngb.zm.Color;
|
||||||
|
|
||||||
|
import java.awt.Graphics2D;
|
||||||
|
|
||||||
|
public class StarParticle extends BasicParticle {
|
||||||
|
|
||||||
|
public StarParticle() {
|
||||||
|
super();
|
||||||
|
this.startColor = Color.PURE_GREEN;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StarParticle( Color startColor ) {
|
||||||
|
this(startColor, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public StarParticle( Color startColor, Color finalColor ) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.color = startColor;
|
||||||
|
this.startColor = startColor;
|
||||||
|
this.finalColor = finalColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void draw( Graphics2D graphics ) {
|
||||||
|
if( isActive() && this.color != null ) {
|
||||||
|
graphics.setColor(this.color.getJavaColor());
|
||||||
|
graphics.drawLine((int) position.x - 3, (int) position.y - 3, (int) position.x + 3, (int) position.y + 3);
|
||||||
|
graphics.drawLine((int) position.x + 3, (int) position.y - 3, (int) position.x - 3, (int) position.y + 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
158
src/test/java/schule/ngb/zm/particles/ParticleExample.java
Normal file
158
src/test/java/schule/ngb/zm/particles/ParticleExample.java
Normal file
@@ -0,0 +1,158 @@
|
|||||||
|
package schule.ngb.zm.particles;
|
||||||
|
|
||||||
|
import schule.ngb.zm.*;
|
||||||
|
import schule.ngb.zm.layers.DrawableLayer;
|
||||||
|
import schule.ngb.zm.layers.DrawingLayer;
|
||||||
|
|
||||||
|
import java.awt.Graphics2D;
|
||||||
|
|
||||||
|
public class ParticleExample extends Testmaschine {
|
||||||
|
|
||||||
|
public static void main( String[] args ) {
|
||||||
|
new ParticleExample();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ParticleExample() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
ParticleEmitter pe1, pe2, pe3;
|
||||||
|
|
||||||
|
Rocket r;
|
||||||
|
|
||||||
|
public void setup() {
|
||||||
|
getLayer(DrawingLayer.class).hide();
|
||||||
|
background.setColor(0);
|
||||||
|
drawing.noStroke();
|
||||||
|
drawing.setFillColor(WHITE);
|
||||||
|
for( int i = 0; i < 1000; i++ ) {
|
||||||
|
drawing.point(random(0, canvasWidth), random(0, canvasHeight));
|
||||||
|
}
|
||||||
|
|
||||||
|
pe1 = new ParticleEmitter(
|
||||||
|
100, 100, 50, 5,
|
||||||
|
// new BasicParticleFactory(PINK, BLUE)
|
||||||
|
new GenericParticleFactory<Particle>(() -> {
|
||||||
|
return new Particle() {
|
||||||
|
@Override
|
||||||
|
public void draw( Graphics2D graphics ) {
|
||||||
|
graphics.setColor(Color.MAGENTA.getJavaColor());
|
||||||
|
graphics.rotate(Constants.radians(45), (int) position.x, (int) position.y);
|
||||||
|
graphics.drawRect((int) position.x - 3, (int) position.y - 3, 6, 6);
|
||||||
|
graphics.rotate(Constants.radians(-45), (int) position.x, (int) position.y);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
})
|
||||||
|
);
|
||||||
|
pe1.randomness = .2;
|
||||||
|
pe1.angle = 45;
|
||||||
|
pe1.strength = 200;
|
||||||
|
|
||||||
|
|
||||||
|
pe2 = new ParticleEmitter(
|
||||||
|
300, 300, 50, 10,
|
||||||
|
new GenericParticleFactory(() -> new StarParticle(RED, new Color(BLUE, 55)))
|
||||||
|
//new GenericParticleFactory(StarParticle.class, RED, new Color(BLUE, 55))
|
||||||
|
);
|
||||||
|
pe2.direction = NORTH.asVector().scale(100);
|
||||||
|
pe2.randomness = .8;
|
||||||
|
pe2.angle = 90;
|
||||||
|
pe2.strength = 200;
|
||||||
|
|
||||||
|
|
||||||
|
pe3 = new ParticleEmitter(
|
||||||
|
100, 400, 20, 8,
|
||||||
|
new BasicParticleFactory(YELLOW, RED)
|
||||||
|
);
|
||||||
|
pe3.direction = SOUTH.asVector();
|
||||||
|
pe3.randomness = .33;
|
||||||
|
pe3.angle = 30;
|
||||||
|
|
||||||
|
DrawableLayer drawables = new DrawableLayer();
|
||||||
|
addLayer(drawables);
|
||||||
|
drawables.add(pe1, pe2, pe3);
|
||||||
|
|
||||||
|
pe1.start();
|
||||||
|
pe2.start();
|
||||||
|
pe3.start();
|
||||||
|
|
||||||
|
r = new Rocket(200, 400);
|
||||||
|
drawables.add(r);
|
||||||
|
r.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update( double delta ) {
|
||||||
|
pe1.update(delta);
|
||||||
|
pe2.update(delta);
|
||||||
|
pe3.update(delta);
|
||||||
|
pe3.position.add(NORTH.asVector().scale(Constants.map(runtime, 0, 1000, 0, 10) * delta));
|
||||||
|
|
||||||
|
if( r.isActive() ) {
|
||||||
|
r.update(delta);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Rocket extends PhysicsObject implements Drawable {
|
||||||
|
|
||||||
|
ParticleEmitter trail;
|
||||||
|
|
||||||
|
private boolean starting = false;
|
||||||
|
|
||||||
|
private double acc = 4;
|
||||||
|
|
||||||
|
public Rocket( double x, double y ) {
|
||||||
|
super(new Vector(x, y));
|
||||||
|
trail = new ParticleEmitter(
|
||||||
|
x, y, 30, 6,
|
||||||
|
new BasicParticleFactory(YELLOW, RED)
|
||||||
|
);
|
||||||
|
trail.direction = SOUTH.asVector();
|
||||||
|
trail.randomness = .33;
|
||||||
|
trail.angle = 30;
|
||||||
|
trail.position = this.position;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start() {
|
||||||
|
starting = true;
|
||||||
|
trail.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isActive() {
|
||||||
|
return starting;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isVisible() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update( double delta ) {
|
||||||
|
super.update(delta);
|
||||||
|
if( this.acceleration.lengthSq() < acc * acc ) {
|
||||||
|
this.accelerate(NORTHWEST.asVector().scale(acc));
|
||||||
|
}
|
||||||
|
|
||||||
|
trail.update(delta);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void draw( Graphics2D graphics ) {
|
||||||
|
graphics.rotate(-Constants.radians(velocity.angle() + 180), position.getIntX(), position.getIntY());
|
||||||
|
trail.draw(graphics);
|
||||||
|
|
||||||
|
graphics.setColor(WHITE.getJavaColor());
|
||||||
|
graphics.fillRect(position.getIntX() - 6, position.getIntY() - 32, 12, 32);
|
||||||
|
graphics.fillPolygon(
|
||||||
|
new int[]{position.getIntX() - 6, position.getIntX(), position.getIntX() + 6},
|
||||||
|
new int[]{position.getIntY() - 32, position.getIntY() - 40, position.getIntY() - 32},
|
||||||
|
3
|
||||||
|
);
|
||||||
|
graphics.rotate(Constants.radians(velocity.angle() + 180), position.getIntX(), position.getIntY());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user