C # development Wpf/silverlight animation and game series Tutorials (Game Course): (34) Map Editor was born!
So far, tutorial example in the game although the implementation of a *, but can not easily set obstacles for the map, and the game all maps are a large picture, the protagonist's move will lead to the form of the map of the non-stop cutting, the larger map brings negative performance loss more obvious. Slicing the map to achieve the maximum performance optimization: Load when loading on demand, map based on the location of the protagonist to show only a specific part of, and if you can also match any outline of the occlusion, then all this will be more perfect interpretation of our game. Development and production of map editor is imminent.
So in this section I will explain how to make a grid based on that Easy-to-use and powerful map editor, and first implement the obstacle setting function and a * search road simulation.
The first step: design layout
Universal editors must be able to adapt to all dimensions of the map, so I choose ScrollViewer as the hosting container for the map, and by setting its horizontalscrollbarvisibility and verticalscrollbarvisibility are auto so that it can adapt to the map size, that is, the local map exceeds the size of the form of the scroll bar appears. Since a scrollviewer can only host one content, so in order to be able to draw and erase obstacles on top of it, we must also add another transparent scrollviewer for carrying the barrier grid (GRID):
As the description says, these two scrollviewer must be consistent, that is, the size of the two, the position of the scroll bar at the same time the same performance:
//滚动窗体二级协动
private void ObstructionViewer_ScrollChanged(object sender, ScrollChangedEventArgs e) {
ScrollViewer scrollViewer = sender as ScrollViewer;
MapViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset);
MapViewer.ScrollToHorizontalOffset(scrollViewer.HorizontalOffset);
}
The second step, the design function
1) Loading map:
OpenFileDialog to open a File selection dialog box, and the file selection filter filters to limit the type of loading pictures are *.jpg and *.png:
//载入地图
private void LoadMap_Click(object sender, RoutedEventArgs e) {
OpenFileDialog loadMap = new OpenFileDialog() {
CheckFileExists = true,
CheckPathExists = true,
Multiselect = false,
Filter = "图像文件(*.jpg,*.png)|*.jpg;*.png",
};
loadMap.FileOk += new System.ComponentModel.CancelEventHandler(loadMap_FileOk);
loadMap.ShowDialog();
}