Skip to content

Tile Highlighting

Mohamad Dabboussi edited this page Oct 16, 2023 · 4 revisions

Function: hoverHighlight() in TerrainComponent

Purpose: The hoverHighlight() function is employed to accentuate the tile over which the mouse currently hovers in a tiled map. Additionally, this method considers the game's current state, such as whether a tower is being placed and if there's enough currency to place a tower. The tile will be highlighted green if the tower can be placed at that tile and red otherwise.

Implementation:

  1. Get the current mouse position and unproject it based on the camera to get the tile's coordinates on the map.

    Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
    camera.unproject(mousePos);
  2. Calculate the tile's x and y index by dividing the mouse's position by the tile size.

    int tileX = (int) (mousePos.x / tileSize);
    int tileY = (int) (mousePos.y / tileSize);
  3. Retrieve the tile at the specified x and y index from the map's first layer.

    final TiledMapTileLayer tileLayer = (TiledMapTileLayer) tiledMap.getLayers().get(0);
    TiledMapTileLayer.Cell currentCell = tileLayer.getCell(tileX, tileY);
  4. If a tile was previously hovered over and it's different from the current tile, restore the original texture of the previously hovered tile.

    if (lastHoveredCell != null && lastHoveredCell != currentCell && originalRegion != null) {
      lastHoveredCell.getTile().setTextureRegion(originalRegion);
    }
  5. If no tower is currently being placed and the cell is not null, change its texture to indicate that the terrain can be used.

    if (ServiceLocator.getCurrencyService().getTower() == null && currentCell != null) {
      ResourceService resourceService = ServiceLocator.getResourceService();
      Texture texture = resourceService.getAsset("images/terrain_use.png", Texture.class);
      currentCell.getTile().setTextureRegion(new TextureRegion(texture));
    }
  6. If the currently hovered tile is different from the last hovered tile:

    • If there's a tower to be placed:
      • If no entities are in the tile and the user has enough currency to buy the tower, highlight the tile in green.
      • Otherwise, highlight the tile in red.
    if (currentCell != null && currentCell != lastHoveredCell) {
      originalRegion = currentCell.getTile().getTextureRegion();
    
      ResourceService resourceService = ServiceLocator.getResourceService();
      if (currencyService.getTower() != null) {
        if (!ServiceLocator.getEntityService().entitiesInTile(tileX, tileY) && currencyService.getScrap().canBuy(Integer.parseInt(currencyService.getTower().getPrice()))) {
          Texture texture = resourceService.getAsset("images/green_tile.png", Texture.class);
          currentCell.getTile().setTextureRegion(new TextureRegion(texture));
        } else {
          Texture texture = resourceService.getAsset("images/red_tile.png", Texture.class);
          currentCell.getTile().setTextureRegion(new TextureRegion(texture));
        }
        lastHoveredCell = currentCell;
      }
    }

This function should be called during rendering within the draw() method to keep updating the tile's texture based on the mouse hover position and the game's current state.

Associated Code:

@Override
public void draw(SpriteBatch batch) {
  tiledMapRenderer.setView(camera);
  hoverHighlight();
  tiledMapRenderer.render();
}

The above draw() method configures the view of the tiledMapRenderer based on the camera, invokes the hoverHighlight() function to alter the tile's texture depending on the mouse's hover position and the game's context, and then renders the map.


Test Plan:

The following test plan has been used: Tile Highlighting Test Plan.

Note: For those updating or working with this function, ensure that you have the requisite dependencies and assets, such as "images/green_tile.png", "images/red_tile.png", and "images/terrain_use.png".

Clone this wiki locally