Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package br.player;
- import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
- import com.badlogic.gdx.math.Vector2;
- /**
- *
- * @author André Vinícius Lopes
- */
- public class PlayerMovement {
- private Player player;
- private final int default_x_width = 30;
- private final int default_y_height = 40;
- private final String BLOCKED = "blocked";
- private final String SLOPED = "slope";
- private float oldY, veryOldY;
- //For very private use of this class
- private TiledMapTileLayer.Cell cell, cell1;
- public PlayerMovement(Player p) {
- player = p;
- }
- public void updateMovement(TiledMapTileLayer collisionLayer, Vector2 velocity, float gravity, float delta, float speed, float jump) {
- // apply gravity
- velocity.y -= gravity * delta;
- // clamp velocity
- if (velocity.y > jump) {
- velocity.y = jump;
- } else if (velocity.y < -gravity) {
- velocity.y = -gravity;
- }
- // save old position
- double oldX = player.getX();
- double oldY = player.getY();
- double tileWidth = collisionLayer.getTileWidth();
- double tileHeight = collisionLayer.getTileHeight();
- boolean collisionX = false;
- boolean collisionY = false;
- boolean slopeY = false;
- boolean slopeX = false;
- // move on y
- player.setY(player.getY() + velocity.y * delta);
- if (velocity.y < 0) { // going down
- // bottom left
- cell = collisionLayer.getCell((int) (player.getX() / tileWidth), (int) (player.getY() / tileHeight));
- if (cell != null) {
- collisionY = cell.getTile().getProperties().containsKey(BLOCKED);
- slopeY = cell.getTile().getProperties().containsKey(SLOPED);
- }
- // bottom middle
- if (!collisionY) {
- cell1 = collisionLayer.getCell((int) ((player.getX() + default_x_width / 2) / tileWidth), (int) (player.getY() / tileHeight));
- if (cell1 != null) {
- collisionY = cell1.getTile().getProperties().containsKey(BLOCKED);
- slopeY = cell1.getTile().getProperties().containsKey(SLOPED);
- }
- }
- // bottom right
- if (!collisionY) {
- cell1 = collisionLayer.getCell((int) ((player.getX() + default_x_width) / tileWidth), (int) (player.getY() / tileHeight));
- if (cell1 != null) {
- collisionY = cell1.getTile().getProperties().containsKey(BLOCKED);
- slopeY = cell1.getTile().getProperties().containsKey(SLOPED);
- }
- }
- //player.canJump = collisionY;
- } else if (velocity.y > 0) { // going up
- // top left
- cell = collisionLayer.getCell((int) (player.getX() / tileWidth), (int) ((player.getY() + default_y_height) / tileHeight));
- if (cell != null) {
- collisionY = cell.getTile().getProperties().containsKey(BLOCKED);
- slopeY = cell.getTile().getProperties().containsKey(SLOPED);
- }
- // top middle
- if (!collisionY) {
- cell1 = collisionLayer.getCell((int) ((player.getX() + default_x_width / 2) / tileWidth), (int) ((player.getY() + default_y_height) / tileHeight));
- if (cell1 != null) {
- collisionY = cell1.getTile().getProperties().containsKey(BLOCKED);
- slopeY = cell1.getTile().getProperties().containsKey(SLOPED);
- }
- }
- // top right
- if (!collisionY) {
- cell1 = collisionLayer.getCell((int) ((player.getX() + default_x_width) / tileWidth), (int) ((player.getY() + default_y_height) / tileHeight));
- if (cell1 != null) {
- collisionY = cell1.getTile().getProperties().containsKey(BLOCKED);
- slopeY = cell1.getTile().getProperties().containsKey(SLOPED);
- }
- }
- }
- // react to y collision
- if (collisionY && !slopeY) {
- player.setY((float) oldY);
- velocity.y = 0;
- }
- if (slopeY) {
- if (!player.pressingLeft && !player.pressingRight) {
- player.setY((float) oldY); // Because its senofTheta
- velocity.y = 0;
- } else {
- //player.setY(oldY + (0.8f * 2)); // Because its senofTheta
- player.setY((float) oldY + (0.8f * 4)); // Because its senofTheta
- velocity.y = 0;
- }
- }
- //Can Jump
- if (collisionY || slopeY) {
- player.canJump = true;
- } else {
- player.canJump = false;
- }
- // move on x
- player.setX(player.getX() + velocity.x * delta);
- if (velocity.x < 0) { // going left
- // top left
- cell = collisionLayer.getCell((int) (player.getX() / tileWidth), (int) ((player.getY() + default_y_height) / tileHeight));
- if (cell != null) {
- slopeX = cell.getTile().getProperties().containsKey(SLOPED);
- collisionX = cell.getTile().getProperties().containsKey(BLOCKED);
- }
- // middle left
- if (!collisionX) {
- cell1 = collisionLayer.getCell((int) (player.getX() / tileWidth), (int) ((player.getY() + default_y_height / 2) / tileHeight));
- if (cell1 != null) {
- slopeX = cell1.getTile().getProperties().containsKey(SLOPED);
- collisionX = cell1.getTile().getProperties().containsKey(BLOCKED);
- }
- }
- // bottom left
- if (!collisionX) {
- cell1 = collisionLayer.getCell((int) (player.getX() / tileWidth), (int) (player.getY() / tileHeight));
- if (cell1 != null) {
- slopeX = cell1.getTile().getProperties().containsKey(SLOPED);
- collisionX = cell1.getTile().getProperties().containsKey(BLOCKED);
- }
- }
- } else if (velocity.x > 0) { // going right
- // top right
- cell = collisionLayer.getCell((int) ((player.getX() + default_x_width) / tileWidth), (int) ((player.getY() + default_y_height) / tileHeight));
- if (cell != null) {
- slopeX = cell.getTile().getProperties().containsKey(SLOPED);
- collisionX = cell.getTile().getProperties().containsKey(BLOCKED);
- }
- // middle right
- if (!collisionX) {
- cell1 = collisionLayer.getCell((int) ((player.getX() + default_x_width) / tileWidth), (int) ((player.getY() + default_y_height / 2) / tileHeight));
- if (cell1 != null) {
- slopeX = cell1.getTile().getProperties().containsKey(SLOPED);
- collisionX = cell1.getTile().getProperties().containsKey(BLOCKED);
- }
- }
- // bottom right
- if (!collisionX) {
- cell1 = collisionLayer.getCell((int) ((player.getX() + default_x_width) / tileWidth), (int) (player.getY() / tileHeight));
- if (cell1 != null) {
- slopeX = cell1.getTile().getProperties().containsKey(SLOPED);
- collisionX = cell1.getTile().getProperties().containsKey(BLOCKED);
- }
- }
- }
- /*
- System.out.println("NanoTime" + System.nanoTime());
- System.out.println("Player Collision X" + collisionX);
- System.out.println("Player Collision Y" + collisionY);
- System.out.println("Player Slope X" + slopeX);
- System.out.println("Player Slope Y" + slopeY);
- System.out.println("Player Can Jump" + player.canJump);
- System.out.println("\n");
- */
- // react to x collision
- if (collisionX && !slopeX) {
- player.setX((float) oldX);
- velocity.x = 0;
- }
- if (slopeX && velocity.x != 0) {
- if (slopeX && velocity.x > 0) {
- player.setX((float) oldX + 1.5f);
- }
- if (slopeX && velocity.x < 0) {
- player.setX((float) oldX - 1.5f);
- }
- if (slopeX && velocity.x == 0) {
- //?????
- }
- }
- }
- public void updateMovement2(TiledMapTileLayer collisionLayer, Vector2 velocity, float gravity, float delta, float speed, float jump) {
- // apply gravity
- velocity.y -= gravity * delta;
- player.setY(player.getY() + velocity.y * delta);
- // clamp velocity
- if (velocity.y > jump) {
- velocity.y = jump;
- } else if (velocity.y < -gravity) {
- velocity.y = -gravity;
- }
- // save old position
- double oldX = player.getX();
- double oldY = player.getY();
- int tw = 1;
- int th = 1;
- int px = (int) (player.getX() / 16);
- int py = (int) (player.getY() / 16);
- int spriteHeight = 46 / 16;
- int spriteWeight = player.getRegionWidth() / 16;
- boolean collisionY = false;
- boolean collisionX = false;
- player.setY(player.getY() + velocity.y * delta);
- System.out.println("");
- //Analyze Y Below Sprite
- if (!collisionY) {
- //Check Block 04
- TiledMapTileLayer.Cell cell04 = collisionLayer.getCell(px, (py));
- if (cell04 != null) {
- collisionY = cell04.getTile().getProperties().containsKey("blocked");
- System.out.println("Block 04");
- }
- }
- if (!collisionY) {
- //Check Block 05
- TiledMapTileLayer.Cell cell05 = collisionLayer.getCell(px + tw, (py));
- if (cell05 != null) {
- collisionY = cell05.getTile().getProperties().containsKey("blocked");
- System.out.println("Block 05");
- }
- }
- if (!collisionY) {
- //Check Block 06
- TiledMapTileLayer.Cell cell06 = collisionLayer.getCell(px + 2 * tw, (py));
- if (cell06 != null) {
- collisionY = cell06.getTile().getProperties().containsKey("blocked");
- System.out.println("Block 06");
- }
- }
- // react to y collision
- if (collisionY) {
- player.setY((float) oldY);
- //velocity.y = 0;
- player.canJump = true;
- }
- //End of Analyzing
- //Analyze going left
- if (velocity.x < 0) {
- if (!collisionX) {
- //Check Block 0
- TiledMapTileLayer.Cell cell0 = collisionLayer.getCell(px - tw, py + 2 * th);
- if (cell0 != null) {
- collisionX = cell0.getTile().getProperties().containsKey("blocked");
- System.out.println("Block 0");
- }
- }
- if (!collisionX) {
- //Check Block 1
- TiledMapTileLayer.Cell cell1 = collisionLayer.getCell(px - tw, (py + th));
- if (cell1 != null) {
- collisionX = cell1.getTile().getProperties().containsKey("blocked");
- System.out.println("Block 1");
- }
- }
- if (!collisionX) {
- //Check Block 2
- TiledMapTileLayer.Cell cell2 = collisionLayer.getCell(px - tw, (py));
- if (cell2 != null) {
- //collisionX = cell2.getTile().getProperties().containsKey("blocked");
- System.out.println("Block 2");
- }
- }
- //End of Analyzing Going left
- }
- if (velocity.x > 0) {
- //Analyzing Going RIght
- //Check Block 7
- if (!collisionX) {
- TiledMapTileLayer.Cell cell7 = collisionLayer.getCell((px + (3 * tw)), (py));
- if (cell7 != null) {
- //collisionX = cell7.getTile().getProperties().containsKey("blocked");
- System.out.println("Block 7");
- }
- }
- if (!collisionX) {
- //Check Block 8
- TiledMapTileLayer.Cell cell8 = collisionLayer.getCell(px + 3 * tw, (py + th));
- if (cell8 != null) {
- collisionX = cell8.getTile().getProperties().containsKey("blocked");
- System.out.println("Block 8");
- }
- }
- if (!collisionX) {
- //Check Block 9
- TiledMapTileLayer.Cell cell9 = collisionLayer.getCell(px + 3 * tw, (py + 2 * th));
- if (cell9 != null) {
- collisionX = cell9.getTile().getProperties().containsKey("blocked");
- System.out.println("Block 9");
- }
- }
- }
- player.setX(player.getX() + velocity.x * delta);
- if (collisionX) {
- player.setX((float) oldX);
- velocity.x = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement