SHOW:
|
|
- or go back to the newest paste.
| 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){
|
| 53 | + | for(Brick brick : bricks){
|
| 54 | - | if(!b.isDestroyed()){
|
| 54 | + | if(!brick.isDestroyed()){
|
| 55 | - | if(ball.getRectangle().overlaps(b.getRectangle())){
|
| 55 | + | if(ball.getRectangle().overlaps(brick.getRectangle())){
|
| 56 | - | |
| 56 | + | |
| 57 | - | if(ball.getLeft() < b.getLeft() && b.getLeft() < ball.getRight()){ball.setYvel(-3);}
|
| 57 | + | if(ball.getTop() > brick.getBottom()){ball.setYvel(-1 * ball.getYvel());}
|
| 58 | - | |
| 58 | + | if(ball.getTop() > brick.getTop()){ball.setYvel(-1 * ball.getYvel());}
|
| 59 | - | b.lifeDown(); |
| 59 | + | |
| 60 | - | } |
| 60 | + | if(ball.getRight() > brick.getLeft()){ball.setXvel(-1 * ball.getXvel());}
|
| 61 | - | } |
| 61 | + | if(ball.getLeft() < brick.getRight()){ball.setXvel(-1 * ball.getXvel());}
|
| 62 | - | } |
| 62 | + | |
| 63 | if(ball.getBottom() > brick.getBottom()){ball.setYvel(-1 * ball.getYvel());}
| |
| 64 | ||
| 65 | brick.lifeDown(); | |
| 66 | } | |
| 67 | }} | |
| 68 | ||
| 69 | } | |
| 70 | ||
| 71 | ||
| 72 | ||
| 73 | public void update(){
| |
| 74 | stage.setCamera(cam); | |
| 75 | stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1/30f)); | |
| 76 | ||
| 77 | //paddle.x = ball.x - (paddle.width/2); | |
| 78 | ||
| 79 | - | for(Brick brick : bricks){
|
| 79 | + | |
| 80 | - | if(!brick.isDestroyed()){
|
| 80 | + | |
| 81 | - | if(ball.getRectangle().overlaps(brick.getRectangle())){
|
| 81 | + | |
| 82 | ||
| 83 | - | if(ball.getTop() > brick.getBottom()){ball.setYvel(-1 * ball.getYvel());}
|
| 83 | + | |
| 84 | - | if(ball.getTop() > brick.getTop()){ball.setYvel(-1 * ball.getYvel());}
|
| 84 | + | |
| 85 | } | |
| 86 | - | if(ball.getRight() > brick.getLeft()){ball.setXvel(-1 * ball.getXvel());}
|
| 86 | + | |
| 87 | - | if(ball.getLeft() < brick.getRight()){ball.setXvel(-1 * ball.getXvel());}
|
| 87 | + | |
| 88 | public void draw(){
| |
| 89 | - | if(ball.getBottom() > brick.getBottom()){ball.setYvel(-1 * ball.getYvel());}
|
| 89 | + | |
| 90 | } | |
| 91 | ||
| 92 | - | }} |
| 92 | + | |
| 93 | @Override | |
| 94 | public boolean keyTyped(char arg0) {
| |
| 95 | // TODO Auto-generated method stub | |
| 96 | return false; | |
| 97 | } | |
| 98 | ||
| 99 | ||
| 100 | ||
| 101 | @Override | |
| 102 | public boolean keyUp(int keycode) {
| |
| 103 | // TODO Auto-generated method stub | |
| 104 | paddle.keyUp(keycode); | |
| 105 | return true; | |
| 106 | } | |
| 107 | ||
| 108 | ||
| 109 | ||
| 110 | @Override | |
| 111 | public boolean scrolled(int arg0) {
| |
| 112 | // TODO Auto-generated method stub | |
| 113 | return false; | |
| 114 | } | |
| 115 | ||
| 116 | ||
| 117 | ||
| 118 | @Override | |
| 119 | public boolean touchDown(int x, int y, int arg2, int arg3) {
| |
| 120 | // TODO Auto-generated method stub | |
| 121 | Vector3 pos = new Vector3(); | |
| 122 | cam.unproject(pos.set(x, y, 0)); | |
| 123 | ball.x = pos.x; | |
| 124 | ball.y = pos.y; | |
| 125 | return true; | |
| 126 | } | |
| 127 | ||
| 128 | ||
| 129 | ||
| 130 | @Override | |
| 131 | public boolean touchDragged(int x, int y, int arg2) {
| |
| 132 | ||
| 133 | return false; | |
| 134 | } | |
| 135 | ||
| 136 | ||
| 137 | ||
| 138 | @Override | |
| 139 | public boolean touchMoved(int arg0, int arg1) {
| |
| 140 | // TODO Auto-generated method stub | |
| 141 | return false; | |
| 142 | } | |
| 143 | ||
| 144 | ||
| 145 | ||
| 146 | @Override | |
| 147 | public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
| |
| 148 | // TODO Auto-generated method stub | |
| 149 | return false; | |
| 150 | } | |
| 151 | ||
| 152 | ||
| 153 | ||
| 154 | @Override | |
| 155 | public boolean keyDown(int keycode) {
| |
| 156 | // TODO Auto-generated method stub | |
| 157 | paddle.keyDown(keycode); | |
| 158 | return true; | |
| 159 | } | |
| 160 | } |