1
0
mirror of https://github.com/Klagarge/PokeHES.git synced 2025-03-14 06:44:33 +00:00
PokeHES/src/Screen/ScreenMap.java

180 lines
5.8 KiB
Java
Raw Normal View History

2022-06-01 16:47:53 +02:00
package Screen;
2022-06-07 21:35:13 +02:00
import java.util.Map;
import java.util.TreeMap;
import java.util.Vector;
2022-06-02 12:38:42 +02:00
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.MapObjects;
import com.badlogic.gdx.maps.MapProperties;
2022-06-07 21:35:13 +02:00
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapRenderer;
import com.badlogic.gdx.maps.tiled.TiledMapTile;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
2022-06-02 13:26:43 +02:00
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
2022-06-07 21:35:13 +02:00
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
2022-06-02 12:38:42 +02:00
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Vector2;
2022-06-07 21:35:13 +02:00
import Entity.Player;
2022-06-08 18:47:33 +02:00
import ch.hevs.gdx2d.components.screen_management.RenderingScreen;
2022-06-07 21:35:13 +02:00
import ch.hevs.gdx2d.lib.GdxGraphics;
2022-06-02 12:38:42 +02:00
2022-06-08 18:47:33 +02:00
public class ScreenMap extends RenderingScreen{
2022-06-02 12:38:42 +02:00
// tiles management
private Vector<TiledMapTileLayer> tiledLayer = new Vector<>();
private MapObjects doors;
Map<String,TiledMap> tMap = new TreeMap<String,TiledMap>();
Map<String,TiledMapRenderer> tMapRenderer = new TreeMap<String,TiledMapRenderer>();
2022-06-12 12:35:08 +02:00
public String map;
2022-06-02 12:38:42 +02:00
public float zoom;
private int width;
2022-06-07 21:35:13 +02:00
public int tileWidth;
2022-06-02 12:38:42 +02:00
private int height;
2022-06-07 21:35:13 +02:00
public int tileHeight;
private Player player;
2022-06-08 20:18:22 +02:00
2022-06-02 12:38:42 +02:00
private void createMap(String name){
2022-06-06 13:48:20 +02:00
TiledMap tm =new TmxMapLoader().load("./resources/map/"+ name + ".tmx");
2022-06-02 12:38:42 +02:00
tMap.put(name,tm);
tMapRenderer.put(name,new OrthogonalTiledMapRenderer(tm));
}
2022-06-08 18:47:33 +02:00
@Override
public void onInit() {
2022-06-02 12:38:42 +02:00
// Set initial zoom
zoom = 1;
try { map = player.getMap(); } catch (Exception e) {}
2022-06-02 12:38:42 +02:00
// create map
createMap("test");
createMap("test_couloir");
2022-06-03 15:50:48 +02:00
createMap("desert");
2022-06-02 12:38:42 +02:00
}
2022-06-08 18:47:33 +02:00
@Override
public void onGraphicRender(GdxGraphics g) {
2022-06-09 17:32:47 +02:00
2022-06-03 15:50:48 +02:00
tiledLayer.clear();
2022-06-13 18:06:38 +02:00
try { map = player.getMap(); } catch (Exception e) { System.out.println("error for get map");}
2022-06-03 15:50:48 +02:00
for (int i = 0; i < 50; i++) {
2022-06-02 12:38:42 +02:00
try { tiledLayer.add((TiledMapTileLayer) tMap.get(map).getLayers().get(i)); } catch (Exception e) { }
}
2022-06-13 18:06:38 +02:00
if (tiledLayer.size() <= 0) System.out.println("TiledLayer empty !!!");
2022-06-02 12:38:42 +02:00
TiledMapTileLayer tl = tiledLayer.get(0);
width = tl.getWidth();
tileWidth = (int) tl.getTileWidth();
height = tl.getHeight();
tileHeight = (int) tl.getTileHeight();
2022-06-09 17:32:47 +02:00
2022-06-02 13:26:43 +02:00
//System.out.println(width + " x " + height + " - " + tileWidth + " x " + tileHeight);
2022-06-02 12:38:42 +02:00
try {
2022-06-09 17:32:47 +02:00
doors = tMap.get(map).getLayers().get("door").getObjects();
2022-06-03 15:50:48 +02:00
} catch (Exception e) { doors = null; }
2022-06-02 12:38:42 +02:00
2022-06-09 17:32:47 +02:00
2022-06-02 12:38:42 +02:00
// Render the tileMap
2022-06-12 12:35:08 +02:00
g.zoom(zoom);
try {g.moveCamera(player.getPosition().x, player.getPosition().y, width * tileWidth, height * tileHeight);}
catch (Exception e) {System.out.println("Fail to move camera");}
2022-06-10 18:49:02 +02:00
2022-06-02 12:38:42 +02:00
tMapRenderer.get(map).setView(g.getCamera());
tMapRenderer.get(map).render();
2022-06-09 17:32:47 +02:00
2022-06-02 12:38:42 +02:00
g.drawFPS();
}
2022-06-08 18:47:33 +02:00
2022-06-07 21:35:13 +02:00
public Vector<TiledMapTile> getTile(Vector2 position, int offsetX, int offsetY) {
2022-06-02 12:38:42 +02:00
Vector<TiledMapTile> tiles = new Vector<>();
2022-06-09 17:32:47 +02:00
2022-06-02 12:38:42 +02:00
for (TiledMapTileLayer tl : tiledLayer) {
2022-06-03 15:50:48 +02:00
int x = (int) (position.x / tileWidth) + offsetX;
int y = (int) (position.y / tileHeight) + offsetY;
2022-06-02 12:38:42 +02:00
try {
2022-06-02 13:26:43 +02:00
Cell cell = tl.getCell(x, y);
2022-06-03 15:50:48 +02:00
if (cell == null) continue;
tiles.add(cell.getTile());
2022-06-09 17:32:47 +02:00
} catch (Exception e) { System.out.println("error: cell");}
2022-06-02 12:38:42 +02:00
}
return tiles;
}
2022-06-07 21:35:13 +02:00
public boolean isWalkable(Vector<TiledMapTile> tile) {
2022-06-02 12:38:42 +02:00
if (tile == null) return false;
2022-06-13 18:06:38 +02:00
if (tile.size() == 0) return false;
boolean walkable = true;
2022-06-02 12:38:42 +02:00
for (TiledMapTile tiledMapTile : tile) {
Object test = tiledMapTile.getProperties().get("walkable");
2022-06-13 18:06:38 +02:00
walkable = Boolean.parseBoolean(test.toString()) ? walkable:false;
2022-06-02 12:38:42 +02:00
}
return walkable;
}
2022-06-07 21:35:13 +02:00
public float getSpeed(Vector<TiledMapTile> tile) {
2022-06-02 12:38:42 +02:00
float speed = 0;
for (TiledMapTile tiledMapTile : tile) {
Object test = tiledMapTile.getProperties().get("speed");
float newSpeed = Float.parseFloat(test.toString());
speed = newSpeed > speed ? newSpeed:speed;
}
return speed;
}
2022-06-07 21:35:13 +02:00
public boolean isDoor(Vector2 position) {
2022-06-03 15:50:48 +02:00
if (doors == null) return false;
2022-06-02 12:38:42 +02:00
boolean onDoor = false;
Integer x = null;
Integer y = null;
int ox = 0;
int oy = 0;
try {
2022-06-03 15:50:48 +02:00
x = (int) (position.x / tileWidth);
y = (int) (position.y / tileHeight);
2022-06-02 12:38:42 +02:00
} catch (Exception e) { }
for (MapObject object : doors){
MapProperties mapProperties = null;
try { mapProperties = object.getProperties(); } catch (Exception e) { }
try { ox = (int) ((float) mapProperties.get("x")); } catch (Exception e) { }
try { oy = (int) ((float) mapProperties.get("y")); } catch (Exception e) { }
2022-06-03 15:50:48 +02:00
ox /= tileWidth;
oy /= tileHeight;
2022-06-02 12:38:42 +02:00
2022-06-03 15:50:48 +02:00
if ((x != null || y != null) && (x == ox && y == oy)) {
onDoor = true;
2022-06-07 21:35:13 +02:00
try { Door.nextMap = mapProperties.get("nextMap").toString(); } catch (Exception e) { }
try { Door.nextX = Integer.parseInt(mapProperties.get("nextX").toString()); } catch (Exception e) { }
try { Door.nextY = Integer.parseInt(mapProperties.get("nextY").toString()); } catch (Exception e) { }
2022-06-13 18:06:38 +02:00
try { Door.direction = Player.Direction.valueOf(mapProperties.get("nextDirection").toString()); } catch (Exception e) { }
2022-06-03 15:50:48 +02:00
}
2022-06-02 12:38:42 +02:00
}
return onDoor;
}
2022-06-07 21:35:13 +02:00
public static class Door {
public static String nextMap;
public static Integer nextX;
public static Integer nextY;
2022-06-13 18:06:38 +02:00
public static Player.Direction direction;
2022-06-03 15:50:48 +02:00
2022-06-07 21:35:13 +02:00
public static void reset(){
2022-06-03 15:50:48 +02:00
nextMap = null;
nextX = null;
nextY = null;
2022-06-13 18:06:38 +02:00
direction = null;
2022-06-03 15:50:48 +02:00
}
}
public void setPlayer(Player p) {
player = p;
}
2022-06-01 16:47:53 +02:00
}