Search in sources :

Example 1 with CollidableComponent

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

the class ScifiFactory method newPlayer.

@Spawns("player")
public Entity newPlayer(SpawnData data) {
    PhysicsComponent physics = new PhysicsComponent();
    physics.setFixtureDef(new FixtureDef().friction(0).density(0.25f));
    BodyDef bd = new BodyDef();
    bd.setFixedRotation(true);
    physics.setBodyDef(bd);
    physics.setBodyType(BodyType.DYNAMIC);
    return Entities.builder().from(data).type(ScifiType.PLAYER).bbox(new HitBox("main", BoundingShape.circle(15))).bbox(new HitBox("lower", new Point2D(15 - 5, 30), BoundingShape.box(10, 10))).with(physics, new CollidableComponent(true)).with(new PlayerControl()).build();
}
Also used : HitBox(com.almasb.fxgl.physics.HitBox) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) Point2D(javafx.geometry.Point2D) CollidableComponent(com.almasb.fxgl.entity.component.CollidableComponent) BodyDef(com.almasb.fxgl.physics.box2d.dynamics.BodyDef) FixtureDef(com.almasb.fxgl.physics.box2d.dynamics.FixtureDef)

Example 2 with CollidableComponent

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

the class FXShooterApp method spawnEnemy.

private void spawnEnemy() {
    Entity enemy = new Entity();
    enemy.getTypeComponent().setValue(EntityType.ENEMY);
    enemy.getPositionComponent().setValue(getWidth(), 460);
    enemy.getViewComponent().setView(new EntityView(new Rectangle(40, 40, Color.RED)), true);
    enemy.addComponent(new CollidableComponent(true));
    enemy.addComponent(new HPComponent(5));
    enemy.addControl(new EnemyControl(new Point2D(getWidth() / 2, getHeight() / 2)));
    getGameWorld().addEntity(enemy);
}
Also used : Entity(com.almasb.fxgl.entity.Entity) EntityView(com.almasb.fxgl.entity.view.EntityView) Point2D(javafx.geometry.Point2D) CollidableComponent(com.almasb.fxgl.entity.component.CollidableComponent) Rectangle(javafx.scene.shape.Rectangle)

Example 3 with CollidableComponent

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

the class PlayerControl method shoot.

public void shoot(Point2D direction) {
    Entity bullet = new Entity();
    bullet.getTypeComponent().setValue(FXShooterApp.EntityType.BULLET);
    bullet.getPositionComponent().setValue(getEntity().getComponent(PositionComponent.class).getValue().add(20, 20));
    bullet.getViewComponent().setView(new EntityView(new Rectangle(10, 2, Color.BLACK)), true);
    bullet.addComponent(new CollidableComponent(true));
    bullet.addControl(new OffscreenCleanControl());
    bullet.addControl(new ProjectileControl(direction, 10 * 60));
    BulletComponent bulletData = new BulletComponent();
    bulletData.setDamage(1);
    bulletData.setHp(1);
    bulletData.setSpeed(10);
    bullet.addComponent(bulletData);
    getEntity().getWorld().addEntity(bullet);
}
Also used : Entity(com.almasb.fxgl.entity.Entity) EntityView(com.almasb.fxgl.entity.view.EntityView) PositionComponent(com.almasb.fxgl.entity.component.PositionComponent) CollidableComponent(com.almasb.fxgl.entity.component.CollidableComponent) Rectangle(javafx.scene.shape.Rectangle) ProjectileControl(com.almasb.fxgl.entity.control.ProjectileControl) OffscreenCleanControl(com.almasb.fxgl.entity.control.OffscreenCleanControl)

Example 4 with CollidableComponent

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

the class MarioApp method initInput.

