movement, shooting, collisions and health working

git-svn-id: svn://anubis/gvsu@101 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
josh 2008-04-13 22:51:57 +00:00
parent 419cfec649
commit 238f639535
5 changed files with 101 additions and 12 deletions

View File

@ -230,6 +230,16 @@ public class BlobWars extends JFrame
// System.out.println("processUpdates: calling updateShot(\"" + shotInfo + "\")"); // System.out.println("processUpdates: calling updateShot(\"" + shotInfo + "\")");
m_world.updateShot(shotInfo); m_world.updateShot(shotInfo);
} }
else if (s.startsWith("RMPLAYER:"))
{
String playerName = s.substring(9);
m_world.removePlayer(playerName);
}
else if (s.startsWith("RMSHOT:"))
{
Integer shotid = Integer.parseInt(s.substring(7));
m_world.removeShot(shotid);
}
} }
} }

View File

@ -30,7 +30,13 @@ public class BlobWarsPanel extends JPanel
/* draw players */ /* draw players */
for (Player p : m_world.getPlayers().values()) for (Player p : m_world.getPlayers().values())
{ {
drawPlayer(p, g); if (p.health > 0.0)
drawPlayer(p, g);
}
for (Shot s : m_world.getShots().values())
{
drawShot(s, g);
} }
} }
@ -43,7 +49,7 @@ public class BlobWarsPanel extends JPanel
/* draw the blob circle */ /* draw the blob circle */
g.setColor(p.name.equals(m_gui.getPlayerName()) g.setColor(p.name.equals(m_gui.getPlayerName())
? new Color(0.7f, 1.0f, 1.0f) ? new Color(0.8f, 1.0f, 1.0f)
: Color.CYAN); : Color.CYAN);
g.fillOval((int) (xscl - rxscl), g.fillOval((int) (xscl - rxscl),
(int) (getHeight() - (yscl + ryscl)), (int) (getHeight() - (yscl + ryscl)),
@ -105,4 +111,24 @@ public class BlobWarsPanel extends JPanel
? ((int) (yscl - ryscl - height)) ? ((int) (yscl - ryscl - height))
: ((int) (yscl + ryscl)) )); : ((int) (yscl + ryscl)) ));
} }
private void drawShot(Shot s, Graphics g)
{
double xscl = s.x * getWidth();
double yscl = s.y * getHeight();
double rxscl = s.radius * getWidth();
double ryscl = s.radius * getHeight();
/* draw the shot as a circle */
g.setColor(Color.ORANGE);
g.fillOval((int) (xscl - rxscl),
(int) (getHeight() - (yscl + ryscl)),
(int) (2 * rxscl),
(int) (2 * ryscl));
g.setColor(Color.RED);
g.drawOval((int) (xscl - rxscl),
(int) (getHeight() - (yscl + ryscl)),
(int) (2 * rxscl),
(int) (2 * ryscl));
}
} }

View File

@ -144,7 +144,6 @@ public class BlobWarsServer
pushPlayers.put(name, players.get(name)); pushPlayers.put(name, players.get(name));
} }
} }
m_lastPlayers = PlayerClone(players);
/* calculate updated shots */ /* calculate updated shots */
HashMap<Integer, Shot> pushShots = HashMap<Integer, Shot> pushShots =
@ -163,9 +162,8 @@ public class BlobWarsServer
pushShots.put(shotid, shots.get(shotid)); pushShots.put(shotid, shots.get(shotid));
} }
} }
m_lastShots = ShotClone(shots);
/* write updated info to each client */ /* build up the update string */
String sendLine = ""; String sendLine = "";
for (Player p : pushPlayers.values()) for (Player p : pushPlayers.values())
{ {
@ -175,6 +173,32 @@ public class BlobWarsServer
{ {
sendLine += "SHOT:" + s + "\n"; sendLine += "SHOT:" + s + "\n";
} }
/* add remove messages for all players / shots that are gone */
if (m_lastPlayers != null)
{
for (String playerName : m_lastPlayers.keySet())
{
if (!players.containsKey(playerName))
{
/* player playerName has left since last update */
sendLine += "RMPLAYER:" + playerName + "\n";
}
}
}
if (m_lastShots != null)
{
for (Integer id : m_lastShots.keySet())
{
if (!shots.containsKey(id))
{
/* shot has expired since last update */
sendLine += "RMSHOT:" + id + "\n";
}
}
}
/* write updated info to each client */
synchronized (m_socketToPlayerName) synchronized (m_socketToPlayerName)
{ {
for (Socket client : m_socketToPlayerName.keySet()) for (Socket client : m_socketToPlayerName.keySet())
@ -190,6 +214,10 @@ public class BlobWarsServer
} }
} }
} }
/* save the player / shot data into the "last" variables */
m_lastPlayers = PlayerClone(players);
m_lastShots = ShotClone(shots);
} }
} }
} }
@ -227,7 +255,7 @@ public class BlobWarsServer
} }
if (line == null) if (line == null)
break; break;
System.out.println("SERVER GOT LINE: " + line); // System.out.println("SERVER GOT LINE: " + line);
if (line.equals("QUIT")) if (line.equals("QUIT"))
break; break;
ClientUpdate cu = new ClientUpdate(); ClientUpdate cu = new ClientUpdate();

View File

@ -3,14 +3,14 @@ import java.util.*;
public class BlobWarsWorld public class BlobWarsWorld
{ {
public static final double SHOT_SPEED = 0.3; public static final double SHOT_SPEED = 0.4;
public static final double PLAYER_SPEED = 0.2; 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 / 2;
private HashMap<String, Player> m_players; private HashMap<String, Player> m_players;
private HashMap<Integer, Shot> m_shots; private HashMap<Integer, Shot> m_shots;
private long m_lastStepTime; private long m_lastStepTime;
private Integer m_shotID; private int m_shotID;
public BlobWarsWorld() public BlobWarsWorld()
{ {
@ -91,24 +91,33 @@ public class BlobWarsWorld
m_players.remove(name); m_players.remove(name);
} }
public void removeShot(Integer id)
{
m_shots.remove(id);
}
public void shoot(String playerName) public void shoot(String playerName)
{ {
if (!m_players.containsKey(playerName)) if (!m_players.containsKey(playerName))
return; return;
/* TODO: check validity of shot, create Shot object */
Player p = m_players.get(playerName); Player p = m_players.get(playerName);
if (p.health <= 0.0)
return;
long curtime = (new Date()).getTime(); long curtime = (new Date()).getTime();
if ( ((curtime - p.lastShotTime) / 1000.0) if ( ((curtime - p.lastShotTime) / 1000.0)
< Player.SHOT_DELAY) > Player.SHOT_DELAY)
{ {
/* calculate X & Y coordinates of shot */ /* calculate X & Y coordinates of shot */
double x = p.x + Math.cos(p.r) * (p.radius + Shot.DEFAULT_RADIUS); double x = p.x + Math.cos(p.r) *
double y = p.y + Math.sin(p.r) * (p.radius + Shot.DEFAULT_RADIUS); (0.01 + p.radius + Shot.DEFAULT_RADIUS);
double y = p.y + Math.sin(p.r) *
(0.01 + p.radius + Shot.DEFAULT_RADIUS);
m_shotID++; m_shotID++;
Shot s = new Shot(m_shotID, curtime, x, y); Shot s = new Shot(m_shotID, curtime, x, y);
s.dx = Math.cos(p.r) * SHOT_SPEED; s.dx = Math.cos(p.r) * SHOT_SPEED;
s.dy = Math.sin(p.r) * SHOT_SPEED; s.dy = Math.sin(p.r) * SHOT_SPEED;
m_shots.put(m_shotID, s); m_shots.put(m_shotID, s);
p.lastShotTime = curtime;
} }
} }
@ -117,8 +126,11 @@ public class BlobWarsWorld
if (!m_players.containsKey(playerName)) if (!m_players.containsKey(playerName))
return; return;
Player p = m_players.get(playerName); Player p = m_players.get(playerName);
if (p.health <= 0.0)
return;
p.dx = Math.cos(p.r) * PLAYER_SPEED; p.dx = Math.cos(p.r) * PLAYER_SPEED;
p.dy = Math.sin(p.r) * PLAYER_SPEED; p.dy = Math.sin(p.r) * PLAYER_SPEED;
p.dr = 0;
} }
public void moveDown(String playerName) public void moveDown(String playerName)
@ -136,6 +148,8 @@ public class BlobWarsWorld
if (!m_players.containsKey(playerName)) if (!m_players.containsKey(playerName))
return; return;
Player p = m_players.get(playerName); Player p = m_players.get(playerName);
if (p.health <= 0.0)
return;
p.dr += PLAYER_SPIN_SPEED; p.dr += PLAYER_SPIN_SPEED;
if (Math.abs(p.dr) < 0.001) if (Math.abs(p.dr) < 0.001)
p.dr = 0; p.dr = 0;
@ -146,6 +160,8 @@ public class BlobWarsWorld
if (!m_players.containsKey(playerName)) if (!m_players.containsKey(playerName))
return; return;
Player p = m_players.get(playerName); Player p = m_players.get(playerName);
if (p.health <= 0.0)
return;
p.dr -= PLAYER_SPIN_SPEED; p.dr -= PLAYER_SPIN_SPEED;
if (Math.abs(p.dr) < 0.001) if (Math.abs(p.dr) < 0.001)
p.dr = 0; p.dr = 0;
@ -219,6 +235,10 @@ public class BlobWarsWorld
{ {
s.x += s.dx * elapsed; s.x += s.dx * elapsed;
s.y += s.dy * elapsed; s.y += s.dy * elapsed;
if (s.x < 0.0) s.x += 1.0;
if (s.x > 1.0) s.x -= 1.0;
if (s.y < 0.0) s.y += 1.0;
if (s.y > 1.0) s.y -= 1.0;
} }
} }
@ -248,7 +268,11 @@ public class BlobWarsWorld
m_shots.remove(ifound); m_shots.remove(ifound);
} }
if (p.health < 0.0) if (p.health < 0.0)
{
/* player is "dead" */
p.health = 0.0; p.health = 0.0;
p.dx = p.dy = p.dr = 0;
}
} }
} }
m_lastStepTime = curtime; m_lastStepTime = curtime;

View File

@ -24,6 +24,7 @@ public class Player extends GameItem implements Cloneable
this.dr = 0; this.dr = 0;
this.dx = 0; this.dx = 0;
this.dy = 0; this.dy = 0;
this.lastShotTime = 0;
} }
public Player clone() public Player clone()