Skip to content

Instantly share code, notes, and snippets.

@JoseRivas1998
Last active July 19, 2017 17:22
Show Gist options
  • Select an option

  • Save JoseRivas1998/5b8cf4c1d3080c11794d to your computer and use it in GitHub Desktop.

Select an option

Save JoseRivas1998/5b8cf4c1d3080c11794d to your computer and use it in GitHub Desktop.
Tiled Programming Tutorial
package com.sshsgd.tiled;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
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.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
public class Game extends ApplicationAdapter {
World w;
OrthographicCamera cam;
ShapeRenderer sr;
@Override
public void create () {
w = new World();
float width = Gdx.graphics.getWidth();
float height = Gdx.graphics.getHeight();
cam = new OrthographicCamera(width, height);
cam.translate(width * .5f, height * .5f);
cam.update();
sr = new ShapeRenderer();
}
@Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if(Gdx.input.isKeyPressed(Keys.UP)) {
cam.position.y++;
}
if(Gdx.input.isKeyPressed(Keys.DOWN)) {
cam.position.y--;
}
if(Gdx.input.isKeyPressed(Keys.RIGHT)) {
cam.position.x++;
}
if(Gdx.input.isKeyPressed(Keys.LEFT)) {
cam.position.x--;
}
cam.update();
w.render(cam);
sr.begin(ShapeType.Line);
sr.setProjectionMatrix(cam.combined);
sr.setColor(Color.BLUE);
for(Rectangle r : w.getBounds()) {
sr.rect(r.x, r.y, r.width, r.height);
}
for(Vector2 v : w.getObjects()) {
sr.ellipse(v.x, v.y, 10, 10);
}
sr.end();
}
@Override
public void dispose() {
sr.dispose();
}
}
package com.sshsgd.tiled;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.maps.MapLayer;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.objects.EllipseMapObject;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Ellipse;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
public class World {
private Array<Rectangle> bounds;
private Array<Vector2> objects;
private TiledMap tileMap;
private OrthogonalTiledMapRenderer tmr;
private float tileSize;
public World() {
bounds = new Array<Rectangle>();
objects = new Array<Vector2>();
createTiles();
}
private void createTiles() {
tileMap = new TmxMapLoader().load("maps/map1.tmx");
tmr = new OrthogonalTiledMapRenderer(tileMap);
tileSize = tileMap.getProperties().get("tilewidth", Integer.class);
TiledMapTileLayer ground;
MapLayer object;
ground = (TiledMapTileLayer) tileMap.getLayers().get("Tile Layer 1");
object = tileMap.getLayers().get("Object Layer 1");
createLayer(ground, bounds);
createObjectLayer(object, objects);
}
private void createLayer(TiledMapTileLayer layer, Array<Rectangle> rect) {
for(int row = 0; row < layer.getHeight(); row++) {
for(int col = 0; col < layer.getWidth(); col++) {
Cell cell = layer.getCell(col, row);
if(cell == null) continue;
if(cell.getTile() == null) continue;
rect.add(new Rectangle(col * tileSize, row * tileSize, tileSize, tileSize));
}
}
}
private void createObjectLayer(MapLayer layer, Array<Vector2> objects) {
for(MapObject mo : layer.getObjects()) {
Ellipse e = ((EllipseMapObject) mo).getEllipse();
float x = e.x;
float y = e.y;
objects.add(new Vector2(x, y));
}
}
public void render(OrthographicCamera cam) {
tmr.setView(cam);
tmr.render();
}
public Array<Rectangle> getBounds() {
return bounds;
}
public void setBounds(Array<Rectangle> bounds) {
this.bounds = bounds;
}
public Array<Vector2> getObjects() {
return objects;
}
public void setObjects(Array<Vector2> objects) {
this.objects = objects;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment