Created
August 4, 2015 06:35
-
-
Save smoak/d2c1997199cf168c0a29 to your computer and use it in GitHub Desktop.
TiledGame trying to get it to work
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.mygdx.game; | |
import com.badlogic.gdx.ApplicationAdapter; | |
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.graphics.GL20; | |
import com.badlogic.gdx.graphics.OrthographicCamera; | |
import com.badlogic.gdx.graphics.Texture; | |
import com.badlogic.gdx.graphics.g2d.*; | |
import com.badlogic.gdx.maps.MapLayers; | |
import com.badlogic.gdx.maps.tiled.TiledMap; | |
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell; | |
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer; | |
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer; | |
import com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile; | |
import com.badlogic.gdx.math.Vector2; | |
import com.badlogic.gdx.utils.viewport.*; | |
public class TilesGame extends ApplicationAdapter { | |
private OrthographicCamera camera; | |
private OrthogonalTiledMapRenderer mapRenderer; | |
private BitmapFont font; | |
private TiledMap tileMap; | |
private Viewport viewport; | |
private Hero hero; | |
private TextureRegion heroTextureRegion; | |
private float delta; | |
private float screenWidth, screenHeight; | |
static class Hero { | |
static float WIDTH, HEIGHT; | |
final Vector2 position = new Vector2(); | |
} | |
@Override | |
public void create() { | |
screenHeight = Gdx.graphics.getHeight(); | |
screenWidth = Gdx.graphics.getWidth(); | |
font = new BitmapFont(); | |
camera = new OrthographicCamera(); | |
viewport = new ScreenViewport(camera); | |
camera.position.set(viewport.getWorldWidth() / 2, viewport.getWorldHeight() / 2, 0); | |
viewport.apply(); | |
createMap(); | |
createHero(); | |
} | |
private void createMap() { | |
tileMap = loadMap(); | |
mapRenderer = new OrthogonalTiledMapRenderer(tileMap); | |
} | |
private TiledMap loadMap() { | |
// omitted for brevity | |
} | |
private void createHero() { | |
// omitted for brevity | |
} | |
@Override | |
public void render() { | |
Gdx.gl.glClearColor(0, 0, 0, 1); | |
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); | |
camera.update(); | |
delta = Gdx.graphics.getDeltaTime(); | |
mapRenderer.setView(camera); | |
mapRenderer.render(); | |
renderHero(); | |
// renderFPS(); | |
renderCameraCoords(); | |
} | |
private void renderCameraCoords() { | |
Batch batch = mapRenderer.getBatch(); | |
batch.begin(); | |
font.draw(batch, camera.position.toString(), 0, 0); | |
batch.end(); | |
} | |
private void renderFPS() { | |
Batch batch = mapRenderer.getBatch(); | |
batch.begin(); | |
font.draw(batch, "FPS: " + Gdx.graphics.getFramesPerSecond(), viewport.getScreenX(), 2); | |
batch.end(); | |
} | |
private void renderHero() { | |
Batch batch = mapRenderer.getBatch(); | |
batch.begin(); | |
batch.draw(heroTextureRegion, hero.position.x, hero.position.y); | |
batch.end(); | |
} | |
@Override | |
public void resize(int w, int h) { | |
viewport.update(w, h); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment