diff --git a/assets/main.tsx b/assets/main.tsx index 8b9c5ce..7649ac0 100644 --- a/assets/main.tsx +++ b/assets/main.tsx @@ -1,7 +1,16 @@ - + - + + + + + + + + + + diff --git a/assets/test.tmx b/assets/test.tmx index 03930fd..b8519f0 100644 --- a/assets/test.tmx +++ b/assets/test.tmx @@ -1,28 +1,28 @@ - + -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, -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, -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, -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, -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, -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, -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, -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, -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, -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, -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, -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, -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, -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,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,1,1,1,1,1,1,1,1,1,1,2, +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, +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, +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, +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, +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, +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, +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, +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, +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 diff --git a/core/src/com/redstrate/watersymbol/AStar.java b/core/src/com/redstrate/watersymbol/AStar.java index d6412f4..e882323 100644 --- a/core/src/com/redstrate/watersymbol/AStar.java +++ b/core/src/com/redstrate/watersymbol/AStar.java @@ -1,7 +1,14 @@ 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 java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.List; @@ -20,7 +27,7 @@ public class AStar { private static final int maxOpenNodes = 200; - public static ArrayList path(Vector2 start, Vector2 end, List collisionPoints) { + public static ArrayList path(Vector2 start, Vector2 end, List collisionPoints, TiledMap map) { Node startNode = new Node(); 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) { children.add(newNode); } diff --git a/core/src/com/redstrate/watersymbol/screens/GameScreen.java b/core/src/com/redstrate/watersymbol/screens/GameScreen.java index 6c34843..c88f2a4 100644 --- a/core/src/com/redstrate/watersymbol/screens/GameScreen.java +++ b/core/src/com/redstrate/watersymbol/screens/GameScreen.java @@ -55,14 +55,14 @@ public class GameScreen implements Screen { tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap); Unit player = new Unit(); - player.positionX = 0; - player.positionY = 0; + player.positionX = 1; + player.positionY = 1; player.team = Unit.Team.Player; units.add(player); Unit player2 = new Unit(); - player2.positionX = 0; + player2.positionX = 1; player2.positionY = 5; player2.team = Unit.Team.Player; @@ -70,7 +70,7 @@ public class GameScreen implements Screen { Unit enemy = new Unit(); enemy.positionX = 5; - enemy.positionY = 0; + enemy.positionY = 1; enemy.team = Unit.Team.Enemy; units.add(enemy); @@ -159,7 +159,7 @@ public class GameScreen implements Screen { } void moveUnitTo(int x, int y) { - ArrayList path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) x, (float) y), getCollision()); + ArrayList path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) x, (float) y), getCollision(), tiledMap); // don't allow invalid movements if(path != null) { @@ -285,7 +285,7 @@ public class GameScreen implements Screen { int newFixedX = (int) (newX / 32.0); int newFixedY = (int) (newY / 32.0); - ArrayList path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) newFixedX, (float) newFixedY), getCollision()); + ArrayList path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) newFixedX, (float) newFixedY), getCollision(), tiledMap); if (path != null) { Collections.reverse(path); List realPath = path.subList(0, Math.min(currentUnit.maxDistance, path.size())); @@ -371,7 +371,7 @@ public class GameScreen implements Screen { currentlyAwaitingAuxilaryAction = true; } else { // now that we have the closest unit, lets path to it. - ArrayList path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) closestUnit.positionX, (float) closestUnit.positionY), getCollision()); + ArrayList path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) closestUnit.positionX, (float) closestUnit.positionY), getCollision(), tiledMap); if (path != null) { Collections.reverse(path); List realPath = path.subList(0, Math.min(currentUnit.maxDistance, path.size()));