Multiplayer goes through Apple's GameCenter service, and I've got that all set up and working. I am at the point now where:
1. All devices can connect to each other
2. I can send packets to all players (or just specific ones)
3. I can elect a device to act as the "Host" or "Server", where all the logic should go.
But now I'm a bit stuck at just how the architecture of this game should work. Below I'll show a basic pseudo-code outline of how my game works currently:
// Main game loop
void gameLoop () {
// Called around 60 times/sec
// Calculates a delta… assume that's done
makeGameControllerUpdateCurrentSceneWithDelta(delta);
render();
}
// Where the game controller updates the current Scene (aka level)
void makeGameControllerUpdateCurrentSceneWithDelta(delta) {
// input has already been received from the device's accelerometer at this point.
// Assume the scene is not in a paused state and thus should be updated…
// Update every Entity (player) in the scene
updateAllEntitiesWithDelta(delta);
// Do collision detection
updateCollisionDetectionForAllEntitiesWithDelta(delta);
// Check to see if a player has won
updateMatchStateWithDelta(delta);
// Update UI
updateUIWithDelta(delta);
}
So my basic question is, where does my networking code go? Also, how should my networking really work? That is, my understanding is:
1. All players should send data to the host device on every frame (or less, maybe say 20 times per second?).
2. The host device computes the real logic of the game (collision? win conditions? updating entity positions? What?)
3. The host sends the new "truth" to all players in the game
4. All players then update immediately? (OR Should this "from the server" value be stored for the next time they render? I guess those are both the same thing)
Making the clients wait lock-step for the server's truth to come back seems like it would really interrupt the flow of the game (if on a cellular network, for example, it could get bad). So I'm guessing client-side they should do some kind of prediction, but how? Just use the local input unless what comes back from the server tells them otherwise? But won't the "Server truth" be stale by then?
Thanks so much for any help you can offer!