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

The following examples show how to use com.badlogic.gdx.physics.box2d.Body#getWorld() . 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: BombSystem.java    From Bomberman_libGdx with MIT License 6 votes vote down vote up
private boolean checkMovable(Body body, Vector2 from, Vector2 to) {
    World b2dWorld = body.getWorld();
    moveable = true;

    RayCastCallback rayCastCallback = new RayCastCallback() {
        @Override
        public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {
            if (fixture.getFilterData().categoryBits == GameManager.INDESTRUCTIIBLE_BIT
                    | fixture.getFilterData().categoryBits == GameManager.BREAKABLE_BIT
                    | fixture.getFilterData().categoryBits == GameManager.BOMB_BIT
                    | fixture.getFilterData().categoryBits == GameManager.ENEMY_BIT
                    | fixture.getFilterData().categoryBits == GameManager.PLAYER_BIT) {
                moveable = false;
                return 0;
            }
            return 0;
        }
    };

    b2dWorld.rayCast(rayCastCallback, from, to);
    return moveable;
}
 
Example 2
Source File: PlayerSystem.java    From Pacman_libGdx with MIT License 5 votes vote down vote up
private boolean checkMovable(Body body, MoveDir dir) {
    canMove = true;
    World world = body.getWorld();

    RayCastCallback rayCastCallback = new RayCastCallback() {
        @Override
        public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {
            if (fixture.getFilterData().categoryBits == GameManager.WALL_BIT || fixture.getFilterData().categoryBits == GameManager.GATE_BIT) {
                canMove = false;
                return 0;
            }
            return 0;
        }
    };

    for (int i = 0; i < 2; i++) {
        tmpV1.set(body.getPosition());
        switch (dir) {
            case UP:
                tmpV2.set(body.getPosition().x - (i - 0.5f) * 0.2f, body.getPosition().y + 0.6f);
                break;
            case DOWN:
                tmpV2.set(body.getPosition().x - (i - 0.5f) * 0.2f, body.getPosition().y - 0.6f);
                break;
            case LEFT:
                tmpV2.set(body.getPosition().x - 0.6f, body.getPosition().y - (i - 0.5f) * 0.2f);
                break;
            case RIGHT:
                tmpV2.set(body.getPosition().x + 0.6f, body.getPosition().y - (i - 0.5f) * 0.2f);
                break;
            default:
                break;
        }

        world.rayCast(rayCastCallback, tmpV1, tmpV2);
    }

    return canMove;
}
 
Example 3
Source File: EnemySystem.java    From Bomberman_libGdx with MIT License 5 votes vote down vote up
protected boolean hitSomethingVertical(final Body body, Vector2 fromV, Vector2 toV) {
    World b2dWorld = body.getWorld();
    hit = false;

    RayCastCallback rayCastCallback = new RayCastCallback() {

        @Override
        public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {
            // if hit the player, ignore it
            if (fixture.getFilterData().categoryBits == GameManager.PLAYER_BIT || fixture.getFilterData().categoryBits == GameManager.POWERUP_BIT) {
                return 0;
            }

            if (fraction < 1.0f) {
                hit = true;
            }
            return 0;
        }
    };

    for (int i = 0; i < 3; i++) {
        Vector2 tmpV = new Vector2(toV);
        b2dWorld.rayCast(rayCastCallback, fromV, tmpV.add((1 - i) * 0.4f, 0));

    }
    return hit;
}
 
Example 4
Source File: EnemySystem.java    From Bomberman_libGdx with MIT License 5 votes vote down vote up
protected boolean hitSomethingHorizontal(final Body body, Vector2 fromV, Vector2 toV) {
    World b2dWorld = body.getWorld();
    hit = false;

    RayCastCallback rayCastCallback = new RayCastCallback() {

        @Override
        public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {
            // if hit the player or power-up item, ignore it
            if (fixture.getFilterData().categoryBits == GameManager.PLAYER_BIT || fixture.getFilterData().categoryBits == GameManager.POWERUP_BIT) {
                return 0;
            }

            if (fraction < 1.0f) {
                hit = true;
            }
            return 0;
        }
    };

    for (int i = 0; i < 3; i++) {
        Vector2 tmpV = new Vector2(toV);
        b2dWorld.rayCast(rayCastCallback, fromV, tmpV.add(0, (1 - i) * 0.4f));

    }
    return hit;
}
 
Example 5
Source File: BombSystem.java    From Bomberman_libGdx with MIT License 4 votes vote down vote up
@Override
protected void process(int entityId) {
    Bomb bomb = mBomb.get(entityId);
    State state = mState.get(entityId);
    RigidBody rigidBody = mRigidBody.get(entityId);

    Body body = rigidBody.body;

    bomb.countDown -= world.getDelta();

    if (bomb.countDown <= 0) {
        // explode
        bomb.state = Bomb.State.EXPLODING;
    }

    switch (bomb.state) {
        case EXPLODING:
            state.setCurrentState("exploding");
            GameManager.getInstance().playSound("Explosion.ogg", 1.0f, MathUtils.random(0.6f, 0.8f), 0);
            // create explosion
            ActorBuilder actorBuilder = ActorBuilder.init(body.getWorld(), world);
            actorBuilder.createExplosion(body.getPosition().x, body.getPosition().y, bomb.power);

            // destroy itself
            World b2dWorld = body.getWorld();
            b2dWorld.destroyBody(body);
            world.delete(entityId);
            break;
        case MOVING_UP:
            if (checkMovable(body, fromV.set(body.getPosition()), toV.set(body.getPosition().x, body.getPosition().y + 0.55f))) {
                body.setLinearVelocity(0, bomb.speed);
            } else {
                body.setLinearVelocity(0, 0);
                body.setTransform(MathUtils.floor(body.getPosition().x) + 0.5f, MathUtils.floor(body.getPosition().y) + 0.5f, 0);
                bomb.state = Bomb.State.NORMAL;
            }
            break;
        case MOVING_DOWN:
            if (checkMovable(body, fromV.set(body.getPosition()), toV.set(body.getPosition().x, body.getPosition().y - 0.55f))) {
                body.setLinearVelocity(0, -bomb.speed);
            } else {
                body.setLinearVelocity(0, 0);
                body.setTransform(MathUtils.floor(body.getPosition().x) + 0.5f, MathUtils.floor(body.getPosition().y) + 0.5f, 0);
                bomb.state = Bomb.State.NORMAL;
            }
            break;
        case MOVING_LEFT:
            if (checkMovable(body, fromV.set(body.getPosition()), toV.set(body.getPosition().x - 0.55f, body.getPosition().y))) {
                body.setLinearVelocity(-bomb.speed, 0);
            } else {
                body.setLinearVelocity(0, 0);
                body.setTransform(MathUtils.floor(body.getPosition().x) + 0.5f, MathUtils.floor(body.getPosition().y) + 0.5f, 0);
                bomb.state = Bomb.State.NORMAL;
            }
            break;

        case MOVING_RIGHT:
            if (checkMovable(body, fromV.set(body.getPosition()), toV.set(body.getPosition().x + 0.55f, body.getPosition().y))) {
                body.setLinearVelocity(bomb.speed, 0);
            } else {
                body.setLinearVelocity(0, 0);
                body.setTransform(MathUtils.floor(body.getPosition().x) + 0.5f, MathUtils.floor(body.getPosition().y) + 0.5f, 0);
                bomb.state = Bomb.State.NORMAL;
            }
            break;
        case NORMAL:
        default:
            state.setCurrentState("normal");
            break;
    }
}