Network-move from lolobby to actual game

Source: Internet
Author: User
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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.