Original Post
I am going to go ahead and paste my code, then ask some questions about it. My questions are: What is the best way to handle collision? Right now, as you can see, I just passed a Rectangle into the constructor. But, what if I want more objects to test for collisions? Would I pass every single object into the Paddle class? Or should I just have some static data structure which holds all the collidable objects in my GameManager (The main class which holds objects and what not). Or should i have a reference to GameManager inside each GameObject so that i can grab other objects when needed. This is the main design issue im confused about. Also, are methods like moveUp and moveDown better than just doing a getRectangle and editing the values of the rectangle from the GameManager? I am pretty much asking for any feedback on how to improve my class designs.
public class Paddle {
public static final int SPEED = 5;
public static final int WIDTH = 30;
public static final int HEIGHT = 100;
//The paddle itself and dimensons
private Rectangle paddle;
//the collidable screen
private Rectangle windowRect;
public Paddle(Rectangle windowRect){
this.paddle = new Rectangle(0, 0, WIDTH, HEIGHT);
this.windowRect = windowRect;
}
public void moveUp(){
// Move the paddle
paddle.translate(0, (-1)*SPEED);
// if paddle went off screen, move it back
if (!windowRect.contains(paddle)){
paddle.translate(0, SPEED);
}
}
public void moveDown(){
// Move the paddle
paddle.translate(0, SPEED);
// if paddle went off screen, move it back
if (!windowRect.contains(paddle)){
paddle.translate(0, (-1)*SPEED);
}
}
//Called from GameManager
public void update(long elapsedTime) {
}
//Called from GameManager
public void draw(Graphics2D g){
g.setColor(Color.red);
g.fillRect(paddle.x, paddle.y, paddle.width, paddle.height);
}
}