Search in sources :

Example 1 with ParticleComponent

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

the class ParticleShowcaseSample2 method spawnParticles.

private void spawnParticles(Entity enemy) {
    var emitter1 = ParticleEmitters.newExplosionEmitter(30);
    emitter1.setMaxEmissions(1);
    emitter1.setNumParticles(650);
    emitter1.setEmissionRate(0.86);
    emitter1.setSize(1, 14);
    emitter1.setScaleFunction(i -> FXGLMath.randomPoint2D().multiply(0.01));
    emitter1.setExpireFunction(i -> Duration.seconds(random(0.25, 3)));
    emitter1.setInterpolator(Interpolators.EXPONENTIAL.EASE_OUT());
    emitter1.setAccelerationFunction(() -> new Point2D(0, random(0, 1)));
    // emitter1.setVelocityFunction(i -> Point2D.ZERO);
    // emitter1.setVelocityFunction(i -> FXGLMath.randomPoint2D().multiply(random(5, 25)));
    var name = names[index++];
    System.out.println("Using " + name);
    if (index == names.length) {
        index = 0;
    }
    // var c = FXGLMath.randomColor();
    var c = Color.ORANGERED;
    emitter1.setSourceImage(texture("particles/" + "circle_05.png", 32, 32).multiplyColor(c));
    emitter1.setAllowParticleRotation(true);
    emitter1.setControl(p -> {
        var x = p.position.x;
        var y = p.position.y;
        var noiseValue = FXGLMath.noise2D(x * 0.002 * t, y * 0.002 * t);
        var angle = FXGLMath.toDegrees((noiseValue + 1) * Math.PI * 1.5);
        angle %= 360.0;
        var v = Vec2.fromAngle(angle).normalizeLocal().mulLocal(FXGLMath.random(1.0, 5));
        var vx = p.velocity.x * 0.8f + v.x * 0.2f;
        var vy = p.velocity.y * 0.8f + v.y * 0.2f;
        p.velocity.x = vx;
        p.velocity.y = vy;
    });
    entityBuilder().at(enemy.getPosition().add(16, 16)).with(new ParticleComponent(emitter1)).with(new ExpireCleanComponent(Duration.seconds(6))).buildAndAttach();
}
Also used : ParticleComponent(com.almasb.fxgl.particle.ParticleComponent) ExpireCleanComponent(com.almasb.fxgl.dsl.components.ExpireCleanComponent) Point2D(javafx.geometry.Point2D)

Example 2 with ParticleComponent

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

the class ParticleShowcaseSample2 method initInput.

