Search in sources :

Example 91 with Rectangle2D

use of javafx.geometry.Rectangle2D in project FXGL by AlmasB.

the class UnoSample method initGame.

@Override
protected void initGame() {
    centerCardPosition = new Point2D(getAppWidth() / 2 - 100 / 2, getAppHeight() / 2 - 150 / 2);
    getGameWorld().addEntityFactory(new UnoFactory());
    spawn("Background");
    player = new Hand("Player");
    enemy = new Hand("AI");
    var currentCard = spawnCard(deck.drawCard());
    currentCard.setPosition(centerCardPosition);
    set("currentCard", currentCard);
    nextHand = player;
    cardsLayout.put(player, new Rectangle2D(0, getAppHeight() - 150, getAppWidth(), 150));
    cardsLayout.put(enemy, new Rectangle2D(0, 0 - 150, getAppWidth(), 150));
    cardsLayout.forEach((hand, bounds) -> {
        hand.setChangeCallback(() -> {
            hand.cardsProperty().sort(Comparator.comparingInt(e -> {
                Card card = e.getObject("card");
                return card.getSuit().ordinal() * 1000 + card.getRank().ordinal();
            }));
            evaluateAndLayout(hand);
        });
        if (hand == player) {
            getWorldProperties().<Entity>addListener("currentCard", (prev, now) -> {
                evaluateAndLayout(hand);
            });
        }
    });
    for (int i = 0; i < NUM_BASE_CARDS; i++) {
        player.addCard(spawnCard(deck.drawCard()));
        enemy.addCard(spawnCard(deck.drawCard()));
    }
}
Also used : Button(javafx.scene.control.Button) Color(javafx.scene.paint.Color) Rectangle2D(javafx.geometry.Rectangle2D) ExpireCleanComponent(com.almasb.fxgl.dsl.components.ExpireCleanComponent) CacheHint(javafx.scene.CacheHint) HashMap(java.util.HashMap) Bindings(javafx.beans.binding.Bindings) Text(javafx.scene.text.Text) Duration(javafx.util.Duration) Interpolators(com.almasb.fxgl.animation.Interpolators) Map(java.util.Map) GameSettings(com.almasb.fxgl.app.GameSettings) Point2D(javafx.geometry.Point2D) SpawnData(com.almasb.fxgl.entity.SpawnData) Comparator(java.util.Comparator) GameApplication(com.almasb.fxgl.app.GameApplication) Entity(com.almasb.fxgl.entity.Entity) FXGL(com.almasb.fxgl.dsl.FXGL) Entity(com.almasb.fxgl.entity.Entity) Point2D(javafx.geometry.Point2D) Rectangle2D(javafx.geometry.Rectangle2D) CacheHint(javafx.scene.CacheHint)

Example 92 with Rectangle2D

use of javafx.geometry.Rectangle2D in project FXGL by AlmasB.

the class DevMenuSample method initInput.

@Override
protected void initInput() {
    onKeyDown(KeyCode.F, () -> {
        System.out.println(getop("pos").hashCode());
        Vec2 v = geto("pos");
        v.set(FXGLMath.randomPoint(new Rectangle2D(0, 0, getAppWidth(), getAppHeight())));
        set("pos", v);
        System.out.println(geto("pos").toString());
    });
}
Also used : Vec2(com.almasb.fxgl.core.math.Vec2) Rectangle2D(javafx.geometry.Rectangle2D)

Example 93 with Rectangle2D

use of javafx.geometry.Rectangle2D in project FXGL by AlmasB.

the class BenchmarkSample method initGame.

@Override
protected void initGame() {
    frames = 0;
    components = new Component[NUM_OBJECTS];
    textures = new Texture[NUM_OBJECTS];
    getGameWorld().addEntityFactory(new BenchmarkFactory());
    for (int i = 0; i < NUM_OBJECTS; i++) {
        if (IS_JAVAFX) {
            var t = texture("ball.png", 100, 100);
            textures[i] = t;
            addUINode(t, random(100, 500), random(100, 500));
        } else {
            var e = spawn("ball");
            e.setUpdateEnabled(false);
            // var c = new ProjectileComponent(new Point2D(random(0.0, 1.0), random(0.0, 1.0)), FXGLMath.map(i, 0, 2000, 10, 60));
            var c = new RandomMoveComponent(new Rectangle2D(0, 0, getAppWidth(), getAppHeight()), random(10, 400));
            components[i] = c;
            ComponentHelper.setEntity(c, e);
        }
    }
}
Also used : RandomMoveComponent(com.almasb.fxgl.dsl.components.RandomMoveComponent) Rectangle2D(javafx.geometry.Rectangle2D)

Example 94 with Rectangle2D

use of javafx.geometry.Rectangle2D in project FXGL by AlmasB.

the class CircleNNFactory method newCircle.

@Spawns("circle")
public Entity newCircle(SpawnData data) {
    var circleComponent = new CircleComponent();
    var color = FXGLMath.randomColor().brighter();
    var view = new Circle(32, 32, 30, null);
    view.setStrokeWidth(2);
    view.setStroke(color);
    var hp = new HealthIntComponent(49);
    var viewHP = new Circle(32, 32, 30);
    viewHP.radiusProperty().bind(hp.valueProperty().divide(49.0).multiply(30));
    var viewRank = getUIFactoryService().newText("", Color.MIDNIGHTBLUE, 12.0);
    viewRank.setTranslateX(32);
    viewRank.setTranslateY(32);
    var e = entityBuilder(data).type(CIRCLE).bbox(new HitBox(BoundingShape.box(64, 64))).view(view).view(viewHP).collidable().with("isShielded", false).with("rank", 1).with(new TimeComponent()).with(new EffectComponent()).with(hp).with(new RandomMoveComponent(new Rectangle2D(0, 0, getAppWidth(), getAppHeight()), 300)).with(circleComponent).with(new BlockCollisionComponent()).build();
    viewHP.fillProperty().bind(Bindings.when(e.getProperties().booleanProperty("isShielded")).then(Color.WHITE).otherwise(Color.color(0.5, 0.78, 0.2, 0.5).brighter()));
    viewRank.textProperty().bind(e.getProperties().intProperty("rank").asString());
    return e;
}
Also used : Circle(javafx.scene.shape.Circle) HitBox(com.almasb.fxgl.physics.HitBox) TimeComponent(com.almasb.fxgl.entity.components.TimeComponent) Rectangle2D(javafx.geometry.Rectangle2D) Spawns(com.almasb.fxgl.entity.Spawns)

Example 95 with Rectangle2D

use of javafx.geometry.Rectangle2D 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

Rectangle2D (javafx.geometry.Rectangle2D)96 Image (javafx.scene.image.Image)14 Point2D (javafx.geometry.Point2D)13 Scene (javafx.scene.Scene)11 Color (javafx.scene.paint.Color)11 Screen (javafx.stage.Screen)11 List (java.util.List)8 IOException (java.io.IOException)7 Rectangle (javafx.scene.shape.Rectangle)7 Stage (javafx.stage.Stage)7 GameApplication (com.almasb.fxgl.app.GameApplication)6 GameSettings (com.almasb.fxgl.app.GameSettings)6 FXGL (com.almasb.fxgl.dsl.FXGL)6 KeyCode (javafx.scene.input.KeyCode)6 Duration (javafx.util.Duration)6 Interpolators (com.almasb.fxgl.animation.Interpolators)4 FXGLMath (com.almasb.fxgl.core.math.FXGLMath)4 Entity (com.almasb.fxgl.entity.Entity)4 BufferedImage (java.awt.image.BufferedImage)4 Collectors (java.util.stream.Collectors)4