public MainCharacter(RenderWindow window) {
   super(window);
   setSprite("Pics/Human/Ash_Sprite.jpg", new IntRect(0, 10, 16, 26));
   Vector2i windowSize = window.getSize();
   setScale(3, 3);
   setPosition(new Vector2f(windowSize.x / 2 - 24, windowSize.y / 2 - 24));
 }
Пример #2
0
  @Override
  public void draw(RenderTarget renderTarget, RenderStates renderStates) {
    int adjustedFrame =
        Math.round((animationFrame * ANIMATION_FRAMES) / (ANIMATION_FRAMES * ANIMATION_SPEED));
    // TODO: Improve efficiency if required. There is no use in looping through tiles immediately
    // adjacent to the
    // start of the chunk.
    // Apply the tile sheet to the tiles.
    RenderStates states = new RenderStates(TILE_SHEET);
    // Get the player's current position.
    Vector2f playerPosition = player.getPosition();
    // Get the window's current size.
    Vector2i windowSize = WINDOW.getSize();

    // Determine how many tiles fit the window horizontally and vertically taking zoom into account,
    // then halve
    // both values.
    int xDistance = (int) Math.ceil(windowSize.x / (TILE_SIZE * 2 / ZOOM));
    int yDistance = (int) Math.ceil(windowSize.y / (TILE_SIZE * 2 / ZOOM));
    Vector2f distance = new Vector2f(xDistance + 1, yDistance + 1);

    // Create a rectangle representing the positions currently viewable by the player.
    FloatRect visibleArea =
        new FloatRect(
            playerPosition.x - distance.x,
            playerPosition.y - distance.y,
            distance.x * 2,
            distance.y * 2);
    // Create a set to keep track of the already rendered chunks.
    Set<Integer> renderedChunks = new HashSet<Integer>();
    // Loop through every position currently in view.
    for (float i = visibleArea.left; i <= visibleArea.left + visibleArea.width; i++) {
      for (float j = visibleArea.top; j <= visibleArea.top + visibleArea.height; j++) {
        // Convert the current position to a chunk ID.
        int chunkID = positionToChunkID(new Vector2f(i, j));
        // If the chunk is valid and hasn't been drawn yet, draw it.
        if (isValidChunkID(chunkID) && !renderedChunks.contains(chunkID)) {
          // Draw the chunk vertex array with the tile sheet.
          VERTEX_ARRAYS[chunkID][0].draw(renderTarget, states);
          if (adjustedFrame > 0) {
            VERTEX_ARRAYS[chunkID][adjustedFrame].draw(renderTarget, states);
          }
          // Add the drawn chunk ID to the set to check against in order to save resources by not
          // drawing
          // it twice.
          renderedChunks.add(chunkID);
        }
      }
    }
  }