Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public boolean fromFile(FileHandle file) {
- XmlReader reader = new XmlReader();
- XmlReader.Element xml;
- try {
- xml = reader.parse(file);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return false;
- }
- Array<Element> paths = xml.getChildByName("g").getChildrenByName("path");
- for (Element path: paths) {
- switch ( path.get("id") ) {
- case "outside":
- ArrayList<Vector2> vertexList = new ArrayList<Vector2>();
- // get the path string
- String[] pathParts = path.get("d").split(" ");
- for (String s: pathParts) {
- if (Character.isDigit(s.charAt(0)) || s.charAt(0) == '-') {
- String[] coords = s.split(",");
- vertexList.add( new Vector2( Float.parseFloat(coords[0]), Float.parseFloat(coords[1]) ) );
- }
- }
- this.vertices = new Vector2[vertexList.size()];
- this.vertices = vertexList.toArray(this.vertices);
- break;
- default:
- break;
- }
- }
- return true;
- }
- public void assignPhysicsBody(World world) {
- BodyDef bodyDef = new BodyDef();
- bodyDef.type = BodyType.StaticBody;
- this.body = world.createBody(bodyDef);
- ChainShape outside = new ChainShape();
- outside.createLoop(this.vertices);
- this.body.createFixture(outside, 0);
- outside.dispose();
- }
- @Override
- public void draw(SpriteBatch batch, float parentAlpha) {
- batch.end();
- this.shapeRenderer.setProjectionMatrix(world.getCamera().combined);
- this.shapeRenderer.identity();
- this.shapeRenderer.begin(ShapeType.Line);
- this.shapeRenderer.setColor(0, 0, 1, 1);
- int length = this.vertices.length;
- for (int i = 1; i < length; i++) {
- this.shapeRenderer.line(this.vertices[i-1].x, this.vertices[i-1].y,
- this.vertices[i].x, this.vertices[i].y);
- }
- this.shapeRenderer.line(this.vertices[length-1].x, this.vertices[length-1].y,
- this.vertices[0].x, this.vertices[0].y);
- this.shapeRenderer.end();
- batch.begin();
- }
Advertisement
Add Comment
Please, Sign In to add comment