Search in sources :

Example 6 with UserAction

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

the class BasicGameApp3 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);
    input.addAction(new UserAction("Play Sound") {

        @Override
        protected void onActionBegin() {
            getAudioPlayer().playSound("drop.wav");
        }
    }, KeyCode.F);
}
Also used : UserAction(com.almasb.fxgl.input.UserAction) Input(com.almasb.fxgl.input.Input)

Example 7 with UserAction

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

the class BasicGameApp4 method initInput.

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

        @Override
        protected void onAction() {
            player.getControl(DudeControl.class).moveRight();
        }
    }, KeyCode.D);
    getInput().addAction(new UserAction("Left") {

        @Override
        protected void onAction() {
            player.getControl(DudeControl.class).moveLeft();
        }
    }, KeyCode.A);
}
Also used : UserAction(com.almasb.fxgl.input.UserAction)

Example 8 with UserAction

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

the class PhysicsSample 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);
}
Also used : UserAction(com.almasb.fxgl.input.UserAction) Input(com.almasb.fxgl.input.Input)

Example 9 with UserAction

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

the class PlatformerSample method initInput.

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

        @Override
        protected void onActionBegin() {
            player.getComponent(PhysicsComponent.class).setLinearVelocity(new Point2D(-200, 0));
        }
    }, KeyCode.A);
    input.addAction(new UserAction("Right") {

        @Override
        protected void onActionBegin() {
            player.getComponent(PhysicsComponent.class).setLinearVelocity(new Point2D(200, 0));
        }
    }, KeyCode.D);
    input.addAction(new UserAction("Jump") {

        @Override
        protected void onActionBegin() {
            PhysicsComponent physics = player.getComponent(PhysicsComponent.class);
            if (physics.isOnGround()) {
                double dx = physics.getLinearVelocity().getX();
                physics.setLinearVelocity(new Point2D(dx, -100));
            }
        }
    }, KeyCode.W);
    input.addAction(new UserAction("Grow") {

        @Override
        protected void onActionBegin() {
            double x = player.getX();
            double y = player.getY();
            player.removeFromWorld();
            player = createPlayer(x, y, 60, 80);
        }
    }, KeyCode.SPACE);
}
Also used : UserAction(com.almasb.fxgl.input.UserAction) Input(com.almasb.fxgl.input.Input) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) Point2D(javafx.geometry.Point2D)

Example 10 with UserAction

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

the class RealPhysicsSample method initInput.

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

        @Override
        protected void onActionBegin() {
            Entity box = createPhysicsEntity();
            // 3. set hit box (-es) to specify bounding shape
            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().setView(new Rectangle(80, 40, Color.BLUE));
            getGameWorld().addEntity(box);
        }
    }, MouseButton.PRIMARY);
    input.addAction(new UserAction("Spawn Ball") {

        @Override
        protected void onActionBegin() {
            Entity ball = createPhysicsEntity();
            // 3. set hit box to specify bounding shape
            ball.getBoundingBoxComponent().addHitBox(new HitBox("Test", BoundingShape.circle(20)));
            ball.getViewComponent().setView(new Circle(20, Color.RED));
            getGameWorld().addEntity(ball);
        }
    }, MouseButton.SECONDARY);
}
Also used : UserAction(com.almasb.fxgl.input.UserAction) Entity(com.almasb.fxgl.entity.Entity) Circle(javafx.scene.shape.Circle) Input(com.almasb.fxgl.input.Input) HitBox(com.almasb.fxgl.physics.HitBox) Point2D(javafx.geometry.Point2D) Rectangle(javafx.scene.shape.Rectangle)

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