Exemplo n.º 1
0
 public void destroyLevel() {
   for (Obstacle obstacle : obs) {
     gameWorldPhysics.destroyBody(obstacle.getBody());
   }
   obs.clear();
   gameWorldPhysics.destroyBody(goal.getBody());
   goal = null;
 }
Exemplo n.º 2
0
  @Override
  public void dispose() {

    planetTexture.dispose();
    backgroundTexture.dispose();
    planetCoreTexture.dispose();
    ballTexture.dispose();
    batch.dispose();
    font.dispose();

    // disposing bodies
    world.destroyBody(planetCoreModel);
    world.destroyBody(planetModel);
    for (int i = 0; i < MAX_BALLS; i++) world.destroyBody(ballModels[i]);
    world.dispose();
  }
Exemplo n.º 3
0
  @Override
  public boolean touchDown(int x, int y, int pointer, int newParam) {
    // translate the mouse coordinates to world coordinates
    testPoint.set(x, y, 0);
    camera.unproject(testPoint);

    // ask the world which bodies are within the given
    // bounding box around the mouse pointer
    hitBody = null;
    world.QueryAABB(
        callback, testPoint.x - 0.1f, testPoint.y - 0.1f, testPoint.x + 0.1f, testPoint.y + 0.1f);

    // if we hit something we create a new mouse joint
    // and attach it to the hit body.
    if (hitBody != null) {
      MouseJointDef def = new MouseJointDef();
      def.bodyA = groundBody;
      def.bodyB = hitBody;
      def.collideConnected = true;
      def.target.set(testPoint.x, testPoint.y);
      def.maxForce = 1000.0f * hitBody.getMass();

      mouseJoint = (MouseJoint) world.createJoint(def);
      hitBody.setAwake(true);
    } else {
      for (Body box : boxes) world.destroyBody(box);
      boxes.clear();
      createBoxes();
    }

    return false;
  }
Exemplo n.º 4
0
 public void setCustomPortal(World world, float x, float y) {
   if (customsPortalBody != null) {
     world.destroyBody(customsPortalBody);
   }
   final Vector2 portalPos = new Vector2(x - offset.x, y - offset.y);
   portals.put("portal_custom", portalPos);
   customsPortalBody = addPortalBody(world, "portal_custom", portalPos);
 }
Exemplo n.º 5
0
  public void render(OrthographicCamera camera) {
    // update the world with a fixed time step
    world.step(Gdx.app.getGraphics().getDeltaTime() * 10, 8, 3);
    if (mouseJoint != null) mouseJoint.setTarget(bob.getBody().getWorldCenter());

    // Destroy mouseJoint? (drop item)
    if (destroyMousejoint == true) {
      if (!world.isLocked()) {
        world.destroyJoint(mouseJoint);
        mouseJoint = null;
        destroyMousejoint = false;
      }
    }

    // Delete any bodies up for deletion
    if (!bodiesToDelete.isEmpty()) {
      // Make sure it is safe to delete!!
      if (!world.isLocked()) {
        for (Body body : bodiesToDelete) {
          world.destroyBody(body);
          body.setUserData(null);
          body = null;
        }
        bodiesToDelete.clear(); // Don't forget to clear the null bodies!
      }
    }

    // Create any bodies up for creation
    if (!bodiesToCreate.isEmpty()) {
      // Make sure it is safe to create!!
      if (!world.isLocked()) {
        for (BodyDef body : bodiesToCreate) {
          world.createBody(body);
        }
        bodiesToCreate.clear(); // Don't forget to clear!
      }
    }

    // Create any joints up for creation
    if (!jointsToCreate.isEmpty()) {
      // Make sure it is safe to create!!
      if (!world.isLocked()) {
        for (JointDef body : jointsToCreate) {
          mouseJoint = (MouseJoint) world.createJoint(body);
        }
        jointsToCreate.clear(); // Don't forget to clear!
      }
    }
    // render the world using the debug renderer
    renderer.render(world, camera.combined);
  }
  @Override
  public void dispose() {
    body.setActive(false);
    body.setAwake(false);

    Iterator<Joint> joints = world.getJoints();

    while (joints.hasNext()) {
      Joint joint = joints.next();

      if (joint.getBodyA() == body || joint.getBodyB() == body) {
        world.destroyJoint(joint);
      }
    }

    world.destroyBody(body);
    body = null;
  }
Exemplo n.º 7
0
 private static void cleanRemovableBodies() {
   ///// WORLD Step////////CleanStuff//
   if (removableBodies.size > 0) {
     boolean clearRemovable = false;
     for (Body body : removableBodies) {
       body.setActive(
           false); // Body wont be active while it waits deletion; This must happen outside the
                   // Step process;
       if (!WORLD.isLocked()) {
         WORLD.destroyBody(body);
         clearRemovable = true;
       }
     }
     if (clearRemovable) {
       removableBodies.clear();
       // collisionPairsDictionary.clear();
     }
   }
   ///// WORLD Step////////CleanStuff//
 }
Exemplo n.º 8
0
  public void defineBigMario() {
    Vector2 marioPosition = b2body.getPosition();
    world.destroyBody(b2body);

    BodyDef bDef = new BodyDef();
    bDef.position.set(marioPosition.add(0 / MarioBros.PPM, 10 / MarioBros.PPM));
    bDef.type = BodyDef.BodyType.DynamicBody;
    b2body = world.createBody(bDef);

    FixtureDef fdef = new FixtureDef();
    CircleShape shape = new CircleShape();
    shape.setRadius(6 / MarioBros.PPM);
    fdef.filter.categoryBits = MarioBros.MARIO_BIT;
    fdef.filter.maskBits =
        MarioBros.GROUND_BIT
            | MarioBros.COIN_BIT
            | MarioBros.BRICK_BIT
            | MarioBros.ENEMY_BIT
            | MarioBros.OBJECT_BIT
            | MarioBros.ENEMY_HEAD_BIT
            | MarioBros.ITEM_BIT;

    fdef.shape = shape;
    b2body.createFixture(fdef);
    shape.setPosition(new Vector2(0, -14 / MarioBros.PPM));
    fdef.shape = shape;
    b2body.createFixture(fdef).setUserData(this);

    EdgeShape head = new EdgeShape();
    head.set(
        new Vector2(-2 / MarioBros.PPM, 6 / MarioBros.PPM),
        new Vector2(2 / MarioBros.PPM, 6 / MarioBros.PPM));
    fdef.filter.categoryBits = MarioBros.MARIO_HEAD_BIT;
    fdef.filter.maskBits = MarioBros.BRICK_BIT | MarioBros.COIN_BIT;
    fdef.shape = head;
    fdef.isSensor = true;

    b2body.createFixture(fdef).setUserData(this);

    timeToDefineBigMario = false;
  }
Exemplo n.º 9
0
  public void update(float dt) {
    handleInput();

    world.step(dt, 6, 2);

    Array<Body> bodies = cl.getBodiesToKill();
    for (int i = 0; i < bodies.size; i++) {
      Body b = bodies.get(i);
      coins.removeValue((Coin) b.getUserData(), true);
      world.destroyBody(b);
      player.collectCoin();
    }
    bodies.clear();
    player.update(dt);
    for (int i = 0; i < coins.size; i++) {
      coins.get(i).update(dt);
    }
    if (player.getBody().getLinearVelocity().x > 0) {
      Game.ass.loadTexture("res/images/bunny.png", "bunny");
    } else if (player.getBody().getLinearVelocity().x < 0) {
      Game.ass.loadTexture("res/images/bunny2.png", "bunny");
    }
  }
Exemplo n.º 10
0
 private void createGraphVisible() {
   if (graphVisibleBodies == null) graphVisibleBodies = new ArrayList<Body>();
   for (int i = 0; i < graphVisibleBodies.size(); i++)
     world.destroyBody(graphVisibleBodies.get(i));
   graphVisibleBodies.clear();
   if (nodesVisible)
     for (Vector2 node : movementGraph.vertexSet())
       PhysicsHelper.createCircle(world, BodyType.StaticBody, .1f, 1, FactionType.NEUTRAL)
           .setTransform(node, 0);
   if (edgesVisible)
     for (DefaultWeightedEdge edge : movementGraph.edgeSet()) {
       Vector2 source = movementGraph.getEdgeSource(edge),
           target = movementGraph.getEdgeTarget(edge);
       PhysicsHelper.createEdge(
           world,
           BodyType.StaticBody,
           source.x,
           source.y,
           target.x,
           target.y,
           1,
           FactionType.NEUTRAL);
     }
 }
Exemplo n.º 11
0
 public void destroy() {
   if (body != null) {
     world.destroyBody(body);
     body = null;
   }
 }
Exemplo n.º 12
0
 public void destroyBody(WorldBody body) {
   world.destroyBody(body.getBox2DBody());
 }
Exemplo n.º 13
0
    void update(float delta) {
      world.step(delta, 6, 300);
      // detect allowed for player space
      if (playerBody.getPosition().x > frontEdge) {
        frontEdge = playerBody.getPosition().x;
        backEdge = frontEdge - (16 + 4) < 0 ? 0 : frontEdge - (16 + 4);
      }

      // broadcast player position
      shared.pPos.update(pos -> pos.set(playerBody.getPosition()));

      // broadcast player cable blocks position
      for (int idx = 0; idx < cable.getBodyList().size(); idx++) {
        final Vector2 blockPos = cable.getBodyList().get(idx).getPosition();
        shared.cableDots.update(idx, pos -> pos.set(blockPos));
      }

      // broadcast bots positions
      bots.entrySet()
          .forEach(
              (kv) -> {
                int id = kv.getKey();
                shared.bots.update(id, data -> data.pos.set(kv.getValue().getPosition()));
              });

      if (playerInAttack) {
        // raycast via bots
        Vector2 fireTo = new Vector2();
        Vector2 pPos = playerBody.getPosition();

        if (shared.pDirection.get().dirX == DirectionX.RIGHT) fireTo.x = pPos.x + 8;
        else fireTo.x = pPos.x - 8;

        switch (shared.pDirection.get().dirY) {
          case UP:
            fireTo.y = pPos.y + 8;
            break;
          case MIDDLE:
            fireTo.y = pPos.y;
            break;
          case DOWN:
            fireTo.y = pPos.y - 8;
            break;
        }

        world.rayCast(
            (fixture, point, normal, fraction) -> {
              if (fixture.getUserData() == null) return -1;

              if (fixture.getUserData().getClass() == BotUserData.class) {
                BotUserData data = (BotUserData) fixture.getUserData();
                data.health -= delta;
                // System.out.print("Bot with id:" + data.id + "on damage!");
                if (data.health < 0) {
                  if (!data.killed) {
                    System.out.println("Bot with id:" + data.id + "died!");
                    botKilled.write(
                        data.id,
                        () -> {
                          System.out.println("World locked? =>" + world.isLocked());
                          Body botBody = bots.get(data.id);
                          world.destroyBody(botBody);
                          bots.remove(data.id);
                          shared.bots.free(data.id);
                        });
                    data.killed = true;
                  }
                }
              }

              return -1;
            },
            pPos,
            fireTo);
      }
    }
Exemplo n.º 14
0
 @Override
 public void finish() {
   world.destroyBody(physicalBody);
 }
Exemplo n.º 15
0
  @Override
  public void draw() {
    Gdx.gl.glClearColor(0, 0.2f, 1, 1);
    camera.position.interpolate(
        new Vector3(camera.position.x, camY, 0), 0.1f, Interpolation.pow2Out);
    camera.update();
    batch.setProjectionMatrix(camera.combined);

    if (!pause) world.step(1 / 60f, 8, 3);

    world.getBodies(bodies);
    batch.begin();
    for (i = 0; i < 5; i++) {
      if (backgrounds[i]
          .getBoundingRectangle()
          .overlaps(
              new Rectangle(
                  camera.position.x - camera.viewportWidth / 2,
                  camera.position.y - camera.viewportHeight / 2,
                  camera.viewportWidth,
                  camera.viewportHeight))) {
        backgrounds[i].draw(batch);
      }
    }
    traySprite.draw(batch);
    mpHandler.update(
        Gdx.graphics.getDeltaTime(), true, batch, tap, new Vector2(unprojected.x, unprojected.y));
    for (i = 0; i < bodies.size; i++) {
      scbd = bodies.get(i);
      if (scbd != tmpbd) {
        bodyY.add(scbd.getPosition().y);
      }
      if (scbd.getWorldCenter().y < -2
          || scbd.getWorldCenter().x < -1.3f
          || scbd.getWorldCenter().x > 6.5f) {
        mpHandler.removeFromBody(scbd);
        world.destroyBody(scbd);
        if (!gameOver) {
          if (score > MainClass.highScore) {
            newHighScoreSound.play(MainClass.sound);
          } else {
            gameOverSound.play(MainClass.sound);
          }
          gameOver = true;
          scoreLabel.addAction(new AlphaAction());
          muffinSource.remove();
          pauseButton.remove();
          gameOverDialog.show(this, score);
        }
      }
      if (scbd.getType() == BodyDef.BodyType.DynamicBody) {
        AdjustToBody(muffinSprite, muffinOrigin, scbd, 1.228f, 1);
        muffinSprite.draw(batch);
        if (scbd.isAwake()) {
          if (Math.abs(scbd.getLinearVelocity().len2()) > 10) {
            AdjustToBody(scaredmuffinface, muffinOrigin, scbd, 1.228f, 1);
            scaredmuffinface.draw(batch);
          } else {
            AdjustToBody(awakemuffinface, muffinOrigin, scbd, 1.228f, 1);
            awakemuffinface.draw(batch);
          }
        } else {
          AdjustToBody(normalmuffinface, muffinOrigin, scbd, 1.228f, 1);
          normalmuffinface.draw(batch);
        }
      }
    }
    batch.end();
    camY = 0;
    if (bodyY.size > 5) {
      bodyY.sort();
      for (i = bodyY.size - 3; i < bodyY.size; i++) {
        camY += bodyY.get(i);
      }
      camY /= 3;
      camY += 2;
    }
    if (camY < 4) camY = 4;
    if (drag) {
      tmpbd.setLinearVelocity(
          (unprojected.x - 0.5f - tmpbd.getPosition().x) * 20,
          (unprojected.y - 0.5f - tmpbd.getPosition().y) * 20);
    }
    super.draw();
    bodies.clear();
    bodyY.clear();
  }
Exemplo n.º 16
0
 public void remover() {
   mundo.destroyBody(corpoCima);
   mundo.destroyBody(corpoBaixo);
 }