Эх сурвалжийг харах

Merge remote-tracking branch 'origin/develop' into develop

Kenneth van Ewijk 10 жил өмнө
parent
commit
d5365a59db

+ 1 - 1
.classpath

@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <classpath>
 	<classpathentry kind="src" path="src"/>
-	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/java-8-openjdk"/>
 	<classpathentry kind="output" path="bin"/>
 </classpath>

+ 44 - 0
src/model/levels/Level.java

@@ -0,0 +1,44 @@
+package model.levels;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import model.objects.Ball;
+import model.objects.PowerUp;
+
+public class Level {
+	private List<Ball> startballs;
+	private List<PowerUp> powerups;
+	private int time;
+	
+	public Level(){
+		this.startballs = new ArrayList<Ball>();
+		this.powerups = new ArrayList<PowerUp>();
+	}
+	
+	public void addBall(Ball b){
+		startballs.add(b);
+	}
+	
+	public void addPowerUp(PowerUp p){
+		powerups.add(p);
+	}
+	
+	public void setTime(int t){
+		time = t;
+	}
+
+	public List<Ball> getStartballs() {
+		return startballs;
+	}
+
+	public List<PowerUp> getPowerups() {
+		return powerups;
+	}
+
+	public int getTime() {
+		return time;
+	}
+	
+	
+}

+ 3 - 0
src/model/levels/level1.bb

@@ -0,0 +1,3 @@
+2,600,red,200,200,-1,false
+speed|life
+14000

+ 3 - 0
src/model/levels/level2.bb

@@ -0,0 +1,3 @@
+5,600,green,200,200,-1,false|5,600,green,500,200,1,false
+speed|life
+14000

+ 11 - 2
src/model/objects/Ball.java

