Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Graphics2D;
- public class Player {
- private double x;
- private double y;
- private double dx;
- private double dy;
- // player speed
- private double moveSpeed = 0.6;
- // player cant go faster than this speed
- private double maxSpeed = 4.2;
- // stop slowing by
- private double stopSpeed = 0.30;
- private double maxFallingSpeed = 12;
- private double jumpStart = -11.0;
- // pull you down with this speed
- private double gravity = 0.64;
- private int width = 22;
- private int height = 22;
- // user press key
- private boolean left;
- private boolean right;
- private boolean jumping;
- private boolean falling;
- // check if player can moves to these tiles
- // true = cant move
- private boolean topLeft;
- private boolean topRight;
- private boolean bottomLeft;
- private boolean bottomRight;
- private TileMap tileMap;
- /**************************/
- /*** CONSTRUCTOR METHOD ***/
- /**************************/
- public Player(TileMap tm) {
- this.tileMap = tm;
- }
- // can jump when not falling
- public void setJumping(boolean b) {
- if (!falling) {
- jumping = true;
- }
- }
- /*******************/
- /*** player Move ***/
- private void playerMove() {
- // Determine if Player is moving left or right
- if (left) { // is moving left
- dx -= moveSpeed; // move left
- if (dx < -maxSpeed) { // dont go faster than maxSpeed
- dx = -maxSpeed;
- }
- } else if (right) { // is moving right
- dx += moveSpeed; // move right
- if (dx > maxSpeed) { // dont go faster than maxSpeed
- dx = maxSpeed;
- }
- } else { // not moving so coming to stop
- if (dx > 0) { // still moving right
- dx -= stopSpeed; // start slowing down lil by lil
- if (dx < 0) { // full stop
- dx = 0;
- }
- } else if (dx < 0) { // was moving left
- dx += stopSpeed; // start slowing down lil by lil
- if (dx > 0) { // full stop
- dx = 0;
- }
- }
- }
- // Determine if player is jumping or falling
- if (jumping) {
- dy = jumpStart; // start moving up fast
- falling = true;
- jumping = false;
- }
- if (falling) {
- dy += gravity; // slowly come down
- if (dy > maxFallingSpeed) { // dont go fall faster than maxSpeed
- dy = maxFallingSpeed;
- }
- } else { // on ground
- dy = 0;
- }
- }// End of playerMove method
- /*******************************/
- /*** calculateCorners method ***/
- public void calculateCorners(double x, double y) {
- int leftTile = tileMap.getColTile((int) (x - width / 2));
- int rightTile = tileMap.getColTile((int) (x + width / 2) - 1);
- int topTile = tileMap.getColTile((int) (y - height / 2));
- int bottomTile = tileMap.getColTile((int) (y + height / 2) - 1);
- // check if player can move to tiles
- // 0 = cant move up
- topLeft = tileMap.getTile(topTile, leftTile) == 0;
- topRight = tileMap.getTile(topTile, rightTile) == 0;
- bottomLeft = tileMap.getTile(bottomTile, leftTile) == 0;
- bottomRight = tileMap.getTile(bottomTile, rightTile) == 0;
- }// End of calculateCorners
- /************************************/
- /*** playerCollision method ***/
- public void playerCollision() {
- // get current tile where player is on
- int currCol = tileMap.getColTile((int) x);
- int currRow = tileMap.getRowTile((int) y);
- // player next distence x and y
- // just add dx to x
- double tox = x + dx;
- double toy = y + dy;
- // dont way to change real x-y yet
- double tempX = x;
- double tempY = y;
- // check if player can move up or down
- calculateCorners(x, toy);
- if (dy < 0) { // moving up
- if (topLeft || topRight) { // cant move up
- dy = 0;
- tempY = currRow * tileMap.getTileSize() + height / 2;
- } else { // free to move up
- tempY += dy;
- }
- }
- if (dy > 0) { // move down
- if (bottomLeft || bottomRight) { // cant move down
- dy = 0;
- falling = false;
- tempY = (currRow + 1) * tileMap.getTileSize() - height / 2;
- } else { // free to move down
- tempY += dy;
- }
- }
- // check if player can move right or left
- calculateCorners(tox, y);
- if (dx < 0) { // moving left
- if (topLeft || bottomLeft) {
- dx = 0;
- tempX = currCol * tileMap.getTileSize() + width / 2;
- } else { // free to left
- tempX += dx;
- }
- }
- if (dx > 0) { // move right
- if (topRight || bottomRight) {
- dx = 0;
- tempX = (currCol + 1) * tileMap.getTileSize() - width / 2;
- } else {
- tempX += dx;
- }
- }
- // check for falling in hole
- if (!falling) {
- calculateCorners(x, y + 1);
- if (!bottomLeft && !bottomRight) {
- falling = true;
- }
- }
- // set x and y from temp values
- x = tempX;
- y = tempY;
- // move the map
- tileMap.setX((int) (GamePanel.WINDOW_WIDTH / 2 - x));
- tileMap.setY((int) (GamePanel.WINDOW_HEIGHT / 2 - y));
- }// End of playerCollision method
- /*********************/
- /*** update METHOD ***/
- /*********************/
- public void update() {
- playerMove();
- playerCollision();
- } // End of update method
- /*********************/
- /*** draw METHOD *****/
- /*********************/
- public void draw(Graphics2D g) {
- int tx = tileMap.getX();
- int ty = tileMap.getY();
- // (x-width/2) = so its middle point player
- g.setColor(Color.RED);
- g.fillRect((int) (tx + (x - width / 2)), (int) (ty + (y - height / 2)),
- width, height);
- }
- /***************************/
- /***************************/
- /***************************/
- /***************************/
- /***************************/
- /*** GETTERS AND SETTERS ***/
- /***************************/
- /***************************/
- /***************************/
- /***************************/
- /***************************/
- public void setLeft(boolean left) {
- this.left = left;
- }
- public void setRight(boolean right) {
- this.right = right;
- }
- public void setX(double x) {
- this.x = x;
- }
- public void setY(double y) {
- this.y = y;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement