Add collision for map tiles
This commit is contained in:
parent
f3ad6c0ffa
commit
e5e6fef3cc
4 changed files with 58 additions and 31 deletions
|
@ -1,7 +1,16 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<tileset version="1.8" tiledversion="1.8.4" name="main" tilewidth="32" tileheight="32" tilecount="1" columns="0">
|
<tileset version="1.8" tiledversion="1.8.4" name="main" tilewidth="32" tileheight="32" tilecount="2" columns="0">
|
||||||
<grid orientation="orthogonal" width="1" height="1"/>
|
<grid orientation="orthogonal" width="1" height="1"/>
|
||||||
<tile id="0">
|
<tile id="0" type="grass">
|
||||||
|
<properties>
|
||||||
|
<property name="walkable" type="bool" value="true"/>
|
||||||
|
</properties>
|
||||||
|
<image width="32" height="32" source="grass.png"/>
|
||||||
|
</tile>
|
||||||
|
<tile id="1" type="fake grass (real)">
|
||||||
|
<properties>
|
||||||
|
<property name="walkable" type="bool" value="false"/>
|
||||||
|
</properties>
|
||||||
<image width="32" height="32" source="grass.png"/>
|
<image width="32" height="32" source="grass.png"/>
|
||||||
</tile>
|
</tile>
|
||||||
</tileset>
|
</tileset>
|
||||||
|
|
|
@ -1,28 +1,28 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<map version="1.8" tiledversion="1.8.4" orientation="orthogonal" renderorder="right-down" width="30" height="20" tilewidth="32" tileheight="32" infinite="0" nextlayerid="2" nextobjectid="1">
|
<map version="1.8" tiledversion="1.8.4" orientation="orthogonal" renderorder="right-down" width="30" height="20" tilewidth="32" tileheight="32" infinite="0" nextlayerid="3" nextobjectid="6">
|
||||||
<tileset firstgid="1" source="main.tsx"/>
|
<tileset firstgid="1" source="main.tsx"/>
|
||||||
<layer id="1" name="Tile Layer 1" width="30" height="20">
|
<layer id="1" name="Tile Layer 1" width="30" height="20">
|
||||||
<data encoding="csv">
|
<data encoding="csv">
|
||||||
1,1,1,1,1,1,1,1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1,1,1,1,2,
|
||||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,
|
||||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,
|
||||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,
|
||||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
|
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
|
||||||
</data>
|
</data>
|
||||||
</layer>
|
</layer>
|
||||||
</map>
|
</map>
|
||||||
|
|
|
@ -1,7 +1,14 @@
|
||||||
package com.redstrate.watersymbol;
|
package com.redstrate.watersymbol;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.maps.MapObjects;
|
||||||
|
import com.badlogic.gdx.maps.objects.RectangleMapObject;
|
||||||
|
import com.badlogic.gdx.maps.tiled.TiledMap;
|
||||||
|
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
|
||||||
|
import com.badlogic.gdx.math.Intersector;
|
||||||
|
import com.badlogic.gdx.math.Rectangle;
|
||||||
import com.badlogic.gdx.math.Vector2;
|
import com.badlogic.gdx.math.Vector2;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -20,7 +27,7 @@ public class AStar {
|
||||||
|
|
||||||
private static final int maxOpenNodes = 200;
|
private static final int maxOpenNodes = 200;
|
||||||
|
|
||||||
public static ArrayList<Vector2> path(Vector2 start, Vector2 end, List<Vector2> collisionPoints) {
|
public static ArrayList<Vector2> path(Vector2 start, Vector2 end, List<Vector2> collisionPoints, TiledMap map) {
|
||||||
Node startNode = new Node();
|
Node startNode = new Node();
|
||||||
startNode.position = start;
|
startNode.position = start;
|
||||||
|
|
||||||
|
@ -79,6 +86,17 @@ public class AStar {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TiledMapTileLayer.Cell cell = null;
|
||||||
|
try {
|
||||||
|
TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get(0);
|
||||||
|
cell = layer.getCell((int) (newNode.position.x), (int) (newNode.position.y));
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!cell.getTile().getProperties().get("walkable", Boolean.class))
|
||||||
|
shouldIgnore = true;
|
||||||
|
|
||||||
if(!shouldIgnore) {
|
if(!shouldIgnore) {
|
||||||
children.add(newNode);
|
children.add(newNode);
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,14 +55,14 @@ public class GameScreen implements Screen {
|
||||||
tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);
|
tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);
|
||||||
|
|
||||||
Unit player = new Unit();
|
Unit player = new Unit();
|
||||||
player.positionX = 0;
|
player.positionX = 1;
|
||||||
player.positionY = 0;
|
player.positionY = 1;
|
||||||
player.team = Unit.Team.Player;
|
player.team = Unit.Team.Player;
|
||||||
|
|
||||||
units.add(player);
|
units.add(player);
|
||||||
|
|
||||||
Unit player2 = new Unit();
|
Unit player2 = new Unit();
|
||||||
player2.positionX = 0;
|
player2.positionX = 1;
|
||||||
player2.positionY = 5;
|
player2.positionY = 5;
|
||||||
player2.team = Unit.Team.Player;
|
player2.team = Unit.Team.Player;
|
||||||
|
|
||||||
|
@ -70,7 +70,7 @@ public class GameScreen implements Screen {
|
||||||
|
|
||||||
Unit enemy = new Unit();
|
Unit enemy = new Unit();
|
||||||
enemy.positionX = 5;
|
enemy.positionX = 5;
|
||||||
enemy.positionY = 0;
|
enemy.positionY = 1;
|
||||||
enemy.team = Unit.Team.Enemy;
|
enemy.team = Unit.Team.Enemy;
|
||||||
|
|
||||||
units.add(enemy);
|
units.add(enemy);
|
||||||
|
@ -159,7 +159,7 @@ public class GameScreen implements Screen {
|
||||||
}
|
}
|
||||||
|
|
||||||
void moveUnitTo(int x, int y) {
|
void moveUnitTo(int x, int y) {
|
||||||
ArrayList<Vector2> path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) x, (float) y), getCollision());
|
ArrayList<Vector2> path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) x, (float) y), getCollision(), tiledMap);
|
||||||
|
|
||||||
// don't allow invalid movements
|
// don't allow invalid movements
|
||||||
if(path != null) {
|
if(path != null) {
|
||||||
|
@ -285,7 +285,7 @@ public class GameScreen implements Screen {
|
||||||
int newFixedX = (int) (newX / 32.0);
|
int newFixedX = (int) (newX / 32.0);
|
||||||
int newFixedY = (int) (newY / 32.0);
|
int newFixedY = (int) (newY / 32.0);
|
||||||
|
|
||||||
ArrayList<Vector2> path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) newFixedX, (float) newFixedY), getCollision());
|
ArrayList<Vector2> path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) newFixedX, (float) newFixedY), getCollision(), tiledMap);
|
||||||
if (path != null) {
|
if (path != null) {
|
||||||
Collections.reverse(path);
|
Collections.reverse(path);
|
||||||
List<Vector2> realPath = path.subList(0, Math.min(currentUnit.maxDistance, path.size()));
|
List<Vector2> realPath = path.subList(0, Math.min(currentUnit.maxDistance, path.size()));
|
||||||
|
@ -371,7 +371,7 @@ public class GameScreen implements Screen {
|
||||||
currentlyAwaitingAuxilaryAction = true;
|
currentlyAwaitingAuxilaryAction = true;
|
||||||
} else {
|
} else {
|
||||||
// now that we have the closest unit, lets path to it.
|
// now that we have the closest unit, lets path to it.
|
||||||
ArrayList<Vector2> path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) closestUnit.positionX, (float) closestUnit.positionY), getCollision());
|
ArrayList<Vector2> path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) closestUnit.positionX, (float) closestUnit.positionY), getCollision(), tiledMap);
|
||||||
if (path != null) {
|
if (path != null) {
|
||||||
Collections.reverse(path);
|
Collections.reverse(path);
|
||||||
List<Vector2> realPath = path.subList(0, Math.min(currentUnit.maxDistance, path.size()));
|
List<Vector2> realPath = path.subList(0, Math.min(currentUnit.maxDistance, path.size()));
|
||||||
|
|
Reference in a new issue