Search in sources :

Example 51 with Entity

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

the class MarioApp method initPhysics.

@Override
protected void initPhysics() {
    getPhysicsWorld().setGravity(0, 500 * 2.5);
    getPhysicsWorld().addCollisionHandler(new CollisionHandler(MarioType.PLAYER, MarioType.GHOST_PLATFORM) {

        @Override
        protected void onCollisionBegin(Entity a, Entity platform) {
            platform.getView().setVisible(true);
        }

        @Override
        protected void onCollisionEnd(Entity a, Entity platform) {
            platform.getView().setVisible(false);
        }
    });
    getPhysicsWorld().addCollisionHandler(new CollisionHandler(MarioType.PLAYER, MarioType.PORTAL) {

        @Override
        protected void onCollisionBegin(Entity a, Entity b) {
            nextLevel = true;
        }
    });
    getPhysicsWorld().addCollisionHandler(new CollectibleHandler(MarioType.PLAYER, MarioType.COIN, "drop.wav"));
}
Also used : CollectibleHandler(com.almasb.fxgl.physics.handler.CollectibleHandler) Entity(com.almasb.fxgl.entity.Entity) CollisionHandler(com.almasb.fxgl.physics.CollisionHandler)

Example 52 with Entity

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

the class PhysicsSample2 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 onCollision(Entity player, Entity enemy) {
            isColliding = true;
        }
    });
}
Also used : Entity(com.almasb.fxgl.entity.Entity) CollisionHandler(com.almasb.fxgl.physics.CollisionHandler) PhysicsWorld(com.almasb.fxgl.physics.PhysicsWorld)

Example 53 with Entity

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

the class AssetsSample method approach2.

private void approach2() {
    Entity brick2 = new Entity();
    brick2.setPosition(200, 300);
    // if you don't need texture reference
    brick2.setViewFromTexture("brick.png");
    getGameWorld().addEntity(brick2);
}
Also used : Entity(com.almasb.fxgl.entity.Entity)

Example 54 with Entity

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

the class AnimColorSample method initGame.

@Override
protected void initGame() {
    Rectangle playerView = new Rectangle(40, 40);
    Entity player = Entities.builder().at(100, 100).viewFromNode(playerView).with(new ColorComponent()).buildAndAttach();
    playerView.fillProperty().bind(player.getComponent(ColorComponent.class).valueProperty());
    Entities.animationBuilder().duration(Duration.seconds(2)).repeat(4).color(player).fromColor(Color.AQUA).toColor(Color.BURLYWOOD).buildAndPlay();
}
Also used : Entity(com.almasb.fxgl.entity.Entity) ColorComponent(com.almasb.fxgl.entity.component.ColorComponent) Rectangle(javafx.scene.shape.Rectangle)

Example 55 with Entity

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

the class DrawableSample method initGame.

@Override
protected void initGame() {
    BiConsumer<GraphicsContext, Entity> drawing = (g, entity) -> {
        Point2D pos = entity.getPosition();
        g.setFill(Color.BLUE);
        g.fillRect(pos.getX(), pos.getY(), 40, 40);
    };
    Entity entity = new Entity();
    entity.setPosition(400, 300);
    entity.addComponent(new DrawableComponent(drawing));
    Entity entity2 = new Entity();
    entity2.setPosition(750, 300);
    entity2.addComponent(new DrawableComponent(drawing));
    getGameWorld().addEntities(entity, entity2);
}
Also used : Color(javafx.scene.paint.Color) DrawableComponent(com.almasb.fxgl.entity.component.DrawableComponent) GraphicsContext(javafx.scene.canvas.GraphicsContext) BiConsumer(java.util.function.BiConsumer) Point2D(javafx.geometry.Point2D) GameSettings(com.almasb.fxgl.settings.GameSettings) GameApplication(com.almasb.fxgl.app.GameApplication) Entity(com.almasb.fxgl.entity.Entity) Entity(com.almasb.fxgl.entity.Entity) DrawableComponent(com.almasb.fxgl.entity.component.DrawableComponent) GraphicsContext(javafx.scene.canvas.GraphicsContext) Point2D(javafx.geometry.Point2D)

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