XNa 3.0 preliminary -- the emergence of gameservices

Source: Internet
Author: User
1.8 gameservices implementation

As explained in tutorial 1-6CodeIn reusable gamecomponent class, these components can be cameras, particle systems, user input, billboard, etc.

One of the main advantages of using the gamecomponent class is that you can easily switch between them, such as the camera mode. To change the first-person Camera mode to the four-element Camera mode, you only need to include a code in the initialize method of the game class.

However, you must ensure that the remaining code will not be changed when switching components (the Code must use the camera ).

Solution

You can make the camera component have the same interface, such as the (custom) icamerainterface interface. When initializing the camera component, you need to let the game class know that from now on, the game should contain a component that implements the icamerainterface interface. In xNa, the component registers itself as an icamerainterface-type gameservice.

Once the previous step is completed, the other part of the Code simply requests the current icamerainterface service from the game class, and the game class returns the camera that provides the current icamerainterface service. This means that your code does not need to know whether the current Camera mode is the first-person Camera mode or the four-element Camera mode.

Working Principle

Interface is used to defineProgramAn Interface contains a list of functions (that is, methods). When your class implements an interface, you must implement the methods defined in the interface.

The following code defines an icamerainterface interface:

Interface icamerainterface {vector3 position {Get;} vector3 forward {Get;} vector3 upvector {Get;} matrix viewmatrix {Get;} matrix projectionmatrix {Get ;}}

All classes that implement the icamerainterface interface must implement these five getter methods. Whether it is a first-person camera or a four-element camera, as long as this interface is implemented, the main program will be able to access these five fields, for the rest of the code, it is not important to know whether the current camera is the first-person mode or the Quaternary model. the only important thing is that the camera can generate a view and projection matrix, and there may be some other direction vectors. Therefore, you should implement a camera with icamerainterface for the game class.

Implement gamecomponent Interface

In this example, there are two camera components. Because you do not draw things on the screen, you do not need to use drawablegamecomponent. Therefore, the camera components are inherited from the gamecomponent class. You also need to implement the interface on the component:

 
Class quakecameragc: gamecomponent, icamerainterface {...}

Note:Although a class can only inherit from one base class (in this example, it is a gamecomponent class), it can implement multiple interfaces.

In the next step, you must determine the method defined in the Implementation interface of the class. The quakecamera class and quaternion class have been implemented in the tutorial 2-3 and 2-4.

Register icamerainterface Service

You must have one camera class at a time to provide the icamerainterface service. When activating a camera component, you must let the game class know that the camera component is the current icamerainterface so that the game class can return the camera when other code requests the provider of the current icamerainterface.

You can perform this step by registering this camera as a gameservice of the game services set:

 
Public quakecameragc (Game): Base (game) {game. Services. addservice (typeof (icamerainterface), this );}

You add this object (the newly created first-person camera component) to the interface set and provide the icamerainterface service.

Usage

When you want to use a camera (such as obtaining the view and projection matrices), you must require the game class to return the current icamerainterface. In the returned object, you can obtain all fields defined in icamerainterface.

 
Protected override void draw (gametime) {Device. clear (clearoptions. target | clearoptions. depthbuffer, color. cornflowerblue, 1, 0); icamerainterface camera; camera = (icamerainterface) services. getservice (typeof (icamerainterface); ccross. draw (camera. viewmatrix, camera. projectionmatrix); base. draw (gametime );}

The code in the draw method does not know whether it is a first-person camera or a quaternary camera. It only knows the method defined in icamerainterface. The advantage of this approach is that you can easily switch the Camera mode, because other code does not know this change.

Note:In the initialize method of the game class, you also define a camera as a global variable to store the link of the current icamerainterface.

Use multiple gamecomponents

Gameservices is useful for ensuring the interoperability of different gamecomponent classes. In this example, you have a camera component that provides the icamerainterface service. Other component classes can obtain this camera by querying the icamerainterface service.

This means that you do not need to provide raw links between different components, such as the ugly update method in the previous tutorial. In this example, you create a camera component that provides the icamerainterface service to access this service from any other component, for example, from the initialize method of the billboard component:

 
Public override void initialize () {Device = game. graphicsdevice; camera = (icamerainterface) game. Services. getservice (typeof (icamerainterface); base. initialize ();}

Next, the update and draw methods of the billboard component can access the camera fields.

The game class needs to initialize the camera and billboard components. The camera component subscribes to the icamerainterface service and allows the billboard component to access the camera.

The camera is automatically updated, and the billboard component is also automatically updated. The billboard component can access the camera through the icamerainterface service and then draw its own. The entire process requires two lines of code added to the game class. You can easily switch between the two modes of the camera.

Change the update sequence of gamecomponent

In this example, your billboard component requires the output of the camera component, so you should ensure that the camera is updated before the billboard. You can perform this step before adding components to the components set in the game class:

 
Public game1 () {graphics = new graphicsdevicemanager (this); content. rootdirectory = "content"; // gamecomponent camcomponent = new quakecameragc (this); gamecomponent camcomponent = new quatcamgc (this); gamecomponent billcomponent = new billboardgc (this); camcomponent. updateorder = 0; billcomponent. updateorder = 1; components. add (camcomponent); components. add (billcomponent );}

First, you have created the camera and billboard components. Before adding them to the components set, you have set the update sequence for them. A small number indicates that they are updated first.

To use gamecomponent and gameservices to switch camera mode, you only need to change a line of code in the program. In the previous Code, quaternion mode is active. To switch to the quake mode, you only need to remove the comments before the quakecamera gamecomponent class and add comments before the quatcam gamecomponent class. The remaining Code does not need to be modified, because only the icamerainterface service provided by the camera component is required.

Code

The code of the game1 constructor shown above is to get all the code for the camera and make the billboard work. The initialize, (UN) loadcontent, update, and draw methods are empty. After the camera component is created, it registers itself to the icamerainterface service:

 
Public quakecameragc (Game): Base (game) {game. Services. addservice (typeof (icamerainterface), this );}

Whenever other Code requires a camera, you only need to request the game class to return the icamerainterface service:

 
Icamerainterface camera; camera = (icamerainterface) services. getservice (typeof (icamerainterface); ccross. Draw (camera. viewmatrix, camera. projectionmatrix );

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.