Java Code Examples for com.badlogic.gdx.physics.box2d.Body#applyLinearImpulse()

The following examples show how to use com.badlogic.gdx.physics.box2d.Body#applyLinearImpulse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PlayerSystem.java    From Pacman_libGdx with MIT License 5 votes vote down vote up
@Override
protected void processEntity(Entity entity, float deltaTime) {
    PlayerComponent player = playerM.get(entity);
    StateComponent state = stateM.get(entity);
    MovementComponent movement = movementM.get(entity);
    Body body = movement.body;

    if (player.hp > 0) {
        if ((Gdx.input.isKeyPressed(Input.Keys.D) || Gdx.input.isKeyPressed(Input.Keys.RIGHT)) && checkMovable(body, MoveDir.RIGHT)) {
            body.applyLinearImpulse(tmpV1.set(movement.speed, 0).scl(body.getMass()), body.getWorldCenter(), true);

        } else if ((Gdx.input.isKeyPressed(Input.Keys.A) || Gdx.input.isKeyPressed(Input.Keys.LEFT)) && checkMovable(body, MoveDir.LEFT)) {
            body.applyLinearImpulse(tmpV1.set(-movement.speed, 0).scl(body.getMass()), body.getWorldCenter(), true);

        } else if ((Gdx.input.isKeyPressed(Input.Keys.W) || Gdx.input.isKeyPressed(Input.Keys.UP)) && checkMovable(body, MoveDir.UP)) {
            body.applyLinearImpulse(tmpV1.set(0, movement.speed).scl(body.getMass()), body.getWorldCenter(), true);

        } else if ((Gdx.input.isKeyPressed(Input.Keys.S) || Gdx.input.isKeyPressed(Input.Keys.DOWN))&& checkMovable(body, MoveDir.DOWN)) {
            body.applyLinearImpulse(tmpV1.set(0, -movement.speed).scl(body.getMass()), body.getWorldCenter(), true);

        }

        if (body.getLinearVelocity().len2() > movement.speed * movement.speed) {
            body.setLinearVelocity(body.getLinearVelocity().scl(movement.speed / body.getLinearVelocity().len()));
        }
    }

    // player is invincible for 3 seconds when spawning
    if (GameManager.instance.playerIsInvincible) {
        player.invincibleTimer += deltaTime;
        if (player.invincibleTimer >= 3.0f) {
            GameManager.instance.playerIsInvincible = false;
            player.invincibleTimer = 0;
        }
    }

    player.playerAgent.update(deltaTime);
    state.setState(player.currentState);
}
 
Example 2
Source File: EnemySystem.java    From Bomberman_libGdx with MIT License 4 votes vote down vote up
private void handleBoss1(int entityId) {
    Body body = rigidBody.body;
    ActorBuilder actorBuilder = ActorBuilder.init(body.getWorld(), world);

    if (boss1TargetCorners[boss1CurrentTarget].dst2(body.getPosition()) < 0.1f) {
        boss1CurrentTarget = MathUtils.random(0, 3);
    }

    if (enemy.receivedDamage > 0) {
        if (enemy.state != Enemy.State.DAMAGED) {
            enemy.damage(1);    // boss only take 1 damage per time

            // chance to create PowerUp item
            if (Math.random() < 0.2) {
                actorBuilder.createPowerUp(body.getPosition().x, body.getPosition().y - 2f);
            }
        }
        enemy.receivedDamage = 0;
        enemy.state = Enemy.State.DAMAGED;
    }

    if (enemy.hp <= 0) {
        enemy.state = Enemy.State.DYING;
    }

    enemy.lifetime += world.getDelta();

    // Boss1 attack
    if (enemy.hp > 0 && MathUtils.random() < 0.005) {
        enemy.state = Enemy.State.ATTACKING_DOWN;
    }

    switch (enemy.state) {
        case ATTACKING_LEFT:
        case ATTACKING_RIGHT:
        case ATTACKING_UP:
        case ATTACKING_DOWN:
            state.setCurrentState("attacking_down");
            if (state.getStateTime() > 0.6f) {
                actorBuilder.createExplosion(body.getPosition().x, body.getPosition().y - 4f, 1);
                GameManager.getInstance().playSound("Boss1Hammer.ogg");
                changeWalkingState(enemy);
            }
            break;
        case DAMAGED:
            state.setCurrentState("damaged");
            if (state.getStateTime() > 0.2f) {
                changeWalkingState(enemy);
            }
            break;
        case DYING:
            state.setCurrentState("dying");
            Filter filter = body.getFixtureList().get(0).getFilterData();
            filter.maskBits = GameManager.NOTHING_BIT;
            body.getFixtureList().get(0).setFilterData(filter);

            if (state.getStateTime() <= 0) {
                actorBuilder.createBoss1Explosion(body.getPosition().x, body.getPosition().y);
                enemy.lifetime = 0;
            }

            if (enemy.lifetime > 0.2f) {
                GameManager.getInstance().playSound("Explosion.ogg", 1.0f, MathUtils.random(0.9f, 1.1f), 0);
                enemy.lifetime -= 0.4f;
            }

            if (state.getStateTime() > 2.2f) {
                // decrease enemy count
                GameManager.enemiesLeft--;

                // if no enemy left, create the portal
                if (GameManager.enemiesLeft <= 0) {
                    actorBuilder.createPortal();
                    GameManager.getInstance().playSound("PortalAppears.ogg");
                    GameManager.getInstance().playMusic("Victory.ogg", false);
                }

                body.getWorld().destroyBody(body);
                world.delete(entityId);
            }
            break;
        case WALKING_LEFT:
        case WALKING_RIGHT:
        case WALKING_UP:
        case WALKING_DOWN:
        default:
            state.setCurrentState("walking_down");
            toVector.set(boss1TargetCorners[boss1CurrentTarget]);
            toVector.sub(body.getPosition());
            toVector.nor();

            if (body.getLinearVelocity().len2() < enemy.getSpeed() * enemy.getSpeed()) {
                body.applyLinearImpulse(toVector.scl(enemy.getSpeed()), body.getWorldCenter(), true);
            }
            break;
    }
}