Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.LLGames.mygdxgame.entities;
- import com.badlogic.gdx.Input.Keys;
- import com.badlogic.gdx.InputProcessor;
- import com.badlogic.gdx.graphics.g2d.Sprite;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
- public class Player extends Sprite implements InputProcessor {
- int x, y;
- private TiledMapTileLayer collisionLayer;
- public Player(Sprite sprite, TiledMapTileLayer collisionlayer) {
- super(sprite);
- this.collisionLayer = collisionLayer;
- }
- @Override
- public void draw(SpriteBatch spriteBatch) {
- update();
- super.draw(spriteBatch);
- }
- public void update() {
- setX(x);
- setY(y);
- }
- public void pos(int xa, int ya) {
- x = xa;
- y = ya;
- }
- public int getPos(int a) {
- int b = 0;
- if (a == 0) {b = x;}
- else {b = y;}
- return b;
- }
- //My problem is in the following function. Without collision detection, movements work fine :)
- //My tiles are 32 * 32 and my player is a png 32 width and 64 height
- public void move(int mov){
- switch (mov) {
- case 0 : // UP
- if(!isCellBlocked((x + 16)/ 32, (y + 64) / 32));
- y += 32;
- break ;
- case 1 : // DOWN
- if(!isCellBlocked((x + 16) / 32, y / 32))
- y -= 32;
- break ;
- case 2 : // LEFT
- if(!isCellBlocked((x - 32) / 32, y / 32))
- x -= 32;
- break ;
- case 3 : // RIGHT
- if(!isCellBlocked((x + 64) / 32, (y + 64) / 32))
- x += 32;
- break ;
- }
- }
- public boolean isCellBlocked(int x, int y) {
- Cell cell = collisionLayer.getCell(x, y);
- if(cell == null)
- return false; // the cell is not blocked (because there's none there)
- TiledMapTile tile = cell.getTile();
- if(tile != null) // check if there's a tile in the cell first to avoid an exception
- return tile.getProperties().containsKey("blocked");
- return false; // if there's no tile in the cell, it's not blocked as well
- }
- @Override
- public boolean keyDown(int keycode) {
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- public boolean keyUp(int keycode) {
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- public boolean keyTyped(char character) {
- switch(character) {
- case 'Z' :
- case 'z' :
- move(0);
- break ;
- case 'Q' :
- case 'q' :
- move(2);
- break ;
- case 'S' :
- case 's' :
- move(1);
- break ;
- case 'D' :
- case 'd' :
- move(3);
- break ;
- }
- return true;
- }
- @Override
- public boolean touchDown(int screenX, int screenY, int pointer, int button) {
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- public boolean touchUp(int screenX, int screenY, int pointer, int button) {
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- public boolean touchDragged(int screenX, int screenY, int pointer) {
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- public boolean mouseMoved(int screenX, int screenY) {
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- public boolean scrolled(int amount) {
- // TODO Auto-generated method stub
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment