Application of Design Mode in multiplayer online shooting games (2)

Source: Internet
Author: User

(6) Observer Mode

Online shooting games can display the survival information of teammates and enemies in real time. If a teammate or enemy is killed, all online game players will receive the corresponding message, A central role control class (centercontroller) can be provided to implement message transmission. In the central role controller, a set is defined to store all player information. If a player) call the centercontroller notification method notifyplayers (). This method traverses the user information set and calls the display () method of each player to display the information about the killed event, the message displayed when a teammate is killed and an enemy is killed is different. When the policyplayers () method is used to notify other users, the role object will be deleted from the user information collection. You can use the observer mode to send one-to-multiple messages, as shown in figure 6:

Figure 6 observer mode instance class diagram

In Figure 6, centercontroller acts as the observation target, observer acts as the abstract observer, and player acts as the specific observer. In the player class, the name attribute indicates the role name, And the type attribute indicates the role type, such as "team a" or "Team B. When the die () method of player is executed, the notifyplayers () method of centercontroller is called, and the prompt method of other player objects is called in the notifyplayers () method, if a teammate is killed, displayteam () is called. If an enemy is killed, displayenemy () is called. The detach () method is called to delete the killed player object. The policyplayers () of the centercontroller class is called () the code snippet of the method is as follows:

For (Object PLAYER: players) {If (player. getname (). equals (name) {This. detach (player); // Delete the role to be killed} else {If (player. getType (). equals (type) {player. displayteam (name); // a prompt message is displayed for teammates.} else {player. displayenemy (name); // displayed enemy information }}}

 

(7) Singleton Mode

To save system resources, you can use the singleton mode in online shooting games to implement some managers, such as scenemanager and soundmanager, 7. Scenario manager scenemanager class:

Figure 7 Singleton mode instance class diagram

The implementation code snippet of the scenemanager class is as follows [NOTE: The following code does not consider multi-threaded access ]:

Class scenemanager {Private Static scenemanager smanager = NULL; private scenemanager () {// initialization code} public synchronized static scenemanager getinstance () {If (smanager = NULL) {smanager = new scenemanager ();} return smanager;} public void manage () {// business method }}

(8) status Mode

In a shooting game, a game role has several different states, such as normal, paused, and killed. In different states, the behavior of the role object is different, you can use the status mode to design and implement role state conversion, as shown in Figure 8:

Figure 8 Status mode instance class diagram

In figure 8, the game role player acts as the Environment class, and the State acts as the abstract state class. Its subclasses normalstate, pausestate, and deathstate act as the specific state class, and pause () in the specific state class () status conversion can be implemented in methods such as start () and beattacked (). The code snippets of the normalstate class are as follows:

Class normalstate extends State {public void pause () // pause the game {// pause the code to omit the player. setstate (New pausestate (this); // Changes to paused state} public void start () // game start {// The game program is running, this method is unavailable} public void beattacked () // attacked {// other Code omitted if (lifevalue <= 0) {player. setstate (New deathstate (this); // convert to dead state} public void shot () // shot {// Code omitted} public void move () // move {// Code omitted }}

(9) Adapter Mode

To increase the flexibility of the game, some shooting games can also be operated through the game handle. The operation programs and drivers of the game handle are provided by the game handle manufacturer, to make the current shooting game compatible with the game handle, you can use the adapter mode for design, as shown in Figure 9:

Figure 9 adapter mode instance class diagram

In Figure 9, The gamepadsadapter acts as an adapter that adapts the gamepadskey method in the gamepadskey to an existing system and moves () in the method, you can call the handle () method of the movekey class. In the shot () method, you can call the handle () method of shotkey to control game running through the handle.

 

[Author: Liu Wei http://blog.csdn.net/lovelion]

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.