Advertisement
dermetfan

fix guess 1

Oct 12th, 2013
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package br.player;
  2.  
  3. import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
  4. import com.badlogic.gdx.math.Vector2;
  5.  
  6. /**
  7.  *
  8.  * @author André Vinícius Lopes
  9.  */
  10. public class PlayerMovement {
  11.  
  12.     private Player player;
  13.     private final int default_x_width = 30;
  14.     private final int default_y_height = 40;
  15.     private final String BLOCKED = "blocked";
  16.     private final String SLOPED = "slope";
  17.     private float oldY, veryOldY;
  18.  
  19.     //For very private use of this class
  20.     private TiledMapTileLayer.Cell cell, cell1;
  21.  
  22.     public PlayerMovement(Player p) {
  23.         player = p;
  24.  
  25.     }
  26.  
  27.     public void updateMovement(TiledMapTileLayer collisionLayer, Vector2 velocity, float gravity, float delta, float speed, float jump) {
  28.  
  29.         // apply gravity
  30.         velocity.y -= gravity * delta;
  31.  
  32.         // clamp velocity
  33.         if (velocity.y > jump) {
  34.             velocity.y = jump;
  35.         } else if (velocity.y < -gravity) {
  36.             velocity.y = -gravity;
  37.         }
  38.  
  39.         // save old position
  40.         double oldX = player.getX();
  41.         double oldY = player.getY();
  42.  
  43.         double tileWidth = collisionLayer.getTileWidth();
  44.         double tileHeight = collisionLayer.getTileHeight();
  45.  
  46.         boolean collisionX = false;
  47.         boolean collisionY = false;
  48.         boolean slopeY = false;
  49.         boolean slopeX = false;
  50.  
  51.         // move on y
  52.         player.setY(player.getY() + velocity.y * delta);
  53.  
  54.         if (velocity.y < 0) { // going down
  55.             // bottom left
  56.             cell = collisionLayer.getCell((int) (player.getX() / tileWidth), (int) (player.getY() / tileHeight));
  57.             if (cell != null) {
  58.                 collisionY = cell.getTile().getProperties().containsKey(BLOCKED);
  59.                 slopeY = cell.getTile().getProperties().containsKey(SLOPED);
  60.             }
  61.             // bottom middle
  62.             if (!collisionY) {
  63.                 cell1 = collisionLayer.getCell((int) ((player.getX() + default_x_width / 2) / tileWidth), (int) (player.getY() / tileHeight));
  64.                 if (cell1 != null) {
  65.                     collisionY = cell1.getTile().getProperties().containsKey(BLOCKED);
  66.                     slopeY = cell1.getTile().getProperties().containsKey(SLOPED);
  67.                 }
  68.             }
  69.  
  70.             // bottom right
  71.             if (!collisionY) {
  72.                 cell1 = collisionLayer.getCell((int) ((player.getX() + default_x_width) / tileWidth), (int) (player.getY() / tileHeight));
  73.                 if (cell1 != null) {
  74.                     collisionY = cell1.getTile().getProperties().containsKey(BLOCKED);
  75.                     slopeY = cell1.getTile().getProperties().containsKey(SLOPED);
  76.                 }
  77.             }
  78.  
  79.             //player.canJump = collisionY;
  80.         } else if (velocity.y > 0) { // going up
  81.             // top left
  82.             cell = collisionLayer.getCell((int) (player.getX() / tileWidth), (int) ((player.getY() + default_y_height) / tileHeight));
  83.             if (cell != null) {
  84.                 collisionY = cell.getTile().getProperties().containsKey(BLOCKED);
  85.                 slopeY = cell.getTile().getProperties().containsKey(SLOPED);
  86.             }
  87.  
  88.             // top middle
  89.             if (!collisionY) {
  90.                 cell1 = collisionLayer.getCell((int) ((player.getX() + default_x_width / 2) / tileWidth), (int) ((player.getY() + default_y_height) / tileHeight));
  91.                 if (cell1 != null) {
  92.                     collisionY = cell1.getTile().getProperties().containsKey(BLOCKED);
  93.                     slopeY = cell1.getTile().getProperties().containsKey(SLOPED);
  94.                 }
  95.             }
  96.  
  97.             // top right
  98.             if (!collisionY) {
  99.                 cell1 = collisionLayer.getCell((int) ((player.getX() + default_x_width) / tileWidth), (int) ((player.getY() + default_y_height) / tileHeight));
  100.                 if (cell1 != null) {
  101.                     collisionY = cell1.getTile().getProperties().containsKey(BLOCKED);
  102.                     slopeY = cell1.getTile().getProperties().containsKey(SLOPED);
  103.                 }
  104.             }
  105.         }
  106.  
  107.         // react to y collision
  108.         if (collisionY && !slopeY) {
  109.             player.setY((float) oldY);
  110.             velocity.y = 0;
  111.         }
  112.  
  113.         if (slopeY) {
  114.  
  115.             if (!player.pressingLeft && !player.pressingRight) {
  116.  
  117.                 player.setY((float) oldY); // Because its senofTheta
  118.                 velocity.y = 0;
  119.  
  120.             } else {
  121.                 //player.setY(oldY + (0.8f * 2)); // Because its senofTheta
  122.                 player.setY((float) oldY + (0.8f * 4)); // Because its senofTheta
  123.                 velocity.y = 0;
  124.             }
  125.         }
  126.  
  127.         //Can Jump
  128.         if (collisionY || slopeY) {
  129.             player.canJump = true;
  130.         } else {
  131.             player.canJump = false;
  132.         }
  133.  
  134.         // move on x
  135.         player.setX(player.getX() + velocity.x * delta);
  136.  
  137.         if (velocity.x < 0) { // going left
  138.             // top left
  139.             cell = collisionLayer.getCell((int) (player.getX() / tileWidth), (int) ((player.getY() + default_y_height) / tileHeight));
  140.             if (cell != null) {
  141.                 slopeX = cell.getTile().getProperties().containsKey(SLOPED);
  142.                 collisionX = cell.getTile().getProperties().containsKey(BLOCKED);
  143.             }
  144.  
  145.             // middle left
  146.             if (!collisionX) {
  147.                 cell1 = collisionLayer.getCell((int) (player.getX() / tileWidth), (int) ((player.getY() + default_y_height / 2) / tileHeight));
  148.                 if (cell1 != null) {
  149.                     slopeX = cell1.getTile().getProperties().containsKey(SLOPED);
  150.                     collisionX = cell1.getTile().getProperties().containsKey(BLOCKED);
  151.                 }
  152.             }
  153.  
  154.             // bottom left
  155.             if (!collisionX) {
  156.                 cell1 = collisionLayer.getCell((int) (player.getX() / tileWidth), (int) (player.getY() / tileHeight));
  157.                 if (cell1 != null) {
  158.                     slopeX = cell1.getTile().getProperties().containsKey(SLOPED);
  159.                     collisionX = cell1.getTile().getProperties().containsKey(BLOCKED);
  160.                 }
  161.             }
  162.         } else if (velocity.x > 0) { // going right
  163.             // top right
  164.             cell = collisionLayer.getCell((int) ((player.getX() + default_x_width) / tileWidth), (int) ((player.getY() + default_y_height) / tileHeight));
  165.             if (cell != null) {
  166.                 slopeX = cell.getTile().getProperties().containsKey(SLOPED);
  167.                 collisionX = cell.getTile().getProperties().containsKey(BLOCKED);
  168.             }
  169.             // middle right
  170.             if (!collisionX) {
  171.                 cell1 = collisionLayer.getCell((int) ((player.getX() + default_x_width) / tileWidth), (int) ((player.getY() + default_y_height / 2) / tileHeight));
  172.                 if (cell1 != null) {
  173.                     slopeX = cell1.getTile().getProperties().containsKey(SLOPED);
  174.                     collisionX = cell1.getTile().getProperties().containsKey(BLOCKED);
  175.                 }
  176.             }
  177.  
  178.             // bottom right
  179.             if (!collisionX) {
  180.                 cell1 = collisionLayer.getCell((int) ((player.getX() + default_x_width) / tileWidth), (int) (player.getY() / tileHeight));
  181.                 if (cell1 != null) {
  182.                     slopeX = cell1.getTile().getProperties().containsKey(SLOPED);
  183.                     collisionX = cell1.getTile().getProperties().containsKey(BLOCKED);
  184.                 }
  185.             }
  186.         }
  187.  
  188.         /*
  189.          System.out.println("NanoTime" + System.nanoTime());
  190.          System.out.println("Player Collision X" + collisionX);
  191.          System.out.println("Player Collision Y" + collisionY);
  192.          System.out.println("Player Slope X" + slopeX);
  193.          System.out.println("Player Slope Y" + slopeY);
  194.          System.out.println("Player Can Jump" + player.canJump);
  195.          System.out.println("\n");
  196.          */
  197.         // react to x collision
  198.         if (collisionX && !slopeX) {
  199.             player.setX((float) oldX);
  200.             velocity.x = 0;
  201.         }
  202.         if (slopeX && velocity.x != 0) {
  203.             if (slopeX && velocity.x > 0) {
  204.                 player.setX((float) oldX + 1.5f);
  205.             }
  206.  
  207.             if (slopeX && velocity.x < 0) {
  208.                 player.setX((float) oldX - 1.5f);
  209.             }
  210.  
  211.             if (slopeX && velocity.x == 0) {
  212.                 //?????
  213.             }
  214.         }
  215.  
  216.     }
  217.  
  218.     public void updateMovement2(TiledMapTileLayer collisionLayer, Vector2 velocity, float gravity, float delta, float speed, float jump) {
  219.  
  220.         // apply gravity
  221.         velocity.y -= gravity * delta;
  222.        
  223.         player.setY(player.getY() + velocity.y * delta);
  224.        
  225.         // clamp velocity
  226.         if (velocity.y > jump) {
  227.             velocity.y = jump;
  228.         } else if (velocity.y < -gravity) {
  229.             velocity.y = -gravity;
  230.         }
  231.  
  232.         // save old position
  233.         double oldX = player.getX();
  234.         double oldY = player.getY();
  235.  
  236.         int tw = 1;
  237.         int th = 1;
  238.         int px = (int) (player.getX() / 16);
  239.         int py = (int) (player.getY() / 16);
  240.         int spriteHeight = 46 / 16;
  241.         int spriteWeight = player.getRegionWidth() / 16;
  242.  
  243.         boolean collisionY = false;
  244.         boolean collisionX = false;
  245.  
  246.         player.setY(player.getY() + velocity.y * delta);
  247.  
  248.         System.out.println("");
  249.         //Analyze Y Below Sprite
  250.         if (!collisionY) {
  251.             //Check Block 04  
  252.             TiledMapTileLayer.Cell cell04 = collisionLayer.getCell(px, (py));
  253.             if (cell04 != null) {
  254.                 collisionY = cell04.getTile().getProperties().containsKey("blocked");
  255.                 System.out.println("Block 04");
  256.             }
  257.         }
  258.         if (!collisionY) {
  259.             //Check Block 05  
  260.             TiledMapTileLayer.Cell cell05 = collisionLayer.getCell(px + tw, (py));
  261.             if (cell05 != null) {
  262.                 collisionY = cell05.getTile().getProperties().containsKey("blocked");
  263.                 System.out.println("Block 05");
  264.             }
  265.         }
  266.  
  267.         if (!collisionY) {
  268.             //Check Block 06
  269.             TiledMapTileLayer.Cell cell06 = collisionLayer.getCell(px + 2 * tw, (py));
  270.             if (cell06 != null) {
  271.                 collisionY = cell06.getTile().getProperties().containsKey("blocked");
  272.                 System.out.println("Block 06");
  273.  
  274.             }
  275.         }
  276.  
  277.     // react to y collision
  278.         if (collisionY) {
  279.             player.setY((float) oldY);
  280.             //velocity.y = 0;
  281.             player.canJump = true;
  282.         }
  283.  
  284.         //End of Analyzing
  285.         //Analyze going left
  286.         if (velocity.x < 0) {
  287.             if (!collisionX) {
  288.                 //Check Block 0
  289.                 TiledMapTileLayer.Cell cell0 = collisionLayer.getCell(px - tw, py + 2 * th);
  290.                 if (cell0 != null) {
  291.                     collisionX = cell0.getTile().getProperties().containsKey("blocked");
  292.                     System.out.println("Block 0");
  293.                 }
  294.             }
  295.  
  296.             if (!collisionX) {
  297.                 //Check Block 1
  298.                 TiledMapTileLayer.Cell cell1 = collisionLayer.getCell(px - tw, (py + th));
  299.                 if (cell1 != null) {
  300.                     collisionX = cell1.getTile().getProperties().containsKey("blocked");
  301.                     System.out.println("Block 1");
  302.  
  303.                 }
  304.             }
  305.  
  306.             if (!collisionX) {
  307.                 //Check Block 2
  308.                 TiledMapTileLayer.Cell cell2 = collisionLayer.getCell(px - tw, (py));
  309.                 if (cell2 != null) {
  310.                     //collisionX = cell2.getTile().getProperties().containsKey("blocked");
  311.                     System.out.println("Block 2");
  312.                 }
  313.             }
  314.  
  315.             //End of Analyzing Going left
  316.         }
  317.  
  318.         if (velocity.x > 0) {
  319.             //Analyzing Going RIght
  320.             //Check Block 7
  321.             if (!collisionX) {
  322.                 TiledMapTileLayer.Cell cell7 = collisionLayer.getCell((px + (3 * tw)), (py));
  323.                 if (cell7 != null) {
  324.                     //collisionX = cell7.getTile().getProperties().containsKey("blocked");
  325.                     System.out.println("Block 7");
  326.  
  327.                 }
  328.             }
  329.  
  330.             if (!collisionX) {
  331.                 //Check Block 8
  332.                 TiledMapTileLayer.Cell cell8 = collisionLayer.getCell(px + 3 * tw, (py + th));
  333.                 if (cell8 != null) {
  334.                     collisionX = cell8.getTile().getProperties().containsKey("blocked");
  335.                     System.out.println("Block 8");
  336.  
  337.                 }
  338.             }
  339.  
  340.             if (!collisionX) {
  341.                 //Check Block 9
  342.                 TiledMapTileLayer.Cell cell9 = collisionLayer.getCell(px + 3 * tw, (py + 2 * th));
  343.                 if (cell9 != null) {
  344.                     collisionX = cell9.getTile().getProperties().containsKey("blocked");
  345.                     System.out.println("Block 9");
  346.  
  347.                 }
  348.             }
  349.         }
  350.  
  351.         player.setX(player.getX() + velocity.x * delta);
  352.  
  353.         if (collisionX) {
  354.             player.setX((float) oldX);
  355.             velocity.x = 0;
  356.         }
  357.  
  358.     }
  359.  
  360. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement