premiere version de tilemap (buggé)
This commit is contained in:
47
src/main/java/bzh/risotto/GameMap.java
Normal file
47
src/main/java/bzh/risotto/GameMap.java
Normal file
@@ -0,0 +1,47 @@
|
||||
package bzh.risotto;
|
||||
|
||||
import bzh.risotto.tilemap.TileMap;
|
||||
import bzh.risotto.tilemap.TileSet;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.maps.tiled.TiledMap;
|
||||
import com.badlogic.gdx.maps.tiled.TiledMapTileSet;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class GameMap {
|
||||
|
||||
private TileSet tileSet;
|
||||
private TileMap tileMap;
|
||||
|
||||
public GameMap() {
|
||||
|
||||
this.tileSet = new TileSet("tileset.png", new Vector2(16,16), new Vector2(5,5));
|
||||
List<List<Integer>> map = loadMap();
|
||||
this.tileMap = new TileMap(this.tileSet, map);
|
||||
}
|
||||
|
||||
private List<List<Integer>> loadMap() {
|
||||
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
|
||||
List<Integer> row;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
row = new ArrayList<>();
|
||||
for (int j = 0; j < 10; j++) {
|
||||
int tileId = (int) (Math.random()*3);
|
||||
row.add(tileId);
|
||||
}
|
||||
res.add(row);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public void render(SpriteBatch spriteBatch) {
|
||||
this.tileMap.render(spriteBatch);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,15 +1,24 @@
|
||||
package bzh.risotto;
|
||||
|
||||
import com.badlogic.gdx.ApplicationListener;
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.utils.ScreenUtils;
|
||||
import com.badlogic.gdx.utils.viewport.FitViewport;
|
||||
|
||||
public class Minesweeper implements ApplicationListener {
|
||||
|
||||
private FitViewport viewport;
|
||||
private SpriteBatch spriteBatch;
|
||||
|
||||
private GameMap gameMap;
|
||||
|
||||
@Override
|
||||
public void create() {
|
||||
viewport = new FitViewport(100,150);
|
||||
spriteBatch = new SpriteBatch();
|
||||
|
||||
gameMap = new GameMap();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -31,6 +40,10 @@ public class Minesweeper implements ApplicationListener {
|
||||
}
|
||||
|
||||
private void draw() {
|
||||
ScreenUtils.clear(Color.RED);
|
||||
spriteBatch.begin();
|
||||
gameMap.render(spriteBatch);
|
||||
spriteBatch.end();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
31
src/main/java/bzh/risotto/tilemap/Tile.java
Normal file
31
src/main/java/bzh/risotto/tilemap/Tile.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package bzh.risotto.tilemap;
|
||||
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
|
||||
/**
|
||||
* Class that represent a tile of a tileset or tilemap
|
||||
*/
|
||||
public class Tile {
|
||||
|
||||
/** the texture of the tile */
|
||||
private final TextureRegion tileTexture;
|
||||
|
||||
/**
|
||||
* Create a tile
|
||||
*
|
||||
* @param tileTexture the texture of the tile
|
||||
*/
|
||||
public Tile(TextureRegion tileTexture) {
|
||||
this.tileTexture = tileTexture;
|
||||
}
|
||||
|
||||
/**
|
||||
* render the tile
|
||||
*/
|
||||
public void render(SpriteBatch spriteBatch, Vector2 pos) {
|
||||
spriteBatch.draw(this.tileTexture, pos.x, pos.y);
|
||||
}
|
||||
|
||||
}
|
||||
75
src/main/java/bzh/risotto/tilemap/TileMap.java
Normal file
75
src/main/java/bzh/risotto/tilemap/TileMap.java
Normal file
@@ -0,0 +1,75 @@
|
||||
package bzh.risotto.tilemap;
|
||||
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Class that store and render a TileMap with a Specific Tileset
|
||||
*/
|
||||
public class TileMap {
|
||||
|
||||
/** tileset of the map */
|
||||
private final TileSet tileSet;
|
||||
|
||||
/** 2D list that represent the map */
|
||||
private final List<List<Integer>> map;
|
||||
|
||||
/** 2D list that store the map tiles */
|
||||
private final List<List<Tile>> tileList;
|
||||
|
||||
/**
|
||||
* Load and create the tileMap from a 2D integer list
|
||||
*
|
||||
* @param tileSet tileset used by the tilemap
|
||||
* @param map 2D list of integer that represent the map
|
||||
*/
|
||||
public TileMap(TileSet tileSet, List<List<Integer>> map) {
|
||||
this.tileSet = tileSet;
|
||||
this.map = map;
|
||||
this.tileList = loadTileMap();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the tilemap
|
||||
*
|
||||
* @return 2D list of tiles
|
||||
*/
|
||||
private List<List<Tile>> loadTileMap() {
|
||||
List<List<Tile>> res = new ArrayList<>();
|
||||
|
||||
List<Tile> tmpList;
|
||||
int tileId;
|
||||
Tile tile;
|
||||
for (int j = 0; j < this.map.size(); j++) {
|
||||
tmpList = new ArrayList<>();
|
||||
for (int i = 0; i < this.map.get(j).size(); i++) {
|
||||
tileId = this.map.get(j).get(i);
|
||||
tile = this.tileSet.getTile(tileId);
|
||||
tmpList.add(tile);
|
||||
}
|
||||
res.add(tmpList);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* render the map on the screen
|
||||
*/
|
||||
public void render(SpriteBatch spriteBatch) {
|
||||
|
||||
Vector2 tileSize = this.tileSet.getTileSize();
|
||||
Vector2 pos;
|
||||
Tile tile;
|
||||
for (int j = 0; j < tileList.size(); j++) {
|
||||
for (int i = 0; i < tileList.get(j).size(); i++) {
|
||||
tile = tileList.get(j).get(i);
|
||||
pos = new Vector2(i*tileSize.x,j*tileSize.y);
|
||||
tile.render(spriteBatch, pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
90
src/main/java/bzh/risotto/tilemap/TileSet.java
Normal file
90
src/main/java/bzh/risotto/tilemap/TileSet.java
Normal file
@@ -0,0 +1,90 @@
|
||||
package bzh.risotto.tilemap;
|
||||
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Class that load and store a tileset
|
||||
*/
|
||||
public class TileSet {
|
||||
|
||||
/** tiles textures image (tileset.png) */
|
||||
private final Texture tilesTexture;
|
||||
|
||||
/** Array that store eache individual tiles of the tileset */
|
||||
private final ArrayList<Tile> tiles;
|
||||
|
||||
/** size of a tile */
|
||||
private final Vector2 tileSize;
|
||||
|
||||
/** number of tiles in the tileset */
|
||||
private final Vector2 tilesetSize;
|
||||
|
||||
/**
|
||||
* Constructor of the class
|
||||
* Loads an array of tile from the tileset
|
||||
*
|
||||
* @param tilesTexturePath the path to the tiles texture image
|
||||
* @param tileSize the size of a tiles
|
||||
* @param tilesetSize number of tiles in the tiles texture image
|
||||
*/
|
||||
public TileSet(String tilesTexturePath, Vector2 tileSize, Vector2 tilesetSize) {
|
||||
|
||||
this.tileSize = tileSize;
|
||||
this.tilesetSize = tilesetSize;
|
||||
|
||||
this.tilesTexture = new Texture(tilesTexturePath);
|
||||
this.tiles = loadTiles();
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the tiles from the tileset image
|
||||
*
|
||||
* @return a list of tiles
|
||||
*/
|
||||
private ArrayList<Tile> loadTiles() {
|
||||
ArrayList<Tile> tileArray = new ArrayList<>();
|
||||
|
||||
Tile tile;
|
||||
for (int i = 0; i < this.tilesetSize.y; i++) {
|
||||
for (int j = 0; j < this.tilesetSize.x; j++) {
|
||||
|
||||
tile = new Tile(
|
||||
new TextureRegion(
|
||||
this.tilesTexture,
|
||||
j*tileSize.x,
|
||||
i*tilesetSize.y,
|
||||
tileSize.x,
|
||||
tileSize.y
|
||||
)
|
||||
);
|
||||
|
||||
tileArray.add(tile);
|
||||
}
|
||||
}
|
||||
|
||||
return tileArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Acces a tile in the tileset
|
||||
*
|
||||
* @param id id of the tile to acces
|
||||
* @return the tiles
|
||||
*/
|
||||
public Tile getTile(int id) {
|
||||
return this.tiles.get(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Acces the tile size of the tileset
|
||||
*
|
||||
* @return tile size
|
||||
*/
|
||||
public Vector2 getTileSize() {
|
||||
return tileSize;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user