Search in sources :

Example 6 with ParticleComponent

use of com.almasb.fxgl.particle.ParticleComponent in project FXGL by AlmasB.

the class SmokeSample method initGame.

@Override
protected void initGame() {
    getGameScene().setBackgroundColor(Color.BLACK);
    e = ParticleEmitters.newSmokeEmitter();
    e.setBlendMode(BlendMode.SRC_OVER);
    e.setSize(15, 30);
    e.setNumParticles(10);
    e.setEmissionRate(0.25);
    e.setStartColor(Color.color(0.6, 0.55, 0.5, 0.47));
    e.setEndColor(Color.BLACK);
    e.setExpireFunction(i -> Duration.seconds(16));
    e.setVelocityFunction(i -> new Point2D(FXGLMath.randomDouble() - 0.5, 0));
    // Entities.builder()
    // .at(getWidth() / 2, getHeight() - 100)
    // .with(new ParticleComponent(e), new RandomMoveControl(2))
    // .buildAndAttach(getGameWorld());
    emitter = ParticleEmitters.newFireEmitter();
    // emitter.setSize(5, 15);
    // emitter.setVelocityFunction(i -> new Point2D(FXGLMath.random() - 0.5, -FXGLMath.random() * 3));
    // emitter.setAccelerationFunction(() -> new Point2D(0, 0.05));
    // emitter.setExpireFunction(i -> Duration.seconds(3));
    // emitter.setScaleFunction(i -> new Point2D(FXGLMath.random(0, 0.01), FXGLMath.random(-0.05, 0.05)));
    // emitter.setStartColor(Color.YELLOW);
    // emitter.setEndColor(Color.RED);
    // emitter.setBlendMode(BlendMode.SRC_OVER);
    // emitter.setSourceImage(texture("particleTexture2.png").toColor(Color.rgb(230, 75, 40)).getImage());
    entity = entityBuilder().with(new ParticleComponent(emitter)).buildAndAttach();
// entity = Entities.builder()
// .at(getWidth() / 2, getHeight() / 2)
// .with(new ParticleComponent(emitter))
// .buildAndAttach(getGameWorld());
// 
// 
// 
// Entities.builder()
// .at(250, 250)
// .viewFromNode(new Rectangle(40, 40, Color.BLUE))
// .with(new CircularMovementComponent(10, 25))
// .buildAndAttach(getGameWorld());
}
Also used : ParticleComponent(com.almasb.fxgl.particle.ParticleComponent) Point2D(javafx.geometry.Point2D)

Example 7 with ParticleComponent

use of com.almasb.fxgl.particle.ParticleComponent in project FXGL by AlmasB.

the class RainSample method initGame.

@Override
protected void initGame() {
    entityBuilder().view(texture("underwater3.png", getAppWidth(), getAppHeight())).buildAndAttach();
    // example - multiply color with existing
    ParticleEmitter emitter = ParticleEmitters.newRainEmitter(getAppWidth() / 2);
    emitter.setSourceImage(texture("rain.png").multiplyColor(Color.BLUE));
    entityBuilder().with(new ParticleComponent(emitter)).buildAndAttach();
    // example - set color
    ParticleEmitter emitter2 = ParticleEmitters.newRainEmitter(getAppWidth() / 2);
    emitter2.setSourceImage(texture("rain.png").toColor(Color.YELLOW));
    emitter2.setInterpolator(Interpolators.EXPONENTIAL.EASE_OUT());
    entityBuilder().at(getAppWidth() / 2, 0).with(new ParticleComponent(emitter2)).buildAndAttach();
}
Also used : ParticleEmitter(com.almasb.fxgl.particle.ParticleEmitter) ParticleComponent(com.almasb.fxgl.particle.ParticleComponent)

Example 8 with ParticleComponent

use of com.almasb.fxgl.particle.ParticleComponent in project FXGL by AlmasB.

the class AttractorSample method initGame.

@Override
protected void initGame() {
    points = new ArrayList<>();
    points.add(new Point2D(100, 100));
    points.add(new Point2D(getAppWidth() - 100, getAppHeight() - 100));
    getGameScene().setBackgroundColor(Color.BLACK);
    emitter = ParticleEmitters.newExplosionEmitter(300);
    emitter.setBlendMode(BlendMode.ADD);
    emitter.setMaxEmissions(1);
    emitter.setNumParticles(1440);
    emitter.setExpireFunction(i -> Duration.seconds(20000));
    emitter.setEmissionRate(0.52);
    emitter.setSize(4, 64);
    emitter.setScaleFunction(i -> new Point2D(0, 0));
    emitter.setAccelerationFunction(() -> Point2D.ZERO);
    emitter.setVelocityFunction(i -> Point2D.ZERO);
    // emitter.setVelocityFunction(i -> FXGLMath.randomPoint2D().multiply(random(1, 45)));
    emitter.setSpawnPointFunction(i -> new Point2D(random(-20, 20), random(-20, 20)));
    // emitter.setStartColor(Color.color(0.8, 0.2, 0.1, 0.85));
    emitter.setSourceImage(texture("particles/circle_03.png", 64, 64).multiplyColor(Color.color(0.9, 0.1, 0.2, random(0.2, 0.9))));
    var e = entityBuilder().with(new ParticleComponent(emitter)).buildAndAttach();
    emitter.setControl(p -> {
        // using general model adapted from https://github.com/jodiDL/SimpleParticleSystem
        var x = p.position.x;
        var y = p.position.y;
        var noiseValue = FXGLMath.noise2D(x * 0.002 * t, y * 0.002 * t);
        var v = e.getPosition().subtract(p.position.x, p.position.y);
        var distanceSquared = v.magnitude() * v.magnitude();
        var power = 250.0 / max(10, min(distanceSquared, 1600)) * (noiseValue + 1) * random(1, 4);
        p.acceleration.set(v.normalize().multiply(power));
        if (p.position.x < 0) {
            p.position.x = 0;
            p.velocity.x = 1;
        }
        if (p.position.x > getAppWidth() - 10) {
            p.position.x = getAppWidth() - 10;
            p.velocity.x = -1;
        }
        if (p.position.y < 0) {
            p.position.y = 0;
            p.velocity.y = 1;
        }
        if (p.position.y > getAppHeight() - 10) {
            p.position.y = getAppHeight() - 10;
            p.velocity.y = -1;
        }
    });
    e.xProperty().bind(getInput().mouseXWorldProperty().subtract(10));
    e.yProperty().bind(getInput().mouseYWorldProperty().subtract(10));
}
Also used : ParticleComponent(com.almasb.fxgl.particle.ParticleComponent) Point2D(javafx.geometry.Point2D)

Example 9 with ParticleComponent

use of com.almasb.fxgl.particle.ParticleComponent in project FXGL by AlmasB.

the class ParticleScaleSample method initGame.

@Override
protected void initGame() {
    ParticleEmitter emitter = ParticleEmitters.newFireEmitter();
    emitter.setSpawnPointFunction(i -> new Point2D(0, 0));
    emitter.setSourceImage(texture("brick.png"));
    emitter.setBlendMode(BlendMode.SRC_OVER);
    emitter.setSize(12, 14);
    emitter.setScaleFunction(i -> new Point2D(0, 0));
    emitter.setEntityScaleFunction(() -> new Point2D(1.75, 1.75));
    emitter.setScaleOriginFunction(i -> new Point2D(12, 32));
    var e = entityBuilder().at(250, 250).viewWithBBox("brick.png").scale(1.75, 1.75).with(new ParticleComponent(emitter)).buildAndAttach();
    e.getTransformComponent().setScaleOrigin(new Point2D(12, 32));
}
Also used : ParticleEmitter(com.almasb.fxgl.particle.ParticleEmitter) ParticleComponent(com.almasb.fxgl.particle.ParticleComponent) Point2D(javafx.geometry.Point2D)

Example 10 with ParticleComponent

use of com.almasb.fxgl.particle.ParticleComponent in project FXGL by AlmasB.

the class PolynomialFunctionSample method initGame.