@Override
protected void initInput() {
    onBtnDown(MouseButton.SECONDARY, () -> {
        var c = FXGLMath.randomColor();
        emitter.setStartColor(c);
        emitter.setEndColor(c.invert());
        var name = names[index++];
        System.out.println("Using " + name);
        emitter.setSourceImage(texture("particles/" + name, 32, 32).multiplyColor(c));
        if (index == names.length) {
            index = 0;
        }
    });
    onKeyDown(KeyCode.R, () -> {
        emitter.setAllowParticleRotation(!emitter.isAllowParticleRotation());
    });
    onBtnDown(MouseButton.PRIMARY, () -> {
        emitter = ParticleEmitters.newExplosionEmitter(60);
        emitter.setMaxEmissions(3);
        emitter.setNumParticles(350);
        emitter.setEmissionRate(0.86);
        emitter.setSize(1, 24);
        emitter.setScaleFunction(i -> FXGLMath.randomPoint2D().multiply(0.02));
        emitter.setExpireFunction(i -> Duration.seconds(random(1.25, 3.5)));
        var i = Interpolators.values()[interpolatorIndex++];
        if (interpolatorIndex == Interpolators.values().length)
            interpolatorIndex = 0;
        debug.setText("Easing (out): " + i.toString());
        emitter.setInterpolator(i.EASE_OUT());
        emitter.setAccelerationFunction(() -> new Point2D(0, random(1, 3)));
        // emitter.setVelocityFunction(i -> Point2D.ZERO);
        // emitter.setVelocityFunction(i -> FXGLMath.randomPoint2D().multiply(random(1, 15)));
        var name = names[index++];
        System.out.println("Using " + name);
        if (index == names.length) {
            index = 0;
        }
        // FXGLMath.randomColor();
        var c = Color.YELLOW;
        // name before
        emitter.setSourceImage(texture("particles/" + "flare_01.png", 32, 32).multiplyColor(c));
        emitter.setAllowParticleRotation(true);
        // emitter.setControl(p -> {
        // var x = p.position.x;
        // var y = p.position.y;
        // 
        // var noiseValue = FXGLMath.noise2D(x * 0.002 * t, y * 0.002 * t);
        // var angle = FXGLMath.toDegrees((noiseValue + 1) * Math.PI * 1.5);
        // 
        // angle %= 360.0;
        // 
        // var v = Vec2.fromAngle(angle).normalizeLocal().mulLocal(FXGLMath.random(1.0, 15));
        // 
        // var vx = p.velocity.x * 0.8f + v.x * 0.2f;
        // var vy = p.velocity.y * 0.8f + v.y * 0.2f;
        // 
        // p.velocity.x = vx;
        // p.velocity.y = vy;
        // });
        e = entityBuilder().at(getAppWidth() / 2, getAppHeight() / 2).with(new ParticleComponent(emitter)).with(new ExpireCleanComponent(Duration.seconds(6))).buildAndAttach();
    // e.xProperty().bind(getInput().mouseXWorldProperty().subtract(10));
    // e.yProperty().bind(getInput().mouseYWorldProperty().subtract(10));
    // var tank = entityBuilder()
    // .type(Type.PLAYER)
    // .at(e.getPosition().subtract(16, 16))
    // .viewWithBBox(texture("tank_player.png"))
    // .with(new ExpireCleanComponent(Duration.seconds(3)))
    // .scale(0, 0)
    // .buildAndAttach();
    // 
    // tank.getViewComponent().setOpacity(0);
    // 
    // animationBuilder()
    // .delay(Duration.seconds(0.5))
    // .fadeIn(tank)
    // .buildAndPlay();
    // 
    // animationBuilder()
    // .delay(Duration.seconds(0.5))
    // .scale(tank)
    // .from(new Point2D(0, 0))
    // .to(new Point2D(1, 1))
    // .buildAndPlay();
    });
    onKeyDown(KeyCode.F, () -> {
        entityBuilder().type(Type.BULLET).at(getInput().getMouseXWorld(), getInput().getMouseYWorld()).viewWithBBox("tank_bullet.png").with(new ProjectileComponent(new Point2D(1, 0), 870)).collidable().buildAndAttach();
    });
}
Also used : ParticleComponent(com.almasb.fxgl.particle.ParticleComponent) ExpireCleanComponent(com.almasb.fxgl.dsl.components.ExpireCleanComponent) Point2D(javafx.geometry.Point2D) ProjectileComponent(com.almasb.fxgl.dsl.components.ProjectileComponent)

Example 3 with ParticleComponent

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

the class ParticlesSample method initInput.

@Override
protected void initInput() {
    onBtnDown(MouseButton.PRIMARY, () -> {
        // 1. create and configure emitter + component
        ParticleEmitter emitter = ParticleEmitters.newFireEmitter();
        emitter.setBlendMode(BlendMode.SRC_OVER);
        emitter.setMaxEmissions(100);
        ParticleComponent component = new ParticleComponent(emitter);
        // 2. add component to entity
        entityBuilder().at(getInput().getMousePositionWorld()).with(component).buildAndAttach();
    });
    onBtnDown(MouseButton.SECONDARY, () -> {
        // 1. create and configure emitter + component
        ParticleEmitter emitter = ParticleEmitters.newExplosionEmitter(400);
        emitter.setSourceImage(image("brick.png"));
        ParticleComponent component = new ParticleComponent(emitter);
        // 2. add component to entity
        entityBuilder().at(getInput().getMousePositionWorld()).with(component).buildAndAttach();
    });
}
Also used : ParticleEmitter(com.almasb.fxgl.particle.ParticleEmitter) ParticleComponent(com.almasb.fxgl.particle.ParticleComponent)

Example 4 with ParticleComponent

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

the class ParticleShowcaseSample method initGame.

@Override
protected void initGame() {
    getGameScene().setBackgroundColor(Color.BLACK);
    // getGameScene().setCursorInvisible();
    emitter = ParticleEmitters.newExplosionEmitter(300);
    emitter.setMaxEmissions(Integer.MAX_VALUE);
    emitter.setNumParticles(50);
    emitter.setEmissionRate(0.86);
    emitter.setSize(1, 24);
    emitter.setScaleFunction(i -> FXGLMath.randomPoint2D().multiply(0.01));
    emitter.setExpireFunction(i -> Duration.seconds(random(0.25, 2.5)));
    emitter.setAccelerationFunction(() -> Point2D.ZERO);
    // emitter.setVelocityFunction(i -> Point2D.ZERO);
    emitter.setVelocityFunction(i -> FXGLMath.randomPoint2D().multiply(random(1, 45)));
    // emitter.setSourceImage(texture("particles/star_09.png", 32, 32));
    emitter.setAllowParticleRotation(true);
    emitter.setControl(p -> {
        // if (p.getView().getParent() != null && p.getView().getParent().getEffect() == null) {
        // p.getView().getParent().setEffect(new BoxBlur(5, 5, 2));
        // }
        var x = p.position.x;
        var y = p.position.y;
        var noiseValue = FXGLMath.noise2D(x * 0.002 * t, y * 0.002 * t);
        var angle = FXGLMath.toDegrees((noiseValue + 1) * Math.PI * 1.5);
        angle %= 360.0;
        var v = Vec2.fromAngle(angle).normalizeLocal().mulLocal(FXGLMath.random(1.0, 25));
        var vx = p.velocity.x * 0.8f + v.x * 0.2f;
        var vy = p.velocity.y * 0.8f + v.y * 0.2f;
        p.velocity.x = vx;
        p.velocity.y = vy;
    });
    e = entityBuilder().with(new ParticleComponent(emitter)).buildAndAttach();
    e.xProperty().bind(getInput().mouseXWorldProperty().subtract(10));
    e.yProperty().bind(getInput().mouseYWorldProperty().subtract(10));
}
Also used : ParticleComponent(com.almasb.fxgl.particle.ParticleComponent)

Example 5 with ParticleComponent

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

the class ParticleShowcaseSample3 method initGame.

@Override
protected void initGame() {
    getGameScene().setBackgroundColor(Color.BLACK);
    emitter = ParticleEmitters.newExplosionEmitter(300);
    emitter.setMaxEmissions(Integer.MAX_VALUE);
    emitter.setNumParticles(90);
    emitter.setEmissionRate(0.25);
    emitter.setSize(1, 16);
    emitter.setScaleFunction(i -> FXGLMath.randomPoint2D().multiply(0.01));
    emitter.setExpireFunction(i -> Duration.seconds(random(0.25, 0.5)));
    emitter.setAccelerationFunction(() -> Point2D.ZERO);
    emitter.setVelocityFunction(i -> FXGLMath.randomPoint2D().multiply(random(0.25, 5.0)));
    emitter.setSpawnPointFunction(i -> Vec2.fromAngle(i * 4).toPoint2D().multiply(90).add(70, 70));
    // emitter.setVelocityFunction(i -> FXGLMath.randomPoint2D().multiply(random(1, 45)));
    // emitter.setSourceImage(texture("particles/star_09.png", 32, 32));
    emitter.setAllowParticleRotation(true);
    // emitter.setControl(p -> {
    // var x = p.position.x;
    // var y = p.position.y;
    // 
    // var noiseValue = FXGLMath.noise2D(x * 0.002 * t, y * 0.002 * t);
    // var angle = FXGLMath.toDegrees((noiseValue + 1) * Math.PI * 1.5);
    // 
    // angle %= 360.0;
    // 
    // var v = Vec2.fromAngle(angle).normalizeLocal().mulLocal(FXGLMath.random(1.0, 25));
    // 
    // var vx = p.velocity.x * 0.8f + v.x * 0.2f;
    // var vy = p.velocity.y * 0.8f + v.y * 0.2f;
    // 
    // p.velocity.x = vx;
    // p.velocity.y = vy;
    // });
    var circle = new Circle(80, Color.LIME);
    circle.setStrokeWidth(2.5);
    var stackPane = new StackPane(circle, getUIFactoryService().newText("Play", Color.WHITESMOKE, 58));
    e = entityBuilder().at(150, 150).view(stackPane).with(new ParticleComponent(emitter)).buildAndAttach();
    // e.xProperty().bind(getInput().mouseXWorldProperty().subtract(10));
    // e.yProperty().bind(getInput().mouseYWorldProperty().subtract(10));
    e.getViewComponent().getParent().hoverProperty().addListener((observableValue, old, isHover) -> {
        var particleComponent = e.getComponent(ParticleComponent.class);
        if (isHover) {
            particleComponent.resumeEmitter();
        } else {
            particleComponent.pauseEmitter();
        }
    });
    circle.strokeProperty().bind(Bindings.when(e.getViewComponent().getParent().hoverProperty()).then(Color.LIGHTGREEN).otherwise(Color.GRAY));
    circle.fillProperty().bind(Bindings.when(e.getViewComponent().getParent().hoverProperty()).then(Color.GREEN).otherwise(Color.LIME));
}
Also used : Circle(javafx.scene.shape.Circle) ParticleComponent(com.almasb.fxgl.particle.ParticleComponent) StackPane(javafx.scene.layout.StackPane)

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