Forráskód Böngészése

Added comments.
Removed errors.
Added levels

Kenneth van Ewijk 10 éve
szülő
commit
6319651469

+ 1 - 0
server/server/Main.java

@@ -3,6 +3,7 @@ package server;
 public class Main {
 
 	public static void main(String[] args) {
+		//Start a server on port
 		new Server(1234);
 	}
 

+ 1 - 1
server/server/NetworkUser.java

@@ -16,6 +16,7 @@ public class NetworkUser {
 	private Match match;
 	private int pid;
 
+	//A thread which handles the server communication to a client
 	public NetworkUser(Socket client) {
 		this.client = client;
 		try {
@@ -30,7 +31,6 @@ public class NetworkUser {
 			System.out.println(name);
 			client.setSoTimeout(0);
 		} catch (IOException e) {
-			e.printStackTrace();
 		}
 		receivethread = new Thread(new Runnable() {
 			@Override

+ 5 - 14
server/server/Server.java

@@ -11,8 +11,11 @@ public class Server {
 	private ArrayList<NetworkUser> matchMakingUsers;
 
 	public Server(final int port) {
+		
+		//Users waiting to play
 		matchMakingUsers = new ArrayList<NetworkUser>();
 
+		//Server Thread
 		Thread clientconnect = new Thread(new Runnable() {
 			@Override
 			public void run() {
@@ -22,21 +25,10 @@ public class Server {
 						try {
 							Socket client = server.accept();
 
+							//User connects and is added to wait list
 							matchMakingUsers.add(new NetworkUser(client));
 
-							// Check if all users in the matchMakingList are
-							// still connected
-							// Iterator<NetworkUser> i =
-							// matchMakingUsers.iterator();
-							// while(i.hasNext()){
-							// if(!i.next().checkConnection()){
-							// i.remove();
-							// System.out.println("Someone dissconected :(");
-							//
-							// }
-							// }
-							//
-							// Check if there are enough players to make a match
+							//Match first two users
 							if (matchMakingUsers.size() >= 2) {
 								new Thread(new Match(matchMakingUsers.get(0), matchMakingUsers.get(1)));
 								matchMakingUsers.remove(1);
@@ -44,7 +36,6 @@ public class Server {
 							}
 							System.out.println("New client connected: " + client.getRemoteSocketAddress());
 						} catch (IOException e) {
-							e.printStackTrace();
 						}
 					}
 				} catch (IOException e) {

+ 6 - 5
server/server/match/Match.java

@@ -31,6 +31,10 @@ public class Match implements Runnable {
 	private int timeleft;
 	private long lasttime;
 
+	
+	//Contains all the server game logic.
+	//The server decides when collision occurs or a player is allowed to move.
+	//Also manages health, time and chat
 	public Match(NetworkUser nwuser1, NetworkUser nwuser2) {
 		nwuser1.startMatch(nwuser1.getName(), nwuser2.getName(), 1, this);
 		nwuser2.startMatch(nwuser2.getName(), nwuser1.getName(), 2, this);
@@ -52,6 +56,7 @@ public class Match implements Runnable {
 		}, 0, 1000 / 30);
 	}
 
+	//Play a selected level
 	public void playLevel(Level level) {
 		balls.clear();
 		if(level == null){
@@ -161,9 +166,7 @@ public class Match implements Runnable {
 					bal.update();
 				}
 			}
-		} catch (ConcurrentModificationException e) {
-
-		}
+		} catch (ConcurrentModificationException e) {}
 		timeleft -= (System.currentTimeMillis() - lasttime);
 		if (balls.size() == 0) {
 			if (player1.getHealth() > 0)
@@ -240,12 +243,10 @@ public class Match implements Runnable {
 		try {
 			nwuser1.close();
 		} catch (IOException e) {
-			e.printStackTrace();
 		}
 		try {
 			nwuser2.close();
 		} catch (IOException e) {
-			e.printStackTrace();
 		}
 	}
 

+ 1 - 0
server/server/match/levels/Level.java

@@ -9,6 +9,7 @@ public class Level {
 	private List<Ball> startballs;
 	private int time;
 
+	//Contains all the info to start a level
 	public Level() {
 		this.startballs = new ArrayList<Ball>();
 	}

+ 2 - 0
server/server/match/levels/level3.bb

@@ -0,0 +1,2 @@
+2,200,green,200,200,-1,0.0|3,300,magenta,350,200,1,0.0|2,150,yellow,500,100,1,0.0
+40000

+ 2 - 0
server/server/match/levels/level4.bb

@@ -0,0 +1,2 @@
+4,400,blue,600,100,-1,0.0
+40000

+ 2 - 0
server/server/match/levels/level5.bb

@@ -0,0 +1,2 @@
+7,650,green,300,100,1,0.0
+120000

+ 2 - 0
server/server/match/levels/level6.bb

@@ -0,0 +1,2 @@
+4,500,magenta,200,600,-1,0.0|5,400,yellow,300,500,1,0.0|3,500,blue,600,550,-1,0.0
+120000

+ 2 - 0
server/server/match/objects/Ball.java

@@ -13,6 +13,7 @@ public class Ball {
 	double rx, ry; // position
 	double vx, vy; // velocity
 
+	//Contains the logic of a ball
 	public Ball(int size, int bounceheight, Color color, int x, int y, int direction, double velocity) {
 		this.color = color;
 		this.size = size * 20;
@@ -26,6 +27,7 @@ public class Ball {
 		bal = new Ellipse2D.Double(x, y, this.size, this.size);
 	}
 
+	//Collision
 	public boolean hitLine(ShootingLine l) {
 		return bal.intersects(l.getX(), l.getY() - l.getHeight(), l.getWidth(), l.getHeight());
 	}

+ 1 - 0
server/server/match/objects/DrawObject.java

@@ -8,6 +8,7 @@ public class DrawObject {
 	protected Point2D position;
 	protected BufferedImage image;
 
+	//Contains the logic for offline DrawObjects
 	public DrawObject(BufferedImage image) {
 		this.image = image;
 

+ 4 - 0
server/server/match/objects/Player.java

@@ -11,6 +11,7 @@ public class Player extends DrawObject {
 	private int score, health, speed = 10;
 	private Double beginlocation;
 
+	//Contains the logic for players
 	public Player(BufferedImage image, int x, int y) {
 		super(image.getSubimage(38, 0, 40, 54));
 		super.setPosition(x, y);
@@ -18,18 +19,21 @@ public class Player extends DrawObject {
 		health = 5;
 	}
 
+	//Walk left when key is pressed
 	public void walkLeft() {
 		if (getX() - speed > 10) {
 			setX(getX() - speed);
 		}
 	}
 
+	//Walk right when key is pressed
 	public void walkRight() {
 		if (getX() + getWidth() + speed < Window.WIDTH - 10) {
 			setX(getX() + speed);
 		}
 	}
 
+	//Reset player to start location
 	public void reset() {
 		setPosition(beginlocation);
 		speed = 10;

+ 1 - 0
server/server/match/objects/ShootingLine.java

@@ -3,6 +3,7 @@ package server.match.objects;
 public class ShootingLine {
 	private int startx, starty, length;
 
+	//Contains logic of a line
 	public ShootingLine(int startx, int starty) {
 		this.startx = startx;
 		this.starty = starty;

+ 2 - 0
src/main/Main.java

@@ -3,6 +3,8 @@ package main;
 public class Main {
 
 	public static void main(String[] args) {
+		
+		//Start new game
 		new Window();
 	}
 

+ 9 - 0
src/main/Window.java

@@ -17,33 +17,41 @@ import control.GameControl;
 @SuppressWarnings("serial")
 public class Window extends JFrame {
 
+	//Window width and height
 	public static final int WIDTH = 800;
 	public static final int HEIGHT = 800;
 
+	//Update and draw speed
 	public static final int UPDATES_PER_SECOND = 30;
 	public static final int FRAMES_PER_SECOND = 30;
 
 	public Window() {
+		//Open new window
 		super("Bubble Trouble");
 		setSize(WIDTH, HEIGHT);
 		setResizable(false);
 		setDefaultCloseOperation(3);
 
+		//Start gamestate manager and MVC model
 		GameStateManager gsm = new GameStateManager();
 		GameModel model = new GameModel(gsm);
 		GameView view = new GameView(model, gsm);
 		GameControl control = new GameControl(view, model, gsm);
 
+		//Keyboard
 		addKeyListener(control);
 
 		setContentPane(view);
 
 		setVisible(true);
+		
+		//Start playing the theme song
 		playAudio();
 	}
 	
 	private void playAudio()
 	{
+		//Load theme song from resources and play
 		Clip clip = null;
 		try {
 			clip = AudioSystem.getClip();
@@ -53,6 +61,7 @@ public class Window extends JFrame {
 			clip.close();
 		}
 	    
+		//Loop the theme song
 	    clip.loop(Clip.LOOP_CONTINUOUSLY);
 	}
 	

+ 1 - 0
src/model/GameModel.java

@@ -15,6 +15,7 @@ public class GameModel implements ActionListener {
 	public GameModel(GameStateManager gsm) {
 		this.gsm = gsm;
 
+		//Start the update timer
 		t = new Timer(1000 / Window.UPDATES_PER_SECOND, this);
 		t.start();
 	}

+ 10 - 2
src/model/GameStateManager.java

@@ -5,7 +5,6 @@ import java.awt.event.KeyEvent;
 import java.util.ArrayList;
 import java.util.List;
 
-import model.state.EndState;
 import model.state.MenuState;
 import model.state.PlayState;
 import model.state.State;
@@ -15,16 +14,17 @@ public class GameStateManager {
 	private List<State> gamestates;
 	private State currentState;
 
+	//Manage all the states and allow to switch between them
 	public GameStateManager() {
 		gamestates = new ArrayList<State>();
 
 		gamestates.add(new MenuState(this));
 		gamestates.add(new PlayState(this));
-		gamestates.add(new EndState(this));
 
 		currentState = gamestates.get(0);
 	}
 
+	//Change the current state
 	public void setState(String name) {
 		for (State s : gamestates) {
 			if (s.getName() == name) {
@@ -34,6 +34,7 @@ public class GameStateManager {
 		}
 	}
 	
+	//Get the current state
 	public State getState(String name) {
 		for (State s : gamestates) {
 			if (s.getName() == name) {
@@ -43,6 +44,7 @@ public class GameStateManager {
 		return null;
 	}
 
+	//Private method to change the state
 	private void changeState(State st) {
 		if (!currentState.equals(st)) {
 			currentState.exit();
@@ -51,14 +53,20 @@ public class GameStateManager {
 		}
 	}
 
+	//Update the current state
 	public void update() {
 		currentState.update();
 	}
 
+	//Paint the current state
 	public void paint(Graphics2D g2d) {
 		currentState.paint(g2d);
 	}
 
+	/*
+	 * Send keypresses to the current state
+	 */
+	
 	public void keyPressed(KeyEvent e) {
 		currentState.keyPressed(e);
 	}

+ 11 - 2
src/model/InfoPanel.java

@@ -17,6 +17,7 @@ public class InfoPanel {
 	private int OFFSET = 610;
 	private List<String> chat;
 
+	//Draw the game hud
 	public InfoPanel(PlayState play) {
 		this.play = play;
 		chat = new ArrayList<String>();
@@ -24,6 +25,7 @@ public class InfoPanel {
 
 	public void paint(Graphics2D g2d) {
 		g2d.setColor(Color.red);
+		//TIME
 		g2d.fillRect(200, OFFSET, 400 / 100 * play.getTimeleftpercent(), 20);
 
 		g2d.setColor(Color.black);
@@ -32,38 +34,45 @@ public class InfoPanel {
 
 		// Player 1
 		g2d.drawLine(35, OFFSET, 35, Window.HEIGHT);
+		//P1 Name
 		g2d.drawString(play.getPlayer1().getName(), 40, OFFSET + 5 + g2d.getFont().getSize());
+		//P1 Score
 		g2d.drawString(play.getPlayer1().getScore() + " xp", 40, OFFSET + 5 + g2d.getFont().getSize() * 2 + 5);
+		//P1 Health
 		for (int i = 0; i < play.getPlayer1().getHealth(); i++) {
 			g2d.drawImage(Images.getImage(ImageType.HEARTH), null, 5, OFFSET + (30 * i));
 		}
 		g2d.drawLine(35, OFFSET + g2d.getFont().getSize() * 3, 200, OFFSET + g2d.getFont().getSize() * 3);
-		g2d.drawString("<Powerups>", 40, OFFSET + g2d.getFont().getSize() * 4);
 
 		// Chat
 		g2d.drawLine(200, OFFSET, 200, Window.HEIGHT);
+		//Chat
 		for(int i=0; i<chat.size(); i++)
 		{
 			g2d.drawString(chat.get(i), 210, OFFSET + 50  + g2d.getFont().getSize()*i);
 		}
 		g2d.drawLine(600, OFFSET, 600, Window.HEIGHT);
 		g2d.drawLine(200, OFFSET + 60  + g2d.getFont().getSize()*3, 600, OFFSET + 60  + g2d.getFont().getSize()*3);
+		//Current message being typed
 		g2d.drawString(play.chatMessage, 270, Window.HEIGHT - 20 - g2d.getFont().getSize());
 
 		g2d.drawLine(Window.WIDTH - 35, OFFSET, Window.WIDTH - 35, Window.HEIGHT);
+		//P2 Name
 		g2d.drawString(play.getPlayer2().getName(), Window.WIDTH - 40 - play.getPlayer2().getName().length() * 15, OFFSET + 5 + g2d.getFont().getSize());
+		//P2 Score
 		g2d.drawString(play.getPlayer2().getScore() + " xp", Window.WIDTH - 40 - ((play.getPlayer1().getScore() + " ").length() + 3) * 15, OFFSET + 5 + g2d.getFont().getSize() * 2 + 5);
+		//P2 Health
 		for (int i = 0; i < play.getPlayer2().getHealth(); i++) {
 			g2d.drawImage(Images.getImage(ImageType.HEARTH), null, Window.WIDTH - 30, OFFSET + (30 * i));
 		}
 		g2d.drawLine(Window.WIDTH - 35, OFFSET + g2d.getFont().getSize() * 3, Window.WIDTH - 200, OFFSET + g2d.getFont().getSize() * 3);
-		g2d.drawString("<Powerups>", Window.WIDTH - 40 - 140, OFFSET + g2d.getFont().getSize() * 4);
 		
 		if(play.typing)
 			g2d.setColor(Color.RED);
 		g2d.drawString("Chat: ", 210, Window.HEIGHT - 20 - g2d.getFont().getSize());
 	}
 
+	//Add a chat message
 	public void addChatMessage(String msg) {
 		chat.add(msg);
 		

+ 13 - 1
src/model/NetworkConnection.java

@@ -19,11 +19,13 @@ public class NetworkConnection {
 	private PlayState play;
 	private Socket server;
 
+	//Connection to the server
 	public NetworkConnection(PlayState play) {
 		this.play = play;
 		play.setOverlayText("Waiting for other players..");
 	}
 
+	//Try to connect to the server
 	public void connect(String name) throws UnknownHostException, IOException {
 		if (checkConnection()) {
 			throw new IOException("Already connected!");
@@ -49,6 +51,7 @@ public class NetworkConnection {
 		receivethread.start();
 	}
 
+	//Check if the connection already exists
 	public boolean checkConnection() {
 		if (dout == null)
 			return false;
@@ -60,6 +63,7 @@ public class NetworkConnection {
 		return true;
 	}
 
+	//Receive a message from the server and decode it
 	private void receiveMessage() {
 		try {
 			String message = din.readUTF();
@@ -71,10 +75,12 @@ public class NetworkConnection {
 			case "2": // Update message
 				messagesplit = message.split(",");
 
+				//Update players
 				String[] playersplit = messagesplit[0].split("\\|");
 				play.updatePlayerInfo(Integer.parseInt(playersplit[1]), Integer.parseInt(playersplit[2]), Integer.parseInt(playersplit[3]), Integer.parseInt(playersplit[4]), play.getPlayer1());
 				play.updatePlayerInfo(Integer.parseInt(playersplit[5]), Integer.parseInt(playersplit[6]), Integer.parseInt(playersplit[7]), Integer.parseInt(playersplit[8]), play.getPlayer2());
 
+				//Update Balls
 				String[] ballsplit = messagesplit[1].split("\\|");
 				if (ballsplit.length / 4 != play.getBalls().size()) {
 					play.getBalls().clear();
@@ -83,9 +89,11 @@ public class NetworkConnection {
 					play.getBall(i).updateInfo(Integer.parseInt(ballsplit[(i * 4) + 0]), Integer.parseInt(ballsplit[(i * 4) + 1]), Color.decode(ballsplit[(i * 4) + 3]), Integer.parseInt(ballsplit[(i * 4) + 2]));
 				}
 
+				//Update lines
 				String[] linesplit = messagesplit[2].split("\\|");
 				play.updateLines(Integer.parseInt(linesplit[0]), Integer.parseInt(linesplit[1]), Integer.parseInt(linesplit[2]), Integer.parseInt(linesplit[3]), Integer.parseInt(linesplit[4]), Integer.parseInt(linesplit[5]));
-
+				
+				//Update time
 				play.setTimeleftpercent(Integer.parseInt(messagesplit[3]));
 				break;
 			case "3": //Overlay text
@@ -105,15 +113,18 @@ public class NetworkConnection {
 
 	}
 
+	//Send control request to server
 	public void sendInput(int direction, int shoot) {
 		sendMessage("1|" + direction + "|" + shoot);
 	}
 
+	//Send chat message to server
 	public void sendChatMessage(String str)
 	{
 		sendMessage("2|" + str);
 	}
 	
+	//Close connection to server and stop the thread
 	public void close() throws IOException{
 		receivethread.stop();
 		din.close();
@@ -121,6 +132,7 @@ public class NetworkConnection {
 		server.close();
 	}
 
+	//Send a message
 	private void sendMessage(String m) {
 		try {
 			dout.writeUTF(m);

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

@@ -64,6 +64,8 @@ public class Ball {
 		bal.setFrame(getX(), getY(), size, size);
 	}
 
+	
+	//Update info based on network data
 	public void updateInfo(int x, int y, Color color, int size) {
 		setX(x);
 		setY(y);

+ 2 - 0
src/model/objects/ShootingLine.java

@@ -18,6 +18,8 @@ public class ShootingLine {
 		line = new Path2D.Double();
 		line.moveTo(startx, starty);
 		int tempy = starty;
+		
+		//Create curve
 		for (int i = 0; i < length; i++) {
 			line.curveTo(startx + 10, tempy - 10, startx - 10, tempy - 20, startx, tempy - 30);
 			tempy -= 30;

+ 0 - 51
src/model/state/EndState.java

@@ -1,51 +0,0 @@
-package model.state;
-
-import java.awt.Graphics2D;
-import java.awt.event.KeyEvent;
-
-import model.GameStateManager;
-
-public class EndState extends State {
-
-	public EndState(GameStateManager gsm) {
-		super("end", gsm);
-	}
-
-	@Override
-	public void init() {
-	}
-
-	@Override
-	public void exit() {
-	}
-
-	@Override
-	public void update() {
-	}
-
-	@Override
-	public void paint(Graphics2D g2) {
-		g2.drawString("GAME OVER", 100, 100);
-	}
-
-	@Override
-	public void keyPressed(KeyEvent e) {
-		// TODO Auto-generated method stub
-
-	}
-
-	@Override
-	public void keyReleased(KeyEvent e) {
-		if (e.getKeyCode() == KeyEvent.VK_ENTER) {
-			gsm.setState("menu");
-		}
-
-	}
-
-	@Override
-	public void keyTyped(KeyEvent e) {
-		// TODO Auto-generated method stub
-
-	}
-
-}

+ 7 - 0
src/model/state/MenuState.java

@@ -12,9 +12,13 @@ import resources.image.Images.ImageType;
 
 public class MenuState extends State {
 	
+	//Create random username on start
 	public static String name = "Guest" + Math.round((Math.random()*1000));
+	
+	//Disallow certain characters in name
 	private static int[] not_allowed = {KeyEvent.VK_COMMA, KeyEvent.VK_SEMICOLON, KeyEvent.VK_PERIOD, KeyEvent.VK_QUOTE, KeyEvent.VK_QUOTEDBL, KeyEvent.VK_SHIFT};
 
+	//This is the menu state, it is the default state when the game starts
 	public MenuState(GameStateManager gsm) {
 		super("menu", gsm);
 	}
@@ -58,11 +62,13 @@ public class MenuState extends State {
 	@Override
 	public void keyPressed(KeyEvent e) {
 		if (e.getKeyCode() == KeyEvent.VK_ENTER) {
+			//Start the game
 			gsm.setState("play");
 		}
 		
 		if(e.getKeyCode() == KeyEvent.VK_BACK_SPACE && name.length() > 0)
 		{
+			//Remove last letter from name when BACKSPACE is pressed
 			name = name.substring(0, name.length()-1);
 		}
 	}
@@ -71,6 +77,7 @@ public class MenuState extends State {
 	public void keyReleased(KeyEvent e) {
 		if(name.length() <= 15 && e.getKeyCode() != KeyEvent.VK_BACK_SPACE && !Arrays.asList(not_allowed).contains(e.getKeyCode()))
 		{
+			//Add allowed characters to name
 			name += e.getKeyChar();
 		}
 	}

+ 15 - 6
src/model/state/PlayState.java

@@ -31,11 +31,15 @@ public class PlayState extends State {
 	public String chatMessage = "";
 	public boolean typing = false;
 
+	//The state in which the game runs
 	public PlayState(GameStateManager gsm) {
 		super("play", gsm);
+		player1 = null;
+		player2 = null;
 	}
 
 	/* NETWORK CONTROL */
+	//This is called when a match has been made by the server
 	public void newMatch(String p1, String p2) {
 		player1 = new Player(Images.getImage(ImageType.PLAYER1), 40, 550, p1);
 		player2 = new Player(Images.getImage(ImageType.PLAYER2), 90, 550, p2);
@@ -48,6 +52,8 @@ public class PlayState extends State {
 	}
 
 	/* INIT AND EXIT */
+	
+	//This is called when this state loads
 	@Override
 	public void init() {
 		matchStarted = false;
@@ -58,13 +64,13 @@ public class PlayState extends State {
 		}
 	}
 
+	//Close the match and connection
 	@Override
 	public void exit() {
 		try {
 			matchStarted = false;
 			nwc.close();
 		} catch (IOException e) {
-			e.printStackTrace();
 		}
 	}
 
@@ -91,11 +97,14 @@ public class PlayState extends State {
 
 	@Override
 	public void update() {
-		if(player1.getHealth()  == 0 && player2.getHealth() == 0){
-			setOverlayText("Game over");
+		if(player1 != null && player2 != null)
+		{
+			if(player1.getHealth()  == 0 && player2.getHealth() == 0){
+				setOverlayText("Game over");
+			}
+			player1.update();
+			player2.update();
 		}
-		player1.update();
-		player2.update();
 	}
 
 	@Override
@@ -159,7 +168,6 @@ public class PlayState extends State {
 		try {
 			nwc.close();
 		} catch (IOException e) {
-			e.printStackTrace();
 		}
 		matchStarted = false;
 	}
@@ -232,6 +240,7 @@ public class PlayState extends State {
 	public void keyTyped(KeyEvent e) {
 	};
 
+	//Add a chatmessage to this match
 	public void addChatMessage(String msg) {
 		infopanel.addChatMessage(msg);
 	}

+ 1 - 0
src/model/state/State.java

@@ -11,6 +11,7 @@ public abstract class State {
 
 	protected String name;
 
+	//Super class of all the implemented states
 	public State(String name, GameStateManager gsm) {
 		this.name = name;
 		this.gsm = gsm;

+ 3 - 1
src/resources/image/Images.java

@@ -12,9 +12,11 @@ public class Images {
 
 	private static ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();
 
+	
 	public Images() {
 	}
-
+	
+	//Load all images on start
 	static {
 		try {
 			images.add(ImageIO.read(Main.class.getResource("/resources/image/player1_sprite.png")));

+ 2 - 0
src/view/GameView.java

@@ -28,10 +28,12 @@ public class GameView extends JPanel implements ActionListener {
 
 		setBackground(Color.WHITE);
 
+		//Start paint timer
 		t = new Timer(1000 / Window.FRAMES_PER_SECOND, this);
 		t.start();
 	}
 
+	//Paint the game
 	@Override
 	public void paintComponent(Graphics g) {
 		super.paintComponent(g);