Esempio n. 1
0
  private void addFace(final Scene pScene, final float pX, final float pY) {
    this.mFaceCount++;

    final AnimatedSprite face;
    final Body body;

    final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);

    if (this.mFaceCount % 2 == 0) {
      face = new AnimatedSprite(pX, pY, this.mBoxFaceTextureRegion);
      body =
          PhysicsFactory.createBoxBody(
              this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
    } else {
      face = new AnimatedSprite(pX, pY, this.mCircleFaceTextureRegion);
      body =
          PhysicsFactory.createCircleBody(
              this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
    }

    face.setUpdatePhysics(false);

    pScene.getTopLayer().addEntity(face);
    this.mPhysicsWorld.registerPhysicsConnector(
        new PhysicsConnector(face, body, true, true, false, false));
  }
  @Override
  public Scene onLoadScene() {
    mEngine.registerUpdateHandler(new FPSLogger());

    final Scene scene = new Scene(2);
    scene.setBackground(new ColorBackground(0, 0, 0));
    scene.setOnSceneTouchListener(this);

    mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);

    final Shape ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2);
    final Shape roof = new Rectangle(0, 0, CAMERA_WIDTH, 2);
    final Shape left = new Rectangle(0, 0, 2, CAMERA_HEIGHT);
    final Shape right = new Rectangle(CAMERA_WIDTH - 2, 0, 2, CAMERA_HEIGHT);

    final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
    PhysicsFactory.createBoxBody(mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);

    scene.getFirstChild().attachChild(ground);
    scene.getFirstChild().attachChild(roof);
    scene.getFirstChild().attachChild(left);
    scene.getFirstChild().attachChild(right);

    scene.registerUpdateHandler(mPhysicsWorld);

    scene.setOnAreaTouchListener(this);

    return scene;
  }
  private void addFace(final float pX, final float pY) {
    final Scene scene = mEngine.getScene();

    mFaceCount++;

    final AnimatedSprite face;
    final Body body;

    final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);

    if (mFaceCount % 2 == 0) {
      face = new AnimatedSprite(pX, pY, mBoxFaceTextureRegion);
      body =
          PhysicsFactory.createBoxBody(mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
    } else {
      face = new AnimatedSprite(pX, pY, mCircleFaceTextureRegion);
      body =
          PhysicsFactory.createCircleBody(
              mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
    }

    face.animate(200, true);

    scene.registerTouchArea(face);
    scene.getLastChild().attachChild(face);
    mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));
  }
Esempio n. 4
0
  private void addObstacle(final Scene pScene, final float pX, final float pY) {
    final Sprite box = new Sprite(pX, pY, 32, 32, this.mBoxTextureRegion);

    final FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(0.1f, 0.5f, 0.5f);
    final Body boxBody =
        PhysicsFactory.createBoxBody(this.mPhysicsWorld, box, BodyType.DynamicBody, boxFixtureDef);
    boxBody.setLinearDamping(10);
    boxBody.setAngularDamping(10);

    this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(box, boxBody, true, true));

    pScene.getChild(LAYER_OBSTACLES).attachChild(box);
  }
Esempio n. 5
0
  private void addFace(final float pX, final float pY) {
    final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);

    final AnimatedSprite face = new AnimatedSprite(pX, pY, this.mBoxFaceTextureRegion).animate(100);
    final Body body =
        PhysicsFactory.createBoxBody(
            this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);

    this.mScene.attachChild(face);
    this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));

    if (this.mFaceCount == 0) {
      this.mChaseCamera.setChaseEntity(face);
    }

    this.mFaceCount++;
  }
Esempio n. 6
0
  private void initLevelBorders(final Scene pScene) {
    final Shape topOuter = new Rectangle(0, 0, LEVEL_WIDTH * 2, 2);
    final Shape bottomOuter = new Rectangle(0, LEVEL_HEIGHT - 2, LEVEL_WIDTH * 2, 2);
    final Shape leftOuter = new Rectangle(0, 0, 2, LEVEL_HEIGHT);
    final Shape rightOuter = new Rectangle(LEVEL_WIDTH * 2 - 2, 0, 2, LEVEL_HEIGHT);

    final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
    PhysicsFactory.createBoxBody(
        this.mPhysicsWorld, bottomOuter, BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, topOuter, BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(
        this.mPhysicsWorld, leftOuter, BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(
        this.mPhysicsWorld, rightOuter, BodyType.StaticBody, wallFixtureDef);

    final IEntity firstChild = pScene.getChild(LAYER_BORDERS);
    firstChild.attachChild(bottomOuter);
    firstChild.attachChild(topOuter);
    firstChild.attachChild(leftOuter);
    firstChild.attachChild(rightOuter);
  }
Esempio n. 7
0
  @Override
  public Scene onLoadScene() {
    final Scene scene = new Scene(2, true, 4, (COUNT_VERTICAL - 1) * (COUNT_HORIZONTAL - 1));
    scene.setBackground(new ColorBackground(0, 0, 0));
    scene.setOnSceneTouchListener(this);

    this.mPhysicsWorld =
        new PhysicsWorld(
            new Vector2(
                0, 2 * SensorManager.GRAVITY_EARTH / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT),
            false);

    final Shape ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2);
    final Shape roof = new Rectangle(0, 0, CAMERA_WIDTH, 2);
    final Shape left = new Rectangle(0, 0, 2, CAMERA_HEIGHT);
    final Shape right = new Rectangle(CAMERA_WIDTH - 2, 0, 2, CAMERA_HEIGHT);

    final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);

    scene.getBottomLayer().addEntity(ground);
    scene.getBottomLayer().addEntity(roof);
    scene.getBottomLayer().addEntity(left);
    scene.getBottomLayer().addEntity(right);

    for (int x = 1; x < COUNT_HORIZONTAL; x++) {
      for (int y = 1; y < COUNT_VERTICAL; y++) {
        final float pX = (((float) CAMERA_WIDTH) / COUNT_HORIZONTAL) * x + y;
        final float pY = (((float) CAMERA_HEIGHT) / COUNT_VERTICAL) * y;
        this.addFace(scene, pX - 16, pY - 16);
      }
    }

    scene.registerUpdateHandler(
        new TimerHandler(
            2,
            new ITimerCallback() {
              @Override
              public void onTimePassed(final TimerHandler pTimerHandler) {
                scene.unregisterUpdateHandler(pTimerHandler);
                scene.registerUpdateHandler(PhysicsBenchmark.this.mPhysicsWorld);
                scene.registerUpdateHandler(
                    new TimerHandler(
                        10,
                        new ITimerCallback() {
                          @Override
                          public void onTimePassed(TimerHandler pTimerHandler) {
                            PhysicsBenchmark.this.mPhysicsWorld.setGravity(
                                new Vector2(
                                    0,
                                    -SensorManager.GRAVITY_EARTH
                                        / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT));
                          }
                        }));
              }
            }));

    return scene;
  }
Esempio n. 8
0
public class Barrier {

  private Scene scene;
  private PhysicsWorld world;
  private Engine engine;
  private static final FixtureDef BRICK_DEF = PhysicsFactory.createFixtureDef(1f, 0.01f, 10f);

  private ArrayList<Vector2> locs = new ArrayList<Vector2>(10);

  public Barrier(Scene scene, PhysicsWorld world, Engine engine) {
    setScene(scene);
    setWorld(world);
    setEngine(engine);
  }

  public Engine getEngine() {
    return engine;
  }

  public void setEngine(Engine engine) {
    this.engine = engine;
  }

  public PhysicsWorld getWorld() {
    return world;
  }

  public void setWorld(PhysicsWorld world) {
    this.world = world;
  }

  public ArrayList<Vector2> getLocs() {
    return locs;
  }

  public void setLocs(ArrayList<Vector2> locs) {
    this.locs = locs;
  }

  public void addBlock(float x, float y, TiledTextureRegion tr) {
    locs.add(new Vector2(x, y));
    final ImmoveableObject block =
        new ImmoveableObject(x, y, tr, getEngine(), getScene(), getWorld());

    block.setBody(PhysicsFactory.createBoxBody(getWorld(), block, BodyType.DynamicBody, BRICK_DEF));

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.isSensor = true;
    PolygonShape boxShape = new PolygonShape();
    boxShape.setAsBox(block.getWidth() / 2, block.getHeight() / 2);
    fixtureDef.shape = boxShape;
    block.getBody().createFixture(fixtureDef);

    getScene().attachChild(block);
    getWorld().registerPhysicsConnector(new PhysicsConnector(block, block.getBody(), true, true));
  }

  public Scene getScene() {
    return scene;
  }

  public void setScene(Scene scene) {
    this.scene = scene;
  }
}