Пример #1
0
  @Override
  public void create() {
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();

    bitmapFont = new BitmapFont(Gdx.files.internal("data/arial-15.fnt"), false);

    Gdx.input.setInputProcessor(this);

    mCamPos = new Vector3();
    mCurrentPos = new Vector3();

    camera = new OrthographicCamera(100, 100 * h / w);
    camera.position.set(50, 50, 0);
    camera.zoom = 1.8f;
    camera.update();

    textCam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    textCam.position.set(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2, 0);
    textCam.zoom = 1;
    textCam.update();

    loader = new RubeSceneLoader();

    scene = loader.loadScene(Gdx.files.internal("data/palm.json"));

    debugRender = new Box2DDebugRenderer();

    batch = new SpriteBatch();
    polygonBatch = new PolygonSpriteBatch();

    textureMap = new HashMap<String, Texture>();
    textureRegionMap = new HashMap<Texture, TextureRegion>();

    createSpatialsFromRubeImages(scene);
    createPolySpatialsFromRubeFixtures(scene);

    mWorld = scene.getWorld();
    // configure simulation settings
    mVelocityIter = scene.velocityIterations;
    mPositionIter = scene.positionIterations;
    if (scene.stepsPerSecond != 0) {
      mSecondsPerStep = 1f / scene.stepsPerSecond;
    }
    mWorld.setContactListener(this);
    //
    // example of custom property handling
    //
    Array<Body> bodies = scene.getBodies();
    if ((bodies != null) && (bodies.size > 0)) {
      for (int i = 0; i < bodies.size; i++) {
        Body body = bodies.get(i);
        String gameInfo = (String) scene.getCustom(body, "GameInfo", null);
        if (gameInfo != null) {
          System.out.println("GameInfo custom property: " + gameInfo);
        }
      }
    }

    // Example of accessing data based on name
    System.out.println("body0 count: " + scene.getNamed(Body.class, "body0").size);
    // Note: the scene has two fixture9 names defined, but these are in turn subdivided into
    // multiple fixtures and thus appear several times...
    System.out.println("fixture9 count: " + scene.getNamed(Fixture.class, "fixture9").size);
    scene.printStats();

    //
    // validate the custom settings attached to world object..
    //
    boolean testBool = (Boolean) scene.getCustom(mWorld, "testCustomBool", false);
    int testInt = (Integer) scene.getCustom(mWorld, "testCustomInt", 0);
    float testFloat = (Float) scene.getCustom(mWorld, "testCustomFloat", 0);
    Color color = (Color) scene.getCustom(mWorld, "testCustomColor", null);
    Vector2 vec = (Vector2) scene.getCustom(mWorld, "testCustomVec2", null);
    String string = (String) scene.getCustom(mWorld, "testCustomString", null);

    if (testBool == false) {
      throw new GdxRuntimeException(
          "testCustomBool not read correctly! Expected: " + true + " Actual: " + testBool);
    }
    if (testInt != 8675309) {
      throw new GdxRuntimeException(
          "testCustomInt not read correctly! Expected: " + 8675309 + " Actual: " + testInt);
    }
    if (testFloat != 1.25f) {
      throw new GdxRuntimeException(
          "testCustomFloat not read correctly! Expected: " + 1.25f + " Actual: " + testFloat);
    }
    if (color == null) {
      throw new GdxRuntimeException("testCustomColor is reporting null!");
    }
    if ((color.r != 17f / 255)
        || (color.g != 29f / 255)
        || (color.b != 43f / 255)
        || (color.a != 61f / 255)) {
      throw new GdxRuntimeException(
          "testCustomColor not read correctly!  Expected: "
              + new Color(17f / 255, 29f / 255, 43f / 255, 61f / 255)
              + " Actual: "
              + color);
    }
    if (vec == null) {
      throw new GdxRuntimeException("testCustomVec2 is reporting null!");
    }
    if ((vec.x != 314159) || (vec.y != 21718)) {
      throw new GdxRuntimeException(
          "testCustomVec2 is not read correctly!  Expected: "
              + new Vector2(314159, 21718)
              + " Actual: "
              + vec);
    }
    if (string == null) {
      throw new GdxRuntimeException("testCustomString is reporting null!");
    }
    if (!string.equalsIgnoreCase("excelsior!")) {
      throw new GdxRuntimeException(
          "testCustomString is not read correctly!  Expected: Excelsior! Actual: " + string);
    }
    scene.clear(); // no longer need any scene references
  }
Пример #2
0
  @Override
  public void create() {
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();

    Gdx.input.setInputProcessor(this);

    mB2Controllers = new Array<B2Controller>();

    mCamPos = new Vector3();
    mCurrentPos = new Vector3();

    camera = new OrthographicCamera(100, 100 * h / w);
    camera.position.set(50, 50, 0);
    camera.zoom = 1.8f;
    camera.update();

    loader = new RubeSceneLoader();

    scene = loader.loadScene(Gdx.files.internal("data/palmcontrollers.json"));

    debugRender = new Box2DDebugRenderer();

    batch = new SpriteBatch();
    polygonBatch = new PolygonSpriteBatch();

    textureMap = new HashMap<String, Texture>();
    textureRegionMap = new HashMap<Texture, TextureRegion>();

    createSpatialsFromRubeImages(scene);
    createPolySpatialsFromRubeFixtures(scene);

    mWorld = scene.getWorld();
    // configure simulation settings
    mVelocityIter = scene.velocityIterations;
    mPositionIter = scene.positionIterations;
    if (scene.stepsPerSecond != 0) {
      mSecondsPerStep = 1f / scene.stepsPerSecond;
    }
    mWorld.setContactListener(this);

    //
    // example of custom property handling
    //
    Array<Body> bodies = scene.getBodies();
    if ((bodies != null) && (bodies.size > 0)) {
      for (int i = 0; i < bodies.size; i++) {
        Body body = bodies.get(i);
        String gameInfo = (String) scene.getCustom(body, "GameInfo", null);
        if (gameInfo != null) {
          System.out.println("GameInfo custom property: " + gameInfo);
        }
      }
    }

    // Instantiate any controllers that are in the scene
    Array<Fixture> fixtures = scene.getFixtures();
    if ((fixtures != null) && (fixtures.size > 0)) {
      for (int i = 0; i < fixtures.size; i++) {
        Fixture fixture = fixtures.get(i);
        int controllerType = (Integer) scene.getCustom(fixture, "ControllerType", 0);
        switch (controllerType) {
          case B2Controller.BUOYANCY_CONTROLLER:
            // only allow polygon buoyancy controllers for now..
            if (fixture.getShape().getType() == Shape.Type.Polygon) {
              float bodyHeight = fixture.getBody().getPosition().y;
              // B2BuoyancyController b2c = new B2BuoyancyController();

              // need to calculate the fluid surface height for the buoyancy controller
              PolygonShape shape = (PolygonShape) fixture.getShape();
              shape.getVertex(0, mTmp);
              float maxHeight =
                  mTmp.y + bodyHeight; // initialize the height, transforming to 'world'
              // coordinates

              // find the maxHeight
              for (int j = 1; j < shape.getVertexCount(); j++) {
                shape.getVertex(j, mTmp);
                maxHeight =
                    Math.max(maxHeight, mTmp.y + bodyHeight); // transform to world coordinates
              }
              B2BuoyancyController b2c =
                  new B2BuoyancyController(
                      B2BuoyancyController.DEFAULT_SURFACE_NORMAL, // assume up
                      (Vector2)
                          scene.getCustom(
                              fixture,
                              "ControllerVelocity",
                              B2BuoyancyController.DEFAULT_FLUID_VELOCITY),
                      mWorld.getGravity(),
                      maxHeight,
                      fixture.getDensity(),
                      (Float)
                          scene.getCustom(
                              fixture, "LinearDrag", B2BuoyancyController.DEFAULT_LINEAR_DRAG),
                      (Float)
                          scene.getCustom(
                              fixture, "AngularDrag", B2BuoyancyController.DEFAULT_ANGULAR_DRAG));
              fixture.setUserData(b2c); // reference back to the controller from the fixture (see
              // beginContact/endContact)
              mB2Controllers.add(b2c); // add it to the list so it can be stepped later
            }
            break;

          case B2Controller.GRAVITY_CONTROLLER:
            {
              B2GravityController b2c = new B2GravityController();
              b2c =
                  new B2GravityController(
                      (Vector2)
                          scene.getCustom(
                              fixture, "ControllerVelocity", B2GravityController.DEFAULT_GRAVITY));
              fixture.setUserData(b2c);
              mB2Controllers.add(b2c);
            }
            break;
        }
      }
    }
    scene.printStats();
    scene.clear(); // no longer need any scene references
  }