
COMMUNICATING IN (NEAR) REAL TIME 169
Continued from opposite page.
// if the last player's on the bottom, add to the top:
else if (lastPlayerAdded.paddleV == nextBottomPaddleV) {
nextTopPaddleV = nextTopPaddleV + paddleHeight * 2;
v = nextTopPaddleV;
}
} else { // if there are no players, add to the top:
v = nextTopPaddleV;
}
// make a new Player object with the position you just calculated
// and using the Client that generated the serverEvent:
Player newPlayer = new Player(h, v, thisClient);
// add the new Player to the playerList:
playerList.add(newPlayer);
// Announce the new Player:
print("We have a new player: ");
println(newPlayer.client.ip());
newPlayer.client.write("hi\r\n");
}
void listenToClients() {
// get the next client that sends a message:
Client speakingClient = myServer.available();
Player speakingPlayer = null;
// iterate over the playerList to figure out whose
// client sent the message:
for (int p = 0; p < playerList.size(); p++) {
// get the next object in the ArrayList, convert it to a Player:
Player thisPlayer = (Player)playerList.get(p);
// compare the client of thisPlayer to the client that sent a message;
// if they're the same, then this is the Player we want:
if (thisPlayer.client == speakingClient) {
speakingPlayer = thisPlayer;
}
}
// read what the client sent:
if (speakingPlayer !