فهرست منبع

Added collision detection with balls, lines and player

jancoow 10 سال پیش
والد
کامیت
a24d379edd
3فایلهای تغییر یافته به همراه76 افزوده شده و 41 حذف شده
  1. 26 4
      src/model/objects/Ball.java
  2. 15 6
      src/model/objects/ShootingLine.java
  3. 35 31
      src/model/state/PlayState.java

+ 26 - 4
src/model/objects/Ball.java

@@ -14,10 +14,13 @@ public class Ball {
 	double rx = 100, ry = 100; // position
 	double vx = 1, vy = 1; // velocity
 
-	public Ball(int size, int bounceheight, Color color, int x, int y) {
+	public Ball(int size, int bounceheight, Color color, int x, int y, int direction) {
 		this.color = color;
 		this.size = size*20;
 		this.bounceheight = bounceheight;
+		setDirection(direction);
+		rx = x;
+		ry = y;
 		bal = new Ellipse2D.Double(x, y, this.size, this.size);
 	}
 
@@ -33,9 +36,9 @@ public class Ball {
 			vx = 1;
 
 		if (ry <= (Window.HEIGHT - bounceheight))
-			vy= 3;
+			vy= 4;
 		else if (ry >= (600-size))
-			vy = -3;
+			vy = -4;
 
 		rx += vx;
 		ry += vy;
@@ -45,7 +48,7 @@ public class Ball {
 	}
 
 	public int getSize() {
-		return size;
+		return size/20;
 	}
 
 	public int getBounceHeight() {
@@ -67,6 +70,10 @@ public class Ball {
 	public int getHeight() {
 		return (int) bal.getHeight();
 	}
+	
+	public Color getColor() {
+		return color;
+	}
 
 	public void setX(int x) {
 		bal.setFrame(x, getY(), getWidth(), getHeight());
@@ -83,5 +90,20 @@ public class Ball {
 	public void setHeight(int h) {
 		bal.setFrame(getX(), getY(), getWidth(), h);
 	}
+	
+	public void setDirection(int d){
+		if(d == -1 || d == 1){
+			vx = d;
+		}
+	}
+	
+	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());
+	}
+	
 
 }

+ 15 - 6
src/model/objects/ShootingLine.java

@@ -30,20 +30,29 @@ public class ShootingLine {
 		g2d.draw(line);
 		g2d.drawLine(startx, getMaxHeight(), startx+8, getMaxHeight()+15);
 		g2d.drawLine(startx, getMaxHeight(), startx-8, getMaxHeight()+15);
-		
 	}
 	
 	public int getLength(){
 		return length;
 	}
 	
-	public Path2D getLine(){
-		return line;
-	}
-	
 	public int getMaxHeight(){
 		return starty - length * 30;
 	}
 	
-
+	public int getX(){
+		return startx;
+	}
+	
+	public int getY(){
+		return starty;
+	}
+	
+	public int getWidth(){
+		return 10;
+	}
+	
+	public int getHeight(){
+		return getLength() * 30;
+	}
 }

+ 35 - 31
src/model/state/PlayState.java

@@ -2,13 +2,9 @@ package model.state;
 
 import java.awt.Color;
 import java.awt.Graphics2D;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
 import java.awt.event.KeyEvent;
 import java.util.ArrayList;
-import java.util.Iterator;
-
-import javax.swing.Timer;
+import java.util.ListIterator;
 
 import main.Window;
 import model.GameStateManager;
@@ -18,24 +14,21 @@ import model.objects.ShootingLine;
 import resources.image.Images;
 import resources.image.Images.ImageType;
 
-public class PlayState extends State implements ActionListener{
+public class PlayState extends State{
 	
 	Player player;
-	ArrayList<ShootingLine> lines;
+	ShootingLine line;
 	ArrayList<Ball> balls;
 	
-	Timer t;
 	int direction;
 	
 	public PlayState(GameStateManager gsm) {
 		super("play", gsm);
 		
 		player = new Player(Images.getImage(ImageType.PLAYER1), 40, 550);
-		lines = new ArrayList<ShootingLine>();
 		balls = new ArrayList<Ball>();
-		balls.add(new Ball(5, 400, Color.red, 50, 50));
+		balls.add(new Ball(5, 400, Color.red, 300, 50,-1));
 		
-		t = new Timer(1000/5, this);
 	}
 
 	/* INIT AND EXIT */
@@ -68,28 +61,45 @@ public class PlayState extends State implements ActionListener{
 				player.setX(32);
 			}
 		player.update();
-		for(Ball b: balls){
-			b.update();
+		if(line != null){
+			line.setLength(line.getLength()+1);
+			if(line.getMaxHeight() <0){
+				line = null;
+			}
 		}
-		Iterator<ShootingLine> i = lines.iterator();
-		while(i.hasNext()){
-			ShootingLine l = i.next();
-			if(l.getMaxHeight() < 0){
-				i.remove();
+		
+		//collision detection
+		ListIterator<Ball> b = balls.listIterator(); 
+		while(b.hasNext()){
+			Ball bal = b.next(); 
+			if(line != null && bal.hitLine(line)){ // line - ball
+				line = null;
+				b.remove();
+				b.add(new Ball(bal.getSize()-1, 400, bal.getColor(), bal.getX(), bal.getY(), -1));
+				b.add(new Ball(bal.getSize()-1, 400, bal.getColor(), bal.getX(), bal.getY(), 1));
+			}else if(bal.hitPlayer(player)){
+				try {
+					throw new Exception("YOU DIED MOTHERFUCKER");
+				} catch (Exception e) {
+					e.printStackTrace();
+				}
+			}else if(bal.getSize() <= 0 ){
+				b.remove();
 			}else{
-				l.setLength(l.getLength() +1);
+				bal.update();
 			}
 		}
+		
+		
 	}
 
 	@Override
 	public void paint(Graphics2D g2d) {	
 		g2d.drawString("Press ESC to stop the game", Window.WIDTH/2, Window.HEIGHT/2);
 		g2d.drawImage(Images.getImage(ImageType.BACKGROUND),null,0,0);
-		
 
-		for(ShootingLine l:lines){
-			l.paint(g2d);
+		if(line != null){
+			line.paint(g2d);
 		}
 		for(Ball b: balls){
 			b.paint(g2d);
@@ -110,8 +120,8 @@ public class PlayState extends State implements ActionListener{
 			direction = -1;
 			break;
 		case KeyEvent.VK_SPACE:
-			if(lines.size() == 0){
-				lines.add(new ShootingLine((int)player.getX()+player.getWidth()/2, 600));
+			if(line == null){
+				line = new ShootingLine((int)player.getX()+player.getWidth()/2, 600);
 			}
 		}
 	}
@@ -129,11 +139,5 @@ public class PlayState extends State implements ActionListener{
 		}
 	}
 	@Override
-	public void keyTyped(KeyEvent e) {}
-
-	@Override
-	public void actionPerformed(ActionEvent arg0) {
-		// TODO Auto-generated method stub
-		
-	}
+	public void keyTyped(KeyEvent e) {};
 }