libgdx: tiled maps

33
libGDX: Tiled Maps Jussi Pohjolainen Tampere University of Applied Sciences

Upload: jussi-pohjolainen

Post on 15-Jul-2015

475 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: libGDX: Tiled Maps

libGDX:  Tiled  Maps  

Jussi  Pohjolainen  Tampere  University  of  Applied  Sciences  

Page 2: libGDX: Tiled Maps

CREATING  TILED  MAPS    

Page 3: libGDX: Tiled Maps

Tiled  Maps  

Page 4: libGDX: Tiled Maps

Tiled  Editor:  Tiled  

•  EdiBng  tools  for  stamp,  fill  and  terrain  brushes  •  Rectangle,  ellipse,  polygon  and  image  objects  can  be  placed  

•  Support  for  orthogonal,  isometric  and  staggered  maps  

•  Download:  http://www.mapeditor.org/

Page 5: libGDX: Tiled Maps

Isometric  (3D  effect)  

Page 6: libGDX: Tiled Maps

Isometric  vs  Isometric  staggered  

Page 7: libGDX: Tiled Maps

Create  Blesheet  png  

Page 8: libGDX: Tiled Maps

CreaBng  new  Map  

Page 9: libGDX: Tiled Maps

Layers  

•  Your  world  may  contain  layers  •  You  can  disable  and  enable  layers  during  runBme  

•  You  can  add  object  layers  for  easy  collision  detec@on  

Page 10: libGDX: Tiled Maps

USING  MAPS  

Page 11: libGDX: Tiled Maps

Loading  Map  

•  To  load  a  map  – TiledMap tiledMap = tiledMap = new TmxMapLoader().load("runner.tmx");

•  To  render  a  map,  use  TiledMapRenderer  – TiledMapRenderer tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);

Page 12: libGDX: Tiled Maps

Render  and  Camera  @Overridepublic void render () { batch.setProjectionMatrix(camera.combined);

// Sets the projection matrix and viewbounds from the given // camera. If the camera changes, you have to call this method // again. The viewbounds are taken from the camera's // position and viewport size as well as the scale. tiledMapRenderer.setView(camera);

// Render all layers to screen. tiledMapRenderer.render();}

Page 13: libGDX: Tiled Maps

MOVE  CAMERA  

Page 14: libGDX: Tiled Maps

Render  and  Camera  @Overridepublic void render () { batch.setProjectionMatrix(camera.combined);

// move camera (YOU HAVE TO IMPLEMENT THIS) moveCamera();

// Update camera movement camera.update();

// Which part of the map are we looking? Use camera's viewport tiledMapRenderer.setView(camera);

// Render all layers to screen. tiledMapRenderer.render();

}

Page 15: libGDX: Tiled Maps

moveCamera()

public void moveCamera() { // Lot of possibilities.

// 1) Just move the camera to some direction: camera.translate(1,0); // moves x++

// 2) Move to certain point camera.position.x = 200;

// 3) Move to direction of some sprite camera.position.x = player.getX();}

Page 16: libGDX: Tiled Maps

COLLISION  DETECTION  

Page 17: libGDX: Tiled Maps

Collision  Checking  /** * Checks if player has collided with collectable */ private void checkCollisions() { // Let's get the collectable rectangles layer MapLayer collisionObjectLayer = (MapLayer)tiledMap.getLayers().get("collectable-rectangles"); // All the rectangles of the layer MapObjects mapObjects = collisionObjectLayer.getObjects(); // Cast it to RectangleObjects array Array<RectangleMapObject> rectangleObjects = mapObjects.getByType(RectangleMapObject.class); // Iterate all the rectangles for (RectangleMapObject rectangleObject : rectangleObjects) { Rectangle rectangle = rectangleObject.getRectangle(); if (playerSprite.getBoundingRectangle().overlaps(rectangle)) { clearCollectable(); } } }

Page 18: libGDX: Tiled Maps

MOVING  OBJECT  

Page 19: libGDX: Tiled Maps

0,0  

32px  

32px  

Page 20: libGDX: Tiled Maps

32,32  

Page 21: libGDX: Tiled Maps

32,32  

1.  Arrow  key  right  pressed  2.  Movement  x  =  x  +  5  

Page 22: libGDX: Tiled Maps

37,32  

1.  Arrow  key  right  pressed  2.  Movement  x  =  x  +  5  3.  Crash!  

Page 23: libGDX: Tiled Maps

37,32  

if  x  =  x  +  5  does  not  collide  with  background  Does  37,32  collide?  

Page 24: libGDX: Tiled Maps

37,32  

We  need  to  check  each  corner!  

69,32  !!  

69,64  37,64  

Page 25: libGDX: Tiled Maps

37,32  

Check  if  the  next  frame  collides  with  bg.  If  not,  move  to  next  frame  

69,32  !!  

69,64  37,64  

Page 26: libGDX: Tiled Maps

37,32  

if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)  &&          !upRightCollision  &&  !downRightCollision)  {            //  try  to  move  player  to  right        //  this  is  NOT  done,  since        //  !downRightCollision  is  false!          playerSprite.translateX(-­‐1  *  moveAmount);  }  

69,32  !!  

69,64  37,64  

Page 27: libGDX: Tiled Maps

ConverBng  Sprite  posiBon  to  Tiled  Map  index  

32px  

32px  

250,120  

0                        1                    2                      3                    4                      5                    6                      7                      8                    9  

8  7  6  5  4  3  2  1  0  

Page 28: libGDX: Tiled Maps

ConverBng  Sprite  posiBon  to  Tiled  Map  index  

32px  

32px  

250  /  32  =  7.8  -­‐>  7  120  /  32  =  3.8  -­‐>  3  

0                        1                    2                      3                    4                      5                    6                      7                      8                    9  

8  7  6  5  4  3  2  1  0  

Page 29: libGDX: Tiled Maps

isFree  -­‐  method  private boolean isFree(float x, float y) { // Calculate coordinates to tile coordinates // example, (34,34) => (1,1) int indexX = (int) x / TILE_WIDTH;

int indexY = (int) y / TILE_HEIGHT; TiledMapTileLayer wallCells = (TiledMapTileLayer)

tiledMap.getLayers().get("wall-layer");

// Is the coordinate / cell free? if(wallCells.getCell(indexX, indexY) != null) {

// There was a cell, it's not free return false; } else {

// There was no cell, it's free return true; } }

Page 30: libGDX: Tiled Maps

Checking  all  Corners  upleft = isFree(leftXPos, upYPos); // truedownleft = isFree(leftXPos, downYPos); // trueupright = isFree(rightXPos, upYPos); // true downright = isFree(rightXPos, downYPos); // false

Page 31: libGDX: Tiled Maps

37,32  

if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)  &&          !upRightCollision  &&  !downRightCollision)  {            //  try  to  move  player  to  right        //  this  is  NOT  done,  since        //  !downRightCollision  is  false!          playerSprite.translateX(-­‐1  *  moveAmount);  }  

69,32  !!  

69,64  37,64  

Page 32: libGDX: Tiled Maps

getMyCorners  public void getMyCorners(float pX, float pY) { // calculate all the corners of the sprite float downYPos = pY; float upYPos = playerSprite.getHeight() + downYPos; float leftXPos = pX; float rightXPos = playerSprite.getWidth() + leftXPos; // update the attributes upleft = isFree(leftXPos, upYPos); downleft = isFree(leftXPos, downYPos); upright = isFree(rightXPos, upYPos); downright = isFree(rightXPos, downYPos);}

Page 33: libGDX: Tiled Maps

movePlayer  private void movePlayer(float delta) { // moveAmount float moveAmount = SPEED * delta;

// Is there Room in left? THIS // modifies the upleft, downleft, upright and upleft attributes! getMyCorners(playerSprite.getX() + -1 * moveAmount,

playerSprite.getY());

// Move left IF there is room!

if(Gdx.input.isKeyPressed(Input.Keys.LEFT) && playerSprite.getX() > TILE_WIDTH && upleft && downleft) {

playerSprite.translateX(-1 * moveAmount); }

...