Example usage for com.badlogic.gdx Gdx input

List of usage examples for com.badlogic.gdx Gdx input

Introduction

In this page you can find the example usage for com.badlogic.gdx Gdx input.

Prototype

Input input

To view the source code for com.badlogic.gdx Gdx input.

Click Source Link

Usage

From source file:MyGdxGame.java

License:Apache License

private void updateKoala(float deltaTime) {
    if (deltaTime == 0)
        return;/* w  ww.j  av a  2 s.c  o m*/
    koala.stateTime += deltaTime;

    // check input and apply to velocity & state
    if ((Gdx.input.isKeyPressed(Keys.SPACE) || isTouched(0.5f, 1)) && koala.grounded) {
        koala.velocity.y += Koala.JUMP_VELOCITY;
        koala.state = Koala.State.Jumping;
        koala.grounded = false;
    }

    if (Gdx.input.isKeyPressed(Keys.LEFT) || Gdx.input.isKeyPressed(Keys.A) || isTouched(0, 0.25f)) {
        koala.velocity.x = -Koala.MAX_VELOCITY;
        if (koala.grounded)
            koala.state = Koala.State.Walking;
        koala.facesRight = false;
    }

    if (Gdx.input.isKeyPressed(Keys.RIGHT) || Gdx.input.isKeyPressed(Keys.D) || isTouched(0.25f, 0.5f)) {
        koala.velocity.x = Koala.MAX_VELOCITY;
        if (koala.grounded)
            koala.state = Koala.State.Walking;
        koala.facesRight = true;
    }

    // apply gravity if we are falling
    koala.velocity.add(0, GRAVITY);

    // clamp the velocity to the maximum, x-axis only
    if (Math.abs(koala.velocity.x) > Koala.MAX_VELOCITY) {
        koala.velocity.x = Math.signum(koala.velocity.x) * Koala.MAX_VELOCITY;
    }

    // clamp the velocity to 0 if it's < 1, and set the state to standign
    if (Math.abs(koala.velocity.x) < 1) {
        koala.velocity.x = 0;
        if (koala.grounded)
            koala.state = Koala.State.Standing;
    }

    // multiply by delta time so we know how far we go
    // in this frame
    koala.velocity.scl(deltaTime);

    // perform collision detection & response, on each axis, separately
    // if the koala is moving right, check the tiles to the right of it's
    // right bounding box edge, otherwise check the ones to the left
    Rectangle koalaRect = rectPool.obtain();
    koalaRect.set(koala.position.x, koala.position.y, Koala.WIDTH, Koala.HEIGHT);
    int startX, startY, endX, endY;
    if (koala.velocity.x > 0) {
        startX = endX = (int) (koala.position.x + Koala.WIDTH + koala.velocity.x);
    } else {
        startX = endX = (int) (koala.position.x + koala.velocity.x);
    }
    startY = (int) (koala.position.y);
    endY = (int) (koala.position.y + Koala.HEIGHT);
    getTiles(startX, startY, endX, endY, tiles);
    koalaRect.x += koala.velocity.x;
    for (Rectangle tile : tiles) {
        if (koalaRect.overlaps(tile)) {
            koala.velocity.x = 0;
            break;
        }
    }
    koalaRect.x = koala.position.x;

    // if the koala is moving upwards, check the tiles to the top of it's
    // top bounding box edge, otherwise check the ones to the bottom
    if (koala.velocity.y > 0) {
        startY = endY = (int) (koala.position.y + Koala.HEIGHT + koala.velocity.y);
    } else {
        startY = endY = (int) (koala.position.y + koala.velocity.y);
    }
    startX = (int) (koala.position.x);
    endX = (int) (koala.position.x + Koala.WIDTH);
    getTiles(startX, startY, endX, endY, tiles);
    koalaRect.y += koala.velocity.y;
    for (Rectangle tile : tiles) {
        if (koalaRect.overlaps(tile)) {
            // we actually reset the koala y-position here
            // so it is just below/above the tile we collided with
            // this removes bouncing :)
            if (koala.velocity.y > 0) {
                koala.position.y = tile.y - Koala.HEIGHT;
                // we hit a block jumping upwards, let's destroy it!
                TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get("walls");
                layer.setCell((int) tile.x, (int) tile.y, null);
            } else {
                koala.position.y = tile.y + tile.height;
                // if we hit the ground, mark us as grounded so we can jump
                koala.grounded = true;
            }
            koala.velocity.y = 0;
            break;
        }
    }
    rectPool.free(koalaRect);

    // unscale the velocity by the inverse delta time and set
    // the latest position
    koala.position.add(koala.velocity);
    koala.velocity.scl(1 / deltaTime);

    // Apply damping to the velocity on the x-axis so we don't
    // walk infinitely once a key was pressed
    koala.velocity.x *= Koala.DAMPING;

}

From source file:MyGdxGame.java

License:Apache License

private boolean isTouched(float startX, float endX) {
    // check if any finge is touch the area between startX and endX
    // startX/endX are given between 0 (left edge of the screen) and 1 (right edge of the screen)
    for (int i = 0; i < 2; i++) {
        float x = Gdx.input.getX(i) / (float) Gdx.graphics.getWidth();
        if (Gdx.input.isTouched(i) && (x >= startX && x <= endX)) {
            return true;
        }/*from  w  w  w  . j  a  va  2  s.  c  om*/
    }
    return false;
}

From source file:OrbitingCameraController.java

License:Open Source License

/**
 * @return false, for further input handling
 * *//*from   ww  w.  ja  v  a2 s  . c o m*/
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    if (Gdx.input.isButtonPressed(Input.Buttons.LEFT)) {

        if (allowPitchMovement)
            pitch += (lastTouchY - (Gdx.graphics.getHeight() - screenY)) / 10;

    } else if (Gdx.input.isButtonPressed(Input.Buttons.MIDDLE)) {

        float dx = lastTouchX - screenX;
        float dy = lastTouchY - (Gdx.graphics.getHeight() - screenY);

        tempV3.set(camera.direction).crs(0, 1, 0).nor().scl(dx / 60f).scl(orbitRadius * .02f);
        pivotPoint.x += tempV3.x;
        pivotPoint.z += tempV3.z;

        tempV3.set(camera.direction).nor().scl(dy / 20f).scl(orbitRadius * .02f);
        pivotPoint.x += tempV3.x;
        pivotPoint.z += tempV3.z;

    } else if (Gdx.input.isButtonPressed(Input.Buttons.RIGHT)) {

        if (allowYawMovement)
            yaw += (lastTouchX - screenX) / 5f;

    }

    lastTouchX = screenX;
    lastTouchY = Gdx.graphics.getHeight() - screenY;
    return false;
}

From source file:ac.uk.dmu.ash.game.screen.CreditsScreen.java

License:Open Source License

/**
 * Creates a new {@link CreditsScreen}.//from www  .j  a v a  2s.c  o  m
 * @param game The game instance.
 * @param assets The game assets
 */
public CreditsScreen(final Game game, final AssetManager assets) {
    this.assets = assets;

    Gdx.input.setInputProcessor(stage);

    Table fpsTable = createTableWithLabel(assets, "ui_skin", Align.left, (Align.top | Align.left), "fps:");
    fpsLabel = (Label) fpsTable.getCells().get(0).getWidget();
    stage.addActor(fpsTable);

    Table creditsTable = createTableWithLabel(assets, "ui_skin", CREDITS);
    stage.addActor(creditsTable);

    TextButton backButton = createTextButton(assets, "ui_skin", "Back", new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            dispose();
            game.setScreen(new MainMenuScreen(game, assets));
        }
    });

    Table buttonsTable = createTable(Align.left | Align.bottom);
    buttonsTable.add(backButton);
    stage.addActor(buttonsTable);
}

From source file:airfoil.Main.java

License:Open Source License

@Override
public void create() {

    Gdx.input.setInputProcessor(this);

    ShaderProgram.pedantic = false;// w w w.ja va 2  s.c  om
    this.bodyShader = new ShaderProgram(Gdx.files.internal("data/shaders/body.vert.glsl").readString(),
            Gdx.files.internal("data/shaders/body.frag.glsl").readString());

    this.alive = this.bodyShader.isCompiled();
    if (!this.alive) {
        Gdx.app.error(Main.Title, "Error compiling body shader " + this.bodyShader.getLog());
        Gdx.app.exit();
    } else {
        this.axesShader = new ShaderProgram(Gdx.files.internal("data/shaders/axes.vert.glsl").readString(),
                Gdx.files.internal("data/shaders/axes.frag.glsl").readString());

        this.alive = this.axesShader.isCompiled();
        if (!this.alive) {
            Gdx.app.error(Main.Title, "Error compiling axes shader " + this.axesShader.getLog());
            Gdx.app.exit();
        }
    }
}

From source file:app.badlogicgames.superjumper.GameScreen.java

License:Apache License

private void updateReady() {
    if (Gdx.input.justTouched()) {
        state = GAME_RUNNING;
    }
}

From source file:app.badlogicgames.superjumper.GameScreen.java

License:Apache License

private void updateRunning(float deltaTime) {
    if (Gdx.input.justTouched()) {
        guiCam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));

        if (pauseBounds.contains(touchPoint.x, touchPoint.y)) {
            Assets.playSound(Assets.clickSound);
            state = GAME_PAUSED;//from  ww w.j  ava2  s . c  om
            return;
        }
    }

    ApplicationType appType = Gdx.app.getType();

    // should work also with Gdx.input.isPeripheralAvailable(Peripheral.Accelerometer)
    if (appType == ApplicationType.Android || appType == ApplicationType.iOS) {
        world.update(deltaTime, Gdx.input.getAccelerometerX());
    } else {
        float accel = 0;
        if (Gdx.input.isKeyPressed(Keys.DPAD_LEFT))
            accel = 5f;
        if (Gdx.input.isKeyPressed(Keys.DPAD_RIGHT))
            accel = -5f;
        world.update(deltaTime, accel);
    }
    if (world.score != lastScore) {
        lastScore = world.score;
        scoreString = "SCORE: " + lastScore;
    }
    if (world.state == World.WORLD_STATE_NEXT_LEVEL) {
        state = GAME_LEVEL_END;
    }
    if (world.state == World.WORLD_STATE_GAME_OVER) {
        state = GAME_OVER;
        if (lastScore >= Settings.highscores[4])
            scoreString = "NEW HIGHSCORE: " + lastScore;
        else
            scoreString = "SCORE: " + lastScore;
        Settings.addScore(lastScore);
        Settings.save();
    }
}

From source file:app.badlogicgames.superjumper.GameScreen.java

License:Apache License

private void updatePaused() {
    if (Gdx.input.justTouched()) {
        guiCam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));

        if (resumeBounds.contains(touchPoint.x, touchPoint.y)) {
            Assets.playSound(Assets.clickSound);
            state = GAME_RUNNING;//ww w . j av  a2s  . co  m
            return;
        }

        if (quitBounds.contains(touchPoint.x, touchPoint.y)) {
            Assets.playSound(Assets.clickSound);
            game.setScreen(new MainMenuScreen(game));
            return;
        }
    }
}

From source file:app.badlogicgames.superjumper.GameScreen.java

License:Apache License

private void updateLevelEnd() {
    if (Gdx.input.justTouched()) {
        world = new World(worldListener);
        renderer = new WorldRenderer(batcher, world);
        world.score = lastScore;/*from   w  ww  .  ja  v a2 s  .c o m*/
        state = GAME_READY;
    }
}

From source file:app.badlogicgames.superjumper.GameScreen.java

License:Apache License

private void updateGameOver() {
    if (Gdx.input.justTouched()) {
        game.setScreen(new MainMenuScreen(game));
    }
}