C # development Wpf/silverlight animation and game series Tutorials (Game Course): (13) Traction Map Movement mode ①
In many of the previous chapters, I explained the implementation of the map structure, which left a key question: is the role moving or the map moving in the game? How are the moving (displacement) relationships between them implemented?
So in the next chapters I'll do a detailed analysis of these two questions.
First of all, we have to start with the game mode. At present 2D overlooking the game to even the strategy, SLG, RPG (ARPG) and other types of games for the mainstream. In the real-time strategy, SLG map, the map movement principle is: When the mouse is in the game window 8 edges, the map begins to move, I call it the Traction Map movement mode.
As pictured above, we can make this analogy: compare the game window to our camcorder (the Windows13 window in the picture above), in the world of maps, we see only areas of the camera lens (the game window) visible from the camera (other imaginary parts of the window that are not visible). But the game window is the same as using a stent to fix the lens can not be moved, then how to see the various parts of the scene? Of course it's only going to move the background map picture so that we need the scenery part to render in the window. Therefore, the map is triggered when the mouse enters the 8 areas (blue and brown) as described in the above diagram.
To make it easier for everyone to understand, I take the blue area on the left side of the window as an example: when my mouse moves to the left in the game window close to the edge, the map picture begins to reverse to the right speed or acceleration movement; Similarly, all objects in the map are moving in the same direction and speed as the map image, This gives us a visual sense: We are pulling the game window to the left of the mouse to explore the map and object objects. Some friends will ask: if there are 1000 objects on a map, do I have to move these 1000 objects every 10 milliseconds? This performance is completely unscientific! Yes, in actual development, if a map has a large number of objects, we certainly do not (if the map is a small map, or the object is not much, this is entirely feasible and easier to achieve). Having understood this principle, let's look at how these operations are done in Wpf/silverlight. The first thing that needs to be done is when the map moves, according to the direction of the map movement (in the interface refresh thread CompositionTarget or interval of 10 milliseconds dispatchertimer) through the foreach High-performance for All Object Objects (Spirit) of X, Y coordinates, and when do you want to show these objects? To determine the map coordinate points for the center point of the current game window, and to make a circular range with a rectangular range or radius of R, with the point as the Midpoint (center), search: If an object's X, The y-coordinate dynamically loads its display entity into the form canvas (CARRIER.CHILDREN.ADD (Spirit)) and then lays it in its corresponding x,y coordinate position (canvas.setleft (spirit,x); Canvas.settop (Spirit,y), and continue to move according to the map (always modify Canvas.setleft (), Canvas.settop ()); Similarly, the map center coordinates are constantly changing in the map movement, If some object's x,y coordinates are outside the center of the map, we remove it from the form canvas (Carrier.Children.Remove (Spirit)), at which point the objects are returned to the state of waiting to be displayed, their X, The Y coordinates are also constantly changing in the background thread, and as long as the local diagram center appears near them again, they repeat the steps again.