Advertisement
dermetfan

Atle517's player class 12.07.2013

Aug 11th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.53 KB | None | 0 0
  1. package com.mayogames.zombiecubes.entities;
  2.  
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.Input;
  5. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  6. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  7. import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
  8. import com.badlogic.gdx.math.Rectangle;
  9. import com.badlogic.gdx.scenes.scene2d.ui.Touchpad;
  10. import com.mayogames.zombiecubes.Assets;
  11. import com.mayogames.zombiecubes.World;
  12. import com.mayogames.zombiecubes.ZombieCubes;
  13.  
  14. public class Player {
  15.  
  16.     ZombieCubes zombieCubes;
  17.     private World world;
  18.     private Touchpad touchpad;
  19.  
  20.     TextureRegion currentPlayerSprite;
  21.  
  22.     private float x;
  23.     private float y;
  24.  
  25.     private float speedX;
  26.     private float speedY;
  27.  
  28.     private int speedBoost;
  29.  
  30.     private int dir;
  31.     private int rotation;
  32.     private int rotationRightHand;
  33.     private int addX;
  34.     private int addY;
  35.     private int rightHandAdjX;
  36.     private int rightHandAdjY;
  37.  
  38.     private int moveSpeed = 3;
  39.  
  40.     private int maxHP = 3;
  41.     private int hp = maxHP;
  42.  
  43.     private boolean invincible = false;
  44.     private int reloadDivident = 1;
  45.     private boolean drawHands = true;
  46.  
  47.     public Rectangle rectPlayer = new Rectangle(0, 0, 0, 0);
  48.  
  49.     private TextureRegion currentFrame;
  50.     private float stateTime;
  51.  
  52.     /** the String for solid tiles in the tile properties */
  53.     private String solidKey = "Solid";
  54.  
  55.     public Player(int x, int y, ZombieCubes zombieCubes, World world, Touchpad touchpad) {
  56.         this.x = x;
  57.         this.y = y;
  58.         this.zombieCubes = zombieCubes;
  59.         this.world = world;
  60.         this.touchpad = touchpad;
  61.  
  62.         currentPlayerSprite = Assets.player1;
  63.     }
  64.  
  65.     private float boundingTolerance = 2;
  66.  
  67.     public void tick() {
  68.         move();
  69.         rectPlayer.set(x, y, 32, 32);
  70.  
  71.         //Collision
  72.         //Save old position
  73.         float oldX = getX();
  74.         float oldY = getY();
  75.         float tileWidth = world.getCollisionLayer().getTileWidth(), tileHeight = world.getCollisionLayer().getTileHeight();
  76.         boolean collisionX = false, collisionY = false;
  77.         Cell tmpCell;
  78.  
  79.         rectPlayer.setSize(rectPlayer.getWidth() / world.getUnitScale(), rectPlayer.getWidth() / world.getUnitScale());
  80.  
  81.         // move on x
  82.         setX(getX() + speedX);
  83.  
  84.         if(speedX < 0) { // going left
  85.             // top left
  86.             if((tmpCell = world.getCollisionLayer().getCell((int) ((getX() + boundingTolerance) / tileWidth), (int) ((getY() + rectPlayer.getHeight() - boundingTolerance) / tileHeight))).getTile() != null)
  87.                 collisionX = isCellSolid(tmpCell);
  88.  
  89.             // middle left
  90.             if(!collisionX && (tmpCell = world.getCollisionLayer().getCell((int) ((getX() + boundingTolerance) / tileWidth), (int) ((getY() + rectPlayer.getHeight() / 2) / tileHeight))).getTile() != null)
  91.                 collisionX = isCellSolid(tmpCell);
  92.  
  93.             // bottom left
  94.             if(!collisionX && (tmpCell = world.getCollisionLayer().getCell((int) ((getX() + boundingTolerance) / tileWidth), (int) ((getY() + boundingTolerance) / tileHeight))).getTile() != null)
  95.                 collisionX = isCellSolid(tmpCell);
  96.         } else if(speedX > 0) { // going right
  97.             // top right
  98.             if((tmpCell = world.getCollisionLayer().getCell((int) ((getX() - boundingTolerance + rectPlayer.getWidth()) / tileWidth), (int) ((getY() - boundingTolerance + rectPlayer.getHeight()) / tileHeight))) != null) {
  99.                 collisionX = isCellSolid(tmpCell);
  100.             }
  101.  
  102.             // middle right
  103.             if(!collisionX && (tmpCell = world.getCollisionLayer().getCell((int) ((getX() - boundingTolerance + rectPlayer.getWidth()) / tileWidth), (int) ((int) (getY() + rectPlayer.getHeight() / 2) / tileHeight))) != null) {
  104.                 collisionX = isCellSolid(tmpCell);
  105.             }
  106.  
  107.             // bottom right
  108.             if(!collisionX && (tmpCell = world.getCollisionLayer().getCell((int) ((getX() - boundingTolerance + rectPlayer.getWidth()) / tileWidth), (int) ((int) (getY() + boundingTolerance) / tileHeight))).getTile() != null) {
  109.                 collisionX = isCellSolid(tmpCell);
  110.             }
  111.         }
  112.  
  113.         // react to x collision
  114.         if(collisionX) {
  115.             setX(oldX);
  116.             speedX = 0;
  117.         }
  118.  
  119.         // move on y
  120.         setY(getY() + speedY);
  121.  
  122.         if(speedY < 0) { // going down
  123.             // bottom left
  124.             if((tmpCell = world.getCollisionLayer().getCell((int) ((getX() + boundingTolerance) / tileWidth), (int) ((getY() + boundingTolerance) / tileHeight))).getTile() != null)
  125.                 collisionY = isCellSolid(tmpCell);
  126.  
  127.             // bottom middle
  128.             if(!collisionY && (tmpCell = world.getCollisionLayer().getCell((int) ((getX() + rectPlayer.getWidth() / 2) / tileWidth), (int) ((getY() + boundingTolerance) / tileHeight))).getTile() != null)
  129.                 collisionY = isCellSolid(tmpCell);
  130.  
  131.             // bottom right
  132.             if(!collisionY && (tmpCell = world.getCollisionLayer().getCell((int) ((getX() - boundingTolerance + rectPlayer.getWidth()) / tileWidth), (int) ((getY() + boundingTolerance) / tileHeight))).getTile() != null)
  133.                 collisionY = isCellSolid(tmpCell);
  134.         } else if(speedY > 0) { // going up
  135.             // top left
  136.             if((tmpCell = world.getCollisionLayer().getCell((int) ((getX() + boundingTolerance) / tileWidth), (int) ((getY() - boundingTolerance + rectPlayer.getHeight()) / tileHeight))).getTile() != null)
  137.                 collisionY = isCellSolid(tmpCell);
  138.  
  139.             // top middle
  140.             if(!collisionY && (tmpCell = world.getCollisionLayer().getCell((int) ((getX() + rectPlayer.getWidth() / 2) / tileWidth), (int) ((getY() - boundingTolerance + rectPlayer.getHeight()) / tileHeight))).getTile() != null)
  141.                 collisionY = isCellSolid(tmpCell);
  142.  
  143.             // top right
  144.             if(!collisionY && (tmpCell = world.getCollisionLayer().getCell((int) ((getX() - boundingTolerance + rectPlayer.getWidth()) / tileWidth), (int) ((getY() - boundingTolerance + rectPlayer.getHeight()) / tileHeight))).getTile() != null)
  145.                 collisionY = isCellSolid(tmpCell);
  146.         }
  147.  
  148.         // react to y collision
  149.         if(collisionY) {
  150.             setY(oldY);
  151.             speedY = 0;
  152.         }
  153.  
  154.         rectPlayer.setSize(rectPlayer.getWidth() * world.getUnitScale(), rectPlayer.getHeight() * world.getUnitScale());
  155.     }
  156.  
  157.     private boolean isCellSolid(Cell cell) {
  158.         return cell.getTile().getProperties().containsKey(solidKey);
  159.     }
  160.  
  161.     public void render(SpriteBatch batch) {
  162.         stateTime += Gdx.graphics.getDeltaTime();
  163.  
  164.         if(speedX == 0 && speedY == 0) {
  165.             currentFrame = Assets.playerHand;
  166.         } else {
  167.             currentFrame = Assets.playerHandAnimation.getKeyFrame(stateTime, true);
  168.         }
  169.  
  170.         batch.draw(currentPlayerSprite, x, y, 32, 32);
  171.  
  172.         if(drawHands) {
  173.             batch.draw(currentFrame, x - 17 + addX, y + addY, currentFrame.getRegionWidth() / 2, currentFrame.getRegionHeight() / 2, 32, 32, 1, 1, rotation);
  174.             batch.draw(currentFrame, x + 18 - addX - rightHandAdjX, y - addY - rightHandAdjY, currentFrame.getRegionWidth() / 2, currentFrame.getRegionHeight() / 2, 32, 32, 1, 1, rotationRightHand);
  175.         }
  176.  
  177.     }
  178.  
  179.     public void faceDirection() {
  180.         switch(dir) {
  181.  
  182.         case 1:
  183.             rotation = 0;
  184.             rotationRightHand = 180;
  185.             addX = 0;
  186.             addY = 0;
  187.             rightHandAdjX = 1;
  188.             rightHandAdjY = 0;
  189.             break;
  190.  
  191.         case 2:
  192.             rotation = 145;
  193.             rotationRightHand = 325;
  194.             addX = 5;
  195.             addY = 14;
  196.             rightHandAdjX = 0;
  197.             rightHandAdjY = -2;
  198.             break;
  199.  
  200.         case 3:
  201.             rotation = 90;
  202.             rotationRightHand = 270;
  203.             addX = 17;
  204.             addY = 18;
  205.             rightHandAdjX = -1;
  206.             rightHandAdjY = 0;
  207.             break;
  208.  
  209.         case 4:
  210.             rotation = 225;
  211.             rotationRightHand = 405;
  212.             addX = 30;
  213.             addY = 12;
  214.             break;
  215.  
  216.         case 5:
  217.             rotation = 180;
  218.             rotationRightHand = 360;
  219.             addX = 34;
  220.             addY = 0;
  221.             rightHandAdjX = 1;
  222.             break;
  223.  
  224.         case 6:
  225.             rotation = 145;
  226.             rotationRightHand = 325;
  227.             addX = 30;
  228.             addY = -10;
  229.             rightHandAdjX = 0;
  230.             rightHandAdjY = -2;
  231.             break;
  232.  
  233.         case 7:
  234.             rotation = 270;
  235.             rotationRightHand = 450;
  236.             addX = 17;
  237.             addY = -18;
  238.             rightHandAdjX = 0;
  239.             rightHandAdjY = 0;
  240.             break;
  241.  
  242.         case 8:
  243.             rotation = 225;
  244.             rotationRightHand = 405;
  245.             addX = 5;
  246.             addY = -13;
  247.             break;
  248.  
  249.         default:
  250.             rotation = 0;
  251.             rotationRightHand = 180;
  252.             addX = 0;
  253.             addY = 0;
  254.             rightHandAdjX = 2;
  255.             break;
  256.         }
  257.     }
  258.  
  259.     public void move() {
  260.  
  261.         if(Gdx.input.isKeyPressed(Input.Keys.W) || touchpad.getKnobPercentY() > 0.2f) {
  262.             speedY = (moveSpeed + speedBoost) * touchpad.getKnobPercentY();
  263.             dir = 1;
  264.         } else if(Gdx.input.isKeyPressed(Input.Keys.S) || touchpad.getKnobPercentY() < -0.2f) {
  265.             speedY = (-moveSpeed - speedBoost) * -touchpad.getKnobPercentY();
  266.             dir = 5;
  267.         } else {
  268.             speedY = 0;
  269.         }
  270.  
  271.         if(Gdx.input.isKeyPressed(Input.Keys.A) || touchpad.getKnobPercentX() < -0.2f) {
  272.             speedX = (-moveSpeed - speedBoost) * -touchpad.getKnobPercentX();
  273.             dir = 7;
  274.         } else if(Gdx.input.isKeyPressed(Input.Keys.D) || touchpad.getKnobPercentX() > 0.2f) {
  275.             speedX = (moveSpeed + speedBoost) * touchpad.getKnobPercentX();
  276.             dir = 3;
  277.         } else {
  278.             speedX = 0;
  279.         }
  280.  
  281.         //Two keys
  282.         if(Gdx.input.isKeyPressed(Input.Keys.W) && Gdx.input.isKeyPressed(Input.Keys.D) || touchpad.getKnobPercentY() > 0.2f && touchpad.getKnobPercentX() > 0.2f)
  283.             dir = 2;
  284.         if(Gdx.input.isKeyPressed(Input.Keys.S) && Gdx.input.isKeyPressed(Input.Keys.D) || touchpad.getKnobPercentY() < -0.2f && touchpad.getKnobPercentX() > 0.2f)
  285.             dir = 4;
  286.         if(Gdx.input.isKeyPressed(Input.Keys.S) && Gdx.input.isKeyPressed(Input.Keys.A) || touchpad.getKnobPercentY() < -0.2f && touchpad.getKnobPercentX() < -0.2f)
  287.             dir = 6;
  288.         if(Gdx.input.isKeyPressed(Input.Keys.W) && Gdx.input.isKeyPressed(Input.Keys.A) || touchpad.getKnobPercentY() > 0.2f && touchpad.getKnobPercentX() < -0.2f)
  289.             dir = 8;
  290.  
  291.         faceDirection();
  292.  
  293.     }
  294.  
  295.     public void gainHP(int healthPoints) {
  296.         if(hp < maxHP && hp > 0)
  297.             hp += healthPoints;
  298.  
  299.         setCurrentPlayerSprite();
  300.     }
  301.  
  302.     public void loseHP(int damage) {
  303.         if(invincible == false && hp > 0)
  304.             hp -= damage;
  305.  
  306.         setCurrentPlayerSprite();
  307.     }
  308.  
  309.     public void setCurrentPlayerSprite() {
  310.         if(hp >= 3)
  311.             currentPlayerSprite = Assets.player1;
  312.         if(hp == 2)
  313.             currentPlayerSprite = Assets.player2;
  314.         if(hp == 1)
  315.             currentPlayerSprite = Assets.player3;
  316.         if(hp <= 0)
  317.             currentPlayerSprite = Assets.zombieCube;
  318.  
  319.         if(invincible == true)
  320.             currentPlayerSprite = Assets.playerMetal;
  321.     }
  322.  
  323.     //Getters and setters
  324.     public float getX() {
  325.         return x;
  326.     }
  327.  
  328.     public void setX(float x) {
  329.         this.x = x;
  330.     }
  331.  
  332.     public float getY() {
  333.         return y;
  334.     }
  335.  
  336.     public void setY(float y) {
  337.         this.y = y;
  338.     }
  339.  
  340.     public float getSpeedX() {
  341.         return speedX;
  342.     }
  343.  
  344.     public void setSpeedX(float speedX) {
  345.         this.speedX = speedX;
  346.     }
  347.  
  348.     public float getSpeedY() {
  349.         return speedY;
  350.     }
  351.  
  352.     public void setSpeedY(float speedY) {
  353.         this.speedY = speedY;
  354.     }
  355.  
  356.     public int getHp() {
  357.         return hp;
  358.     }
  359.  
  360.     public void setHp(int hp) {
  361.         this.hp = hp;
  362.     }
  363.  
  364.     public int getMaxHP() {
  365.         return maxHP;
  366.     }
  367.  
  368.     public void setMaxHP(int maxHP) {
  369.         this.maxHP = maxHP;
  370.     }
  371.  
  372.     public Rectangle getRectPlayer() {
  373.         return rectPlayer;
  374.     }
  375.  
  376.     public void setRectPlayer(Rectangle rectPlayer) {
  377.         this.rectPlayer = rectPlayer;
  378.     }
  379.  
  380.     public boolean isDrawHands() {
  381.         return drawHands;
  382.     }
  383.  
  384.     public void setDrawHands(boolean drawHands) {
  385.         this.drawHands = drawHands;
  386.     }
  387.  
  388.     public int getDir() {
  389.         return dir;
  390.     }
  391.  
  392.     public void setDir(int dir) {
  393.         this.dir = dir;
  394.     }
  395.  
  396.     public boolean isInvincible() {
  397.         return invincible;
  398.     }
  399.  
  400.     public void setInvincible(boolean invincible) {
  401.         this.invincible = invincible;
  402.     }
  403.  
  404.     public int getMoveSpeed() {
  405.         return moveSpeed;
  406.     }
  407.  
  408.     public void setMoveSpeed(int moveSpeed) {
  409.         this.moveSpeed = moveSpeed;
  410.     }
  411.  
  412.     public int getSpeedBoost() {
  413.         return speedBoost;
  414.     }
  415.  
  416.     public void setSpeedBoost(int speedBoost) {
  417.         this.speedBoost = speedBoost;
  418.     }
  419.  
  420.     //the reload divident is here because the player class is used by both powerUps and weapon
  421.     public int getReloadDivident() {
  422.         return reloadDivident;
  423.     }
  424.  
  425.     public void setReloadDivident(int reloadDivident) {
  426.         this.reloadDivident = reloadDivident;
  427.     }
  428.  
  429. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement