Sfoglia il codice sorgente

Fixed bounceheight and physics

Kenneth van Ewijk 10 anni fa
parent
commit
5eaa0ef4ba

+ 2 - 2
src/model/levels/level1.bb

@@ -1,3 +1,3 @@
-2,600,red,200,200,-1,false
+2,200,red,400,400,-1,0.0
 speed|life
-14000
+18000

+ 2 - 2
src/model/levels/level2.bb

@@ -1,3 +1,3 @@
-5,600,green,200,200,-1,false|5,600,green,500,200,1,false
+3,200,green,200,200,-1,1.0|4,300,green,500,200,1,1.0
 speed|life
-14000
+30000

+ 20 - 9
src/model/objects/Ball.java

@@ -9,18 +9,17 @@ import main.Window;
 public class Ball {
 	private Color color;
 	private int size, bounceheight, direction;
-	private boolean velocity;
 	private Ellipse2D.Double bal;
 
 	double rx, ry; 			// position
-	double vx = 1, vy = 1; 	// velocity
+	double vx = 0, vy = 0; 	// velocity
 
-	public Ball(int size, int bounceheight, Color color, int x, int y, int direction, boolean 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.velocity = velocity;
+		this.vy = velocity;
 		setDirection(direction);
 		rx = x;
 		ry = y;
@@ -36,7 +35,7 @@ public class Ball {
 	}
 	
 	public Ball clone(){
-		return new Ball(getSize(), getBounceHeight(), getColor(), getX(), getY(), direction, velocity);
+		return new Ball(getSize(), getBounceHeight(), getColor(), getX(), getY(), direction, vy);
 	}
 	
 	//** Drawing and Calculating **//
@@ -44,6 +43,7 @@ public class Ball {
 	public void paint(Graphics2D g2d) {
 		g2d.setColor(color);
 		g2d.fill(bal);
+		g2d.drawLine(0, 600-bounceheight, 500, 600-bounceheight);
 	}
 
 	public void update() {
@@ -53,16 +53,23 @@ public class Ball {
 			vx = 1;
 		
 		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 <= (Window.HEIGHT - bounceheight))
-//			vy = 4;
-//		else 
 		if (ry >= (600-size))
 			vy *= -1;
 		
 		rx += vx;
 		ry += vy;
-		
 		setX((int) rx);
 		setY((int) ry);
 	}
@@ -111,5 +118,9 @@ public class Ball {
 		}
 	}
 	
+	public double getYSpeed()
+	{
+		return vy;
+	}
 	
 }

+ 3 - 3
src/model/state/PlayState.java

@@ -90,7 +90,7 @@ public class PlayState extends State{
 										Integer.parseInt(ballstring[3]),
 										Integer.parseInt(ballstring[4]),
 										Integer.parseInt(ballstring[5]),
-										Boolean.parseBoolean(ballstring[6])
+										Double.parseDouble(ballstring[6])
 										)
 								);
 				}
@@ -148,8 +148,8 @@ public class PlayState extends State{
 				if(line != null && bal.hitLine(line)){								// Collision between line and ball
 					line = null;
 					b.remove();
-					b.add(new Ball(bal.getSize()-1, 400, bal.getColor(), bal.getX(), bal.getY(), -1, true));
-					b.add(new Ball(bal.getSize()-1, 400, bal.getColor(), bal.getX(), bal.getY(), 1, true));
+					b.add(new Ball(bal.getSize()-1, (int) (bal.getBounceHeight()/1.2), bal.getColor(), bal.getX(), bal.getY(), -1, Math.abs(bal.getYSpeed())*-1));
+					b.add(new Ball(bal.getSize()-1, (int) (bal.getBounceHeight()/1.2), bal.getColor(), bal.getX(), bal.getY(), 1, Math.abs(bal.getYSpeed())*-1));
 				}else if(bal.hitPlayer(player)){														// Collision between player and ball
 					player.setHealth(player.getHealth()-1);
 					playLevel(levels.get(currentlevel));