Advertisement
dermetfan

Atle517's collision detection how i'd do it

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