|
|
@@ -13,6 +13,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
|
|
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
|
|
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
|
|
import com.badlogic.gdx.maps.tiled.TiledMap;
|
|
|
+import com.badlogic.gdx.maps.tiled.TiledMapTile;
|
|
|
import com.badlogic.gdx.maps.tiled.TiledMapTileSet;
|
|
|
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
|
|
|
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
|
|
|
@@ -45,6 +46,7 @@ public class GameScreen implements Screen {
|
|
|
private TiledMap tiledMap;
|
|
|
private OrthogonalTiledMapRenderer tiledMapRenderer;
|
|
|
private Texture tilesetTexture;
|
|
|
+ private TiledMapTileSet runtimeTileset; // will hold the runtime tileset you added
|
|
|
|
|
|
public GameScreen(Main game, String ip) {
|
|
|
this.mainGame = game;
|
|
|
@@ -79,22 +81,25 @@ public class GameScreen implements Screen {
|
|
|
id++;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- // --- Load other textures if you still need them via AssetManager (optional) ---
|
|
|
- // Example shown in your snippet (keep or remove as needed)
|
|
|
- /*mainGame.assetManager.load("tiles/grass.png", Texture.class);
|
|
|
- mainGame.assetManager.load("tiles/flowering_grass.png", Texture.class);
|
|
|
- mainGame.assetManager.load("tiles/stone_path.png", Texture.class);
|
|
|
- mainGame.assetManager.load("tiles/broken_stone_path.png", Texture.class);
|
|
|
- mainGame.assetManager.load("tiles/wall.png", Texture.class);*/
|
|
|
mainGame.assetManager.finishLoading();
|
|
|
|
|
|
// --- Load the Tiled map ---
|
|
|
TiledMap map = new TmxMapLoader().load("level/lobby.tmx");
|
|
|
|
|
|
// Add the runtime tileset to the map so its tiles can be referenced by layers/objects if needed
|
|
|
+ tileset.setName("runtime_grass");
|
|
|
map.getTileSets().addTileSet(tileset);
|
|
|
|
|
|
+ this.tiledMap = map;
|
|
|
+ this.runtimeTileset = map.getTileSets().getTileSet(tileset.getName() /* tileset.getName() may be null */);
|
|
|
+
|
|
|
+ // fallback: if getTileSet by name fails, use first non-empty tileset:
|
|
|
+ if (this.runtimeTileset == null) {
|
|
|
+ for (TiledMapTileSet ts : map.getTileSets()) {
|
|
|
+ if (ts.size() > 0) { this.runtimeTileset = ts; break; }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// --- Setup renderer and camera ---
|
|
|
final float UNIT_SCALE = 1f / TILE_PX; // map tiles are 16px so 1/16f unit scale
|
|
|
OrthogonalTiledMapRenderer renderer = new OrthogonalTiledMapRenderer(map, UNIT_SCALE);
|
|
|
@@ -141,49 +146,55 @@ public class GameScreen implements Screen {
|
|
|
shapeRenderer.setProjectionMatrix(camera.combined);
|
|
|
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
|
|
|
|
|
|
- if (client.getPlayer() != null) {
|
|
|
- renderPlayerGrid(client.getPlayer());
|
|
|
- }
|
|
|
-
|
|
|
for (Map.Entry<UUID, Player> entry : client.getOtherPlayers().entrySet()) {
|
|
|
renderPlayerGrid(entry.getValue());
|
|
|
}
|
|
|
+ if (client.getPlayer() != null) {
|
|
|
+ renderPlayerGrid(client.getPlayer());
|
|
|
+ }
|
|
|
|
|
|
shapeRenderer.end();
|
|
|
}
|
|
|
|
|
|
private void renderWorld() {
|
|
|
World world = client.getWorld();
|
|
|
- if (world == null) return;
|
|
|
- float scale = world.getBlockScale() * 32f; // Assuming 32px tiles
|
|
|
+ if (world == null || runtimeTileset == null) return;
|
|
|
+ float scale = world.getBlockScale(); // world units per tile
|
|
|
|
|
|
for (int x = 0; x < world.getWidth(); x++) {
|
|
|
for (int y = 0; y < world.getHeight(); y++) {
|
|
|
int blockId = world.getBlock(x, y);
|
|
|
if (blockId == 0) continue;
|
|
|
|
|
|
- Texture tex = getTextureForBlock(blockId);
|
|
|
- if (tex != null) {
|
|
|
- spriteBatch.draw(tex, x * scale, y * scale, scale, scale);
|
|
|
+ TextureRegion region = getRegionForBlock(blockId);
|
|
|
+ if (region != null) {
|
|
|
+ spriteBatch.draw(region, x * scale, y * scale, scale, scale);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private Texture getTextureForBlock(int blockId) {
|
|
|
- return switch (blockId) {
|
|
|
- case 1 -> mainGame.assetManager.get("tiles/grass.png", Texture.class);
|
|
|
- case 2 -> mainGame.assetManager.get("tiles/flowering_grass.png", Texture.class);
|
|
|
- case 3 -> mainGame.assetManager.get("tiles/stone_path.png", Texture.class);
|
|
|
- case 4 -> mainGame.assetManager.get("tiles/broken_stone_path.png", Texture.class);
|
|
|
- case 10 -> mainGame.assetManager.get("tiles/wall.png", Texture.class);
|
|
|
- default -> null;
|
|
|
- };
|
|
|
+ private TextureRegion getRegionForBlock(int blockId) {
|
|
|
+ // If your block IDs match the runtime tile IDs you assigned when creating the tileset:
|
|
|
+ TiledMapTile tile = runtimeTileset.getTile(blockId);
|
|
|
+ if (tile == null) return null;
|
|
|
+
|
|
|
+ // Most runtime tiles are StaticTiledMapTile; get the region:
|
|
|
+ if (tile.getTextureRegion() != null) {
|
|
|
+ return tile.getTextureRegion();
|
|
|
+ }
|
|
|
+
|
|
|
+ // fallback for tiled tiles that wrap an atlas region:
|
|
|
+ if (tile instanceof StaticTiledMapTile) {
|
|
|
+ return ((StaticTiledMapTile) tile).getTextureRegion();
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
private void renderPlayerGrid(Player player) {
|
|
|
if (player.avatar == null) return;
|
|
|
- float cellSize = 50f / 16f;
|
|
|
+ float cellSize = 2f;
|
|
|
for (int x = 0; x < 16; x++) {
|
|
|
for (int y = 0; y < 16; y++) {
|
|
|
Color color = player.avatar.getColor(x, y);
|