Search in sources :

Example 66 with Entity

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

the class CirclesSample method initInput.

@Override
protected void initInput() {
    onKeyDown(KeyCode.F, () -> {
        getGameWorld().getEntitiesCopy().forEach(Entity::removeFromWorld);
        for (int i = 0; i < interpolators.size(); i++) {
            var interpolator = interpolators.get(i);
            var box = new Box(1, 1, 1);
            box.setMaterial(new PhongMaterial(Color.BLUE));
            var c = entityBuilder().view(box).buildAndAttach();
            var center = new Point2D(0, 0);
            var point = new Point2D(2 + i * 1, 0.0).add(center);
            animationBuilder().duration(Duration.seconds(8)).interpolator(interpolator).animate(new AnimatedValue<>(0.0, 360.0)).onProgress(deg -> {
                var p = FXGLMath.rotate(point, center, deg);
                c.setX(p.getX());
                c.setY(p.getY());
            }).buildAndPlay();
        }
    });
}
Also used : KeyCode(javafx.scene.input.KeyCode) Color(javafx.scene.paint.Color) Point3D(javafx.geometry.Point3D) FXGLMath(com.almasb.fxgl.core.math.FXGLMath) ArrayList(java.util.ArrayList) Box(javafx.scene.shape.Box) Duration(javafx.util.Duration) List(java.util.List) Interpolator(javafx.animation.Interpolator) PhongMaterial(javafx.scene.paint.PhongMaterial) Interpolators(com.almasb.fxgl.animation.Interpolators) AnimatedValue(com.almasb.fxgl.animation.AnimatedValue) GameSettings(com.almasb.fxgl.app.GameSettings) Point2D(javafx.geometry.Point2D) 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) Box(javafx.scene.shape.Box) PhongMaterial(javafx.scene.paint.PhongMaterial)

Example 67 with Entity

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

the class BoundingBoxComponentTest method setUp.

@BeforeEach
public void setUp() throws Exception {
    Entity entity = new Entity();
    position = entity.getTransformComponent();
    bbox = entity.getBoundingBoxComponent();
}
Also used : Entity(com.almasb.fxgl.entity.Entity) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 68 with Entity

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

the class BoundingBoxComponentTest method testIsCollidingWithAngles.

@Test
public void testIsCollidingWithAngles() throws Exception {
    bbox.addHitBox(new HitBox("ARM", BoundingShape.box(40, 60)));
    Entity entity2 = new Entity();
    entity2.getBoundingBoxComponent().addHitBox(new HitBox("test", BoundingShape.box(50, 50)));
    entity2.setPosition(50, 0);
    entity2.getTransformComponent().setRotationOrigin(new Point2D(0, 0));
    entity2.setRotation(91);
    Entity entity3 = new Entity();
    entity3.getBoundingBoxComponent().addHitBox(new HitBox("test", BoundingShape.box(50, 50)));
    entity3.setPosition(50, 0);
    // both identical but e2 is rotated and e3 is not
    assertTrue(bbox.isCollidingWith(entity2.getBoundingBoxComponent()));
    assertFalse(bbox.isCollidingWith(entity3.getBoundingBoxComponent()));
}
Also used : Entity(com.almasb.fxgl.entity.Entity) HitBox(com.almasb.fxgl.physics.HitBox) Point2D(javafx.geometry.Point2D) Test(org.junit.jupiter.api.Test)

Example 69 with Entity

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

the class PhysicsWorld method raycast.

/**
 * Performs a ray cast from start point to end point.
 *
 * @param start start point
 * @param end end point
 * @return ray cast result
 */
public RaycastResult raycast(Point2D start, Point2D end) {
    raycastCallback.reset();
    jboxWorld.raycast(raycastCallback, toPoint(start), toPoint(end));
    Entity entity = null;
    Point2D point = null;
    if (raycastCallback.getFixture() != null)
        entity = raycastCallback.getFixture().getBody().getEntity();
    if (raycastCallback.getPoint() != null)
        point = toPoint(raycastCallback.getPoint());
    if (entity == null && point == null)
        return RaycastResult.NONE;
    return new RaycastResult(entity, point);
}
Also used : Entity(com.almasb.fxgl.entity.Entity) Point2D(javafx.geometry.Point2D)

Example 70 with Entity

use of com.almasb.fxgl.entity.Entity 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)

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