diff --git a/cs654/final-proj/BlobWars.java b/cs654/final-proj/BlobWars.java index 6e9b746..44b3dab 100644 --- a/cs654/final-proj/BlobWars.java +++ b/cs654/final-proj/BlobWars.java @@ -21,6 +21,7 @@ public class BlobWars extends JFrame private Socket m_socket; private BufferedWriter m_writer; private Vector m_serverUpdates; + private long m_statusUpdateTime; public BlobWars() { @@ -117,6 +118,7 @@ public class BlobWars extends JFrame } /* main client loop */ + boolean exitingDueToNameExisting = false; for (;;) { String line = ""; @@ -129,11 +131,24 @@ public class BlobWars extends JFrame if (line == null) break; // System.out.println("GOT LINE: " + line); + if (line.equals("NAMEEXISTS")) + exitingDueToNameExisting = true; synchronized (m_serverUpdates) { m_serverUpdates.add(line); } } + if (exitingDueToNameExisting) + { + SwingUtilities.invokeLater(new Runnable() { + public void run() + { + m_statusLabel.setText("Player '" + + m_nameField.getText() + + "' already exists!"); + } + }); + } SwingUtilities.invokeLater(new Runnable() { public void run() { @@ -161,6 +176,9 @@ public class BlobWars extends JFrame * since the last time step if we have * not heard anything */ processUpdates(); + if (((new Date()).getTime() - m_statusUpdateTime) > 5000) + if (!m_statusLabel.getText().equals("")) + m_statusLabel.setText(""); m_panel.repaint(); } } @@ -206,6 +224,22 @@ public class BlobWars extends JFrame public void keyReleased(KeyEvent e) { + if (e.getSource() == m_panel) + { + try { + switch (e.getKeyCode()) + { + case KeyEvent.VK_LEFT: + m_writer.write("NOSPIN\n"); + m_writer.flush(); + break; + case KeyEvent.VK_RIGHT: + m_writer.write("NOSPIN\n"); + m_writer.flush(); + break; + } + } catch (Exception ex) {} + } } public void keyTyped(KeyEvent e) @@ -250,6 +284,11 @@ public class BlobWars extends JFrame { String winner = s.substring(7); m_statusLabel.setText(winner + " won the match!"); + m_statusUpdateTime = (new Date()).getTime(); + } + else if (s.equals("NAMEEXISTS")) + { + doDisconnect(); } } } diff --git a/cs654/final-proj/BlobWarsServer.java b/cs654/final-proj/BlobWarsServer.java index b036d13..acca40e 100644 --- a/cs654/final-proj/BlobWarsServer.java +++ b/cs654/final-proj/BlobWarsServer.java @@ -81,6 +81,20 @@ public class BlobWarsServer { if (arr.length < 2) return; + if (m_world.getPlayers().containsKey(arr[1])) + { + /* this player name is already registered */ + try { + BufferedWriter bw = new BufferedWriter( + new OutputStreamWriter( + cu.socket.getOutputStream())); + String sendLine = "NAMEEXISTS\n"; + bw.write(sendLine, 0, sendLine.length()); + bw.flush(); + cu.socket.close(); + } catch (Exception e) {} + return; + } System.out.println("Player '" + arr[1] + "' signed on."); m_world.addPlayer(arr[1]); m_socketToPlayerName.put(cu.socket, arr[1]); @@ -102,6 +116,8 @@ public class BlobWarsServer m_world.moveLeft(playerName); } else if (arr[0].equals("RIGHT")) { m_world.moveRight(playerName); + } else if (arr[0].equals("NOSPIN")) { + m_world.stopSpinning(playerName); } } diff --git a/cs654/final-proj/BlobWarsWorld.java b/cs654/final-proj/BlobWarsWorld.java index eb2323b..2920b5e 100644 --- a/cs654/final-proj/BlobWarsWorld.java +++ b/cs654/final-proj/BlobWarsWorld.java @@ -5,7 +5,7 @@ public class BlobWarsWorld { public static final double SHOT_SPEED = 0.4; public static final double PLAYER_SPEED = 0.2; - public static final double PLAYER_SPIN_SPEED = Math.PI / 2; + public static final double PLAYER_SPIN_SPEED = Math.PI * 0.6; private HashMap m_players; private HashMap m_shots; @@ -32,6 +32,7 @@ public class BlobWarsWorld p.dx = 0; p.dy = 0; } + m_shots.clear(); } public boolean playerExists(String name) @@ -146,7 +147,6 @@ public class BlobWarsWorld return; p.dx = Math.cos(p.r) * PLAYER_SPEED; p.dy = Math.sin(p.r) * PLAYER_SPEED; - p.dr = 0; } public void moveDown(String playerName) @@ -154,9 +154,9 @@ public class BlobWarsWorld if (!m_players.containsKey(playerName)) return; Player p = m_players.get(playerName); - p.dx = 0; - p.dy = 0; - p.dr = 0; +// p.dx -= Math.cos(p.r) * PLAYER_SPEED; +// p.dy -= Math.sin(p.r) * PLAYER_SPEED; + p.dx = p.dy = p.dr = 0; } public void moveLeft(String playerName) @@ -166,8 +166,8 @@ public class BlobWarsWorld Player p = m_players.get(playerName); if (p.health <= 0.0) return; - p.dr += PLAYER_SPIN_SPEED; - if (Math.abs(p.dr) < 0.001) + p.dr = PLAYER_SPIN_SPEED; + if (Math.abs(p.dr) < 0.01) p.dr = 0; } @@ -178,11 +178,21 @@ public class BlobWarsWorld Player p = m_players.get(playerName); if (p.health <= 0.0) return; - p.dr -= PLAYER_SPIN_SPEED; - if (Math.abs(p.dr) < 0.001) + p.dr = -PLAYER_SPIN_SPEED; + if (Math.abs(p.dr) < 0.01) p.dr = 0; } + public void stopSpinning(String playerName) + { + if (!m_players.containsKey(playerName)) + return; + Player p = m_players.get(playerName); + if (p.health <= 0.0) + return; + p.dr = 0; + } + public Vector collidesWith(GameItem gi) { Vector collideList = new Vector(); diff --git a/cs654/final-proj/Player.java b/cs654/final-proj/Player.java index 85d2e38..4d905c9 100644 --- a/cs654/final-proj/Player.java +++ b/cs654/final-proj/Player.java @@ -5,7 +5,7 @@ public class Player extends GameItem implements Cloneable { public static final double DEFAULT_RADIUS = 0.06; public static final double COLLIDE_DAMAGE = 0.002; - public static final double SHOT_DELAY = 4.0; + public static final double SHOT_DELAY = 3.0; public String name; public double health;