Forked from implicit-invocation/TestPathfindingScreen.java
Created
June 29, 2019 06:51
-
-
Save Ali-RS/bff7f1a7fd676fe4fbc4f459228960e4 to your computer and use it in GitHub Desktop.
jwalkable example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<map version="1.0" tiledversion="1.0.1" orientation="orthogonal" renderorder="right-down" width="64" height="48" tilewidth="10" tileheight="10" nextobjectid="6"> | |
<objectgroup name="Obstacles"> | |
<object id="1" x="60" y="100.163"> | |
<polygon points="0,0 1.97011,187.16 210.802,202.921 200.951,153.668 31.5217,149.728 43.3424,0"/> | |
</object> | |
<object id="2" x="136.834" y="45"> | |
<polygon points="0,0 338.859,21.6712 395.992,336.889 336.889,332.948 275.815,78.8043 39.4022,57.1332"/> | |
</object> | |
<object id="3" x="184.117" y="157.296"> | |
<polygon points="0,0 185.19,9.85054 232.473,226.562 179.28,238.383 139.878,45.3125 -3.94022,43.3424"/> | |
</object> | |
<object id="4" x="71.8207" y="307.024"> | |
<polygon points="0,0 250.204,13.7908 250.204,100.476 177.31,94.5652 187.16,37.4321 0,33.4918"/> | |
</object> | |
<object id="5" x="25" y="417.5"> | |
<polyline points="0,0 182.5,37.5 107.5,-32.5 160,-45 247.5,42.5 557.5,40 562.5,-372.5"/> | |
</object> | |
</objectgroup> | |
</map> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dongbat.example; | |
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.Input; | |
import com.badlogic.gdx.ScreenAdapter; | |
import com.badlogic.gdx.graphics.Color; | |
import com.badlogic.gdx.graphics.GL20; | |
import com.badlogic.gdx.graphics.OrthographicCamera; | |
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; | |
import com.badlogic.gdx.maps.MapLayer; | |
import com.badlogic.gdx.maps.MapObject; | |
import com.badlogic.gdx.maps.objects.PolygonMapObject; | |
import com.badlogic.gdx.maps.objects.PolylineMapObject; | |
import com.badlogic.gdx.maps.tiled.TiledMap; | |
import com.badlogic.gdx.maps.tiled.TmxMapLoader; | |
import com.badlogic.gdx.math.Polygon; | |
import com.badlogic.gdx.math.Polyline; | |
import com.badlogic.gdx.math.Vector2; | |
import com.badlogic.gdx.math.Vector3; | |
import com.dongbat.walkable.FloatArray; | |
import com.dongbat.walkable.PathHelper; | |
/** | |
* | |
* @author tao | |
*/ | |
public class TestPathfindingScreen extends ScreenAdapter { | |
private final ShapeRenderer shapeRenderer; | |
private final TiledMap map; | |
private final OrthographicCamera camera; | |
private final Vector2 start = new Vector2(); | |
private final Vector2 end = new Vector2(); | |
private final Vector3 tmp = new Vector3(); | |
private final PathHelper pathHelper; | |
private final FloatArray path = new FloatArray(); | |
private float radius = 0; | |
public TestPathfindingScreen() { | |
camera = new OrthographicCamera(); | |
camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); | |
map = new TmxMapLoader().load("path.tmx"); | |
shapeRenderer = new ShapeRenderer(); | |
pathHelper = new PathHelper(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); | |
MapLayer obstacles = map.getLayers().get("Obstacles"); | |
for (MapObject object : obstacles.getObjects()) { | |
if (object instanceof PolygonMapObject) { | |
PolygonMapObject polygonMapObject = (PolygonMapObject) object; | |
Polygon polygon = polygonMapObject.getPolygon(); | |
pathHelper.addPolygon(polygon.getTransformedVertices()); | |
} | |
if (object instanceof PolylineMapObject) { | |
PolylineMapObject polylineMapObject = (PolylineMapObject) object; | |
Polyline polyline = polylineMapObject.getPolyline(); | |
pathHelper.addPolyline(polyline.getTransformedVertices()); | |
} | |
} | |
} | |
@Override | |
public void render(float delta) { | |
Gdx.gl.glClearColor(0, 0, 0, 1); | |
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); | |
camera.update(); | |
shapeRenderer.setProjectionMatrix(camera.combined); | |
tmp.set(Gdx.input.getX(), Gdx.input.getY(), 0); | |
camera.unproject(tmp); | |
if (Gdx.input.isKeyPressed(Input.Keys.EQUALS)) { | |
radius += 10f * delta; | |
} | |
if (Gdx.input.isKeyPressed(Input.Keys.MINUS)) { | |
if (radius > 1) { | |
radius -= 10f * delta; | |
} | |
} | |
if (Gdx.input.isTouched()) { | |
start.set(tmp.x, tmp.y); | |
} | |
end.set(tmp.x, tmp.y); | |
shapeRenderer.setColor(Color.WHITE); | |
MapLayer obstacles = map.getLayers().get("Obstacles"); | |
for (MapObject object : obstacles.getObjects()) { | |
if (object instanceof PolygonMapObject) { | |
PolygonMapObject polygonMapObject = (PolygonMapObject) object; | |
Polygon polygon = polygonMapObject.getPolygon(); | |
shapeRenderer.begin(ShapeRenderer.ShapeType.Line); | |
shapeRenderer.polygon(polygon.getTransformedVertices()); | |
shapeRenderer.end(); | |
} | |
if (object instanceof PolylineMapObject) { | |
PolylineMapObject polylineMapObject = (PolylineMapObject) object; | |
Polyline polyline = polylineMapObject.getPolyline(); | |
shapeRenderer.begin(ShapeRenderer.ShapeType.Line); | |
shapeRenderer.polyline(polyline.getTransformedVertices()); | |
shapeRenderer.end(); | |
} | |
} | |
pathHelper.findPath(start.x, start.y, end.x, end.y, radius, path); | |
shapeRenderer.setColor(Color.GREEN); | |
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled); | |
shapeRenderer.circle(start.x, start.y, radius); | |
shapeRenderer.end(); | |
shapeRenderer.setColor(Color.RED); | |
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled); | |
shapeRenderer.circle(end.x, end.y, radius); | |
shapeRenderer.end(); | |
float prevX = 0, prevY = 0; | |
shapeRenderer.setColor(Color.YELLOW); | |
for (int i = 0; i < path.size; i += 2) { | |
float x = path.get(i); | |
float y = path.get(i + 1); | |
if (i > 0) { | |
shapeRenderer.begin(ShapeRenderer.ShapeType.Line); | |
shapeRenderer.line(prevX, prevY, x, y); | |
shapeRenderer.end(); | |
} | |
prevX = x; | |
prevY = y; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment