Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gal.doron.ballinthehole;
- import android.content.Context;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Handler;
- import android.os.Looper;
- public class BallMover extends Thread
- {
- Handler handler = new Handler(Looper.getMainLooper());
- private Ball[] balls;
- private Paddle paddle;
- private GameLevels levels;
- private GameView gameview;
- private Context context;
- private enum Directions {Top, Right, Bottom, Left, None};
- public BallMover(Ball[] b, Paddle p, GameLevels levels, GameView view,Context context)
- {
- this.balls = b;
- this.paddle = p;
- this.levels = levels;
- this.gameview = view;
- this.context= context;
- }
- @Override
- public void run()
- {
- while(true)
- {
- if(levels.isFinishLevel())
- {
- levels.setCurrentLevel(levels.getCurrentLevel()+1);
- for(int i=1;i<balls.length;i++)
- {
- balls[i].setVisible(false);
- balls[i].restart();
- }
- balls[0].restart();
- try
- {
- sleep(1000);
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- for(int i=0;i<balls.length;i++)
- {
- if(this.balls[i].isVisible())
- {
- this.balls[i].moveBall();
- checkHitPaddle(i);
- checkHitBrick(i);
- }
- }
- this.gameview.postInvalidate();
- try
- {
- sleep(1);
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- }
- public void checkHitPaddle(int ballI)
- {
- Ball ball = balls[ballI];
- if(ball.Left()<=paddle.Right() && ball.Right()>=paddle.Left())
- {
- if(ball.Bottom()>=paddle.Top())
- {
- ball.bounceUp();
- }
- }
- else if(ball.Bottom()>=paddle.Bottom()+10){
- *************************************************
- //ball.restart();
- }
- }
- public void checkHitBrick(int ballI)
- {
- Ball ball = balls[ballI];
- String direction = getHitBricksDirection(ballI).name();
- if(direction.compareTo("Top")==0)
- ball.bounceUp();
- if(direction.compareTo("Bottom")==0)
- ball.bounceDown();
- if(direction.compareTo("Left")==0)
- ball.bounceLeft();
- if(direction.compareTo("Right")==0)
- ball.bounceRight();
- }
- public Directions getHitBricksDirection(int ballI)
- {
- Ball ball = balls[ballI];
- Bricks bricks = levels.getLevelBricks();
- Brick brick;
- for(int i=0;i<bricks.rows();i++)
- {
- for(int j=0;j<bricks.cols();j++)
- {
- brick = bricks.getBrick(i, j);
- if(brick.getType()!=0)
- {
- if(ball.Left()<=brick.Right() && ball.Right()>=brick.Left())
- {
- if(ball.Bottom()-brick.Top()>=0 && ball.Bottom()-brick.Top()<=1)
- {
- brick.ballHitBrick();
- return Directions.Top;
- }
- if(ball.Top()-brick.Bottom()<=0 && ball.Top()-brick.Bottom()>=-1)
- {
- brick.ballHitBrick();
- return Directions.Bottom;
- }
- }
- if(ball.Top()<=brick.Bottom() && ball.Bottom()>=brick.Top())
- {
- if(ball.Right()-brick.Left()>=0 && ball.Right()-brick.Left()<=1)
- {
- brick.ballHitBrick();
- return Directions.Left;
- }
- if(ball.Left()-brick.Right()<=0 && ball.Left()-brick.Right()>=-1)
- {
- brick.ballHitBrick();
- return Directions.Right;
- }
- }
- }
- }
- }
- return Directions.None;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement