コード例 #1
0
 public void updateQuad() {
   quad.clear();
   for (Entity entity : allEntities) {
     if (entity != null) {
       ArrayList<BoundingShape> bs = entity.getBoundingShapes();
       for (int j = 0; j < bs.size(); j++) {
         quad.insert(bs.get(j));
       }
     }
   }
 }
コード例 #2
0
  @Override
  public void render(float delta) {
    // update and draw stuff
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    camera.update();
    batch.setProjectionMatrix(camera.combined);
    shapes.setProjectionMatrix(camera.combined);

    shapes.begin(ShapeType.Line);
    shapes.setColor(Color.RED);
    for (int x = 0; x < Gdx.graphics.getWidth(); x += Constants.TILE_SIZE) {
      shapes.line(x, 0, x, Gdx.graphics.getHeight());
    }
    for (int y = 0; y < Gdx.graphics.getHeight(); y += Constants.TILE_SIZE) {
      shapes.line(0, y, Gdx.graphics.getWidth(), y);
    }

    shapes.end();

    shapes.begin(ShapeType.Filled);
    shapes.setColor(Color.CYAN);
    for (Entity entity : allEntities) {
      if (entity != null) {
        for (BoundingShape b : entity.getBoundingShapes()) {
          Rectangle2D bounds = b.getShape().getBounds2D();
          shapes.rect(
              (float) bounds.getX() + entity.getX(),
              (float) bounds.getY() + entity.getY(),
              (float) bounds.getWidth(),
              (float) bounds.getHeight());
        }
      }
    }

    shapes.setColor(Color.GREEN);
    for (int x = 0; x < staticGrid.length; x++) {
      for (int y = 0; y < staticGrid[0].length; y++) {
        if (staticGrid[x][y] == 1)
          shapes.rect(
              x * Constants.TILE_SIZE,
              y * Constants.TILE_SIZE,
              Constants.TILE_SIZE,
              Constants.TILE_SIZE);
      }
    }
    shapes.end();

    batch.begin();

    for (Entity entity : allEntities) {
      if (entity != null) {
        if (entity.getSprite() != null) {
          batch.draw(entity.getSprite(), entity.getX(), entity.getY());
        }
      }
    }
    // batch.draw(player.getSprite(), player.getSprite().getX(),
    // player.getSprite().getY(),player.getSprite().getOriginX(),
    //            player.getSprite().getOriginY(),
    //
    // player.getSprite().getWidth(),player.getSprite().getHeight(),player.getSprite().getScaleX(),player.
    //                     getSprite().getScaleY(),player.getSprite().getRotation());

    batch.end();

    batch.begin();
    font.draw(batch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 50, 50);
    font.draw(batch, "Mouse X: " + Gdx.input.getX(), 50, 80);
    font.draw(batch, "Mouse Y: " + Gdx.input.getY(), 50, 110);
    batch.end();
    // debugRenderer.render(world, debugMatrix);
    for (Entity entity : allEntities) {
      if (entity != null) {
        entity.update(delta); // update all entities before handling collisions
      }
    }
    updateQuad();

    ArrayList<BoundingShape> returnObjects = new ArrayList<BoundingShape>();
    for (Entity entity : allEntities) {
      if (entity != null) {
        ArrayList<BoundingShape> bs = entity.getBoundingShapes();
        for (int j = 0; j < bs.size(); j++) {
          returnObjects.clear();
          BoundingShape currentBounding = bs.get(j);
          quad.retrieve(returnObjects, currentBounding);

          for (int x = 0; x < returnObjects.size(); x++) {
            // Run collision detection algorithm between objects
            BoundingShape other = returnObjects.get(x);
            // System.out.println(other.getUserData());
            if (currentBounding.intersects(other)) {
              if (currentBounding.getOwner() != other.getOwner()) {
                currentBounding
                    .getOwner()
                    .collide(
                        new Collision(
                            currentBounding.getOwner(), other.getOwner(), currentBounding, other));
                // System.out.println("Collision: " + currentBounding.getUserData() + " + " +
                // other.getUserData());
              }
            }
          }
        }
        entity.postCollisionUpdate();
      }
    }
  }