Instant strategy type game because of its precise micro-manipulation, grand scene and rich strategy elements are loved by the players, "sand Dunes II" to create a real sense of real-time strategy game form, after Westwood created the "Command and Conquest" series Plus Blizzard's "Warcraft" and "StarCraft" The series will instantly push the RTS development to a climax. It is in this atmosphere, Microsoft's "Empire Times" series bred and born, marching the footsteps of human civilization progress, accompanied me through the high school that life transition era.
In this section, I will explain to you the use of the scene editor to build the Empire Times 2 game demo.
The most important feature of the real-time strategy game is its operation, with the mouse with the keyboard for different combinations of unit management. In the Age of Empires game, the left mouse button is responsible for selecting units, while the right button is directing them to move and attack, when we can fully all the operations are focused on the left button, so that players have a better handle. However, at the same time the diversification of the function will increase the complexity of the relevant code, the left button is responsible for selecting a single unit, but also in the case of holding and not put in the selected range of units, and in the absence of a valid choice of command has been selected units to move the target point; We must make the best use of and connect the MouseLeftButtonDown, MouseLeftButtonUp and MouseMove events to achieve our goal. Here I use a little trick, that is, when the mouse is pressed, if the range is less than 10*10 pixel is determined to be invalid range selection, thus performing other operations; This is also a key technical point, of course, perhaps you have a better solution to the perfect deal with these judgments of concurrency.
When selecting a unit object in a range, the usual practice in an instant strategy game is to draw a rectangle from the point of the mouse click to the dragged target, which we can do in Silverlight, and record the starting coordinate selectedstart when the left key is pressed, as judged by the selected state. You can then do the following in the Mouse movement event:
<summary>
Mouse Move Draw Unit selection range
</summary>
private void MouseMove (object sender, MouseEventArgs e) {
if (ismousecaptured) {
Selectedend = E.getposition (Mainscene.container);
The dragged box must be x,y greater than 10 pixels before it enters the Select range state, otherwise the command move state
if (Math.Abs (selectedend.x-selectedstart.x) >= && math.abs (SELECTEDEND.Y-SELECTEDSTART.Y) >= 10) {
Isselecting = true;
BOOL ScaleX = False, ScaleY = false;
if (Selectedend.x < selectedstart.x) {ScaleX = true;}
if (Selectedend.y < selectedstart.y) {ScaleY = true;}
MainScene.Selector.RenderTransform = new ScaleTransform () {ScaleX = ScaleX -1:1, ScaleY = ScaleY? -1:1};
MainScene.Selector.Width = Math.Abs (selectedend.x-selectedstart.x);
MainScene.Selector.Height = Math.Abs (SELECTEDEND.Y-SELECTEDSTART.Y);
} else {
Isselecting = false;
}
}
}
Although the code is very simple, but can achieve the arbitrary direction of the rectangular rendering, the effect is perfect: