dermetfan

Atle517's player class 21.08.2013

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