player moving now, not drawing shots yet
git-svn-id: svn://anubis/gvsu@100 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
parent
6fce0de00a
commit
419cfec649
@ -6,6 +6,7 @@ import javax.swing.event.*;
|
||||
import javax.swing.border.*;
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class BlobWars extends JFrame
|
||||
{
|
||||
@ -18,10 +19,13 @@ public class BlobWars extends JFrame
|
||||
private javax.swing.Timer m_timer;
|
||||
private Socket m_socket;
|
||||
private BufferedWriter m_writer;
|
||||
private Vector<String> m_serverUpdates;
|
||||
|
||||
public BlobWars()
|
||||
{
|
||||
super("Josh's Blob Wars Game for CS654");
|
||||
|
||||
m_serverUpdates = new Vector<String>();
|
||||
m_world = new BlobWarsWorld();
|
||||
/* TODO: REMOVE */
|
||||
m_world.addPlayer("Alice");
|
||||
@ -91,6 +95,16 @@ public class BlobWars extends JFrame
|
||||
m_panel.requestFocusInWindow();
|
||||
(new Thread(new ClientHandler())).start();
|
||||
m_timer = new javax.swing.Timer(50, m_handler);
|
||||
m_timer.addActionListener(m_handler);
|
||||
m_timer.start();
|
||||
}
|
||||
|
||||
private void doDisconnect()
|
||||
{
|
||||
m_timer.stop();
|
||||
m_connectButton.setEnabled(true);
|
||||
m_nameField.setEnabled(true);
|
||||
m_serverField.setEnabled(true);
|
||||
}
|
||||
|
||||
private class ClientHandler implements Runnable
|
||||
@ -118,7 +132,11 @@ public class BlobWars extends JFrame
|
||||
}
|
||||
if (line == null)
|
||||
break;
|
||||
System.out.println("GOT LINE: " + line);
|
||||
// System.out.println("GOT LINE: " + line);
|
||||
synchronized (m_serverUpdates)
|
||||
{
|
||||
m_serverUpdates.add(line);
|
||||
}
|
||||
}
|
||||
/* TODO: disconnected logic */
|
||||
System.out.println("client disconnected");
|
||||
@ -134,6 +152,11 @@ public class BlobWars extends JFrame
|
||||
{
|
||||
doConnect();
|
||||
}
|
||||
else if (e.getSource() == m_timer)
|
||||
{
|
||||
processUpdates();
|
||||
m_panel.repaint();
|
||||
}
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent e)
|
||||
@ -159,6 +182,10 @@ public class BlobWars extends JFrame
|
||||
m_writer.write("RIGHT\n");
|
||||
m_writer.flush();
|
||||
break;
|
||||
case KeyEvent.VK_SPACE:
|
||||
m_writer.write("SHOOT\n");
|
||||
m_writer.flush();
|
||||
break;
|
||||
}
|
||||
} catch (Exception ex) {}
|
||||
}
|
||||
@ -180,6 +207,32 @@ public class BlobWars extends JFrame
|
||||
}
|
||||
}
|
||||
|
||||
private void processUpdates()
|
||||
{
|
||||
Vector<String> updates = new Vector<String>();
|
||||
synchronized (m_serverUpdates)
|
||||
{
|
||||
updates.addAll(m_serverUpdates);
|
||||
m_serverUpdates.clear();
|
||||
}
|
||||
|
||||
for (String s : updates)
|
||||
{
|
||||
if (s.startsWith("PLAYER:"))
|
||||
{
|
||||
String playerInfo = s.substring(7);
|
||||
// System.out.println("processUpdates: calling updatePlayer(\"" + playerInfo + "\")");
|
||||
m_world.updatePlayer(playerInfo);
|
||||
}
|
||||
else if (s.startsWith("SHOT:"))
|
||||
{
|
||||
String shotInfo = s.substring(5);
|
||||
// System.out.println("processUpdates: calling updateShot(\"" + shotInfo + "\")");
|
||||
m_world.updateShot(shotInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
new BlobWars();
|
||||
|
@ -23,11 +23,52 @@ public class BlobWarsWorld
|
||||
return m_players.containsKey(name);
|
||||
}
|
||||
|
||||
public boolean shotExists(Integer id)
|
||||
{
|
||||
return m_shots.containsKey(id);
|
||||
}
|
||||
|
||||
public Player getPlayer(String name)
|
||||
{
|
||||
return m_players.get(name);
|
||||
}
|
||||
|
||||
public Shot getShot(Integer id)
|
||||
{
|
||||
return m_shots.get(id);
|
||||
}
|
||||
|
||||
public void updatePlayer(String playerInfo)
|
||||
{
|
||||
StringTokenizer st = new StringTokenizer(playerInfo, ":");
|
||||
if (st.hasMoreTokens())
|
||||
{
|
||||
String name = st.nextToken();
|
||||
if (!playerExists(name))
|
||||
addPlayer(name);
|
||||
getPlayer(name).fromString(playerInfo);
|
||||
}
|
||||
}
|
||||
|
||||
public void updateShot(String shotInfo)
|
||||
{
|
||||
StringTokenizer st = new StringTokenizer(shotInfo, ":");
|
||||
if (st.hasMoreTokens())
|
||||
{
|
||||
String idstr = st.nextToken();
|
||||
Integer id = Integer.parseInt(idstr);
|
||||
if (!shotExists(id))
|
||||
addShot(id);
|
||||
getShot(id).fromString(shotInfo);
|
||||
}
|
||||
}
|
||||
|
||||
public void addShot(Integer id)
|
||||
{
|
||||
Shot s = new Shot(id, 0, 0, 0);
|
||||
m_shots.put(id, s);
|
||||
}
|
||||
|
||||
public boolean addPlayer(String name)
|
||||
{
|
||||
if (playerExists(name))
|
||||
@ -77,7 +118,7 @@ public class BlobWarsWorld
|
||||
return;
|
||||
Player p = m_players.get(playerName);
|
||||
p.dx = Math.cos(p.r) * PLAYER_SPEED;
|
||||
p.dy = Math.cos(p.r) * PLAYER_SPEED;
|
||||
p.dy = Math.sin(p.r) * PLAYER_SPEED;
|
||||
}
|
||||
|
||||
public void moveDown(String playerName)
|
||||
@ -87,6 +128,7 @@ public class BlobWarsWorld
|
||||
Player p = m_players.get(playerName);
|
||||
p.dx = 0;
|
||||
p.dy = 0;
|
||||
p.dr = 0;
|
||||
}
|
||||
|
||||
public void moveLeft(String playerName)
|
||||
|
Loading…
x
Reference in New Issue
Block a user