C # development Wpf/silverlight animation and game series Tutorials (Game Course): (16) Traction Map Movement mode ②
Wizard controls make game development even better! With it, from the perfect traction map Mobile mode is a step away. There is only one last link left, everybody refuels.
In the previous section, (C # development Wpf/silverlight animation and game series Tutorials (Game Course): (15) Wizard control turned out! ②) I set the Canvas.setleft (Spirit, Mapleft + spirit.x) in the interface thread, and Canvas.settop (Spirit, Maptop + spirit.y) to achieve the protagonist following the map movement. From this formula we can analyze two factors that affect the position of the protagonist in the window: the first position for the map image (image map) relative to the window (Canvas.getleft (map), canvas.gettop (map)), It is in the mouse traction map to move constantly change, and the protagonist on the map of the movement has nothing to do, the second is the protagonist's own x,y coordinate attributes (SPIRIT.X,SPIRIT.Y), when the protagonist walking around the map, it is always changed. This can be a conclusion: to achieve the protagonist in this mode map of the movement, only need to update its coordinates spirit.x and spirit.y when it is walking, so the interface thread will update the protagonist in the window position and achieve the perfect game animation interface.
Find the entry point, then the implementation is much simpler.
Here, we first need to make some improvements to the * Seek method in the previous chapters. In the previous section, because the map is fixed dead, and the size of the window size, so we simply see the map and the window as a whole. Therefore, in the * Seek process (Astarmove ()) in the same time to achieve the movement of the protagonist relative to the map, that is, based on the object dependency properties of PropertyPath ("Canvas.Left"), PropertyPath ("Canvas.Top") The storyboard animation. However, this cannot be done in the traction Map movement mode, which, based on the previous analysis, must be changed to an storyboard animation based on the object-dependent property of PropertyPath ("X"), PropertyPath ("Y"). The animation at this point may be understood to be more appropriate for a timer that continuously takes the coordinates out of the path sequence points obtained from the seek, since it is only responsible for changing the X,y property of the spirit and not for updating the spirit position in the interface to achieve animation. But this is enough, because it has met the principle of updating the wizard coordinates spirit.x and Spirit.y (the rest of the task to the interface thread to do it, the code as in the previous section, we do not need to ignore). Well, then let's see how I can reinvent the * search again (don't be afraid, the current A * Seek Storyboard animation method is very mature, only need to modify several of its joints that can achieve different purposes, in fact, in the Nineth section, the tenth section has been modified.
First, we define two functions Spiritgamex and Spiritgamey to convert the coordinates of the protagonist in the map coordinate system to the coordinates in the game seek coordinate system:
int GridSize = 20; //单位格子大小
int SpiritCenterX = 4; //主角脚底离主角图片左边的距离(寻路坐标系中)
int SpiritCenterY = 5; //主角脚底离主角顶部的距离(寻路坐标系中)
//将主角的坐标换算成游戏坐标系中的坐标(缩小操作)
int GameSystemX {
get { return ((int)(Spirit.X) / GridSize) + SpiritCenterX; }
}
int GameSystemY {
get { return ((int)(Spirit.Y) / GridSize) + SpiritCenterY; }
}