Advertisement
dermetfan

Ryan Pascal eventual solution

Oct 6th, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.kontra.main;
  2.  
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.Input.Keys;
  5. import com.badlogic.gdx.InputProcessor;
  6. import com.badlogic.gdx.graphics.Color;
  7. import com.badlogic.gdx.graphics.Texture;
  8. import com.badlogic.gdx.graphics.g2d.Sprite;
  9. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  10. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  11. import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
  12. import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
  13. import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
  14. import com.badlogic.gdx.math.Vector2;
  15.  
  16. public class Player extends Entity implements InputProcessor{
  17.     boolean bucket = true;
  18.     Kontra kontra;
  19.     Window window;
  20.     private TiledMapGame tilemap;
  21.     private static int rowNumber = 32;
  22.     private static int ColumnNumber = 32;
  23.     private static int randomstuff = 0;  
  24.      private static Texture   texture = new Texture(Gdx.files.internal("Images/guys.png"));
  25.         private static TextureRegion regions= new TextureRegion(texture, 0, 0, 32, 32);
  26.         private static  TextureRegion CharacterView = regions;
  27.        
  28.        
  29.         float oldX, oldY, tileWidth, tileHeight;
  30.      private TiledMapTileLayer collisionLayer;
  31.         boolean collisionX = false, collisionY = false;
  32.         Vector2 velocityVEC = new Vector2();
  33.         float speed = 60*2 ;
  34.         float gravity2 = 60*1.8f;
  35.         boolean canJump = true;
  36.        
  37.         public Player(float x, float y, TiledMapTileLayer tiledMapTileLayer) {
  38.    
  39.         super(x, y, new TextureRegion(texture, 0, 0, 32, 32));
  40.        
  41.         this.collisionLayer = tiledMapTileLayer;
  42.        
  43.        
  44.     }
  45.  
  46.     @Override
  47.     public void draw(SpriteBatch batch) {
  48.                
  49.         oldX = x;
  50.         oldY = y;
  51.         tileWidth = collisionLayer.getTileWidth();
  52.         tileHeight = collisionLayer.getTileHeight();
  53.                
  54.         update(Gdx.graphics.getDeltaTime());
  55.         super.draw(batch);
  56.    
  57.     }
  58.    
  59.  
  60.  
  61.    
  62.    
  63.     public void update(float delta){
  64.        
  65.         //apply gravity
  66.         velocityVEC.y -= gravity2 * delta;
  67.        
  68.         //clamp gravity
  69.         if(velocityVEC.y > speed){
  70.             velocityVEC.y = speed;
  71.         }else{
  72.             velocityVEC.y = -speed;
  73.         }
  74.        
  75.         // MOVE ON X
  76.         x = x + velocityVEC.x * delta;
  77.         System.out.println(velocityVEC.x);
  78.         System.out.println(x);
  79.         if(velocityVEC.x < 0){
  80.            
  81.            
  82.             //top left
  83.             collisionX = collisionLayer.getCell((int)(x / tileWidth), (int)((y+regions.getRegionHeight()) / tileHeight) ).getTile().getProperties().containsKey("blocked");
  84.             //middle left
  85.             if(!collisionX)
  86.             collisionX = collisionLayer.getCell((int)(x / tileWidth), (int)((y+regions.getRegionHeight() / 2) / tileHeight) ).getTile().getProperties().containsKey("blocked");
  87.             //bottom left
  88.             if(!collisionX){
  89.             collisionX = collisionLayer.getCell((int)(x / tileWidth), (int)((y) / tileHeight) ).getTile().getProperties().containsKey("blocked");
  90.             System.out.println("tes");
  91.             }
  92.            
  93.         }else if(velocityVEC.x > 0){
  94.            
  95.             //top right
  96.             collisionX = collisionLayer.getCell((int)((x + regions.getRegionWidth()) / tileWidth), (int)((y+regions.getRegionHeight()) / tileHeight) ).getTile().getProperties().containsKey("blocked");
  97.             //middle right
  98.             if(!collisionX){
  99.             collisionX = collisionLayer.getCell((int)((x + regions.getRegionWidth())  / tileWidth), (int)((y+regions.getRegionHeight()/2) / tileHeight) ).getTile().getProperties().containsKey("blocked");
  100.            
  101.             }
  102.             //bottom right
  103.             if(!collisionX){
  104.             collisionX = collisionLayer.getCell((int)((x + regions.getRegionWidth())  / tileWidth), (int)((y) / tileHeight) ).getTile().getProperties().containsKey("blocked");
  105.            
  106.             }
  107.            
  108.            
  109.            
  110.            
  111.            
  112.         }
  113.        
  114.        
  115.          if(collisionX){
  116.                 x = oldX;
  117.                 velocityVEC.x = 0;
  118.                
  119.             }
  120.        
  121.         // MOVE ON Y
  122.         y = y + velocityVEC.y * delta;
  123.  
  124.         if(velocityVEC.y < 0){// going down
  125.            
  126.              
  127.             //bottom left
  128.         collisionY = collisionLayer.getCell((int)(x / tileWidth), (int)((y) / tileHeight) ).getTile().getProperties().containsKey("blocked");
  129.         //bottom left
  130.         if(!collisionY)
  131.         collisionY = collisionLayer.getCell((int)((x + regions.getRegionWidth() / 2) / tileWidth), (int)(y / tileHeight) ).getTile().getProperties().containsKey("blocked");
  132.         //bottom right
  133.         if(!collisionY)
  134.         collisionY = collisionLayer.getCell((int)((x  + regions.getRegionWidth()) / tileWidth), (int)((y) / tileHeight) ).getTile().getProperties().containsKey("blocked");
  135.        
  136.    
  137.             canJump = collisionY;
  138.            
  139.         }else if(velocityVEC.y > 0){// going up
  140.            
  141.              
  142.             //top left
  143.             collisionY = collisionLayer.getCell((int)(x / tileWidth), (int)((y + regions.getRegionHeight()) / tileHeight) ).getTile().getProperties().containsKey("blocked");
  144.             //top left
  145.             if(!collisionY)
  146.             collisionY = collisionLayer.getCell((int)(((x + regions.getRegionWidth()) / 2) / tileWidth), (int)((y + regions.getRegionHeight()) / tileHeight) ).getTile().getProperties().containsKey("blocked");
  147.             //top right
  148.             if(!collisionY)
  149.             collisionY = collisionLayer.getCell((int)((x  + regions.getRegionWidth()) / tileWidth), (int)((y + regions.getRegionHeight()) / tileHeight) ).getTile().getProperties().containsKey("blocked");
  150.            
  151.            
  152.            
  153.            
  154.         }
  155.        
  156.         if(collisionY){
  157.             y = oldY;
  158.             velocityVEC.y = 0;
  159.            
  160.            
  161.         }
  162.      
  163.        
  164.        
  165.     }
  166.  
  167.     @Override
  168.     public boolean keyDown(int keycode) {
  169.         switch(keycode){
  170.         case Keys.W:
  171.             //if(canJump){
  172.                 velocityVEC.y = speed;
  173.                 canJump = false;
  174.             //}
  175.         break;
  176.         case Keys.A:
  177.            
  178.             velocityVEC.x = -speed;
  179.         break;
  180.         case Keys.D:
  181.             velocityVEC.x = speed;
  182.         break;
  183.         }
  184.         return true;
  185.     }
  186. @Override
  187.     public boolean keyUp(int keycode) {
  188.     switch(keycode){
  189.         case Keys.W:
  190.            
  191.         break;
  192.         case Keys.A:
  193.             velocityVEC.x = 0;
  194.         break;
  195.         case Keys.D:
  196.             velocityVEC.x = 0;
  197.         break;
  198.     }
  199.         return true;
  200.     }
  201.     @Override
  202.     public boolean keyTyped(char arg0) {return false;}
  203.     @Override
  204.     public boolean mouseMoved(int arg0, int arg1) {return false;}
  205.     @Override
  206.     public boolean scrolled(int arg0) {return false;}
  207.     @Override
  208.     public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {return false;}
  209.     @Override
  210.     public boolean touchDragged(int arg0, int arg1, int arg2) {return false;}
  211.     @Override
  212.     public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {return false;}
  213.    
  214.    
  215.    
  216.    
  217.        
  218.  
  219.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement