Coverage Summary for Class: TileMap (com.mygdx.game.Components)

Class Class, % Method, % Line, %
TileMap 100% (1/1) 50% (4/8) 50% (11/22)


1 package com.mygdx.game.Components; 2  3 import com.badlogic.gdx.Application; 4 import com.badlogic.gdx.Gdx; 5 import com.badlogic.gdx.maps.tiled.TiledMap; 6 import com.badlogic.gdx.maps.tiled.TiledMapRenderer; 7 import com.badlogic.gdx.maps.tiled.TiledMapTileLayer; 8 import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer; 9 import com.badlogic.gdx.math.Vector2; 10 import com.mygdx.game.Managers.RenderLayer; 11 import com.mygdx.game.Managers.RenderingManager; 12 import com.mygdx.game.Managers.ResourceManager; 13  14 import static com.mygdx.utils.Constants.TILE_SIZE; 15  16 /** 17  * Component that allows the rendering of tilemaps (has its own sprite batch) 18  */ 19 public class TileMap extends Component { 20  TiledMap map; 21  TiledMapRenderer renderer; 22  23  private TileMap() { 24  super(); 25  type = ComponentType.TileMap; 26  // CollisionManager.addTileMap(this); 27  } 28  29  /** 30  * @param id resource id of the tilemap 31  * @param layer rendering layer 32  */ 33  public TileMap(int id, RenderLayer layer) { 34  this(); 35  map = ResourceManager.getTileMap(id); 36  if(!(Application.ApplicationType.HeadlessDesktop == Gdx.app.getType())){ 37  renderer = new OrthogonalTiledMapRenderer(map); 38  } 39  40  RenderingManager.addItem(this, layer); 41  42  TILE_SIZE = getTileDim().x; 43  } 44  45  /** 46  * Gets cell at position (in world space, must be n the maps range) 47  * 48  * @param pos pos in world space 49  * @return the cell found 50  */ 51  public TiledMapTileLayer.Cell getCell(Vector2 pos) { 52  Vector2 p = pos.cpy(); 53  TiledMapTileLayer l = (TiledMapTileLayer) map.getLayers().get(1); 54  p.x /= l.getTileWidth(); 55  p.y /= l.getTileHeight(); 56  57  return l.getCell((int) p.x, (int) p.y); 58  } 59  60  public Vector2 getTileDim() { 61  return new Vector2( 62  ((TiledMapTileLayer) map.getLayers().get(0)).getTileWidth(), 63  ((TiledMapTileLayer) map.getLayers().get(0)).getTileHeight()); 64  } 65  66  public TiledMap getTileMap() { 67  return map; 68  } 69  70  /** 71  * Updates the renderer's view with the rendering camera 72  */ 73  @Override 74  public void update() { 75  super.update(); 76  renderer.setView(RenderingManager.getCamera()); 77  } 78  79  /** 80  * draws the first 2 layers 81  */ 82  @Override 83  public void render() { 84  super.render(); 85  renderer.render(new int[]{0, 1}); 86  } 87  88  @Override 89  public void cleanUp() { 90  super.cleanUp(); 91  } 92 }