From e885106efd2b80d836b6b91aa0c7dbc58e95198d Mon Sep 17 00:00:00 2001 From: helori_ollivier Date: Wed, 11 Mar 2026 17:05:04 +0100 Subject: [PATCH] =?UTF-8?q?premiere=20version=20de=20tilemap=20(bugg=C3=A9?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/bzh/risotto/GameMap.java | 47 ++++++++++ src/main/java/bzh/risotto/Minesweeper.java | 13 +++ src/main/java/bzh/risotto/tilemap/Tile.java | 31 +++++++ .../java/bzh/risotto/tilemap/TileMap.java | 75 ++++++++++++++++ .../java/bzh/risotto/tilemap/TileSet.java | 90 +++++++++++++++++++ 5 files changed, 256 insertions(+) create mode 100644 src/main/java/bzh/risotto/GameMap.java create mode 100644 src/main/java/bzh/risotto/tilemap/Tile.java create mode 100644 src/main/java/bzh/risotto/tilemap/TileMap.java create mode 100644 src/main/java/bzh/risotto/tilemap/TileSet.java diff --git a/src/main/java/bzh/risotto/GameMap.java b/src/main/java/bzh/risotto/GameMap.java new file mode 100644 index 0000000..1bf15ec --- /dev/null +++ b/src/main/java/bzh/risotto/GameMap.java @@ -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> map = loadMap(); + this.tileMap = new TileMap(this.tileSet, map); + } + + private List> loadMap() { + + List> res = new ArrayList<>(); + + List 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); + } + +} diff --git a/src/main/java/bzh/risotto/Minesweeper.java b/src/main/java/bzh/risotto/Minesweeper.java index baf0f60..649bd05 100644 --- a/src/main/java/bzh/risotto/Minesweeper.java +++ b/src/main/java/bzh/risotto/Minesweeper.java @@ -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 diff --git a/src/main/java/bzh/risotto/tilemap/Tile.java b/src/main/java/bzh/risotto/tilemap/Tile.java new file mode 100644 index 0000000..53f48bd --- /dev/null +++ b/src/main/java/bzh/risotto/tilemap/Tile.java @@ -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); + } + +} diff --git a/src/main/java/bzh/risotto/tilemap/TileMap.java b/src/main/java/bzh/risotto/tilemap/TileMap.java new file mode 100644 index 0000000..a2a72b1 --- /dev/null +++ b/src/main/java/bzh/risotto/tilemap/TileMap.java @@ -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> map; + + /** 2D list that store the map tiles */ + private final List> 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> map) { + this.tileSet = tileSet; + this.map = map; + this.tileList = loadTileMap(); + } + + /** + * Load the tilemap + * + * @return 2D list of tiles + */ + private List> loadTileMap() { + List> res = new ArrayList<>(); + + List 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); + } + } + } +} diff --git a/src/main/java/bzh/risotto/tilemap/TileSet.java b/src/main/java/bzh/risotto/tilemap/TileSet.java new file mode 100644 index 0000000..e3ac267 --- /dev/null +++ b/src/main/java/bzh/risotto/tilemap/TileSet.java @@ -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 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 loadTiles() { + ArrayList 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; + } +}