Guest User

Untitled

a guest
May 16th, 2012
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1.  
  2. import java.awt.Point;
  3.  
  4. import com.badlogic.gdx.Gdx;
  5. import com.badlogic.gdx.InputProcessor;
  6. import com.badlogic.gdx.graphics.OrthographicCamera;
  7. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  8. import com.badlogic.gdx.math.Rectangle;
  9. import com.badlogic.gdx.math.Vector3;
  10. import com.badlogic.gdx.scenes.scene2d.Stage;
  11.  
  12.  
  13. public class Logic implements InputProcessor {
  14.  
  15.     Stage stage;
  16.     Ball ball;
  17.     Paddle paddle;
  18.     Brick[] bricks = new Brick[20];
  19.     OrthographicCamera cam;
  20.     SpriteBatch batch;
  21.    
  22.     public Logic(OrthographicCamera cam, SpriteBatch batch){
  23.        
  24.         stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
  25.         this.cam = cam;
  26.         this.batch = batch;
  27.         ball = new Ball(100, 100, 15);
  28.         paddle = new Paddle(100, 50);
  29.        
  30.        
  31.         int k = 0;
  32.         for(int i=0; i<4; i++){
  33.             for(int j=0; j<5; j++){
  34.                 bricks[k] = new Brick(j*128+5, i*45+280, Assets.yellowBrick.getRegionWidth(), Assets.yellowBrick.getRegionHeight(), Brick.Type.RED);
  35.                 stage.addActor(bricks[k]);
  36.                 k++;
  37.             }
  38.         }
  39.        
  40.         stage.addActor(ball);
  41.         stage.addActor(paddle);
  42.  
  43.     }
  44.    
  45.    
  46.     public void checkCollision(){
  47.          if ((ball.getRectangle().overlaps(paddle.getRectangle()))) {
  48.  
  49.                 ball.setYvel(3);
  50.             }
  51.          
  52.          
  53.          for(Brick b : bricks){
  54.              if(!b.isDestroyed()){
  55.              if(ball.getRectangle().overlaps(b.getRectangle())){
  56.                  
  57.             if(ball.getLeft() < b.getLeft() && b.getLeft() < ball.getRight()){ball.setYvel(-3);}
  58.            
  59.             b.lifeDown();
  60.              }
  61.              }
  62.          }
  63.          
  64.     }
  65.  
  66.    
  67.    
  68.     public void update(){
  69.         stage.setCamera(cam);
  70.         stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1/30f));
  71.        
  72.         //paddle.x = ball.x - (paddle.width/2);
  73.         checkCollision();
  74.        
  75.         ball.update();
  76.         paddle.update();
  77.  
  78.        
  79.         for(Brick brick : bricks){
  80.         if(!brick.isDestroyed()){
  81.         if(ball.getRectangle().overlaps(brick.getRectangle())){
  82.  
  83.         if(ball.getTop() > brick.getBottom()){ball.setYvel(-1 * ball.getYvel());}  
  84.         if(ball.getTop() > brick.getTop()){ball.setYvel(-1 * ball.getYvel());}
  85.        
  86.         if(ball.getRight() > brick.getLeft()){ball.setXvel(-1 * ball.getXvel());}
  87.         if(ball.getLeft() < brick.getRight()){ball.setXvel(-1 * ball.getXvel());}
  88.  
  89.         if(ball.getBottom() > brick.getBottom()){ball.setYvel(-1 * ball.getYvel());}
  90.        
  91.         }
  92.         }}
  93.        
  94.     }
  95.    
  96.    
  97.     public void draw(){
  98.         stage.draw();
  99.     }
  100.  
  101.  
  102.     @Override
  103.     public boolean keyTyped(char arg0) {
  104.         // TODO Auto-generated method stub
  105.         return false;
  106.     }
  107.  
  108.  
  109.  
  110.     @Override
  111.     public boolean keyUp(int keycode) {
  112.         // TODO Auto-generated method stub
  113.         paddle.keyUp(keycode);
  114.         return true;
  115.     }
  116.  
  117.  
  118.  
  119.     @Override
  120.     public boolean scrolled(int arg0) {
  121.         // TODO Auto-generated method stub
  122.         return false;
  123.     }
  124.  
  125.  
  126.  
  127.     @Override
  128.     public boolean touchDown(int x, int y, int arg2, int arg3) {
  129.         // TODO Auto-generated method stub
  130.         Vector3 pos = new Vector3();
  131.         cam.unproject(pos.set(x, y, 0));
  132.         ball.x = pos.x;
  133.         ball.y = pos.y;
  134.         return true;
  135.     }
  136.  
  137.  
  138.  
  139.     @Override
  140.     public boolean touchDragged(int x, int y, int arg2) {
  141.  
  142.         return false;
  143.     }
  144.  
  145.  
  146.  
  147.     @Override
  148.     public boolean touchMoved(int arg0, int arg1) {
  149.         // TODO Auto-generated method stub
  150.         return false;
  151.     }
  152.  
  153.  
  154.  
  155.     @Override
  156.     public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
  157.         // TODO Auto-generated method stub
  158.         return false;
  159.     }
  160.  
  161.  
  162.  
  163.     @Override
  164.     public boolean keyDown(int keycode) {
  165.         // TODO Auto-generated method stub
  166.         paddle.keyDown(keycode);
  167.         return true;
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment