use of com.badlogic.gdx.maps.tiled.TiledMapTileLayer in project libgdx by libgdx.
the class TiledMapBench method create.
@Override
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera();
camera.setToOrtho(false, (w / h) * 320, 320);
camera.update();
cameraController = new OrthoCamController(camera);
Gdx.input.setInputProcessor(cameraController);
font = new BitmapFont();
batch = new SpriteBatch();
{
tiles = new Texture(Gdx.files.internal("data/maps/tiled/tiles.png"));
TextureRegion[][] splitTiles = TextureRegion.split(tiles, 32, 32);
map = new TiledMap();
MapLayers layers = map.getLayers();
for (int l = 0; l < 20; l++) {
TiledMapTileLayer layer = new TiledMapTileLayer(150, 100, 32, 32);
for (int x = 0; x < 150; x++) {
for (int y = 0; y < 100; y++) {
int ty = (int) (Math.random() * splitTiles.length);
int tx = (int) (Math.random() * splitTiles[ty].length);
Cell cell = new Cell();
cell.setTile(new StaticTiledMapTile(splitTiles[ty][tx]));
layer.setCell(x, y, cell);
}
}
layers.add(layer);
}
}
renderer = new OrthogonalTiledMapRenderer(map);
}
use of com.badlogic.gdx.maps.tiled.TiledMapTileLayer in project libgdx by libgdx.
the class SuperKoalio method updateKoala.
private void updateKoala(float deltaTime) {
if (deltaTime == 0)
return;
if (deltaTime > 0.1f)
deltaTime = 0.1f;
koala.stateTime += deltaTime;
// check input and apply to velocity & state
if ((Gdx.input.isKeyPressed(Keys.SPACE) || isTouched(0.5f, 1)) && koala.grounded) {
koala.velocity.y += Koala.JUMP_VELOCITY;
koala.state = Koala.State.Jumping;
koala.grounded = false;
}
if (Gdx.input.isKeyPressed(Keys.LEFT) || Gdx.input.isKeyPressed(Keys.A) || isTouched(0, 0.25f)) {
koala.velocity.x = -Koala.MAX_VELOCITY;
if (koala.grounded)
koala.state = Koala.State.Walking;
koala.facesRight = false;
}
if (Gdx.input.isKeyPressed(Keys.RIGHT) || Gdx.input.isKeyPressed(Keys.D) || isTouched(0.25f, 0.5f)) {
koala.velocity.x = Koala.MAX_VELOCITY;
if (koala.grounded)
koala.state = Koala.State.Walking;
koala.facesRight = true;
}
if (Gdx.input.isKeyJustPressed(Keys.B))
debug = !debug;
// apply gravity if we are falling
koala.velocity.add(0, GRAVITY);
// clamp the velocity to the maximum, x-axis only
koala.velocity.x = MathUtils.clamp(koala.velocity.x, -Koala.MAX_VELOCITY, Koala.MAX_VELOCITY);
// If the velocity is < 1, set it to 0 and set state to Standing
if (Math.abs(koala.velocity.x) < 1) {
koala.velocity.x = 0;
if (koala.grounded)
koala.state = Koala.State.Standing;
}
// multiply by delta time so we know how far we go
// in this frame
koala.velocity.scl(deltaTime);
// perform collision detection & response, on each axis, separately
// if the koala is moving right, check the tiles to the right of it's
// right bounding box edge, otherwise check the ones to the left
Rectangle koalaRect = rectPool.obtain();
koalaRect.set(koala.position.x, koala.position.y, Koala.WIDTH, Koala.HEIGHT);
int startX, startY, endX, endY;
if (koala.velocity.x > 0) {
startX = endX = (int) (koala.position.x + Koala.WIDTH + koala.velocity.x);
} else {
startX = endX = (int) (koala.position.x + koala.velocity.x);
}
startY = (int) (koala.position.y);
endY = (int) (koala.position.y + Koala.HEIGHT);
getTiles(startX, startY, endX, endY, tiles);
koalaRect.x += koala.velocity.x;
for (Rectangle tile : tiles) {
if (koalaRect.overlaps(tile)) {
koala.velocity.x = 0;
break;
}
}
koalaRect.x = koala.position.x;
// top bounding box edge, otherwise check the ones to the bottom
if (koala.velocity.y > 0) {
startY = endY = (int) (koala.position.y + Koala.HEIGHT + koala.velocity.y);
} else {
startY = endY = (int) (koala.position.y + koala.velocity.y);
}
startX = (int) (koala.position.x);
endX = (int) (koala.position.x + Koala.WIDTH);
getTiles(startX, startY, endX, endY, tiles);
koalaRect.y += koala.velocity.y;
for (Rectangle tile : tiles) {
if (koalaRect.overlaps(tile)) {
// this removes bouncing :)
if (koala.velocity.y > 0) {
koala.position.y = tile.y - Koala.HEIGHT;
// we hit a block jumping upwards, let's destroy it!
TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get("walls");
layer.setCell((int) tile.x, (int) tile.y, null);
} else {
koala.position.y = tile.y + tile.height;
// if we hit the ground, mark us as grounded so we can jump
koala.grounded = true;
}
koala.velocity.y = 0;
break;
}
}
rectPool.free(koalaRect);
// unscale the velocity by the inverse delta time and set
// the latest position
koala.position.add(koala.velocity);
koala.velocity.scl(1 / deltaTime);
// Apply damping to the velocity on the x-axis so we don't
// walk infinitely once a key was pressed
koala.velocity.x *= Koala.DAMPING;
}
use of com.badlogic.gdx.maps.tiled.TiledMapTileLayer in project ultimate-java by pantinor.
the class DungeonRoomTiledMapLoader method load.
public TiledMap load() {
TiledMap map = new TiledMap();
MapProperties mapProperties = map.getProperties();
mapProperties.put("dungeonRoom", room);
mapProperties.put("orientation", "orthogonal");
mapProperties.put("width", mapWidth);
mapProperties.put("height", mapHeight);
mapProperties.put("tilewidth", tileWidth);
mapProperties.put("tileheight", tileHeight);
mapProperties.put("backgroundcolor", "#000000");
TiledMapTileLayer layer = new TiledMapTileLayer(mapWidth, mapHeight, tileWidth, tileHeight);
layer.setName("Map Layer");
layer.setVisible(true);
for (int y = 0; y < mapHeight; y++) {
for (int x = 0; x < mapWidth; x++) {
Tile ct = room.getTile(x, y);
// if (room.getTriggerAt(x, y) != null) ct = GameScreen.baseTileSet.getTileByIndex(3); //temp debugging triggers
Cell cell = new Cell();
TextureRegion tileRegion = atlas.findRegion(ct.getName());
TiledMapTile tmt = new StaticTiledMapTile(tileRegion);
tmt.setId(y * mapWidth + x);
cell.setTile(tmt);
layer.setCell(x, mapHeight - 1 - y, cell);
}
}
map.getLayers().add(layer);
try {
loadCombatPositions(map);
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
use of com.badlogic.gdx.maps.tiled.TiledMapTileLayer in project ultimate-java by pantinor.
the class UltimaMapRenderer method renderTileLayer.
@Override
public void renderTileLayer(TiledMapTileLayer layer) {
stateTime += Gdx.graphics.getDeltaTime();
Color batchColor = batch.getColor();
// Color.toFloatBits(batchColor.r, batchColor.g, batchColor.b, batchColor.a * layer.getOpacity());
float color = 0;
int layerWidth = layer.getWidth();
int layerHeight = layer.getHeight();
int layerTileWidth = (int) (layer.getTileWidth() * unitScale);
int layerTileHeight = (int) (layer.getTileHeight() * unitScale);
float[] vertices = this.vertices;
int col1 = Math.max(0, (int) (viewBounds.x / layerTileWidth));
int col2 = Math.min(layerWidth, (int) ((viewBounds.x + viewBounds.width + layerTileWidth) / layerTileWidth));
int row1 = Math.max(0, (int) (viewBounds.y / layerTileHeight));
int row2 = Math.min(layerHeight, (int) ((viewBounds.y + viewBounds.height + layerTileHeight) / layerTileHeight));
if (bm.getBorderbehavior() == MapBorderBehavior.wrap) {
col1 = (int) (viewBounds.x / layerTileWidth);
col2 = (int) ((viewBounds.x + viewBounds.width + layerTileWidth) / layerTileWidth);
row1 = (int) (viewBounds.y / layerTileHeight);
row2 = (int) ((viewBounds.y + viewBounds.height + layerTileHeight) / layerTileHeight);
}
float y = row2 * layerTileHeight;
float startX = col1 * layerTileWidth;
for (int row = row2; row >= row1; row--) {
float x = startX;
for (int col = col1; col < col2; col++) {
TiledMapTileLayer.Cell cell = null;
if (bm.getBorderbehavior() == MapBorderBehavior.wrap) {
int cx = col;
boolean wrapped = false;
if (col < 0) {
cx = layerWidth + col;
wrapped = true;
} else if (col >= layerWidth) {
cx = col - layerWidth;
wrapped = true;
}
int cy = row;
if (row < 0) {
cy = layerHeight + row;
wrapped = true;
} else if (row >= layerHeight) {
cy = row - layerHeight;
wrapped = true;
}
cell = layer.getCell(cx, cy);
color = wrapped ? getColor(batchColor, -1, -1) : getColor(batchColor, cx, layerHeight - cy - 1);
} else {
cell = layer.getCell(col, row);
color = getColor(batchColor, col, layerHeight - row - 1);
}
if (cell == null) {
x += layerTileWidth;
continue;
}
TiledMapTile tile = cell.getTile();
if (tile != null) {
TextureRegion region = tile.getTextureRegion();
DoorStatus ds = bm.getDoor(col, layerHeight - row - 1);
if (ds != null) {
region = door;
if (ds.locked) {
region = locked_door;
}
if (bm.isDoorOpen(ds)) {
region = brick_floor;
}
}
float x1 = x + tile.getOffsetX() * unitScale;
float y1 = y + tile.getOffsetY() * unitScale;
float x2 = x1 + region.getRegionWidth() * unitScale;
float y2 = y1 + region.getRegionHeight() * unitScale;
float u1 = region.getU();
float v1 = region.getV2();
float u2 = region.getU2();
float v2 = region.getV();
vertices[X1] = x1;
vertices[Y1] = y1;
vertices[C1] = color;
vertices[U1] = u1;
vertices[V1] = v1;
vertices[X2] = x1;
vertices[Y2] = y2;
vertices[C2] = color;
vertices[U2] = u1;
vertices[V2] = v2;
vertices[X3] = x2;
vertices[Y3] = y2;
vertices[C3] = color;
vertices[U3] = u2;
vertices[V3] = v2;
vertices[X4] = x2;
vertices[Y4] = y1;
vertices[C4] = color;
vertices[U4] = u2;
vertices[V4] = v1;
batch.draw(region.getTexture(), vertices, 0, 20);
}
x += layerTileWidth;
}
y -= layerTileHeight;
}
if (bm.getCity() != null) {
for (Person p : bm.getCity().getPeople()) {
if (p == null || p.isRemovedFromMap()) {
continue;
}
if (p.getAnim() != null) {
draw(p.getAnim().getKeyFrame(stateTime, true), p.getCurrentPos().x, p.getCurrentPos().y, p.getX(), p.getY());
} else {
draw(p.getTextureRegion(), p.getCurrentPos().x, p.getCurrentPos().y, p.getX(), p.getY());
}
}
}
List<Creature> crs = bm.getCreatures();
if (crs.size() > 0) {
for (Creature cr : crs) {
if (cr.currentPos == null || !cr.getVisible()) {
continue;
}
if (cr.getTile() == CreatureType.pirate_ship) {
TextureRegion tr = cr.getAnim().getKeyFrames()[cr.sailDir.getVal() - 1];
draw(tr, cr.currentPos.x, cr.currentPos.y, cr.currentX, cr.currentY);
} else {
draw(cr.getAnim().getKeyFrame(stateTime, true), cr.currentPos.x, cr.currentPos.y, cr.currentX, cr.currentY);
}
}
}
}
use of com.badlogic.gdx.maps.tiled.TiledMapTileLayer in project ultimate-java by pantinor.
the class UltimaTiledMapLoader method load.
// for intro map
public TiledMap load(byte[] bytes, int width, int height, TileSet ts, int tileDim) {
this.mapWidth = width;
this.mapHeight = height;
Tile[] tiles = new Tile[width * height];
int pos = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// convert a byte to an unsigned int value
int index = bytes[pos] & 0xff;
pos++;
Tile tile = ts.getTileByIndex(index);
if (tile == null) {
System.out.println("Tile index cannot be found: " + index + " using index 129 for black space.");
tile = ts.getTileByIndex(129);
}
tiles[x + y * width] = tile;
}
}
TiledMap map = new TiledMap();
MapProperties mapProperties = map.getProperties();
mapProperties.put("name", "none");
mapProperties.put("id", "none");
mapProperties.put("orientation", "orthogonal");
mapProperties.put("width", mapWidth);
mapProperties.put("height", mapHeight);
mapProperties.put("tilewidth", tileDim);
mapProperties.put("tileheight", tileDim);
mapProperties.put("backgroundcolor", "#000000");
TiledMapTileLayer layer = new TiledMapTileLayer(mapWidth, mapHeight, tileWidth, tileHeight);
layer.setName("Map Layer");
layer.setVisible(true);
int dx = 0, dy = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
Tile ct = tiles[x + y * width];
Cell cell = new Cell();
Array<TextureAtlas.AtlasRegion> tileRegions = atlas.findRegions(ct.getName());
Array<StaticTiledMapTile> ar = new Array<>();
for (TextureAtlas.AtlasRegion r : tileRegions) {
ar.add(new StaticTiledMapTile(r));
}
if (ar.size == 0) {
System.out.println(ct.getName());
}
TiledMapTile tmt = null;
if (tileRegions.size > 1) {
tmt = new AnimatedTiledMapTile(.7f, ar);
} else {
tmt = ar.first();
}
tmt.setId(dy * mapWidth + dx);
cell.setTile(tmt);
layer.setCell(dx, mapHeight - 1 - dy, cell);
dx++;
}
dx = 0;
dy++;
}
map.getLayers().add(layer);
return map;
}
Aggregations