Search in sources :

Example 46 with UserAction

use of com.almasb.fxgl.input.UserAction in project FXGL by AlmasB.

the class RevoluteJointSample method initInput.

@Override
protected void initInput() {
    getInput().addAction(new UserAction("LMB") {

        private double x;

        private double y;

        @Override
        protected void onActionBegin() {
            x = getInput().getMouseXWorld();
            y = getInput().getMouseYWorld();
        }

        @Override
        protected void onActionEnd() {
            var endx = getInput().getMouseXWorld();
            var endy = getInput().getMouseYWorld();
            spawnBullet(x, y, endx - x, endy - y);
        }
    }, MouseButton.PRIMARY);
    onKeyDown(KeyCode.F, () -> {
        Entity box = createPhysicsEntity();
        box.getBoundingBoxComponent().addHitBox(new HitBox("Left", BoundingShape.box(40, 40)));
        box.getBoundingBoxComponent().addHitBox(new HitBox("Right", new Point2D(40, 0), BoundingShape.box(40, 40)));
        box.getViewComponent().addChild(texture("brick.png", 40, 40).superTexture(texture("brick.png", 40, 40), HorizontalDirection.RIGHT));
        box.setRotationOrigin(new Point2D(40, 20));
        getGameWorld().addEntity(box);
    });
    onBtnDown(MouseButton.SECONDARY, () -> {
        Entity ball = createPhysicsEntity();
        ball.getBoundingBoxComponent().addHitBox(new HitBox("Test", BoundingShape.circle(20)));
        ball.getViewComponent().addChild(texture("ball.png", 40, 40));
        ball.setRotationOrigin(new Point2D(20, 20));
        getGameWorld().addEntity(ball);
    });
}
Also used : UserAction(com.almasb.fxgl.input.UserAction) Entity(com.almasb.fxgl.entity.Entity) HitBox(com.almasb.fxgl.physics.HitBox) Point2D(javafx.geometry.Point2D)

Example 47 with UserAction

use of com.almasb.fxgl.input.UserAction in project FXGL by AlmasB.

the class ScrollingBackgroundSample method initInput.

@Override
protected void initInput() {
    getInput().addAction(new UserAction("Move Right") {

        @Override
        protected void onAction() {
            player.translateX(10);
        }
    }, KeyCode.D);
    getInput().addAction(new UserAction("Move Left") {

        @Override
        protected void onAction() {
            player.translateX(-10);
        }
    }, KeyCode.A);
    onKeyDown(KeyCode.F, "shake", () -> {
        // getGameScene().getViewport().focusOn(new Point2D(2000, getAppHeight() / 2));
        getGameScene().getViewport().shakeTranslational(5);
    });
}
Also used : UserAction(com.almasb.fxgl.input.UserAction)

Example 48 with UserAction

use of com.almasb.fxgl.input.UserAction in project FXGL by AlmasB.

the class EntityActionSample method initInput.

@Override
protected void initInput() {
    getInput().addAction(new UserAction("Move Now") {

        @Override
        protected void onActionBegin() {
            entity.getComponent(ActionComponent.class).cancelActions();
            entity.getComponent(ActionComponent.class).addAction(new MoveAction(getInput().getMouseXWorld(), getInput().getMouseYWorld()));
        }
    }, MouseButton.SECONDARY);
    getInput().addAction(new UserAction("Queue Move Action") {

        @Override
        protected void onActionBegin() {
            entity.getComponent(ActionComponent.class).addAction(new MoveAction(getInput().getMouseXWorld(), getInput().getMouseYWorld()));
        }
    }, MouseButton.PRIMARY);
    getInput().addAction(new UserAction("Remove Current Action") {

        @Override
        protected void onActionBegin() {
            Action a = actionsView.getSelectionModel().getSelectedItem();
            if (a != null) {
                entity.getComponent(ActionComponent.class).removeAction(a);
            }
        }
    }, KeyCode.F);
}
Also used : UserAction(com.almasb.fxgl.input.UserAction) Action(com.almasb.fxgl.entity.action.Action) UserAction(com.almasb.fxgl.input.UserAction) ContinuousAction(com.almasb.fxgl.entity.action.ContinuousAction) InstantAction(com.almasb.fxgl.entity.action.InstantAction)

Aggregations

UserAction (com.almasb.fxgl.input.UserAction)48 Input (com.almasb.fxgl.input.Input)23 Entity (com.almasb.fxgl.entity.Entity)6 Point2D (javafx.geometry.Point2D)6 HitBox (com.almasb.fxgl.physics.HitBox)5 PhysicsComponent (com.almasb.fxgl.physics.PhysicsComponent)3 Rectangle (javafx.scene.shape.Rectangle)3 GameApplication (com.almasb.fxgl.app.GameApplication)2 Action (com.almasb.fxgl.entity.action.Action)2 GameSettings (com.almasb.fxgl.settings.GameSettings)2 Circle (javafx.scene.shape.Circle)2 VoronoiSubdivision (com.almasb.fxgl.algorithm.VoronoiSubdivision)1 DSLKt (com.almasb.fxgl.app.DSLKt)1 FXGLMath (com.almasb.fxgl.core.math.FXGLMath)1 SlowTimeEffect (com.almasb.fxgl.dsl.effects.SlowTimeEffect)1 ParticleControl (com.almasb.fxgl.effect.ParticleControl)1 ParticleEmitter (com.almasb.fxgl.effect.ParticleEmitter)1 Entities (com.almasb.fxgl.entity.Entities)1 ContinuousAction (com.almasb.fxgl.entity.action.ContinuousAction)1 InstantAction (com.almasb.fxgl.entity.action.InstantAction)1