Advertisement
Guest User

Rock.Java

a guest
Jan 20th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.36 KB | None | 0 0
  1.     @Override
  2.     public void updateCollision(LinkedList<MapEntity> mapEntitys, final Player player, final int translateX, final int translateY) {
  3.         this.player = player;
  4.         /* Booleans to detect the players direction */
  5.         boolean movingLeft = player.getGame().mapMovingLeft;
  6.         boolean movingRight = player.getGame().mapMovingRight;
  7.         boolean movingDown = player.getGame().mapMovingDown;
  8.         boolean movingUp = player.getGame().mapMovingUp;
  9.  
  10.         /* Entity's coordinates, relative to the map */
  11.         int startX = getMapEntityX() + translateX;
  12.         int startY = getMapEntityY() + translateY;
  13.         int endX = startX + getMapEntityW();
  14.         int endY = startY + getMapEntityH();
  15.  
  16.         /* Players coordinates, relative to the map */
  17.         int pX1 = player.getAbsX();
  18.         int pY1 = player.getAbsY();
  19.         int pX2 = pX1 + player.getWidth();
  20.         int pY2 = pY1 + player.getHeight();
  21.  
  22.         /* Global Booleans for Up / Down Collision */
  23.         boolean isRightInsideLeft = pX2 >= startX; // players right side is inside entity's startingX.
  24.         boolean isLeftInsideLeft = pX1 >= startX; // players startingX is inside entity's startingX
  25.         boolean isLeftInsideRight = pX1 <= endX; // players startingX is inside entity's endingX.
  26.         boolean isRightOutsideRight = pX2 >= endX; // players endingX is greater than entity's right side.
  27.  
  28.         /* Global Booleans for Left / Right Collision */
  29.         boolean isBottomBelowTop = pY2 >= startY; // bottom of player is greater than entity's top.
  30.         boolean isBottomAboveBottom = pY2 <= endY; // bottom of player is less than entity's bottom.
  31.         boolean isTopAboveTop = pY1 <= startY; // player top is above entity's top.
  32.         boolean isTopBelowTop = pY1 >= startY; // players top is below entity's top.
  33.         boolean isTopAboveBottom = pY1 <= endY; // players top is above entity's bottom.
  34.        
  35.         /* Right / Left Collision Booleans */
  36.         boolean hittingRightSide = pX1 <= endX; /// players let side is hitting entity's right side.
  37.         boolean hittingLeftSide = pX2 >= startX; // players right side is hitting entity's left side.
  38.  
  39.         /* Up / Down Collision Booleans */
  40.         boolean hittingTopSide = pY2 >= startY; // players bottom side is hitting entity's top side.
  41.         boolean hittingBottomSide = pY1 <= endY; // players top side is hitting entity's bottom side.
  42.  
  43.         /*
  44.          * All variables are setup O.O
  45.          * Now to perform collision based off
  46.          * the players moving direction.
  47.          */
  48.         if (movingRight) {
  49.             /*
  50.              * Check if player is hitting left of entity.
  51.              */
  52.             if (isTopBelowTop && isTopAboveBottom && hittingLeftSide) {
  53.                 /*
  54.                  * Players endingX is greater than entity's endingX,
  55.                  * even though the player's still inside of entity's Y axis
  56.                  * we'll return so the player doesn't get yanked backwards.
  57.                  */
  58.                 if (pX2 > endX) {
  59.                     return;
  60.                 }
  61.                 /*
  62.                  * We've detected the Collision, prevent the player from moving
  63.                  * and adjust his position + 1 from the entity's.
  64.                  */
  65.                 player.getGame().leftCollision = true;
  66.                 player.getGame().setMap1X(player.getGame().getMap1X() + 1);
  67.             }
  68.             if (isBottomBelowTop && isBottomAboveBottom && hittingLeftSide) {
  69.                 /*
  70.                  * Players endingX is greater than entity's endingX,
  71.                  * even though the player's still inside of entity's Y axis
  72.                  * we'll return so the player doesn't get yanked backwards.
  73.                  */
  74.                 if (pX2 > endX) {
  75.                     return;
  76.                 }
  77.                 /*
  78.                  * We've detected the Collision, prevent the player from moving
  79.                  * and adjust his position + 1 from the entity's.
  80.                  */
  81.                 player.getGame().leftCollision = true;
  82.                 player.getGame().setMap1X(player.getGame().getMap1X() + 1);
  83.             }
  84.             if (pY1 < startY && pY2 > endY && hittingLeftSide) {
  85.                 /*
  86.                  * Players endingX is greater than entity's endingX,
  87.                  * even though the player's still inside of entity's Y axis
  88.                  * we'll return so the player doesn't get yanked backwards.
  89.                  */
  90.                 if (pX2 > endX) {
  91.                     return;
  92.                 }
  93.                 /*
  94.                  * We've detected the Collision, prevent the player from moving
  95.                  * and adjust his position + 1 from the entity's.
  96.                  */
  97.                 player.getGame().leftCollision = true;
  98.                 player.getGame().setMap1X(player.getGame().getMap1X() + 1);
  99.             }
  100.         }
  101.         if (movingLeft) {
  102.             /*
  103.              * Check if player is hitting right of entity.
  104.              */
  105.         }
  106.         if (movingDown) {
  107.             /*
  108.              * Check if player is hitting top of entity.
  109.              */
  110.         }
  111.         if (movingUp) {
  112.             /*
  113.              * Check if player is hitting bottom of entity.
  114.              */
  115.         }
  116.    
  117.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement