dermetfan

YTPo0Op collision fix suggestion

Oct 3rd, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.LLGames.mygdxgame.entities;
  2.  
  3. import com.badlogic.gdx.Input.Keys;
  4. import com.badlogic.gdx.InputProcessor;
  5. import com.badlogic.gdx.graphics.g2d.Sprite;
  6. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  7. import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
  8.  
  9. public class Player extends Sprite implements InputProcessor {
  10.    
  11.     int x, y;
  12.    
  13.     private TiledMapTileLayer collisionLayer;
  14.    
  15.     public Player(Sprite sprite, TiledMapTileLayer collisionlayer) {
  16.         super(sprite);
  17.         this.collisionLayer = collisionLayer;
  18.         }
  19.        
  20.     @Override
  21.     public void draw(SpriteBatch spriteBatch) {
  22.         update();
  23.         super.draw(spriteBatch);
  24.     }
  25.    
  26.     public void update() {
  27.         setX(x);
  28.         setY(y);
  29.     }
  30.    
  31.     public void pos(int xa, int ya) {
  32.         x = xa;
  33.         y = ya;
  34.     }
  35.    
  36.     public int getPos(int a) {
  37.         int b = 0;
  38.         if (a == 0) {b = x;}
  39.         else {b = y;}
  40.         return b;
  41.     }
  42.  
  43. //My problem is in the following function. Without collision detection, movements work fine :)
  44. //My tiles are 32 * 32 and my player is a png 32 width and 64 height
  45.    
  46.     public void move(int mov){
  47.         switch (mov) {
  48.        
  49.         case 0 : // UP
  50.             if(!isCellBlocked((x + 16)/ 32, (y + 64) / 32));
  51.                 y += 32;
  52.             break ;
  53.            
  54.         case 1 : // DOWN
  55.             if(!isCellBlocked((x + 16) / 32, y / 32))
  56.                 y -= 32;
  57.             break ;
  58.            
  59.         case 2 : // LEFT
  60.             if(!isCellBlocked((x - 32) / 32, y / 32))
  61.                 x -= 32;
  62.             break ;
  63.            
  64.         case 3 : // RIGHT
  65.             if(!isCellBlocked((x + 64) / 32, (y + 64) / 32))
  66.                 x += 32;
  67.             break ;
  68.         }
  69.     }
  70.  
  71.     public boolean isCellBlocked(int x, int y) {
  72.         Cell cell = collisionLayer.getCell(x, y);
  73.         if(cell == null)
  74.             return false; // the cell is not blocked (because there's none there)
  75.         TiledMapTile tile = cell.getTile();
  76.         if(tile != null) // check if there's a tile in the cell first to avoid an exception
  77.             return tile.getProperties().containsKey("blocked");
  78.         return false; // if there's no tile in the cell, it's not blocked as well
  79.     }
  80.  
  81.     @Override
  82.     public boolean keyDown(int keycode) {
  83.         // TODO Auto-generated method stub
  84.         return false;
  85.     }
  86.  
  87.     @Override
  88.     public boolean keyUp(int keycode) {
  89.         // TODO Auto-generated method stub
  90.         return false;
  91.     }
  92.  
  93.     @Override
  94.     public boolean keyTyped(char character) {
  95.         switch(character) {
  96.        
  97.         case 'Z' :
  98.         case 'z' :
  99.             move(0);
  100.             break ;
  101.            
  102.         case 'Q' :
  103.         case 'q' :
  104.             move(2);
  105.             break ;
  106.            
  107.         case 'S' :
  108.         case 's' :
  109.             move(1);
  110.             break ;
  111.            
  112.         case 'D' :
  113.         case 'd' :
  114.             move(3);
  115.             break ;
  116.         }
  117.         return true;
  118.     }
  119.  
  120.     @Override
  121.     public boolean touchDown(int screenX, int screenY, int pointer, int button) {
  122.         // TODO Auto-generated method stub
  123.         return false;
  124.     }
  125.  
  126.     @Override
  127.     public boolean touchUp(int screenX, int screenY, int pointer, int button) {
  128.         // TODO Auto-generated method stub
  129.         return false;
  130.     }
  131.  
  132.     @Override
  133.     public boolean touchDragged(int screenX, int screenY, int pointer) {
  134.         // TODO Auto-generated method stub
  135.         return false;
  136.     }
  137.  
  138.     @Override
  139.     public boolean mouseMoved(int screenX, int screenY) {
  140.         // TODO Auto-generated method stub
  141.         return false;
  142.     }
  143.  
  144.     @Override
  145.     public boolean scrolled(int amount) {
  146.         // TODO Auto-generated method stub
  147.         return false;
  148.     }
  149.    
  150.    
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment