Guest User

Untitled

a guest
Jul 3rd, 2011
101
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Control the movement and the collision of the character
  3.  * @param map The map which we want to check collision
  4.  */
  5. public void ManageMovement(Map map) {
  6.     boolean collision = false;
  7.    
  8.     if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
  9.         for(int i = 0; i < map.GetNumLayers(); i++) {
  10.             if((LeftTile(map, i, 0) > 128) || (LeftTile(map, i, 1) > 128)) {
  11.                 collision = true;
  12.             }
  13.         }
  14.        
  15.         if(!collision) AddPos(-vel, 0.0f);
  16.     }
  17.     else if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
  18.         for(int i = 0; i < map.GetNumLayers(); i++) {
  19.             if((RightTile(map, i, 0) > 128) || (RightTile(map, i, 1) > 128)) {
  20.                 collision = true;
  21.             }
  22.         }
  23.        
  24.         if(!collision) AddPos(vel, 0.0f);
  25.     }
  26.    
  27.     collision = false;
  28.     if(Keyboard.isKeyDown(Keyboard.KEY_UP)) {
  29.         for(int i = 0; i < map.GetNumLayers(); i++) {
  30.             if((UpTile(map, i, 0) > 128) || (UpTile(map, i, 1) > 128)) {
  31.                 collision = true;
  32.             }
  33.         }
  34.        
  35.         if(!collision) AddPos(0.0f, -vel);
  36.     }
  37.     else if(Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
  38.         for(int i = 0; i < map.GetNumLayers(); i++) {
  39.             if((DownTile(map, i, 0) > 128) || (DownTile(map, i, 1) > 128)) {
  40.                 collision = true;
  41.             }
  42.         }
  43.        
  44.         if(!collision) AddPos(0.0f, vel);
  45.     }
  46. }
  47.  
  48. /**
  49.  * A method to get the upper tile next to the character
  50.  * @param map The map which we want to check the tile
  51.  * @param layer The layer of the map
  52.  * @param corner 0 indicates the upper-left corner of the character. 1 indicates the upper-right corner of the character
  53.  * @return The kind of tile next to the character in that direction
  54.  */
  55. public int UpTile(Map map, int layer, int corner) {
  56.     int tile = 255;
  57.    
  58.     switch(corner) {
  59.         case 0:
  60.             tile = map.GetValue(layer, (int) (x/size), (int) ((y-1)/size));
  61.             break;
  62.         case 1:
  63.             tile = map.GetValue(layer, (int) ((x+size-1)/size), (int) ((y-1)/size));
  64.             break;
  65.     }
  66.    
  67.     return tile;
  68. }
  69.  
  70. public int LeftTile(Map map, int layer, int corner) {
  71.     int tile = 255;
  72.    
  73.     switch(corner) {
  74.         case 0:
  75.             tile = map.GetValue(layer, (int) ((x-1)/size), (int) (y/size));
  76.             break;
  77.         case 1:
  78.             tile = map.GetValue(layer, (int) ((x-1)/size), (int) ((y+size-1)/size));
  79.             break;
  80.     }
  81.    
  82.     return tile;
  83. }
  84.  
  85. public int DownTile(Map map, int layer, int corner) {
  86.     int tile = 255;
  87.    
  88.     switch(corner) {
  89.         case 0:
  90.             tile = map.GetValue(layer, (int) (x/size), (int) (y/size + 1));
  91.             break;
  92.         case 1:
  93.             tile = map.GetValue(layer, (int) ((x+size-1)/size), (int) (y/size + 1));
  94.             break;
  95.     }
  96.    
  97.     return tile;
  98. }
  99.  
  100. public int RightTile(Map map, int layer, int corner) {
  101.     int tile = 255;
  102.    
  103.     switch(corner) {
  104.         case 0:
  105.             tile = map.GetValue(layer, (int) (x/size + 1), (int) (y/size));
  106.             break;
  107.         case 1:
  108.             tile = map.GetValue(layer, (int) (x/size + 1), (int) ((y+size-1)/size));
  109.             break;
  110.     }
  111.    
  112.     return tile;
  113. }
RAW Paste Data