Search in sources :

Example 1 with CollidableComponent

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

the class CollisionFilterSample method initGame.

@Override
protected void initGame() {
    entityBuilder().type(EType.PLAYER).viewWithBBox(new Rectangle(40, 40, Color.BLUE)).view(new Text("PLAYER")).at(100, 100).with(new CollidableComponent(true), new PhysicsComponent()).buildAndAttach();
    entityBuilder().type(EType.ENEMY).viewWithBBox(new Rectangle(40, 40, Color.RED)).view(new Text("ENEMY")).at(400, 100).with(new CollidableComponent(true), new PhysicsComponent()).buildAndAttach();
    run(() -> {
        CollidableComponent collidable = new CollidableComponent(true);
        CollidableComponent collidable2 = new CollidableComponent(true);
        if (i == 0) {
            debug("No collision with player");
            collidable.addIgnoredType(EType.PLAYER);
            collidable2.addIgnoredType(EType.PLAYER);
        } else if (i == 1) {
            debug("No collision with enemy");
            collidable.addIgnoredType(EType.ENEMY);
            collidable2.addIgnoredType(EType.ENEMY);
        } else if (i == 2) {
            debug("No collision with player and enemy");
            collidable.addIgnoredType(EType.PLAYER);
            collidable.addIgnoredType(EType.ENEMY);
            collidable2.addIgnoredType(EType.PLAYER);
            collidable2.addIgnoredType(EType.ENEMY);
        }
        i++;
        if (i == 3)
            i = 0;
        // a bullet with no physics
        entityBuilder().type(EType.BULLET).viewWithBBox(new Rectangle(20, 10, Color.BLACK)).at(0, 100).with(new ProjectileComponent(new Point2D(1, 0), 100)).with(new OffscreenCleanComponent()).with(collidable).buildAndAttach();
        // a bullet with physics
        PhysicsComponent physics = new PhysicsComponent();
        physics.setBodyType(BodyType.DYNAMIC);
        physics.setOnPhysicsInitialized(() -> physics.setLinearVelocity(100, 0));
        entityBuilder().type(EType.BULLET).viewWithBBox(new Rectangle(20, 10, Color.BLACK)).at(0, 130).with(physics).with(new OffscreenCleanComponent()).with(collidable2).buildAndAttach();
    }, Duration.seconds(4));
}
Also used : OffscreenCleanComponent(com.almasb.fxgl.dsl.components.OffscreenCleanComponent) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) Point2D(javafx.geometry.Point2D) CollidableComponent(com.almasb.fxgl.entity.components.CollidableComponent) Rectangle(javafx.scene.shape.Rectangle) Text(javafx.scene.text.Text) ProjectileComponent(com.almasb.fxgl.dsl.components.ProjectileComponent)

Example 2 with CollidableComponent

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

the class PhysicsWorld method isIgnored.

private boolean isIgnored(Entity e1, Entity e2) {
    if (!e1.hasComponent(CollidableComponent.class) || !e2.hasComponent(CollidableComponent.class))
        return false;
    CollidableComponent c1 = e1.getComponent(CollidableComponent.class);
    for (Serializable t1 : c1.getIgnoredTypes()) {
        if (e2.isType(t1)) {
            return true;
        }
    }
    CollidableComponent c2 = e2.getComponent(CollidableComponent.class);
    for (Serializable t2 : c2.getIgnoredTypes()) {
        if (e1.isType(t2)) {
            return true;
        }
    }
    return false;
}
Also used : Serializable(java.io.Serializable) CollidableComponent(com.almasb.fxgl.entity.components.CollidableComponent)

Example 3 with CollidableComponent

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

the class BBoxSample method initGame.

@Override
protected void initGame() {
    // entity 1
    player = entityBuilder().type(Type.PLAYER).at(100, 150).viewWithBBox("brick.png").with(new CollidableComponent(true), new DeveloperWASDControl()).with(new RandomMoveComponent(new Rectangle2D(0, 0, getAppWidth(), getAppHeight()), 500)).with(new EffectComponent()).with(new TimeComponent(1.0)).zIndex(250).buildAndAttach();
    player.getTransformComponent().setRotationOrigin(new Point2D(64, 64));
    player.getTransformComponent().scaleOriginXProperty().setValue(64);
    player.getTransformComponent().scaleOriginYProperty().setValue(64);
    // entity 2
    entityBuilder().at(100, 100).viewWithBBox(new Rectangle(40, 40, Color.RED)).with(new CollidableComponent(true)).with(new LiftComponent().yAxisSpeedDuration(150, Duration.seconds(3)).xAxisSpeedDuration(100, Duration.seconds(3))).with(new EffectComponent()).zIndex(250).buildAndAttach();
    // entity 3
    entityBuilder().type(Type.NPC).at(400, 150).bbox(new HitBox(new Point2D(5, 5), BoundingShape.circle(20))).view(texture("enemy1.png").toAnimatedTexture(2, Duration.seconds(1)).loop()).with(new EffectComponent()).buildAndAttach();
}
Also used : DeveloperWASDControl(dev.DeveloperWASDControl) RandomMoveComponent(com.almasb.fxgl.dsl.components.RandomMoveComponent) HitBox(com.almasb.fxgl.physics.HitBox) TimeComponent(com.almasb.fxgl.entity.components.TimeComponent) Point2D(javafx.geometry.Point2D) CollidableComponent(com.almasb.fxgl.entity.components.CollidableComponent) Rectangle2D(javafx.geometry.Rectangle2D) Rectangle(javafx.scene.shape.Rectangle) LiftComponent(com.almasb.fxgl.dsl.components.LiftComponent) EffectComponent(com.almasb.fxgl.dsl.components.EffectComponent)

Example 4 with CollidableComponent

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

the class BasicGameApp method initGame.

@Override
protected void initGame() {
    player = entityBuilder().type(EntityType.PLAYER).at(300, 300).viewWithBBox("brick.png").with(new CollidableComponent(true)).buildAndAttach();
    entityBuilder().type(EntityType.COIN).at(500, 200).viewWithBBox(new Circle(15, 15, 15, Color.YELLOW)).with(new CollidableComponent(true)).buildAndAttach();
}
Also used : Circle(javafx.scene.shape.Circle) CollidableComponent(com.almasb.fxgl.entity.components.CollidableComponent)

Aggregations

CollidableComponent (com.almasb.fxgl.entity.components.CollidableComponent)4 Point2D (javafx.geometry.Point2D)2 Rectangle (javafx.scene.shape.Rectangle)2 EffectComponent (com.almasb.fxgl.dsl.components.EffectComponent)1 LiftComponent (com.almasb.fxgl.dsl.components.LiftComponent)1 OffscreenCleanComponent (com.almasb.fxgl.dsl.components.OffscreenCleanComponent)1 ProjectileComponent (com.almasb.fxgl.dsl.components.ProjectileComponent)1 RandomMoveComponent (com.almasb.fxgl.dsl.components.RandomMoveComponent)1 TimeComponent (com.almasb.fxgl.entity.components.TimeComponent)1 HitBox (com.almasb.fxgl.physics.HitBox)1 PhysicsComponent (com.almasb.fxgl.physics.PhysicsComponent)1 DeveloperWASDControl (dev.DeveloperWASDControl)1 Serializable (java.io.Serializable)1 Rectangle2D (javafx.geometry.Rectangle2D)1 Circle (javafx.scene.shape.Circle)1 Text (javafx.scene.text.Text)1