Exemplo n.º 1
0
Arquivo: Map.java Projeto: twhscs/game
  private void partition() {
    // TODO: Improve partitioning efficiency. Currently O(n^3).

    /*

    Partition the map into multiple vertex arrays for rendering.
    Vertex arrays speed up rendering time.
    We create these arrays only once as opposed to every frame for even greater optimization.

    See: http://www.sfml-dev.org/tutorials/2.0/graphics-vertex-array.php

     */

    // Loop through each chunk.
    for (int chunkID = 0; chunkID < TOTAL_CHUNKS; chunkID++) {
      // Initialize the chunk's vertex array.
      for (int i = 0; i < ANIMATION_FRAMES; i++) {
        VERTEX_ARRAYS[chunkID][i] = new VertexArray(PrimitiveType.QUADS);
      }
      // Get the top left corner of the current chunk.
      Vector2f position = chunkIDToPosition(chunkID);
      // Loop through the current chunk tile by tile.
      for (int i = (int) position.x; i < position.x + CHUNK_SIZE; i++) {
        for (int j = (int) position.y; j < position.y + CHUNK_SIZE; j++) {
          // Make sure the current tile is valid.
          if (isValidPosition(new Vector2f(i, j))) {
            // Get the current tile.
            final Terrain tile = TILE_ARRAY[i][j];
            // Get the correct texture for the current tile.
            Vector2f textureCoordinates = tile.getTextureCoordinates();
            for (int frame = 0; frame < ANIMATION_FRAMES; frame++) {
              Vector2f animatedTexture;
              if (tile.isAnimated()) {
                animatedTexture =
                    Vector2f.add(textureCoordinates, new Vector2f(TILE_SIZE * frame, 0));
              } else {
                animatedTexture = textureCoordinates;
              }
              if (tile.isAnimated() || frame == 0) {
                // Create a vector for each corner of the texture.
                Vector2f[] positions = new Vector2f[4];
                // Set each corner.
                positions[0] = animatedTexture;
                positions[1] = Vector2f.add(animatedTexture, new Vector2f(0, TILE_SIZE));
                positions[2] = Vector2f.add(animatedTexture, new Vector2f(TILE_SIZE, TILE_SIZE));
                positions[3] = Vector2f.add(animatedTexture, new Vector2f(TILE_SIZE, 0));
                // Determine whether or not the tile is to be randomly rotated.
                boolean random = tile.isRandomized();
                boolean flipped = true;
                if (random) {
                  // Randomly choose 1 - 3 rotations.
                  int rotations = (int) (Math.random() * 3) + 1;
                  // For each rotation shift the coordinates in a circular fashion.
                  for (int k = 0; k < rotations; k++) {
                    Vector2f temp;
                    temp = positions[3];
                    positions[3] = positions[2];
                    positions[2] = positions[1];
                    positions[1] = positions[0];
                    positions[0] = temp;
                  }
                  // Randomly determine whether or not to flip with a 50-50 chance.
                  flipped = (Math.random() < 0.5);
                  if (flipped) {
                    // If flipped, flip the texture coordinates.
                    Vector2f temp;
                    temp = positions[0];
                    positions[0] = positions[1];
                    positions[1] = temp;
                    temp = positions[2];
                    positions[2] = positions[3];
                    positions[3] = temp;
                  }
                }
                if (!tile.isRandomized() || flipped) {
                  // Fix for a JSFML bug. See: http://en.sfml-dev.org/forums/index.php?topic=15889.0
                  for (int k = 0; k < 4; k++) {
                    positions[k] = Vector2f.add(positions[k], new Vector2f(0.01f, -0.01f));
                  }
                }
                // Create and add a vertex for the bottom left corner of the tile.
                VERTEX_ARRAYS[chunkID][frame].add(
                    new Vertex(new Vector2f(i * TILE_SIZE, j * TILE_SIZE), positions[0]));
                // Create and add a vertex for the top left corner of the tile.
                VERTEX_ARRAYS[chunkID][frame].add(
                    new Vertex(
                        new Vector2f(i * TILE_SIZE, j * TILE_SIZE + TILE_SIZE), positions[1]));
                // Create and add a vertex for the top right corner of the tile.
                VERTEX_ARRAYS[chunkID][frame].add(
                    new Vertex(
                        new Vector2f(i * TILE_SIZE + TILE_SIZE, j * TILE_SIZE + TILE_SIZE),
                        positions[2]));
                // Create and add a vertex for the bottom right corner of the tile.
                VERTEX_ARRAYS[chunkID][frame].add(
                    new Vertex(
                        new Vector2f(i * TILE_SIZE + TILE_SIZE, j * TILE_SIZE), positions[3]));
              }
            }
          }
        }
      }
    }
  }