MMORPG programming in Silverlight tutorial (11) map mask

Source: Internet
Author: User

I introduce map presentation in the previous chapters. in general, it is enough to simple games; but we need to spend more energy on the map to simulate real world and achieve more realistic effect. this chapter will focus on how to implement map mask.

First, let's have a look at what a complete map shoshould include.

From the picture above, we can see that the bottom picture is the original map, shown in the game. we have discussed it before. now let's put our attention on the top picture. it is a tree, when the sprite moves behind the tree, he will be masked by the tree, so we called it "Mask ".

We cut this tree from the original picture, make the image's background transparent. and last, we add all these picture in the canvas, the sequence from top to bottom is: mask, Sprite, map. make sure all the masks are put in their proper position where they stay before in their original picture.

The principle sound reasonable. Let's implement it in code now.

In our demo, we use this picture as our map:

Obviusly, there are 3 obstructions, I mark them in green grid. and there are two trees in the map, they are both mask, I cut them from the map, and named them mask1.png and mask2.png, as follows:

 

OK, let's modify the code base on the demo in chapter 10.

1st, set up 3 obstructions in the method initmatrix.

 For ( Int Y = 22; y <= 24; y ++ ){ For ( Int X = 5; x <= 16; X ++ ){ // The obstacle is represented by 0 in the matrix. Matrix [x, y] = 0; rect = New  Rectangle () {Fill = New  Solidcolorbrush ( Colors . Green), opacity = 0.3, stroke = New Solidcolorbrush ( Colors . Gray), width = gridsize, Height = gridsize}; carrier. Children. Add (rect ); Canvas . Setleft (rect, x * gridsize ); Canvas . Settop (rect, y * gridsize );}} For ( Int Y = 11; y <= 14; y ++ ){ For ( Int X = 27; x <= 31; X ++) {matrix [x, y] = 0; rect = New  Rectangle () {Fill =New  Solidcolorbrush ( Colors . Green), opacity = 0.3, stroke = New  Solidcolorbrush ( Colors . Gray), width = gridsize, Height = gridsize}; carrier. Children. Add (rect ); Canvas . Setleft (rect, x * gridsize ); Canvas . Settop (rect, y * gridsize );}} For ( Int Y = 18; y <= 21; y ++ ){For ( Int X = 33; x <= 37; X ++) {matrix [x, y] = 0; rect = New  Rectangle () {Fill = New  Solidcolorbrush ( Colors . Green), opacity = 0.3, stroke = New  Solidcolorbrush ( Colors . Gray), width = gridsize, Height = gridsize}; carrier. Children. Add (rect ); Canvas . Setleft (rect, x * gridsize ); Canvas . Settop (rect, y * gridsize );}}

2nd, set up two mask.

 // Create mask  Image Mask1 = New  Image (); Image Mask2 = New  Image (); Private void Initmask () {mask1.width = 238; mask1.height = 244; mask1.source = New  Bitmapimage ((New  Uri ( @ "Map/mask1.png" , Urikind . Relative); mask1.opacity = 0.7; carrier. Children. Add (mask1 ); Canvas . Setzindex (mask1, 10000 ); Canvas . Setleft (mask1, 185 ); Canvas . Settop (mask1, 220); mask2.width = 198; mask2.height = 221; mask2.source = New  Bitmapimage (( New  Uri (@ "Map/mask2.png" , Urikind . Relative); mask2.opacity = 0.7; carrier. Children. Add (mask2 ); Canvas . Setzindex (mask2, 10000 ); Canvas . Setleft (mask2, 466 ); Canvas . Settop (mask2, 11 );}

From the code above, I set the mask1's zindex property to 10000 in the canvas, because the map size is less than 10000 in width and height, the mask will always in front of the sprite.

You can also find I set the mask1.opacity to 0.7, so if the sprite move behind the tree, he won't be hidden by the tree completely, we can still see him, because it's easy to control the sprite, although the sprite image is not so clear, more or less.

OK, press Ctrl + F5, and move the sprite behind the tree, we can see that the sprite was "hidden" from our view.

But, when we move the Sprite in front of the tree, we find a strange appearance, the Sprite is still be hidden by the tree. so we must modify our code to fix this issue. we need to set sprite's zindex property as the sprite moves in Y-coordinate:

 
DoubleY =Canvas. Gettop (sprite );Canvas. Setzindex (sprite ,(Int) Y );

And we also need to set the mask's zindex property to its actual value, rather than 10000.

 
DoubleY =Canvas. Gettop (mask1 );Canvas. Setzindex (mask1 ,(Int) Y );

But it is not the right time to do this modification, I will leave this issue until I set up a Sprite control, I will set up a dependencyproperty in it and implement this dependency between the top and the zindex properties.

BTW, in the game domain, we call this technique that hidden-appear in multiple layers as painter algorithm.

 

Summary: This chapter introduces map mask and part of the painter algorithm.

Next chapter, we will focus on another map type, instance, which can help us to detect the region by picking up the colors. Please focus on it.

Chinese friend, You can also visit this Chinese blog if you feel difficult to read English, http://www.cnblogs.com/alamiye010/archive/2009/06/17/1505346.html, part of my article is base on it.

Demo download: http://silverlightrpg.codeplex.com/releases/view/40978

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.