ball - new color when ball hits paddle
This commit is contained in:
parent
087650adc8
commit
46e689dec3
@ -74,6 +74,11 @@ public class Constants {
|
||||
*/
|
||||
public static final Double BALL_SPEED = 1.20;
|
||||
|
||||
/**
|
||||
* The default component color
|
||||
*/
|
||||
public static final Color COLOR_COMPONENT = new Color(200, 200, 200);
|
||||
|
||||
/**
|
||||
* The component color for the bottom paddle
|
||||
*/
|
||||
|
@ -2,6 +2,9 @@ package break_out.model;
|
||||
|
||||
import break_out.Constants;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* This class contains the information about the balls characteristics and behavior
|
||||
*
|
||||
@ -19,6 +22,16 @@ public class Ball implements IBall {
|
||||
*/
|
||||
private Vector2D direction;
|
||||
|
||||
/**
|
||||
* The balls hit state for paddles; custom implementation
|
||||
*/
|
||||
private boolean hitsPaddle;
|
||||
|
||||
/**
|
||||
* The balls color with default component color
|
||||
*/
|
||||
private Color color = Constants.COLOR_COMPONENT;
|
||||
|
||||
/**
|
||||
* The constructor of a ball
|
||||
* The balls position and direction are initialized here.
|
||||
@ -51,6 +64,53 @@ public class Ball implements IBall {
|
||||
return this.direction;
|
||||
}
|
||||
|
||||
/**
|
||||
* The getter for the balls color
|
||||
*
|
||||
* @return color The balls current color
|
||||
*/
|
||||
public Color getColor() { return this.color; }
|
||||
|
||||
/**
|
||||
* The setter for the balls color
|
||||
*
|
||||
* @param color The balls new color
|
||||
*/
|
||||
public void setColor(Color color) { this.color = color; }
|
||||
|
||||
/**
|
||||
* Creates new random color for the ball and sets it
|
||||
*
|
||||
* @see <a href="https://stackoverflow.com/a/4247219">Stackoverflow Answer 4247219</a>
|
||||
*/
|
||||
public void newRandomColor() {
|
||||
Random random = new Random();
|
||||
|
||||
// random hue without red colors
|
||||
float hue = (random.nextInt(18000) + 6000) / 10000f;
|
||||
|
||||
// saturation between 0.5 and 0.7
|
||||
float saturation = (random.nextInt(2000) + 5000) / 10000f;
|
||||
float luminance = 0.9f;
|
||||
Color randColor = Color.getHSBColor(hue, saturation, luminance);
|
||||
|
||||
setColor(randColor);
|
||||
}
|
||||
|
||||
/**
|
||||
* The getter for the balls hit state
|
||||
*
|
||||
* @return hitsPaddle The balls current hit state
|
||||
*/
|
||||
public boolean getHitState() { return this.hitsPaddle; }
|
||||
|
||||
/**
|
||||
* The setter for the balls hit state
|
||||
*
|
||||
* @param state The balls new hit state
|
||||
*/
|
||||
public void setHitState(boolean state) { this.hitsPaddle = state; }
|
||||
|
||||
/**
|
||||
* updates ball position
|
||||
*/
|
||||
|
@ -122,6 +122,19 @@ public class Level extends Thread implements ILevel {
|
||||
// if ball hits paddle (top|bottom), reflect ball
|
||||
if(this.ball.hitsPaddle(paddleTop)) this.ball.reflectOnPaddle(paddleTop);
|
||||
if(this.ball.hitsPaddle(paddleBottom)) this.ball.reflectOnPaddle(paddleBottom);
|
||||
if(this.ball.hitsPaddle(paddleTop) || this.ball.hitsPaddle(paddleBottom)) {
|
||||
this.ball.setHitState(true);
|
||||
// DEBUG OUTPUT
|
||||
//System.out.println(String.format("hitstate: %s, color: %s", this.ball.getHitState(), this.ball.getColor().getRGB()));
|
||||
}
|
||||
|
||||
// if ball has hit a paddle set a new random color to it
|
||||
else if(this.ball.getHitState()) {
|
||||
this.ball.setHitState(false);
|
||||
this.ball.newRandomColor();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Tells the observer to repaint the components on the playground
|
||||
|
@ -94,7 +94,7 @@ public class Field extends JPanel {
|
||||
g2.fillRect(0, 0, getWidth(), getHeight());
|
||||
|
||||
// Setting the color for the following components
|
||||
g2.setColor(new Color(200, 200, 200));
|
||||
g2.setColor(Constants.COLOR_COMPONENT);
|
||||
|
||||
// Calls the method for drawing the ball
|
||||
drawBall(g2);
|
||||
@ -116,10 +116,17 @@ public class Field extends JPanel {
|
||||
* @param g2 The graphics object
|
||||
*/
|
||||
private void drawBall(Graphics2D g2) {
|
||||
// temporarily save default component color to draw ball in specific color
|
||||
Color temp = g2.getColor();
|
||||
g2.setColor(view.getGame().getLevel().getBall().getColor());
|
||||
|
||||
g2.fillOval((int) view.getGame().getLevel().getBall().getPosition().getX(),
|
||||
(int) view.getGame().getLevel().getBall().getPosition().getY(),
|
||||
Constants.BALL_DIAMETER,
|
||||
Constants.BALL_DIAMETER);
|
||||
|
||||
// reset color to default
|
||||
g2.setColor(temp);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user