Search in sources :

Example 11 with Entity

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

the class PhysicsWorld method beginContact.

@Override
public void beginContact(Contact contact) {
    Entity e1 = (Entity) contact.getFixtureA().getBody().getUserData();
    Entity e2 = (Entity) contact.getFixtureB().getBody().getUserData();
    // check sensors
    if (contact.getFixtureA().isSensor()) {
        e1.getComponent(PhysicsComponent.class).groundedList.add(e2);
        return;
    } else if (contact.getFixtureB().isSensor()) {
        e2.getComponent(PhysicsComponent.class).groundedList.add(e1);
        return;
    }
    if (!areCollidable(e1, e2))
        return;
    CollisionHandler handler = getHandler(e1, e2);
    if (handler != null) {
        CollisionPair pair = getPair(e1, e2);
        // no collision registered, so add the pair
        if (pair == null) {
            pair = Pools.obtain(CollisionPair.class);
            pair.init(e1, e2, handler);
            // add pair to list of collisions so we still use it
            collisions.add(pair);
            HitBox boxA = (HitBox) contact.getFixtureA().getUserData();
            HitBox boxB = (HitBox) contact.getFixtureB().getUserData();
            handler.onHitBoxTrigger(pair.getA(), pair.getB(), e1 == pair.getA() ? boxA : boxB, e2 == pair.getB() ? boxB : boxA);
            pair.collisionBegin();
        }
    }
}
Also used : Entity(com.almasb.fxgl.entity.Entity)

Example 12 with Entity

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

the class PhysicsWorld method endContact.

@Override
public void endContact(Contact contact) {
    Entity e1 = (Entity) contact.getFixtureA().getBody().getUserData();
    Entity e2 = (Entity) contact.getFixtureB().getBody().getUserData();
    // check sensors
    if (contact.getFixtureA().isSensor()) {
        e1.getComponent(PhysicsComponent.class).groundedList.remove(e2);
        return;
    } else if (contact.getFixtureB().isSensor()) {
        e2.getComponent(PhysicsComponent.class).groundedList.remove(e1);
        return;
    }
    if (!areCollidable(e1, e2))
        return;
    CollisionHandler handler = getHandler(e1, e2);
    if (handler != null) {
        int pairIndex = getPairIndex(e1, e2);
        // collision registered, so remove it and put pair back to pool
        if (pairIndex != -1) {
            CollisionPair pair = collisions.get(pairIndex);
            collisions.removeIndex(pairIndex);
            pair.collisionEnd();
            Pools.free(pair);
        }
    }
}
Also used : Entity(com.almasb.fxgl.entity.Entity)

Example 13 with Entity

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

the class PhysicsWorld method checkCollisions.

/**
 * Perform collision detection for all entities that have
 * setCollidable(true) and if at least one entity is not PhysicsEntity.
 * Subsequently fire collision handlers for all entities that have
 * setCollidable(true).
 */
private void checkCollisions() {
    for (Entity e : entities) {
        if (isCollidable(e)) {
            collidables.add(e);
        }
    }
    for (int i = 0; i < collidables.size(); i++) {
        Entity e1 = collidables.get(i);
        for (int j = i + 1; j < collidables.size(); j++) {
            Entity e2 = collidables.get(j);
            CollisionHandler handler = getHandler(e1, e2);
            // if no handler registered, no need to check for this pair
            if (handler == null)
                continue;
            // if no need for manual check, let jbox handle it
            if (!needManualCheck(e1, e2)) {
                continue;
            }
            // check if colliding
            CollisionResult result = e1.getBoundingBoxComponent().checkCollision(e2.getBoundingBoxComponent());
            if (result.hasCollided()) {
                collisionBeginFor(handler, e1, e2, result.getBoxA(), result.getBoxB());
                // put result back to pool only if collided
                Pools.free(result);
            } else {
                collisionEndFor(e1, e2);
            }
        }
    }
    collidables.clear();
}
Also used : Entity(com.almasb.fxgl.entity.Entity)

Example 14 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 (Interpolators interpolator : Interpolators.values()) {
        Text t = getUIFactory().newText(interpolator.toString() + ":");
        t.setFill(Color.BLACK);
        Pane p = new Pane(t);
        p.setTranslateY(i * 50 + 25);
        Line line = new Line(0, i * 50, getWidth(), i * 50);
        line.setStroke(Color.RED);
        getGameScene().addUINodes(p, line);
        Texture texture = DSLKt.texture("bird.png").toAnimatedTexture(2, Duration.seconds(0.5));
        Entity bird = Entities.builder().at(100, i * 50).viewFromNode(texture).buildAndAttach();
        Entities.animationBuilder().interpolator(interpolator.EASE_OUT()).duration(Duration.seconds(2)).repeatInfinitely().translate(bird).from(new Point2D(100, i * 50)).to(new Point2D(400, i * 50)).buildAndPlay();
        i++;
    }
}
Also used : Line(javafx.scene.shape.Line) Entity(com.almasb.fxgl.entity.Entity) Point2D(javafx.geometry.Point2D) Interpolators(com.almasb.fxgl.animation.Interpolators) Text(javafx.scene.text.Text) Pane(javafx.scene.layout.Pane) Texture(com.almasb.fxgl.texture.Texture)

Example 15 with Entity

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

the class PhysicsSample method initPhysics.

@Override
protected void initPhysics() {
    // 3. get physics world and register a collision handler
    // between Type.PLAYER and Type.ENEMY
    PhysicsWorld physics = getPhysicsWorld();
    physics.addCollisionHandler(new CollisionHandler(Type.PLAYER, Type.ENEMY) {

        @Override
        protected void onHitBoxTrigger(Entity player, Entity enemy, HitBox playerBox, HitBox enemyBox) {
            System.out.println(playerBox.getName() + " X " + enemyBox.getName());
        }

        // the order of entities is determined by
        // the order of their types passed into constructor
        @Override
        protected void onCollisionBegin(Entity player, Entity enemy) {
            System.out.println("On Collision Begin");
        }

        @Override
        protected void onCollision(Entity player, Entity enemy) {
            System.out.println("On Collision");
        }

        @Override
        protected void onCollisionEnd(Entity player, Entity enemy) {
            System.out.println("On Collision End");
        }
    });
}
Also used : Entity(com.almasb.fxgl.entity.Entity) HitBox(com.almasb.fxgl.physics.HitBox) CollisionHandler(com.almasb.fxgl.physics.CollisionHandler) PhysicsWorld(com.almasb.fxgl.physics.PhysicsWorld)

Aggregations

Entity (com.almasb.fxgl.entity.Entity)55 Rectangle (javafx.scene.shape.Rectangle)21 Point2D (javafx.geometry.Point2D)15 HitBox (com.almasb.fxgl.physics.HitBox)12 EntityView (com.almasb.fxgl.entity.view.EntityView)8 UserAction (com.almasb.fxgl.input.UserAction)6 CollisionHandler (com.almasb.fxgl.physics.CollisionHandler)6 Input (com.almasb.fxgl.input.Input)5 GameApplication (com.almasb.fxgl.app.GameApplication)4 CollidableComponent (com.almasb.fxgl.entity.component.CollidableComponent)4 PositionComponent (com.almasb.fxgl.entity.component.PositionComponent)4 PhysicsParticleComponent (com.almasb.fxgl.physics.PhysicsParticleComponent)4 ParticleGroupDef (com.almasb.fxgl.physics.box2d.particle.ParticleGroupDef)4 GameSettings (com.almasb.fxgl.settings.GameSettings)4 ParticleControl (com.almasb.fxgl.effect.ParticleControl)3 ParticleEmitter (com.almasb.fxgl.effect.ParticleEmitter)3 Entities (com.almasb.fxgl.entity.Entities)3 PhysicsComponent (com.almasb.fxgl.physics.PhysicsComponent)3 Color (javafx.scene.paint.Color)3 Circle (javafx.scene.shape.Circle)3