Search in sources :

Example 26 with UserAction

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

the class PhysicsCollisionSample method initInput.

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

        @Override
        protected void onActionBegin() {
            System.out.println("Scaling Up Player.");
        }

        @Override
        protected void onAction() {
            scalePlayer(Scale.UP, 0.20);
        }

        @Override
        protected void onActionEnd() {
        }
    }, KeyCode.RIGHT);
    input.addAction(new UserAction("Scale Down") {

        @Override
        protected void onActionBegin() {
            System.out.println("Scaling Down Player.");
        }

        @Override
        protected void onAction() {
            scalePlayer(Scale.DOWN, 0.20);
        }

        @Override
        protected void onActionEnd() {
        }
    }, KeyCode.LEFT);
    input.addAction(new UserAction("Rotate Right") {

        @Override
        protected void onActionBegin() {
            System.out.println("Rotating Player Right.");
        }

        @Override
        protected void onAction() {
            double currentPlayerRotation = player.getRotation();
            player.setRotation(currentPlayerRotation - 1.5);
        }

        @Override
        protected void onActionEnd() {
        }
    }, KeyCode.Q);
    input.addAction(new UserAction("Rotate Left") {

        @Override
        protected void onActionBegin() {
            System.out.println("Rotating Player Right.");
        }

        @Override
        protected void onAction() {
            double currentPlayerRotation = player.getRotation();
            player.setRotation(currentPlayerRotation + 1.5);
        }

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

Example 27 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).setVelocityX(-200);
        }
    }, KeyCode.A, VirtualButton.LEFT);
    input.addAction(new UserAction("Right") {

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

        @Override
        protected void onActionBegin() {
            PhysicsComponent physics = player.getComponent(PhysicsComponent.class);
            if (physics.isOnGround()) {
                physics.setVelocityY(-500);
            }
        // getDevPane().addDebugPoint(player.getCenter());
        }
    }, KeyCode.W, VirtualButton.A);
    onKeyDown(KeyCode.I, "Info", () -> System.out.println(player.getCenter()));
    input.addAction(new UserAction("Grow") {

        @Override
        protected void onActionBegin() {
            player.setScaleX(player.getScaleX() * 1.25);
            player.setScaleY(player.getScaleY() * 1.25);
        // double x = player.getX();
        // double y = player.getY();
        // 
        // player.removeFromWorld();
        // 
        // player = createPlayer(x, y, 60, 80);
        }
    }, KeyCode.SPACE);
    input.addAction(new UserAction("Grow Poly") {

        @Override
        protected void onActionBegin() {
            poly.setScaleX(poly.getScaleX() * 1.25);
            poly.setScaleY(poly.getScaleY() * 1.25);
        }
    }, KeyCode.G);
}
Also used : UserAction(com.almasb.fxgl.input.UserAction) Input(com.almasb.fxgl.input.Input) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent)

Example 28 with UserAction

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

the class SpriteSheetAnimationApp method initInput.

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

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

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

Example 29 with UserAction

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

the class MultiplayerSample method initInput.

@Override
protected void initInput() {
    onKey(W, () -> player1.translateY(-5));
    onKey(S, () -> player1.translateY(5));
    onKey(A, () -> player1.translateX(-5));
    onKey(D, () -> player1.translateX(5));
    onBtnDown(MouseButton.PRIMARY, () -> shoot(player1));
    onKeyDown(F, () -> {
        if (isServer) {
            clientBus.fireEvent(new CustomReplicationEvent("Hello", random(0.0, 1000.0)));
        }
    });
    clientInput = new Input();
    onKeyBuilder(clientInput, W).onAction(() -> player2.translateY(-5));
    onKeyBuilder(clientInput, S).onAction(() -> player2.translateY(5));
    onKeyBuilder(clientInput, A).onAction(() -> player2.translateX(-5));
    onKeyBuilder(clientInput, D).onAction(() -> player2.translateX(5));
    clientInput.addAction(new UserAction("Shoot") {

        @Override
        protected void onActionBegin() {
            shoot(player2);
        }
    }, MouseButton.PRIMARY);
}
Also used : UserAction(com.almasb.fxgl.input.UserAction) Input(com.almasb.fxgl.input.Input)

Example 30 with UserAction

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

the class ParticlesSample method initInput.

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

        @Override
        protected void onActionBegin() {
            // 1. create entity
            Entity explosion = new Entity();
            explosion.setPosition(input.getMousePositionWorld());
            // 2. create and configure emitter + control
            ParticleEmitter emitter = ParticleEmitters.newExplosionEmitter(400);
            ParticleControl control = new ParticleControl(emitter);
            // we also want the entity to destroy itself when particle control is done
            control.setOnFinished(explosion::removeFromWorld);
            // 3. add control to entity
            explosion.addControl(control);
            // 4. add entity to game world
            getGameWorld().addEntity(explosion);
        }
    }, MouseButton.PRIMARY);
}
Also used : UserAction(com.almasb.fxgl.input.UserAction) ParticleEmitter(com.almasb.fxgl.effect.ParticleEmitter) Entity(com.almasb.fxgl.entity.Entity) Input(com.almasb.fxgl.input.Input) ParticleControl(com.almasb.fxgl.effect.ParticleControl)

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