gdx-studio

#libgdx#java#desktop

git clone https://git.pyrossh.dev/gdx-studio

An IDE for creating Games using libgdx and Java supported on all platforms Android, iOS, Desktop


src/scene2d/Map.java
a9b2a64 1
package scene2d;
a62d533 2
import com.badlogic.gdx.graphics.g2d.TextureRegion;
a62d533 3
import com.badlogic.gdx.maps.MapLayer;
a62d533 4
import com.badlogic.gdx.maps.MapLayers;
a62d533 5
import com.badlogic.gdx.maps.MapObjects;
a62d533 6
import com.badlogic.gdx.maps.tiled.TiledMap;
a62d533 7
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
a62d533 8
import com.badlogic.gdx.maps.tiled.TiledMapTileSets;
a62d533 9
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
a62d533 10
import com.badlogic.gdx.scenes.scene2d.Group;
a62d533 11
import com.badlogic.gdx.utils.Array;
a62d533 12
a62d533 13
/** The Map Class
a62d533 14
 * <p>
a62d533 15
 * The Map is a Group which automatically loads all the tiles and arranges them accordingly it is
a62d533 16
 * highly recommended that you override the loadLayer method and customize the map
a62d533 17
 * </p>
a62d533 18
 * @author pyros2097 */
a62d533 19
public class Map extends Group{
a62d533 20
	protected int tileSize;
a62d533 21
	
a62d533 22
	/* List of MapLayers */
a62d533 23
	protected MapLayers mlayers;
a62d533 24
	protected MapObjects mobjects;
a62d533 25
	
a62d533 26
	public static int NoOfColumns;
a62d533 27
	public static int NoOfRows;
a62d533 28
	protected float mapWidth;
a62d533 29
	protected float mapHeight;
a62d533 30
	
a62d533 31
	public static TiledMapTileSets tileSets;
a62d533 32
	private Array<MapActor[][]> tiles = new Array<MapActor[][]>();
a62d533 33
	
a62d533 34
	public Map(){
a62d533 35
		
a62d533 36
	}
a62d533 37
	
a62d533 38
	public Map(int levelNo, int tileSize){
a62d533 39
		setPosition(0, 0);
a62d533 40
		setOrigin(0, 0);
a62d533 41
		TiledMap map = Asset.map(levelNo);
a62d533 42
		this.tileSize = tileSize;
a62d533 43
		mlayers  = map.getLayers();
a62d533 44
		tileSets = map.getTileSets();
a62d533 45
	}
a62d533 46
	
a62d533 47
	public void loadLayer(int layerNo){
a9b2a64 48
		Scene.log("Tiles Layer no: "+layerNo);
a62d533 49
		TiledMapTileLayer layer = (TiledMapTileLayer)mlayers.get(layerNo);
a62d533 50
		NoOfColumns = layer.getWidth();
a9b2a64 51
		Scene.log("MapColumns: "+NoOfColumns);
a62d533 52
		NoOfRows = layer.getHeight();
a9b2a64 53
		Scene.log("MapRows: "+NoOfRows);
a62d533 54
		tiles .add(new MapActor[NoOfRows][NoOfColumns]);
a62d533 55
		for(int i=0; i<NoOfRows; i++)
a62d533 56
			for(int j=0; j<NoOfColumns; j++){
a62d533 57
				Cell c = layer.getCell(j, i);
a62d533 58
				if(c != null){
a62d533 59
					tiles.get(layerNo)[i][j] = new MapActor(c.getTile().getTextureRegion(),
a62d533 60
							i, j, c.getTile().getId(), tileSize);
a62d533 61
					addActor(tiles.get(layerNo)[i][j]);
a62d533 62
				}
a62d533 63
				else{
a62d533 64
					tiles.get(layerNo)[i][j] = new MapActor((TextureRegion)null,i, j, 0, tileSize);
a62d533 65
					addActor(tiles.get(layerNo)[i][j]);
a62d533 66
				}
a62d533 67
		}
a62d533 68
		mapWidth = tileSize * NoOfColumns;
a62d533 69
		mapHeight = tileSize * NoOfRows;
a9b2a64 70
		//Stage.mapOffsetX = mapWidth - Stage.camOffsetX;
a9b2a64 71
		//Stage.mapOffsetY = mapHeight - Stage.camOffsetYTop;
a62d533 72
	}
a62d533 73
	
a62d533 74
	public void loadObjects(int no){
a9b2a64 75
		Scene.log("Objects Layer no: "+no);
a62d533 76
		MapLayer layer1 = mlayers.get(no);
a62d533 77
		mobjects = layer1.getObjects();
a62d533 78
	}
a62d533 79
	
a62d533 80
	public float getWidth(){
a62d533 81
		return mapWidth;
a62d533 82
	}
a62d533 83
	
a62d533 84
	public float getHeight(){
a62d533 85
		return mapHeight;
a62d533 86
	}
a62d533 87
	
a62d533 88
	public int getNoOfColumns(){
a62d533 89
		return NoOfColumns;
a62d533 90
	}
a62d533 91
	
a62d533 92
	public int getNoOfRows(){
a62d533 93
		return NoOfRows;
a62d533 94
	}
a62d533 95
	
a62d533 96
	public MapObjects getMapObjects(){
a62d533 97
		return mobjects;
a62d533 98
	}
a62d533 99
	
a62d533 100
	public MapLayers getMapLayers(){
a62d533 101
		return mlayers;
a62d533 102
	}
7c84877 103
}
7c84877 104
7c84877 105
class MapLayerg extends Group {
7c84877 106
	
7c84877 107
	public MapLayerg(int index){
7c84877 108
		this.setZIndex(index);
7c84877 109
	}
7c84877 110
}
7c84877 111
7c84877 112
class RPGMap {
7c84877 113
	private static RPGMap instance;
7c84877 114
	MapLayerg mapLayer1;
7c84877 115
	MapLayerg mapLayer2;
7c84877 116
	MapLayerg mapLayer3;
7c84877 117
	MapLayerg mapLayer4;
7c84877 118
	MapLayerg mapLayer5;
7c84877 119
	MapLayerg mapLayer6;
7c84877 120
	MapLayerg mapLayer7;
7c84877 121
	
7c84877 122
	private RPGMap(){
7c84877 123
		mapLayer1 = new MapLayerg(1);
7c84877 124
		mapLayer2 = new MapLayerg(2);
7c84877 125
		mapLayer3 = new MapLayerg(3);
7c84877 126
		mapLayer4 = new MapLayerg(4);
7c84877 127
		mapLayer5 = new MapLayerg(5);
7c84877 128
		mapLayer6 = new MapLayerg(6);
7c84877 129
		mapLayer7 = new MapLayerg(7);
7c84877 130
	}
7c84877 131
	
7c84877 132
	public static RPGMap getInstance(){
7c84877 133
		if(instance == null)
7c84877 134
			instance = new RPGMap();
7c84877 135
		return instance;
7c84877 136
	}
a62d533 137
}