@@ -8,16 +8,19 @@ import main.Window;
 
 public class Ball {
 	private Color color;
-	private int size, bounceheight;
+	private int size, bounceheight, direction;
+	private boolean velocity;
 	private Ellipse2D.Double bal;
 
 	double rx, ry; 			// position
 	double vx = 1, vy = 1; 	// velocity
 
-	public Ball(int size, int bounceheight, Color color, int x, int y, int direction) {
+	public Ball(int size, int bounceheight, Color color, int x, int y, int direction, boolean velocity) {
 		this.color = color;
 		this.size = size*20;
 		this.bounceheight = bounceheight;
+		this.direction = direction;
+		this.velocity = velocity;
 		setDirection(direction);
 		rx = x;
 		ry = y;
@@ -32,6 +35,10 @@ public class Ball {
 		return bal.intersects(p.getX(),p.getY(),p.getWidth(),p.getHeigth());
 	}
 	
+	public Ball clone(){
+		return new Ball(getSize(), getBounceHeight(), getColor(), getX(), getY(), direction, velocity);
+	}
+	
 	//** Drawing and Calculating **//
 	
 	public void paint(Graphics2D g2d) {
@@ -103,4 +110,6 @@ public class Ball {
 			vx = d;
 		}
 	}
+	
+	
 }

+ 15 - 1
src/model/objects/Player.java

@@ -1,5 +1,8 @@
 package model.objects;
 
+import java.awt.Point;
+import java.awt.geom.Point2D;
+import java.awt.geom.Point2D.Double;
 import java.awt.image.BufferedImage;
 import java.util.ArrayList;
 
@@ -12,12 +15,14 @@ public class Player extends DrawObject {
 	
 	private long lastMovement;
 	private int lastDirection, frame;
+	private Double beginlocation;
 	private BufferedImage spriteimage;
 	private String name;
 	
 	public Player(BufferedImage image, int x, int y, String name) {
 		super(image.getSubimage(38, 0, 40, 54));
 		super.setPosition(x, y);
+		beginlocation = new Point2D.Double(x,y);
 		this.name = name;
 		spriteimage = image;
 		health = 5;
@@ -51,6 +56,11 @@ public class Player extends DrawObject {
 		}
 	}
 	
+	public void reset(){
+		setPosition(beginlocation);
+		speed = 10;
+	}
+	
 	//** Getters and Setters **//
 	
 	public String getName(){
@@ -63,6 +73,10 @@ public class Player extends DrawObject {
 
 	public int getHealth() {
 		return health;
-	}	
+	}
+	
+	public void setHealth(int health){
+		this.health = health;
+	}
 	
 }

+ 114 - 22
src/model/state/PlayState.java

@@ -3,12 +3,19 @@ package model.state;
 import java.awt.Color;
 import java.awt.Graphics2D;
 import java.awt.event.KeyEvent;
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
 import java.util.ArrayList;
+import java.util.Collection;
+import java.util.ConcurrentModificationException;
+import java.util.List;
 import java.util.ListIterator;
 
-import main.Window;
+import main.Main;
 import model.GameStateManager;
 import model.InfoPanel;
+import model.levels.Level;
 import model.objects.Ball;
 import model.objects.Player;
 import model.objects.ShootingLine;
@@ -20,27 +27,101 @@ public class PlayState extends State{
 	private Player player;
 	private ShootingLine line;
 	private ArrayList<Ball> balls;
+	private ArrayList<Level> levels;
 	private InfoPanel infopanel;
 	private int direction;
 	
+	private int currentlevel;
+	private int timeleft;
+	private long lasttime;
+	
 	public PlayState(GameStateManager gsm) {
 		super("play", gsm);
 		player = new Player(Images.getImage(ImageType.PLAYER1), 40, 550, "Janco");
 		infopanel = new InfoPanel(player, player);
 		balls = new ArrayList<Ball>();
-		balls.add(new Ball(5, 400, Color.red, 300, 50,-1));
+		readLevelsFromFiles();
+		playLevel(levels.get(currentlevel));
 	}
 
+	
+	/** Level file reading **/
+	
+	/**
+	 * Read all level files from a specific directory into level objects
+	 */
+	private void readLevelsFromFiles(){
+		levels = new ArrayList<>();
+		File root = new File(Main.class.getResource("/model/levels").getPath());
+        File[] list = root.listFiles();
+        for ( File f : list ) {
+        	if(f.isFile() && f.getName().endsWith(".bb")){
+        		try {
+					levels.add(readLevel(Files.readAllLines(f.toPath())));		
+				}catch (IOException | IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
+					System.out.println("Level file corrputed");
+					e.printStackTrace();
+				}
+        	}
+        }
+	}
+	
+	/**
+	 * 
+	 * @param levelfile
+	 * @return
+	 * @throws NumberFormatException
+	 * @throws IllegalArgumentException
+	 * @throws IllegalAccessException
+	 * @throws NoSuchFieldException
+	 * @throws SecurityException
+	 */
+	private Level readLevel(List<String> levelfile) throws NumberFormatException, IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException{
+		Level level = new Level();
+		if(levelfile.size() == 3){
+			String[] ballsstrings = levelfile.get(0).split("\\|"); //split all the balls
+			for(String ball:ballsstrings){
+				String[]ballstring = ball.split(",");
+				if(ballstring.length == 7){
+					level.addBall(new Ball(
+										Integer.parseInt(ballstring[0]), 
+										Integer.parseInt(ballstring[1]),
+										(Color)Color.class.getField(ballstring[2]).get(null),
+										Integer.parseInt(ballstring[3]),
+										Integer.parseInt(ballstring[4]),
+										Integer.parseInt(ballstring[5]),
+										Boolean.parseBoolean(ballstring[6])
+										)
+								);
+				}
+			}
+			
+			String[] powerupsstring = levelfile.get(1).split("\\|");
+			
+			level.setTime(Integer.parseInt(levelfile.get(2)));
+		}
+		return level;
+	}
+	
+	public void playLevel(Level level){
+		balls.clear();
+		for(Ball b:level.getStartballs()){
+			balls.add(b.clone());
+		}
+		timeleft = level.getTime();
+		player.reset();
+		line=null;	
+		lasttime = System.currentTimeMillis();
+	}
+	
 	/* INIT AND EXIT */
 	@Override
 	public void init() {
-		System.out.println("Starting play state");
 	}
 	
 	@Override 
 	public void exit()
 	{
-		System.out.println("Stopping play state");
 	}
 
 	
@@ -54,32 +135,43 @@ public class PlayState extends State{
 		player.update();
 		if(line != null){
 			line.setLength(line.getLength()+1);
-			if(line.getMaxHeight() < 0){ 															//Line hit the ceiling, so "remove" it
+			if(line.getMaxHeight() < 0){ 										//Line hit the ceiling, so "remove" it
 				line = null;
 			}
 		}
 		
 		//Collision detection
-		ListIterator<Ball> b = balls.listIterator(); 
-		while(b.hasNext()){
-			Ball bal = b.next(); 
-			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));
-				b.add(new Ball(bal.getSize()-1, 400, bal.getColor(), bal.getX(), bal.getY(), 1));
-			}else if(bal.hitPlayer(player)){														// Collision between player and ball
-				try {
-					throw new Exception("YOU DIED MOTHERFUCKER");
-				} catch (Exception e) {
-					e.printStackTrace();
+		try{
+			ListIterator<Ball> b = balls.listIterator(); 
+			while(b.hasNext()){
+				Ball bal = b.next(); 
+				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));
+				}else if(bal.hitPlayer(player)){														// Collision between player and ball
+					player.setHealth(player.getHealth()-1);
+					playLevel(levels.get(currentlevel));
+				}else if(bal.getSize() <= 0 ){															// Remove ball from arraylist when size is 0 (not visible)
+					b.remove();
+				}else{
+					bal.update();
 				}
-			}else if(bal.getSize() <= 0 ){															// Remove ball from arraylist when size is 0 (not visible)
-				b.remove();
-			}else{
-				bal.update();
 			}
+		}catch(ConcurrentModificationException e){
+			
+		}
+		timeleft -= (System.currentTimeMillis() - lasttime);
+		if(balls.size() == 0){
+			playLevel(levels.get(currentlevel+=1));
+		}else if(timeleft < 0){
+			player.setHealth(player.getHealth()-1);
+			playLevel(levels.get(currentlevel));
+		}else if(player.getHealth() <= 0) {
+			int i = 1/0;
 		}
+		lasttime = System.currentTimeMillis();		
 	}
 
 	@Override

+ 2 - 0
src/view/GameView.java

@@ -4,6 +4,7 @@ import java.awt.Color;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.RenderingHints;
+import java.awt.Toolkit;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 
@@ -40,6 +41,7 @@ public class GameView extends JPanel implements ActionListener{
 		RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 	    g2d.setRenderingHints(rh);
 		gsm.paint(g2d);
+		Toolkit.getDefaultToolkit().sync();
 	}
 
 	@Override