MMORPG programming in Silverlight tutorial (12) map instance (part I)

Source: Internet
Author: User

I introduced how to create map in the game in the previous chapters. The difficulty is the implementation inside the map, such as obstructions.

Let me introduce another method to implement obstructions. We call it map instance, or copy map.

Let's have a look at the picture above. the top and the bottom are in the same size: 800*600. the top picture is the original map, the bottom is the map instance. we find the map instance is made up with some blocks in single cure color. the blocks in black stand for the obstructions in the original map. the white region stands for the area wherever the Sprite can move. the yellow region stands for the transportation that you can move from one point to another one in a long distance. and also you can add red blocks to stand for snare in the game.

The refine in this chapter is, how to pick up the color from the map instance.

private Color PickColor(Image image, int x, int y, double actualWidth){    WriteableBitmap bitmap = new WriteableBitmap(image, null);    int color = bitmap.Pixels[(int)(y * actualWidth) + x];    //convert color integer to byte array    byte[] bytes = BitConverter.GetBytes(color);    //convert byte array to color integer(bytes[3] - A, bytes[2] - R, bytes[1] - G, bytes[0] - B)    return Color.FromArgb(bytes[3], bytes[2], bytes[1], bytes[0]);}
 

In the method pickcolor above, the parameter actualwidth stands for the image's width, so the sentence

(INT) (y * actualwidth) + x

Stands for the accurate point of the image, we get the color of this point in the pixel array. then we convert the color into a byte array. the elements in the array from 0 to 3 stand for argb correspondingly.

 

Now, let's use copy map in our game engine to detect if the distinction is an obstruction. We prepare a new copy map for our demo, as follow:

We init copy map at the beginning.

void InitCopyMap(){    BitmapSource deeper = new BitmapImage((new Uri(@"/Images/Map/Deeper.jpg", UriKind.Relative)));    copyMap = new Image() { Source = deeper, Width = Carrier.Width, Height = Carrier.Height };    Carrier.Children.Add(copyMap);    copyMap.SetValue(Canvas.ZIndexProperty, -100);}

 

In this chapter we use pickcolor method to detect if the distinction is an obstruction, we need to modify the carrier_mouseleftbuttondown method, as follow:

private void Carrier_MouseLeftButtonDown(object sender, MouseButtonEventArgs e){    Point p = e.GetPosition(Carrier);    //check if the distinction is an obstruction    if (PickColor(copyMap, (int)p.X, (int)p.Y, Carrier.ActualWidth) == Colors.Black)    {        MessageBox.Show("The path doesn't exist");        return;    }        ……(ignore some code)
 
    var path = pathFinder.FindPath(start, end);    //if (path == null)    //{    //    MessageBox.Show("The path doesn't exist");    //    return;    //}
 
 
    ……(ignore some code)
}

From the code above, we mark some old code and add some new code, it means we can check if the distinction is an obstruction by copy map, rather than a * algorithm any more. so we can see the same effect as we click on the obstruction, as follow:

In the following chapter, I will use copy map to detect the teleportation, which is marked in yellow in the picture above, the Sprite can move soon from one region to another.

 

Summary: This chapter introduces mask instance to help us detect different regions by picking up the colors.

Next chapter, we will resolve an issue about moving mechanic. 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.