@Override
protected void initInput() {
    DSLKt.onKeyDown(KeyCode.K, "G+", () -> {
        getPhysicsWorld().setGravity(0, -getPhysicsWorld().getJBox2DWorld().getGravity().y * 50 + 10);
        System.out.println(-getPhysicsWorld().getJBox2DWorld().getGravity().y * 50);
    });
    DSLKt.onKeyDown(KeyCode.I, "G-", () -> {
        getPhysicsWorld().setGravity(0, -getPhysicsWorld().getJBox2DWorld().getGravity().y * 50 - 10);
        System.out.println(-getPhysicsWorld().getJBox2DWorld().getGravity().y * 50);
    });
    getInput().addAction(new UserAction("Left") {

        @Override
        protected void onAction() {
            playerControl.left();
        }

        @Override
        protected void onActionEnd() {
            playerControl.stop();
        }
    }, KeyCode.A);
    getInput().addAction(new UserAction("Right") {

        @Override
        protected void onAction() {
            playerControl.right();
        }

        @Override
        protected void onActionEnd() {
            playerControl.stop();
        }
    }, KeyCode.D);
    getInput().addAction(new UserAction("Jump") {

        @Override
        protected void onActionBegin() {
            playerControl.jump();
        }
    }, KeyCode.W);
    getInput().addAction(new UserAction("Enter") {

        @Override
        protected void onActionBegin() {
            stepLoop();
        }
    }, KeyCode.L);
    getInput().addAction(new UserAction("Drop rectangle") {

        @Override
        protected void onActionBegin() {
            Entities.builder().type(MarioType.OBSTACLE).at(getInput().getMousePositionWorld()).viewFromNodeWithBBox(new Rectangle(40, 40)).with(new CollidableComponent(true)).with(new ExpireCleanControl(Duration.seconds(2)).animateOpacity()).buildAndAttach();
        }
    }, MouseButton.PRIMARY);
}
Also used : UserAction(com.almasb.fxgl.input.UserAction) CollidableComponent(com.almasb.fxgl.entity.component.CollidableComponent) Rectangle(javafx.scene.shape.Rectangle) ExpireCleanControl(com.almasb.fxgl.entity.control.ExpireCleanControl)

Example 5 with CollidableComponent

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

the class MarioFactory method newPlayer.

@Spawns("player")
public Entity newPlayer(SpawnData data) {
    PhysicsComponent physics = new PhysicsComponent();
    physics.setFixtureDef(new FixtureDef().friction(0).density(0.25f));
    physics.setGenerateGroundSensor(true);
    BodyDef bd = new BodyDef();
    bd.setFixedRotation(true);
    physics.setBodyDef(bd);
    physics.setBodyType(BodyType.DYNAMIC);
    return Entities.builder().from(data).type(MarioType.PLAYER).bbox(new HitBox("main", BoundingShape.circle(15))).bbox(new HitBox("lower", new Point2D(15 - 5, 30), BoundingShape.box(10, 10))).with(physics, new CollidableComponent(true)).with(new PlayerControl()).build();
}
Also used : HitBox(com.almasb.fxgl.physics.HitBox) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) Point2D(javafx.geometry.Point2D) CollidableComponent(com.almasb.fxgl.entity.component.CollidableComponent) BodyDef(com.almasb.fxgl.physics.box2d.dynamics.BodyDef) FixtureDef(com.almasb.fxgl.physics.box2d.dynamics.FixtureDef)

Aggregations

CollidableComponent (com.almasb.fxgl.entity.component.CollidableComponent)13 HitBox (com.almasb.fxgl.physics.HitBox)7 Rectangle (javafx.scene.shape.Rectangle)7 PhysicsComponent (com.almasb.fxgl.physics.PhysicsComponent)5 Point2D (javafx.geometry.Point2D)5 Entity (com.almasb.fxgl.entity.Entity)4 BodyDef (com.almasb.fxgl.physics.box2d.dynamics.BodyDef)3 FixtureDef (com.almasb.fxgl.physics.box2d.dynamics.FixtureDef)3 DeveloperWASDControl (com.almasb.fxgl.devtools.DeveloperWASDControl)2 EntityView (com.almasb.fxgl.entity.view.EntityView)2 PlayerControl (common.PlayerControl)2 Circle (javafx.scene.shape.Circle)2 PositionComponent (com.almasb.fxgl.entity.component.PositionComponent)1 EffectControl (com.almasb.fxgl.entity.control.EffectControl)1 ExpireCleanControl (com.almasb.fxgl.entity.control.ExpireCleanControl)1 OffscreenCleanControl (com.almasb.fxgl.entity.control.OffscreenCleanControl)1 ProjectileControl (com.almasb.fxgl.entity.control.ProjectileControl)1 UserAction (com.almasb.fxgl.input.UserAction)1