| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- package model.objects;
- import java.awt.Color;
- import java.awt.Graphics2D;
- import java.awt.geom.Ellipse2D;
- import main.Window;
- public class Ball {
- private Color color;
- private int size, bounceheight, direction;
- private Ellipse2D.Double bal;
- double rx, ry; // position
- double vx, vy; // velocity
- public Ball(int size, int bounceheight, Color color, int x, int y, int direction, double velocity) {
- this.color = color;
- this.size = size*20;
- this.bounceheight = bounceheight;
- this.direction = direction;
- this.vx = direction*3;
- this.vy = velocity;
- setDirection(direction);
- rx = x;
- ry = y;
- bal = new Ellipse2D.Double(x, y, this.size, this.size);
- }
- public boolean hitLine(ShootingLine l){
- return bal.intersects(l.getX(), l.getY()-l.getHeight(), l.getWidth(), l.getHeight());
- }
-
- public boolean hitPlayer(Player p){
- return bal.intersects(p.getX(),p.getY(),p.getWidth(),p.getHeigth());
- }
-
- public Ball clone(){
- return new Ball(getSize(), getBounceHeight(), getColor(), getX(), getY(), direction, vy);
- }
-
- //** Drawing and Calculating **//
-
- public void paint(Graphics2D g2d) {
- g2d.setColor(color);
- g2d.fill(bal);
- g2d.drawLine(0, 600-bounceheight, 500, 600-bounceheight);
- }
- public void update() {
- if (rx >= (Window.WIDTH - size - 10))
- vx = -3;
- else if (rx <= 10)
- vx =3;
-
- vy += 0.5;
-
- if(ry+size < 600-bounceheight && vy < 0)
- {
- vy +=0.5;
- }
- else if(ry+size < 600-bounceheight && vy > 0)
- {
- vy -=0.2;
- }
- if (ry >= (600-size))
- vy *= -1;
-
- rx += vx;
- ry += vy;
- setX((int) rx);
- setY((int) ry);
- }
-
- //** Getters and Setters **//
-
- public int getSize() {
- return size/20;
- }
- public int getBounceHeight() {
- return bounceheight;
- }
- public int getX() {
- return (int) bal.getX();
- }
- public int getY() {
- return (int) bal.getY();
- }
- public int getWidth() {
- return (int) bal.getWidth();
- }
- public int getHeight() {
- return (int) bal.getHeight();
- }
-
- public Color getColor() {
- return color;
- }
- public void setX(int x) {
- bal.setFrame(x, getY(), getWidth(), getHeight());
- }
- public void setY(int y) {
- bal.setFrame(getX(), y, getWidth(), getHeight());
- }
-
- public void setDirection(int d){
- if(d == -1 || d == 1){
- direction = d;
- vx = d*3;
- }
- }
-
- public double getYSpeed()
- {
- return vy;
- }
-
- }
|