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

Source: Internet
Author: User

In order to help you learn and master various common design patterns more systematically, the following uses a comprehensive example:"Multiplayer online shooting gamesTo learn how to use the design pattern comprehensively in actual development.

 

Multiplayer online shooting games, such as counter-strike, Cs, Delta troops, and battlefield, are widely welcomed by gamers. In the design of multiplayer online shooting games, multiple design modes can be used. Below I will select some common design patterns for analysis:

 

(1) Abstract Factory Model

Multiple game scenarios are provided in online shooting games. Different game scenarios provide different maps, different background music, and different weather conditions. Therefore, abstract factory models can be used for design, class 1:

Figure 1 Abstract Factory mode instance class diagram

In Figure 1, scenefactory acts as an abstract factory, and its subclass sceneafactory acts as a specific factory. You can create a specific map, music, and weather) to add new scenarios, you only need to add new factory categories for specific scenarios.

 

(2) builder Mode

In online shooting games, each game character role must provide a complete role model, including character modeling, clothing, and weapons. You can use the builder mode to create a complete game role, class 2:

Figure 2 builder mode instance class diagram

In Figure 2, playercreatorctor ctor acts as the conductor. playerbuilder is an abstract builder. Its sub-classes playerbuildera and playerbuilderb are specific builders used to create different game roles. player is the complete product created, it is a complete game role, which contains components such as body, costume, and weapon.

 

(3) Factory method mode

In the shooting game, the appearance, usage, and lethal effects of different weapons (such as AK47 assault rifle, sniper rifle, and pistol) are different. Players can use different weapons, in addition, new weapons can be added during game upgrades, without having to make too many modifications to the existing system. You can use the factory method mode to design the weapon system, as shown in Figure 3:

Figure 3 instance class diagram of factory method mode

In Figure 3, The weaponfactory interface represents the abstract weapon factory, whose subclass ak47gunfactory produces ak47gun, sniperriflefactory produces sniperrile, and the display (), use (), and fire () of different weapons () and other methods have different implementations.

 

(4) iterator Mode

In a shooting game, a player can possess a variety of weapons, such as an AK 47, a pistol, and a dagger. Therefore, the system needs to define a ammunition Library (a collection of weapons ), during the game, you can traverse the magazine library and select the appropriate weapon. You can use the iterator mode when traversing the ammunition library, as shown in figure 4:

Figure 4 iterator mode instance class diagram

Magazine-like code snippets are as follows:

public class Magazine {private ArrayList weapons;private Iterator iterator;public Magazine() {weapons = new ArrayList();iterator = weapons.iterator();}public void display() {while(iterator.hasNext()) {((Weapon)iterator.next()).display();}}......}

In addition to traversing the ammunition library, the iterator mode can also be used to traverse aggregate objects such as team allies.

 

(5) command mode

In a shooting game, you can customize the shortcut keys and set them according to your usage habits. For example, you can set the "w" Key to the "shot" shortcut key, you can also set the shortcut key to "Forward". You can set the shortcut key in command mode, as shown in Figure 5:

Figure 5 command mode instance class diagram

In Figure 5, the shortcutkey acts as the request caller. In its press () method, it will determine which button the user is pressing and then call the execute () method of the command object, in the execute () method of a specific command object, the action () method of the receiver, such as shothandler and goaheadhandler, is called to execute specific operations. During implementation, you can store the specific command class name and keyboard key code in the configuration file. The configuration file format is as follows:

……<FunctionMapping keycode="87" commandClass="ShotCommand"/><FunctionMapping keycode="38" commandClass="GoAheadCommand"/>……

To change the shortcut key, you only need to modify the ing between the key code and the specific command class. To add a new function in the upgraded version of the game, you only need to add a new command class, you can modify the configuration file to set the corresponding buttons. The original class library code does not need to be modified, so it complies with the open and closed principles.

[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.