Search in sources :

Example 61 with Entity

use of com.almasb.fxgl.entity.Entity in project FXGL by AlmasB.

the class UnoSample method onUpdate.

@Override
protected void onUpdate(double tpf) {
    if (playerHandChanged) {
        double sizePerCard = 1.0 * getWidth() / player.cardsProperty().size();
        int i = 0;
        for (Entity card : player.cardsProperty()) {
            card.setX(i++ * sizePerCard);
            card.setY(450);
        }
        playerHandChanged = false;
    }
}
Also used : Entity(com.almasb.fxgl.entity.Entity)

Example 62 with Entity

use of com.almasb.fxgl.entity.Entity in project FXGL by AlmasB.

the class UnoSample method spawn.

private Entity spawn(Card card, int x, int y) {
    Entity cardEntity = (Entity) getGameWorld().spawn("Card", new SpawnData(x, y).put("card", card));
    cardEntity.getView().setOnMouseClicked(e -> {
        if (card.canUseOn(currentCard.getComponent(CardComponent.class).getValue())) {
            Animation<?> animation = Entities.animationBuilder().duration(Duration.seconds(0.35)).translate(cardEntity).from(cardEntity.getPosition()).to(new Point2D(350, 225)).build();
            animation.setOnFinished(() -> {
                player.removeCard(cardEntity);
                playerHandChanged = true;
                currentCard.addControl(new ExpireCleanControl(Duration.seconds(0.5)));
                currentCard = cardEntity;
                currentCard.setRenderLayer(RenderLayer.BACKGROUND);
                // apply special effect
                switch(card.getRank()) {
                    case SP_PLUS2:
                        break;
                    case SP_PLUS4:
                        break;
                    case SP_SKIP:
                        break;
                    default:
                }
                aiMove();
            });
            animation.startInPlayState();
        }
    });
    return cardEntity;
}
Also used : Entity(com.almasb.fxgl.entity.Entity) Point2D(javafx.geometry.Point2D) SpawnData(com.almasb.fxgl.entity.SpawnData) ExpireCleanControl(com.almasb.fxgl.entity.control.ExpireCleanControl)

Example 63 with Entity

use of com.almasb.fxgl.entity.Entity in project FXGL by AlmasB.

the class UnoSample method aiMove.

private void aiMove() {
    Entity chosenCard = null;
    for (Entity card : enemy.cardsProperty()) {
        // can we avoid calls like this?
        if (card.getComponent(CardComponent.class).getValue().canUseOn(currentCard.getComponent(CardComponent.class).getValue())) {
            currentCard.addControl(new ExpireCleanControl(Duration.seconds(0.5)));
            enemy.removeCard(card);
            card.setPosition(new Point2D(350, 225));
            currentCard = card;
            currentCard.setRenderLayer(RenderLayer.BACKGROUND);
            // apply special effect
            chosenCard = card;
            break;
        }
    }
    if (chosenCard == null) {
        while (deck.hasCards()) {
            Entity draw = spawn(deck.drawCard(), 0, -150);
            if (draw.getComponent(CardComponent.class).getValue().canUseOn(currentCard.getComponent(CardComponent.class).getValue())) {
                currentCard.addControl(new ExpireCleanControl(Duration.seconds(0.5)));
                draw.setPosition(new Point2D(350, 225));
                currentCard = draw;
                currentCard.setRenderLayer(RenderLayer.BACKGROUND);
                break;
            } else {
                enemy.addCard(draw);
            }
        }
    }
}
Also used : Entity(com.almasb.fxgl.entity.Entity) Point2D(javafx.geometry.Point2D) ExpireCleanControl(com.almasb.fxgl.entity.control.ExpireCleanControl)

Example 64 with Entity

use of com.almasb.fxgl.entity.Entity in project FXGL by AlmasB.

the class InterpolatorSample2 method initGame.

@Override
protected void initGame() {
    int i = 0;
    for (var interpolator : Interpolators.values()) {
        Text t = getUIFactoryService().newText(interpolator.toString() + ":");
        t.setFill(Color.BLACK);
        Pane p = new Pane(t);
        p.setTranslateY(i * 50 + 25);
        Line line = new Line(0, i * 50, getAppWidth(), i * 50);
        line.setStroke(Color.RED);
        getGameScene().addUINodes(p, line);
        Entity bird = entityBuilder().at(70, i * 50).view(texture("bird.png").toAnimatedTexture(2, Duration.seconds(0.5)).loop()).buildAndAttach();
        animationBuilder().interpolator(interpolator.EASE_OUT()).duration(Duration.seconds(2)).repeatInfinitely().translate(bird).from(new Point2D(120, i * 50)).to(new Point2D(getAppWidth() - 70, i * 50)).buildAndPlay();
        i++;
    }
}
Also used : Line(javafx.scene.shape.Line) Entity(com.almasb.fxgl.entity.Entity) Point2D(javafx.geometry.Point2D) Text(javafx.scene.text.Text) Pane(javafx.scene.layout.Pane)

Example 65 with Entity

use of com.almasb.fxgl.entity.Entity in project FXGL by AlmasB.

the class GraphVisSample method makeNode.

private Entity makeNode(int id) {
    var e = entityBuilder().at(FXGLMath.randomPoint(new Rectangle2D(0, 0, getAppWidth(), getAppHeight()))).view(new Circle(NODE_RADIUS, NODE_RADIUS, NODE_RADIUS)).with("id", id).with(new DraggableComponent()).onClick(entity -> {
    // here you can find out more info about the node in question
    }).buildAndAttach();
    nodes.put(id, e);
    return e;
}
Also used : Color(javafx.scene.paint.Color) Rectangle2D(javafx.geometry.Rectangle2D) FXGLMath(com.almasb.fxgl.core.math.FXGLMath) HashMap(java.util.HashMap) Line(javafx.scene.shape.Line) List(java.util.List) Map(java.util.Map) GameSettings(com.almasb.fxgl.app.GameSettings) DraggableComponent(com.almasb.fxgl.dsl.components.DraggableComponent) Circle(javafx.scene.shape.Circle) GameApplication(com.almasb.fxgl.app.GameApplication) Entity(com.almasb.fxgl.entity.Entity) FXGL(com.almasb.fxgl.dsl.FXGL) Circle(javafx.scene.shape.Circle) Rectangle2D(javafx.geometry.Rectangle2D) DraggableComponent(com.almasb.fxgl.dsl.components.DraggableComponent)

Aggregations

Entity (com.almasb.fxgl.entity.Entity)80 Rectangle (javafx.scene.shape.Rectangle)27 Point2D (javafx.geometry.Point2D)24 HitBox (com.almasb.fxgl.physics.HitBox)20 GameApplication (com.almasb.fxgl.app.GameApplication)8 EntityView (com.almasb.fxgl.entity.view.EntityView)8 UserAction (com.almasb.fxgl.input.UserAction)8 CollisionHandler (com.almasb.fxgl.physics.CollisionHandler)8 Color (javafx.scene.paint.Color)7 Input (com.almasb.fxgl.input.Input)5 PhysicsComponent (com.almasb.fxgl.physics.PhysicsComponent)5 Circle (javafx.scene.shape.Circle)5 Line (javafx.scene.shape.Line)5 GameSettings (com.almasb.fxgl.app.GameSettings)4 FXGLMath (com.almasb.fxgl.core.math.FXGLMath)4 FXGL (com.almasb.fxgl.dsl.FXGL)4 CollidableComponent (com.almasb.fxgl.entity.component.CollidableComponent)4 PhysicsParticleComponent (com.almasb.fxgl.physics.PhysicsParticleComponent)4 ParticleGroupDef (com.almasb.fxgl.physics.box2d.particle.ParticleGroupDef)4 Text (javafx.scene.text.Text)4