fixed tilemap display

This commit is contained in:
helori_ollivier
2026-03-11 18:21:46 +01:00
parent e885106efd
commit c419e425eb
6 changed files with 51 additions and 7 deletions

View File

@@ -31,6 +31,13 @@
<version>1.12.1</version> <version>1.12.1</version>
<classifier>natives-desktop</classifier> <classifier>natives-desktop</classifier>
</dependency> </dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.25.3</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@@ -18,7 +18,7 @@ public class GameMap {
public GameMap() { public GameMap() {
this.tileSet = new TileSet("tileset.png", new Vector2(16,16), new Vector2(5,5)); this.tileSet = new TileSet("tileset.png", new Vector2(16,16), new Vector2(6,6));
List<List<Integer>> map = loadMap(); List<List<Integer>> map = loadMap();
this.tileMap = new TileMap(this.tileSet, map); this.tileMap = new TileMap(this.tileSet, map);
} }
@@ -31,7 +31,9 @@ public class GameMap {
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
row = new ArrayList<>(); row = new ArrayList<>();
for (int j = 0; j < 10; j++) { for (int j = 0; j < 10; j++) {
int tileId = (int) (Math.random()*3);
int tileId = (i+j)%3;
row.add(tileId); row.add(tileId);
} }
res.add(row); res.add(row);

View File

@@ -15,7 +15,7 @@ public class Minesweeper implements ApplicationListener {
@Override @Override
public void create() { public void create() {
viewport = new FitViewport(100,150); viewport = new FitViewport(30,30);
spriteBatch = new SpriteBatch(); spriteBatch = new SpriteBatch();
gameMap = new GameMap(); gameMap = new GameMap();

View File

@@ -2,6 +2,8 @@ package bzh.risotto.tilemap;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -20,6 +22,8 @@ public class TileMap {
/** 2D list that store the map tiles */ /** 2D list that store the map tiles */
private final List<List<Tile>> tileList; private final List<List<Tile>> tileList;
private static final Logger logger = LogManager.getLogger();
/** /**
* Load and create the tileMap from a 2D integer list * Load and create the tileMap from a 2D integer list
* *
@@ -47,6 +51,9 @@ public class TileMap {
tmpList = new ArrayList<>(); tmpList = new ArrayList<>();
for (int i = 0; i < this.map.get(j).size(); i++) { for (int i = 0; i < this.map.get(j).size(); i++) {
tileId = this.map.get(j).get(i); tileId = this.map.get(j).get(i);
logger.debug("Tile id: " + tileId);
tile = this.tileSet.getTile(tileId); tile = this.tileSet.getTile(tileId);
tmpList.add(tile); tmpList.add(tile);
} }
@@ -67,6 +74,7 @@ public class TileMap {
for (int j = 0; j < tileList.size(); j++) { for (int j = 0; j < tileList.size(); j++) {
for (int i = 0; i < tileList.get(j).size(); i++) { for (int i = 0; i < tileList.get(j).size(); i++) {
tile = tileList.get(j).get(i); tile = tileList.get(j).get(i);
pos = new Vector2(i*tileSize.x,j*tileSize.y); pos = new Vector2(i*tileSize.x,j*tileSize.y);
tile.render(spriteBatch, pos); tile.render(spriteBatch, pos);
} }

View File

@@ -3,6 +3,8 @@ package bzh.risotto.tilemap;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.ArrayList; import java.util.ArrayList;
@@ -23,6 +25,8 @@ public class TileSet {
/** number of tiles in the tileset */ /** number of tiles in the tileset */
private final Vector2 tilesetSize; private final Vector2 tilesetSize;
private Logger logger = LogManager.getLogger();
/** /**
* Constructor of the class * Constructor of the class
* Loads an array of tile from the tileset * Loads an array of tile from the tileset
@@ -52,16 +56,23 @@ public class TileSet {
for (int i = 0; i < this.tilesetSize.y; i++) { for (int i = 0; i < this.tilesetSize.y; i++) {
for (int j = 0; j < this.tilesetSize.x; j++) { for (int j = 0; j < this.tilesetSize.x; j++) {
Vector2 p = new Vector2(80,80);
tile = new Tile( tile = new Tile(
new TextureRegion( new TextureRegion(
this.tilesTexture, this.tilesTexture,
j*tileSize.x, j*(int) tileSize.x,
i*tilesetSize.y, i* (int) tileSize.y,
tileSize.x, (int) tileSize.x,
tileSize.y (int) tileSize.y
) )
); );
logger.debug("------");
logger.debug("x:" + j*tileSize.x);
logger.debug("y:" + i*tileSize.y);
logger.debug(tileSize);
tileArray.add(tile); tileArray.add(tile);
} }
} }

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns="https://logging.apache.org/xml/ns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://logging.apache.org/xml/ns
https://logging.apache.org/xml/ns/log4j-config-2.xsd">
<Appenders>
<Console name="CONSOLE">
<PatternLayout pattern="%d [%t] %5p %c{1.} - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="DEBUG">
<AppenderRef ref="CONSOLE"/>
</Root>
</Loggers>
</Configuration>