Problem
After a session is created, you want to give all players time to gather, chat, and instruct them to prepare for the game.
Solution
XNa has basic lolobby functions in the session Status and player's isready attributes.
The session starts in the lolobby status. Only the host can call the networksession. startgame method. This method moves the session to the playing state. The host can be determined based on the isready status of all players.
Working Principle
After a session is created, the sessionstate attribute of the session will have the lolobby value. In this state, you need to allow all players in this session to indicate that they are ready. This can be done by setting their isready to true. This value can be read by all players in the session:
Case gamestate. insession:... {Switch (networksession. sessionstate)... {Case networksessionstate. loby:... {If (keybstate! = Lastkeybstate)... {If (keybstate. iskeydown (Keys. R) {localnetworkgamer localgamer = networksession. localgamers [0]; localgamer. isready =! Localgamer. isready ;}} break; Case networksessionstate. Playing:... {} break;} networksession. Update ();}
The host needs to check whether all players set isready to true. The simplest way is to check the value of networksess ion. iseveryoneready. If everyone is ready, the host will call the networksession. startgame method, which moves the status from loby to playing:
Case networksessionstate. lolobby:... {If (keybstate! = Lastkeybstate)... {If (keybstate. iskeydown (Keys. R)... {localnetworkgamer localgamer = networksession. localgamers [0]; localgamer. isready =! Localgamer. isready ;}} if (networksession. ishost )... {If (networksession. allgamers. count> 1 )... {If (networksession. iseveryoneready )... {networksession. startgame (); log. add ("all players ready -- start the game! ") ;}}} Break;
When the status changes to playing, the session will trigger the gamestarted event, which will be monitored by all players. This allows them to draw game screens and start sending and receiving game data.
When the host decides to end the game, it needs to call the networksession. Endgame method, which resets isready of all players to false and returns the lolobby status.
Case networksessionstate. Playing:... {If (networksession. ishost)... {If (keybstate! = Lastkeybstate)... {If (keybstate. iskeydown (Keys. E)... {networksession. Endgame () ;}}} break;
This will trigger the gameended event of the session. All players can listen to this event so that they can, for example, draw a loby image.
Code
The insession status contains most of the basic code, allowing multiple players to represent their isready and move the host from the loby status to the playing status or return:
Case gamestate. insession:... {Switch (networksession. sessionstate)... {Case networksessionstate. loby:... {If (keybstate! = Lastkeybstate)... {If (keybstate. iskeydown (Keys. R)... {localnetworkgamer localgamer = networksession. localgamers [0]; localgamer. isready =! Localgamer. isready ;}} if (networksession. ishost )... {If (networksession. allgamers. count> 1 )... {If (networksession. iseveryoneready )... {networksession. startgame (); log. add ("all players ready -- start the game! ") ;}}} Break; Case networksessionstate. Playing:... {If (networksession. ishost)... {If (keybstate! = Lastkeybstate )... {If (keybstate. iskeydown (keys. e ))... {networksession. endgame () ;}}} break;} networksession. update ();} break;