Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Point;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.InputProcessor;
- import com.badlogic.gdx.graphics.OrthographicCamera;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.math.Rectangle;
- import com.badlogic.gdx.math.Vector3;
- import com.badlogic.gdx.scenes.scene2d.Stage;
- public class Logic implements InputProcessor {
- Stage stage;
- Ball ball;
- Paddle paddle;
- Brick[] bricks = new Brick[20];
- OrthographicCamera cam;
- SpriteBatch batch;
- public Logic(OrthographicCamera cam, SpriteBatch batch){
- stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
- this.cam = cam;
- this.batch = batch;
- ball = new Ball(100, 100, 15);
- paddle = new Paddle(100, 50);
- int k = 0;
- for(int i=0; i<4; i++){
- for(int j=0; j<5; j++){
- bricks[k] = new Brick(j*128+5, i*45+280, Assets.yellowBrick.getRegionWidth(), Assets.yellowBrick.getRegionHeight(), Brick.Type.RED);
- stage.addActor(bricks[k]);
- k++;
- }
- }
- stage.addActor(ball);
- stage.addActor(paddle);
- }
- public void checkCollision(){
- if ((ball.getRectangle().overlaps(paddle.getRectangle()))) {
- ball.setYvel(3);
- }
- for(Brick b : bricks){
- if(!b.isDestroyed()){
- if(ball.getRectangle().overlaps(b.getRectangle())){
- if(ball.getLeft() < b.getLeft() && b.getLeft() < ball.getRight()){ball.setYvel(-3);}
- b.lifeDown();
- }
- }
- }
- }
- public void update(){
- stage.setCamera(cam);
- stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1/30f));
- //paddle.x = ball.x - (paddle.width/2);
- checkCollision();
- ball.update();
- paddle.update();
- for(Brick brick : bricks){
- if(!brick.isDestroyed()){
- if(ball.getRectangle().overlaps(brick.getRectangle())){
- if(ball.getTop() > brick.getBottom()){ball.setYvel(-1 * ball.getYvel());}
- if(ball.getTop() > brick.getTop()){ball.setYvel(-1 * ball.getYvel());}
- if(ball.getRight() > brick.getLeft()){ball.setXvel(-1 * ball.getXvel());}
- if(ball.getLeft() < brick.getRight()){ball.setXvel(-1 * ball.getXvel());}
- if(ball.getBottom() > brick.getBottom()){ball.setYvel(-1 * ball.getYvel());}
- }
- }}
- }
- public void draw(){
- stage.draw();
- }
- @Override
- public boolean keyTyped(char arg0) {
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- public boolean keyUp(int keycode) {
- // TODO Auto-generated method stub
- paddle.keyUp(keycode);
- return true;
- }
- @Override
- public boolean scrolled(int arg0) {
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- public boolean touchDown(int x, int y, int arg2, int arg3) {
- // TODO Auto-generated method stub
- Vector3 pos = new Vector3();
- cam.unproject(pos.set(x, y, 0));
- ball.x = pos.x;
- ball.y = pos.y;
- return true;
- }
- @Override
- public boolean touchDragged(int x, int y, int arg2) {
- return false;
- }
- @Override
- public boolean touchMoved(int arg0, int arg1) {
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- public boolean keyDown(int keycode) {
- // TODO Auto-generated method stub
- paddle.keyDown(keycode);
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment