Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mayogames.zombiecubes.entities;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.graphics.g2d.TextureRegion;
- import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
- import com.badlogic.gdx.math.Rectangle;
- import com.badlogic.gdx.scenes.scene2d.ui.Touchpad;
- import com.mayogames.zombiecubes.Assets;
- import com.mayogames.zombiecubes.World;
- import com.mayogames.zombiecubes.ZombieCubes;
- public class Player {
- ZombieCubes zombieCubes;
- private World world;
- private Touchpad touchpad;
- TextureRegion currentPlayerSprite;
- private float x;
- private float y;
- private float speedX;
- private float speedY;
- private int speedBoost;
- private int dir;
- private int rotation;
- private int rotationRightHand;
- private int addX;
- private int addY;
- private int rightHandAdjX;
- private int rightHandAdjY;
- private int moveSpeed = 3;
- private int maxHP = 3;
- private int hp = maxHP;
- private boolean invincible = false;
- private int reloadDivident = 1;
- private boolean drawHands = true;
- public Rectangle rectPlayer = new Rectangle(0, 0, 0, 0);
- private TextureRegion currentFrame;
- private float stateTime;
- /** the String for solid tiles in the tile properties */
- private String solidKey = "Solid";
- public Player(int x, int y, ZombieCubes zombieCubes, World world, Touchpad touchpad) {
- this.x = x;
- this.y = y;
- this.zombieCubes = zombieCubes;
- this.world = world;
- this.touchpad = touchpad;
- currentPlayerSprite = Assets.player1;
- }
- private float boundingTolerance = 2;
- public void tick() {
- move();
- rectPlayer.set(x, y, 32, 32);
- //Collision
- //Save old position
- float oldX = getX();
- float oldY = getY();
- float tileWidth = world.getCollisionLayer().getTileWidth(), tileHeight = world.getCollisionLayer().getTileHeight();
- boolean collisionX = false, collisionY = false;
- Cell tmpCell;
- rectPlayer.setSize(rectPlayer.getWidth() / world.getUnitScale(), rectPlayer.getWidth() / world.getUnitScale());
- // move on x
- setX(getX() + speedX);
- if(speedX < 0) { // going left
- // top left
- if((tmpCell = world.getCollisionLayer().getCell((int) ((getX() + boundingTolerance) / tileWidth), (int) ((getY() + rectPlayer.getHeight() - boundingTolerance) / tileHeight))).getTile() != null)
- collisionX = isCellSolid(tmpCell);
- // middle left
- if(!collisionX && (tmpCell = world.getCollisionLayer().getCell((int) ((getX() + boundingTolerance) / tileWidth), (int) ((getY() + rectPlayer.getHeight() / 2) / tileHeight))).getTile() != null)
- collisionX = isCellSolid(tmpCell);
- // bottom left
- if(!collisionX && (tmpCell = world.getCollisionLayer().getCell((int) ((getX() + boundingTolerance) / tileWidth), (int) ((getY() + boundingTolerance) / tileHeight))).getTile() != null)
- collisionX = isCellSolid(tmpCell);
- } else if(speedX > 0) { // going right
- // top right
- if((tmpCell = world.getCollisionLayer().getCell((int) ((getX() - boundingTolerance + rectPlayer.getWidth()) / tileWidth), (int) ((getY() - boundingTolerance + rectPlayer.getHeight()) / tileHeight))) != null) {
- collisionX = isCellSolid(tmpCell);
- }
- // middle right
- if(!collisionX && (tmpCell = world.getCollisionLayer().getCell((int) ((getX() - boundingTolerance + rectPlayer.getWidth()) / tileWidth), (int) ((int) (getY() + rectPlayer.getHeight() / 2) / tileHeight))) != null) {
- collisionX = isCellSolid(tmpCell);
- }
- // bottom right
- if(!collisionX && (tmpCell = world.getCollisionLayer().getCell((int) ((getX() - boundingTolerance + rectPlayer.getWidth()) / tileWidth), (int) ((int) (getY() + boundingTolerance) / tileHeight))).getTile() != null) {
- collisionX = isCellSolid(tmpCell);
- }
- }
- // react to x collision
- if(collisionX) {
- setX(oldX);
- speedX = 0;
- }
- // move on y
- setY(getY() + speedY);
- if(speedY < 0) { // going down
- // bottom left
- if((tmpCell = world.getCollisionLayer().getCell((int) ((getX() + boundingTolerance) / tileWidth), (int) ((getY() + boundingTolerance) / tileHeight))).getTile() != null)
- collisionY = isCellSolid(tmpCell);
- // bottom middle
- if(!collisionY && (tmpCell = world.getCollisionLayer().getCell((int) ((getX() + rectPlayer.getWidth() / 2) / tileWidth), (int) ((getY() + boundingTolerance) / tileHeight))).getTile() != null)
- collisionY = isCellSolid(tmpCell);
- // bottom right
- if(!collisionY && (tmpCell = world.getCollisionLayer().getCell((int) ((getX() - boundingTolerance + rectPlayer.getWidth()) / tileWidth), (int) ((getY() + boundingTolerance) / tileHeight))).getTile() != null)
- collisionY = isCellSolid(tmpCell);
- } else if(speedY > 0) { // going up
- // top left
- if((tmpCell = world.getCollisionLayer().getCell((int) ((getX() + boundingTolerance) / tileWidth), (int) ((getY() - boundingTolerance + rectPlayer.getHeight()) / tileHeight))).getTile() != null)
- collisionY = isCellSolid(tmpCell);
- // top middle
- if(!collisionY && (tmpCell = world.getCollisionLayer().getCell((int) ((getX() + rectPlayer.getWidth() / 2) / tileWidth), (int) ((getY() - boundingTolerance + rectPlayer.getHeight()) / tileHeight))).getTile() != null)
- collisionY = isCellSolid(tmpCell);
- // top right
- if(!collisionY && (tmpCell = world.getCollisionLayer().getCell((int) ((getX() - boundingTolerance + rectPlayer.getWidth()) / tileWidth), (int) ((getY() - boundingTolerance + rectPlayer.getHeight()) / tileHeight))).getTile() != null)
- collisionY = isCellSolid(tmpCell);
- }
- // react to y collision
- if(collisionY) {
- setY(oldY);
- speedY = 0;
- }
- rectPlayer.setSize(rectPlayer.getWidth() * world.getUnitScale(), rectPlayer.getHeight() * world.getUnitScale());
- }
- private boolean isCellSolid(Cell cell) {
- return cell.getTile().getProperties().containsKey(solidKey);
- }
- public void render(SpriteBatch batch) {
- stateTime += Gdx.graphics.getDeltaTime();
- if(speedX == 0 && speedY == 0) {
- currentFrame = Assets.playerHand;
- } else {
- currentFrame = Assets.playerHandAnimation.getKeyFrame(stateTime, true);
- }
- batch.draw(currentPlayerSprite, x, y, 32, 32);
- if(drawHands) {
- batch.draw(currentFrame, x - 17 + addX, y + addY, currentFrame.getRegionWidth() / 2, currentFrame.getRegionHeight() / 2, 32, 32, 1, 1, rotation);
- batch.draw(currentFrame, x + 18 - addX - rightHandAdjX, y - addY - rightHandAdjY, currentFrame.getRegionWidth() / 2, currentFrame.getRegionHeight() / 2, 32, 32, 1, 1, rotationRightHand);
- }
- }
- public void faceDirection() {
- switch(dir) {
- case 1:
- rotation = 0;
- rotationRightHand = 180;
- addX = 0;
- addY = 0;
- rightHandAdjX = 1;
- rightHandAdjY = 0;
- break;
- case 2:
- rotation = 145;
- rotationRightHand = 325;
- addX = 5;
- addY = 14;
- rightHandAdjX = 0;
- rightHandAdjY = -2;
- break;
- case 3:
- rotation = 90;
- rotationRightHand = 270;
- addX = 17;
- addY = 18;
- rightHandAdjX = -1;
- rightHandAdjY = 0;
- break;
- case 4:
- rotation = 225;
- rotationRightHand = 405;
- addX = 30;
- addY = 12;
- break;
- case 5:
- rotation = 180;
- rotationRightHand = 360;
- addX = 34;
- addY = 0;
- rightHandAdjX = 1;
- break;
- case 6:
- rotation = 145;
- rotationRightHand = 325;
- addX = 30;
- addY = -10;
- rightHandAdjX = 0;
- rightHandAdjY = -2;
- break;
- case 7:
- rotation = 270;
- rotationRightHand = 450;
- addX = 17;
- addY = -18;
- rightHandAdjX = 0;
- rightHandAdjY = 0;
- break;
- case 8:
- rotation = 225;
- rotationRightHand = 405;
- addX = 5;
- addY = -13;
- break;
- default:
- rotation = 0;
- rotationRightHand = 180;
- addX = 0;
- addY = 0;
- rightHandAdjX = 2;
- break;
- }
- }
- public void move() {
- if(Gdx.input.isKeyPressed(Input.Keys.W) || touchpad.getKnobPercentY() > 0.2f) {
- speedY = (moveSpeed + speedBoost) * touchpad.getKnobPercentY();
- dir = 1;
- } else if(Gdx.input.isKeyPressed(Input.Keys.S) || touchpad.getKnobPercentY() < -0.2f) {
- speedY = (-moveSpeed - speedBoost) * -touchpad.getKnobPercentY();
- dir = 5;
- } else {
- speedY = 0;
- }
- if(Gdx.input.isKeyPressed(Input.Keys.A) || touchpad.getKnobPercentX() < -0.2f) {
- speedX = (-moveSpeed - speedBoost) * -touchpad.getKnobPercentX();
- dir = 7;
- } else if(Gdx.input.isKeyPressed(Input.Keys.D) || touchpad.getKnobPercentX() > 0.2f) {
- speedX = (moveSpeed + speedBoost) * touchpad.getKnobPercentX();
- dir = 3;
- } else {
- speedX = 0;
- }
- //Two keys
- if(Gdx.input.isKeyPressed(Input.Keys.W) && Gdx.input.isKeyPressed(Input.Keys.D) || touchpad.getKnobPercentY() > 0.2f && touchpad.getKnobPercentX() > 0.2f)
- dir = 2;
- if(Gdx.input.isKeyPressed(Input.Keys.S) && Gdx.input.isKeyPressed(Input.Keys.D) || touchpad.getKnobPercentY() < -0.2f && touchpad.getKnobPercentX() > 0.2f)
- dir = 4;
- if(Gdx.input.isKeyPressed(Input.Keys.S) && Gdx.input.isKeyPressed(Input.Keys.A) || touchpad.getKnobPercentY() < -0.2f && touchpad.getKnobPercentX() < -0.2f)
- dir = 6;
- if(Gdx.input.isKeyPressed(Input.Keys.W) && Gdx.input.isKeyPressed(Input.Keys.A) || touchpad.getKnobPercentY() > 0.2f && touchpad.getKnobPercentX() < -0.2f)
- dir = 8;
- faceDirection();
- }
- public void gainHP(int healthPoints) {
- if(hp < maxHP && hp > 0)
- hp += healthPoints;
- setCurrentPlayerSprite();
- }
- public void loseHP(int damage) {
- if(invincible == false && hp > 0)
- hp -= damage;
- setCurrentPlayerSprite();
- }
- public void setCurrentPlayerSprite() {
- if(hp >= 3)
- currentPlayerSprite = Assets.player1;
- if(hp == 2)
- currentPlayerSprite = Assets.player2;
- if(hp == 1)
- currentPlayerSprite = Assets.player3;
- if(hp <= 0)
- currentPlayerSprite = Assets.zombieCube;
- if(invincible == true)
- currentPlayerSprite = Assets.playerMetal;
- }
- //Getters and setters
- public float getX() {
- return x;
- }
- public void setX(float x) {
- this.x = x;
- }
- public float getY() {
- return y;
- }
- public void setY(float y) {
- this.y = y;
- }
- public float getSpeedX() {
- return speedX;
- }
- public void setSpeedX(float speedX) {
- this.speedX = speedX;
- }
- public float getSpeedY() {
- return speedY;
- }
- public void setSpeedY(float speedY) {
- this.speedY = speedY;
- }
- public int getHp() {
- return hp;
- }
- public void setHp(int hp) {
- this.hp = hp;
- }
- public int getMaxHP() {
- return maxHP;
- }
- public void setMaxHP(int maxHP) {
- this.maxHP = maxHP;
- }
- public Rectangle getRectPlayer() {
- return rectPlayer;
- }
- public void setRectPlayer(Rectangle rectPlayer) {
- this.rectPlayer = rectPlayer;
- }
- public boolean isDrawHands() {
- return drawHands;
- }
- public void setDrawHands(boolean drawHands) {
- this.drawHands = drawHands;
- }
- public int getDir() {
- return dir;
- }
- public void setDir(int dir) {
- this.dir = dir;
- }
- public boolean isInvincible() {
- return invincible;
- }
- public void setInvincible(boolean invincible) {
- this.invincible = invincible;
- }
- public int getMoveSpeed() {
- return moveSpeed;
- }
- public void setMoveSpeed(int moveSpeed) {
- this.moveSpeed = moveSpeed;
- }
- public int getSpeedBoost() {
- return speedBoost;
- }
- public void setSpeedBoost(int speedBoost) {
- this.speedBoost = speedBoost;
- }
- //the reload divident is here because the player class is used by both powerUps and weapon
- public int getReloadDivident() {
- return reloadDivident;
- }
- public void setReloadDivident(int reloadDivident) {
- this.reloadDivident = reloadDivident;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement