Search in sources :

Example 1 with OffscreenCleanComponent

use of com.almasb.fxgl.dsl.components.OffscreenCleanComponent 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)

Aggregations

OffscreenCleanComponent (com.almasb.fxgl.dsl.components.OffscreenCleanComponent)1 ProjectileComponent (com.almasb.fxgl.dsl.components.ProjectileComponent)1 CollidableComponent (com.almasb.fxgl.entity.components.CollidableComponent)1 PhysicsComponent (com.almasb.fxgl.physics.PhysicsComponent)1 Point2D (javafx.geometry.Point2D)1 Rectangle (javafx.scene.shape.Rectangle)1 Text (javafx.scene.text.Text)1