Search in sources :

Example 6 with PlayerControl

use of common.PlayerControl in project FXGL by AlmasB.

the class EventsSample method initGame.

@Override
protected void initGame() {
    playerControl = new PlayerControl();
    player = Entities.builder().type(Type.PLAYER).at(100, 100).bbox(new HitBox("PLAYER_BODY", BoundingShape.box(40, 40))).viewFromNode(new Rectangle(40, 40, Color.BLUE)).with(playerControl).build();
    enemy = Entities.builder().type(Type.ENEMY).at(200, 100).viewFromNodeWithBBox(new Rectangle(40, 40, Color.RED)).build();
    getGameWorld().addEntities(player, enemy);
    // 1. add event trigger and specify when and what to fire
    // you can use a custom event or pre-defined event / with custom event type
    getEventBus().addEventTrigger(new EventTrigger<EntityEvent>(() -> player.getRightX() > enemy.getX(), () -> new EntityEvent(Events.PASSED, player, enemy)));
    // 2. add event handler using code OR
    getEventBus().addEventHandler(MyGameEvent.ANY, event -> {
        System.out.println("Code handler: " + event);
    });
    getMasterTimer().runOnceAfter(() -> getEventBus().fireEvent(new MyGameEvent(MyGameEvent.LOW_HP)), Duration.seconds(1));
}
Also used : HitBox(com.almasb.fxgl.physics.HitBox) EntityEvent(com.almasb.fxgl.entity.EntityEvent) Rectangle(javafx.scene.shape.Rectangle) PlayerControl(common.PlayerControl)

Aggregations

PlayerControl (common.PlayerControl)6 Rectangle (javafx.scene.shape.Rectangle)6 HitBox (com.almasb.fxgl.physics.HitBox)3 Entity (com.almasb.fxgl.entity.Entity)2 CollidableComponent (com.almasb.fxgl.entity.component.CollidableComponent)2 AIControl (com.almasb.fxgl.ai.AIControl)1 EntityEvent (com.almasb.fxgl.entity.EntityEvent)1 EntityView (com.almasb.fxgl.entity.view.EntityView)1