Hi,
I am trying to go forward with my skills and I decided instead of trying stuff out just to try, I should make a game to learn the game concepts. No matter how primitive or buggy it turns out to be, it’d be a good experience.
I’m using Libgdx and Box2d.
So, I decided to make a simple tower defense game. For this, I needed a map with road. So far, I built the map using Tiled, here it is:
https://dl.dropboxusercontent.com/u/19432607/rectanglesIntoPolygons.jpg
I got the road tiles using this code:
public void getTileId() {
TiledMapTileLayer tileLayer = (TiledMapTileLayer) map.getLayers().get("road_layer");
for (int x = 0; x < tileLayer.getWidth(); x++) {
for (int y = 0; y < tileLayer.getHeight(); y++) {
TiledMapTileLayer.Cell myCell = tileLayer.getCell(x, y);
if (myCell.getTile().getProperties().get("tid") != null && myCell.getTile().getProperties().get("tid").equals("road")) {
cells.add(myCell); //myCell is the Array of cells with a property I set, tid is road.
cellLocations.add(new Vector2(x, y)); // This is the Array of the cell locations.
}
}
}
}
Then I drew rectangles around the “road” cells (tiles) using this method:
//tile_height and tile_width are from my map class. But we could also assume that they are both 32.
public void getRoad() {
for (int i = 0; i < tiles.size; i++) {
System.out.println(tiles.get(i).getTile().getId() + " " + tileLocations.get(i));
Rectangle rec = new Rectangle(tileLocations.get(i).x * tile_width, tileLocations.get(i).y * tile_height, tile_width, tile_height);
roadRectangles.add(rec);
}
}
Then I tried to build two polygons so that I could use these polygons as the lines that limit the road. Maybe I could create static bodies in the same position as the polygons to use Box2D. As you can see from the screenshot, I use box2d (They had sprites, but removed them to explain my problem).
I tried this but failed, so funny “rectangles”:
public void getPolygon() {
for (int i = 0; i < roadRectangles.size - 1; i++) {
for (int j = 0; j < roadRectangles.size - 1; j++) {
if (roadRectangles.get(i).x + 32 == roadRectangles.get(j).x && roadRectangles.get(i).y == roadRectangles.get(j).y) {
System.out.println("touching x: " + roadRectangles.get(i).x + " " + roadRectangles.get(j).x);
System.out.println("touching y: " + roadRectangles.get(i).y + " " + roadRectangles.get(j).y);
roadRectangles.get(i).merge(roadRectangles.get(j));
}
}
}
}
That has some big problems: 1- How the road tiles are put into the Array. 2- The roads in the same x or y coordinates but not close are tried to be merged.
What can I do from here?
My original intention was to set the limits of the road to keep the enemies in the road, then I thought about this way. But I can work with anything that will make this possible.
Thanks in advance
EDIT: I know the road is not passable as is, but that’s an easy fix. And if someone wants to see the whole code of my map class, road finding class and any other class, of course I can share it with you guys.