Search in sources :

Example 41 with UserAction

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

the class PhysicsSample2 method initInput.

@Override
protected void initInput() {
    Input input = getInput();
    input.addAction(new UserAction("Move Left") {

        @Override
        protected void onAction() {
            playerControl.left();
        }
    }, KeyCode.A);
    input.addAction(new UserAction("Move Right") {

        @Override
        protected void onAction() {
            playerControl.right();
        }
    }, KeyCode.D);
    input.addAction(new UserAction("Move Up") {

        @Override
        protected void onAction() {
            playerControl.up();
        }
    }, KeyCode.W);
    input.addAction(new UserAction("Move Down") {

        @Override
        protected void onAction() {
            playerControl.down();
        }
    }, KeyCode.S);
    input.addAction(new UserAction("Rotate CCW") {

        @Override
        protected void onAction() {
            player.rotateBy(-5);
        }
    }, KeyCode.Q);
    input.addAction(new UserAction("Rotate CW") {

        @Override
        protected void onAction() {
            player.rotateBy(5);
        }
    }, KeyCode.E);
}
Also used : UserAction(com.almasb.fxgl.input.UserAction) Input(com.almasb.fxgl.input.Input)

Example 42 with UserAction

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

the class MarioApp method initInput.

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

        @Override
        protected void onAction() {
            player.getControl(PlayerControl.class).left();
        }
    }, KeyCode.A);
    getInput().addAction(new UserAction("Right") {

        @Override
        protected void onAction() {
            player.getControl(PlayerControl.class).right();
        }
    }, KeyCode.D);
    getInput().addAction(new UserAction("Jump") {

        @Override
        protected void onAction() {
            player.getControl(PlayerControl.class).jump();
        }
    }, KeyCode.W);
    DSLKt.onKeyDown(KeyCode.F, "asd", () -> {
        player.getControl(EffectControl.class).startEffect(new WobbleEffect(Duration.seconds(3), 3, 7, Orientation.VERTICAL));
    });
    DSLKt.onKeyDown(KeyCode.G, "asd2", () -> {
        player.getControl(EffectControl.class).startEffect(new WobbleEffect(Duration.seconds(3), 2, 4, Orientation.HORIZONTAL));
    });
}
Also used : UserAction(com.almasb.fxgl.input.UserAction) WobbleEffect(com.almasb.fxgl.entity.effect.WobbleEffect) EffectControl(com.almasb.fxgl.entity.control.EffectControl)

Example 43 with UserAction

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

the class BasicGameApp method initInput.

@Override
protected void initInput() {
    // get input service
    Input input = getInput();
    input.addAction(new UserAction("Move Right") {

        @Override
        protected void onAction() {
            // move right 5 pixels
            player.translateX(5);
            getGameState().increment("pixelsMoved", +5);
        }
    }, KeyCode.D);
    input.addAction(new UserAction("Move Left") {

        @Override
        protected void onAction() {
            // move left 5 pixels
            player.translateX(-5);
            getGameState().increment("pixelsMoved", +5);
        }
    }, KeyCode.A);
    input.addAction(new UserAction("Move Up") {

        @Override
        protected void onAction() {
            // move up 5 pixels
            player.translateY(-5);
            getGameState().increment("pixelsMoved", +5);
        }
    }, KeyCode.W);
    input.addAction(new UserAction("Move Down") {

        @Override
        protected void onAction() {
            // move down 5 pixels
            player.translateY(5);
            getGameState().increment("pixelsMoved", +5);
        }
    }, KeyCode.S);
}
Also used : UserAction(com.almasb.fxgl.input.UserAction) Input(com.almasb.fxgl.input.Input)

Example 44 with UserAction

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

the class BBoxSample method initInput.

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

        @Override
        protected void onActionBegin() {
            player.getComponent(EffectComponent.class).startEffect(new SlowTimeEffect(0.2, Duration.seconds(3)));
        }
    }, KeyCode.F);
    getInput().addAction(new UserAction("Change view 1") {

        @Override
        protected void onActionBegin() {
        // byType(Type.NPC).get(0).getComponent(EffectComponent.class).startEffect(new FlashEffect(Color.RED, Duration.seconds(0.75)));
        // player.setView(texture("bird.png").toAnimatedTexture(2, Duration.seconds(0.33)).play());
        }
    }, KeyCode.G);
    onKey(KeyCode.Q, () -> {
        scaleX += 0.1;
        player.setScaleX(scaleX);
    });
    onKey(KeyCode.E, () -> {
        scaleY += 0.1;
        player.setScaleY(scaleY);
    });
    onKey(KeyCode.Z, () -> {
        scaleX -= 0.1;
        player.setScaleX(scaleX);
    });
    onKey(KeyCode.C, () -> {
        scaleY -= 0.1;
        player.setScaleY(scaleY);
    });
    onKey(KeyCode.R, () -> {
        angle -= 5;
        player.setRotation(angle);
    });
    onKey(KeyCode.T, () -> {
        angle += 5;
        player.setRotation(angle);
    });
}
Also used : UserAction(com.almasb.fxgl.input.UserAction) SlowTimeEffect(com.almasb.fxgl.dsl.effects.SlowTimeEffect)

Example 45 with UserAction

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

the class RopeJointSample 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)

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