From 238f639535575e8476707ed4e2f8bf640de059a6 Mon Sep 17 00:00:00 2001 From: josh Date: Sun, 13 Apr 2008 22:51:57 +0000 Subject: [PATCH] movement, shooting, collisions and health working git-svn-id: svn://anubis/gvsu@101 45c1a28c-8058-47b2-ae61-ca45b979098e --- cs654/final-proj/BlobWars.java | 10 ++++++++ cs654/final-proj/BlobWarsPanel.java | 30 +++++++++++++++++++++-- cs654/final-proj/BlobWarsServer.java | 36 ++++++++++++++++++++++++---- cs654/final-proj/BlobWarsWorld.java | 36 +++++++++++++++++++++++----- cs654/final-proj/Player.java | 1 + 5 files changed, 101 insertions(+), 12 deletions(-) diff --git a/cs654/final-proj/BlobWars.java b/cs654/final-proj/BlobWars.java index 9df1e94..5ff9184 100644 --- a/cs654/final-proj/BlobWars.java +++ b/cs654/final-proj/BlobWars.java @@ -230,6 +230,16 @@ public class BlobWars extends JFrame // System.out.println("processUpdates: calling 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); + } } } diff --git a/cs654/final-proj/BlobWarsPanel.java b/cs654/final-proj/BlobWarsPanel.java index 7e80487..eb2606b 100644 --- a/cs654/final-proj/BlobWarsPanel.java +++ b/cs654/final-proj/BlobWarsPanel.java @@ -30,7 +30,13 @@ public class BlobWarsPanel extends JPanel /* draw players */ 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 */ 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); g.fillOval((int) (xscl - rxscl), (int) (getHeight() - (yscl + ryscl)), @@ -105,4 +111,24 @@ public class BlobWarsPanel extends JPanel ? ((int) (yscl - ryscl - height)) : ((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)); + } } diff --git a/cs654/final-proj/BlobWarsServer.java b/cs654/final-proj/BlobWarsServer.java index 4ba830a..e43590b 100644 --- a/cs654/final-proj/BlobWarsServer.java +++ b/cs654/final-proj/BlobWarsServer.java @@ -144,7 +144,6 @@ public class BlobWarsServer pushPlayers.put(name, players.get(name)); } } - m_lastPlayers = PlayerClone(players); /* calculate updated shots */ HashMap pushShots = @@ -163,9 +162,8 @@ public class BlobWarsServer pushShots.put(shotid, shots.get(shotid)); } } - m_lastShots = ShotClone(shots); - /* write updated info to each client */ + /* build up the update string */ String sendLine = ""; for (Player p : pushPlayers.values()) { @@ -175,6 +173,32 @@ public class BlobWarsServer { 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) { 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) break; - System.out.println("SERVER GOT LINE: " + line); +// System.out.println("SERVER GOT LINE: " + line); if (line.equals("QUIT")) break; ClientUpdate cu = new ClientUpdate(); diff --git a/cs654/final-proj/BlobWarsWorld.java b/cs654/final-proj/BlobWarsWorld.java index fa143e7..8e25ace 100644 --- a/cs654/final-proj/BlobWarsWorld.java +++ b/cs654/final-proj/BlobWarsWorld.java @@ -3,14 +3,14 @@ import java.util.*; 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_SPIN_SPEED = Math.PI / 2; private HashMap m_players; private HashMap m_shots; private long m_lastStepTime; - private Integer m_shotID; + private int m_shotID; public BlobWarsWorld() { @@ -91,24 +91,33 @@ public class BlobWarsWorld m_players.remove(name); } + public void removeShot(Integer id) + { + m_shots.remove(id); + } + public void shoot(String playerName) { if (!m_players.containsKey(playerName)) return; - /* TODO: check validity of shot, create Shot object */ Player p = m_players.get(playerName); + if (p.health <= 0.0) + return; long curtime = (new Date()).getTime(); if ( ((curtime - p.lastShotTime) / 1000.0) - < Player.SHOT_DELAY) + > Player.SHOT_DELAY) { /* calculate X & Y coordinates of shot */ - double x = p.x + Math.cos(p.r) * (p.radius + Shot.DEFAULT_RADIUS); - double y = p.y + Math.sin(p.r) * (p.radius + Shot.DEFAULT_RADIUS); + double x = p.x + Math.cos(p.r) * + (0.01 + p.radius + Shot.DEFAULT_RADIUS); + double y = p.y + Math.sin(p.r) * + (0.01 + p.radius + Shot.DEFAULT_RADIUS); m_shotID++; Shot s = new Shot(m_shotID, curtime, x, y); s.dx = Math.cos(p.r) * SHOT_SPEED; s.dy = Math.sin(p.r) * SHOT_SPEED; m_shots.put(m_shotID, s); + p.lastShotTime = curtime; } } @@ -117,8 +126,11 @@ public class BlobWarsWorld if (!m_players.containsKey(playerName)) return; Player p = m_players.get(playerName); + if (p.health <= 0.0) + 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) @@ -136,6 +148,8 @@ public class BlobWarsWorld if (!m_players.containsKey(playerName)) return; 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 = 0; @@ -146,6 +160,8 @@ public class BlobWarsWorld if (!m_players.containsKey(playerName)) return; 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 = 0; @@ -219,6 +235,10 @@ public class BlobWarsWorld { s.x += s.dx * 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); } if (p.health < 0.0) + { + /* player is "dead" */ p.health = 0.0; + p.dx = p.dy = p.dr = 0; + } } } m_lastStepTime = curtime; diff --git a/cs654/final-proj/Player.java b/cs654/final-proj/Player.java index 4ff3a64..85d2e38 100644 --- a/cs654/final-proj/Player.java +++ b/cs654/final-proj/Player.java @@ -24,6 +24,7 @@ public class Player extends GameItem implements Cloneable this.dr = 0; this.dx = 0; this.dy = 0; + this.lastShotTime = 0; } public Player clone()