@Override
protected void initGame() {
    getGameScene().setBackgroundColor(Color.BLACK);
    spawnPoints = new ArrayList<>();
    dest = new HashMap<>();
    for (int y = 0; y < getAppHeight(); y += 10) {
        spawnPoints.add(new Point2D(0, y));
        spawnPoints.add(new Point2D(getAppWidth(), y));
    }
    for (int x = 0; x < getAppWidth(); x += 10) {
        spawnPoints.add(new Point2D(x, 0));
        spawnPoints.add(new Point2D(x, getAppHeight()));
    }
    points = IntStream.rangeClosed(0, getAppWidth()).filter(i -> i % 2 == 0).map(x -> x - getAppWidth() / 2).mapToDouble(x -> x / PIXELS_PER_UNIT).mapToObj(x -> new Point2D(x * PIXELS_PER_UNIT + getAppWidth() / 2.0, getAppHeight() - (func.apply(x) * PIXELS_PER_UNIT + getAppHeight() / 2.0))).filter(p -> new Rectangle2D(0, 0, getAppWidth(), getAppHeight()).contains(p)).collect(Collectors.toList());
    emitter = ParticleEmitters.newExplosionEmitter(300);
    emitter.setBlendMode(BlendMode.ADD);
    emitter.setMaxEmissions(Integer.MAX_VALUE);
    emitter.setNumParticles(750);
    emitter.setEmissionRate(0.02);
    emitter.setSize(1, 24);
    emitter.setSpawnPointFunction(i -> FXGLMath.random(spawnPoints).get());
    emitter.setScaleFunction(i -> FXGLMath.randomPoint2D().multiply(0.01));
    emitter.setExpireFunction(i -> Duration.seconds(random(0.55, 2.5)));
    emitter.setAccelerationFunction(() -> Point2D.ZERO);
    // emitter.setVelocityFunction(i -> Point2D.ZERO);
    emitter.setVelocityFunction(i -> FXGLMath.randomPoint2D().multiply(random(1, 45)));
    emitter.setSourceImage(texture("particles/circle_05.png", 32, 32).multiplyColor(Color.ORANGERED));
    emitter.setAllowParticleRotation(true);
    emitter.setControl(p -> {
        var x = p.position.x;
        var y = p.position.y;
        var point = dest.getOrDefault(p, FXGLMath.random(points).get());
        dest.put(p, point);
        var v = point.subtract(x, y).normalize().multiply(0.016 * 1500);
        p.velocity.x = p.velocity.x * 0.8f + (float) v.getX() * 0.2f;
        p.velocity.y = p.velocity.y * 0.8f + (float) v.getY() * 0.2f;
    });
    var e = entityBuilder().with(new ParticleComponent(emitter)).buildAndAttach();
    // e.xProperty().bind(getInput().mouseXWorldProperty().subtract(10));
    // e.yProperty().bind(getInput().mouseYWorldProperty().subtract(10));
    var lineX = new Line(0, getAppHeight() / 2.0, getAppWidth(), getAppHeight() / 2.0);
    lineX.setStrokeType(StrokeType.OUTSIDE);
    lineX.setStrokeWidth(3);
    lineX.setStroke(Color.WHITE);
    var lineY = new Line(getAppWidth() / 2.0, 0, getAppWidth() / 2.0, getAppHeight());
    lineY.setStrokeType(StrokeType.OUTSIDE);
    lineY.setStrokeWidth(3);
    lineY.setStroke(Color.WHITE);
// addUINode(lineX);
// addUINode(lineY);
}
Also used : IntStream(java.util.stream.IntStream) KeyCode(javafx.scene.input.KeyCode) Color(javafx.scene.paint.Color) Rectangle2D(javafx.geometry.Rectangle2D) java.util(java.util) ParticleEmitter(com.almasb.fxgl.particle.ParticleEmitter) FXGLMath(com.almasb.fxgl.core.math.FXGLMath) ParticleEmitters(com.almasb.fxgl.particle.ParticleEmitters) Particle(com.almasb.fxgl.particle.Particle) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ParticleComponent(com.almasb.fxgl.particle.ParticleComponent) BlendMode(javafx.scene.effect.BlendMode) Line(javafx.scene.shape.Line) Duration(javafx.util.Duration) StrokeType(javafx.scene.shape.StrokeType) GameSettings(com.almasb.fxgl.app.GameSettings) Point2D(javafx.geometry.Point2D) GameApplication(com.almasb.fxgl.app.GameApplication) FXGL(com.almasb.fxgl.dsl.FXGL) Line(javafx.scene.shape.Line) ParticleComponent(com.almasb.fxgl.particle.ParticleComponent) Point2D(javafx.geometry.Point2D) Rectangle2D(javafx.geometry.Rectangle2D)

Aggregations

ParticleComponent (com.almasb.fxgl.particle.ParticleComponent)10 Point2D (javafx.geometry.Point2D)6 ParticleEmitter (com.almasb.fxgl.particle.ParticleEmitter)4 ExpireCleanComponent (com.almasb.fxgl.dsl.components.ExpireCleanComponent)2 GameApplication (com.almasb.fxgl.app.GameApplication)1 GameSettings (com.almasb.fxgl.app.GameSettings)1 FXGLMath (com.almasb.fxgl.core.math.FXGLMath)1 FXGL (com.almasb.fxgl.dsl.FXGL)1 ProjectileComponent (com.almasb.fxgl.dsl.components.ProjectileComponent)1 Particle (com.almasb.fxgl.particle.Particle)1 ParticleEmitters (com.almasb.fxgl.particle.ParticleEmitters)1 java.util (java.util)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 Rectangle2D (javafx.geometry.Rectangle2D)1 BlendMode (javafx.scene.effect.BlendMode)1 KeyCode (javafx.scene.input.KeyCode)1 StackPane (javafx.scene.layout.StackPane)1 Color (javafx.scene.paint.Color)1