name exists message, movement updated

git-svn-id: svn://anubis/gvsu@106 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
josh 2008-04-14 00:46:14 +00:00
parent 723c3cc876
commit 47e272d82f
4 changed files with 75 additions and 10 deletions

View File

@ -21,6 +21,7 @@ public class BlobWars extends JFrame
private Socket m_socket;
private BufferedWriter m_writer;
private Vector<String> 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();
}
}
}

View File

@ -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);
}
}

View File

@ -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<String, Player> m_players;
private HashMap<Integer, Shot> 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,8 +178,18 @@ 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;
}

View File

@ -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;