List of dynamic two-dimensional arrays in game map editor (C #)

Source: Internet
Author: User
Tags map class

In the map editor of "the wings of the Stars", one difficulty is to dynamically store the map background, that is, you can modify the background size of a map as needed during the modification of the map background, and no overflow error occurs. For example, when 10*10 is changed to 15*15, the background of the map containing 10*10 areas is 15*15. Similarly, when 15*15 is changed to 10*10, the map editor captures the background of a map that contains 10*10 areas in 15*15. For example.

C # Use List to implement a dynamic two-dimensional array and save the map background with a dynamic two-dimensional array.

Divide the whole world into multiple tile. Each tile is of the same size and organizes the entire map background in two-dimensional coordinates. Define a tile class to describe each tile, and define a map class to describe the map background.

Tile class

Public class tile <br/>{< br/> Public int x = 0; // coordinates of tile in map world <br/> Public int y = 0; <br/> Public int twidth = 1; // tile size <br/> Public int theight = 1; <br/> Public bitmap tilebitmap; // image of the background part of the image where the tile is located <br/> Public tile (INT width, int height) // constructor <br/>{< br/> twidth = width; <br/> theight = height; <br/> tilebitmap = new Bitmap (twidth, theight, pixelformat. format24bpprgb); <br/>}</P> <p>

Map class

Public class Map <br/>{< br/> Public int mapheight = 1; // map height <br/> Public int mapwidth = 1; // map width <br/> Public int tileheight = 1; // tile height <br/> Public int tilewidth = 1; // tile width <br/> public list maptiles; // list linked list <br/> public map (INT mapwidth, int mapheight, int tilewidth, int tileheight) <br/>{< br/> mapwidth = mapwidth; <br/> mapheight = mapheight; <br/> tileheight = tileheight; <br/> tilewidth = tilewidth; <br/> maptiles = new list (); <br/> tile temptile = new tile (tilewidth, tileheight); </P> <p> for (INT I = 0; I <mapheight; I ++) <br/> {<br/> for (Int J = 0; j <mapwidth; j ++) <br/> {<br/> temptile. X = J; <br/> temptile. y = I; <br/> maptiles. add (temptile ); <br/>}< br/> // <br/> // change the tile in the Map <br/> /// <br/> Public bool changetile (int x, int y, tile) <br/>{< br/> tile. X = x; <br/> tile. y = y; <br/> bool T = false; <br/> tile otile = new tile (tilewidth, tileheight); <br/> If (tilewidth = tile. twidth) & (tileheight = tile. theight) & (x <= mapwidth) & (Y <= mapheight )) <br/>{< br/> try <br/>{< br/> foreach (tile e in maptiles) <br/>{< br/> If (E. X = x & E. y = y) <br/>{< br/> otile = E; <br/> break; <br/>}< br/> maptiles. remove (otile); <br/> maptiles. add (tile); <br/> T = true; <br/>}< br/> catch <br/>{< br/> MessageBox. show ("failed to change tile"); <br/> T = false; <br/>}< br/> return T; <br/>}< br/> // clear the specified tile <br/> Public bool removetile (int x, int y) <br/>{< br/> try <br/> {<br/> foreach (tile e in maptiles) <br/>{< br/> If (x = E. X & Y = E. y) <br/>{< br/> maptiles. remove (E); <br/> return true; <br/>}< br/> return false; <br/>}< br/> catch <br/> {<br/> MessageBox. show ("failed to delete tile"); <br/> return false; <br/>}< br/> Public tile returntile (int x, int y) <br/>{< br/> foreach (tile e in maptiles) <br/>{< br/> If (x = E. X & Y = E. y) <br/>{< br/> return E; <br/>}< br/> return NULL; <br/>}</P> <p>

Define the object mymap of a map class. When the size of the tile remains the same and the height and width of the map change, run the following code to ensure the dynamic change of the map background. Mapconfig is a static class that stores public map configuration information.

If (mapconfig. tilewidth = mapconfig. tilelastwidth & amp; mapconfig. titleheight = mapconfig. titlelastheight) <br/>{< br/> int tempw = mymap. mapwidth; // original map width <br/> int temph = mymap. mapheight; // original map height <br/> List tempmap = mymap. maptiles; // temporary linked list to store the original map linked list <br/> mymap = new map (mapconfig. mapwidth, mapconfig. mapheight, mapconfig. tilewidth, mapconfig. titleheight ); // create a new map <br/> // The temporary linked list is assigned to the map linked list of the new map as long as it is smaller than the new map size <br/> foreach (tile TM in tempmap) <br/>{< br/> If (TM. x <mymap. mapwidth & TM. Y <mymap. mapheight) <br/>{< br/> mymap. changetile (TM. x, TM. y, Tm); <br/>}< br/>

Map editor download author other source code download

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.