您的位置:首页 > 编程语言 > C#

游戏地图编辑器之list动态二维数组(C#)

2011-07-01 09:07 190 查看
在《星空之翼》的地图编辑器中,其中遇到的一个难题就是动态存储地图背景,就是在修改地图背景过程中可以任意修改地图背景的大小,并且不会发生溢出错误。例如10*10改为15*15的时候,在15*15中包含10*10区域的地图的背景,同样,15*15改为10*10的时候,地图编辑器要截取15*15中包含10*10区域的地图背景。如下图。









C#采用list实现动态二维数组,地图背景用动态二维数组保存。

将整个世界切分成多个Tile,每个Tile 大小相同,用二维坐标形式组织整个地图背景。定义一个Tile类来描述每个Tile,定义一个Map类来描述地图背景。

Tile类

public class Tile 
   { 
       public int x = 0;                  //Tile在地图世界的坐标
       public int y = 0;                     
       public int TWidth =1;              //Tile的尺寸
       public int THeight = 1; 
       public Bitmap TileBitMap;          //Tile所在地图背景部分的图片
       public Tile(int width, int height) //构造函数
       { 
           TWidth = width; 
           THeight = height; 
           TileBitMap = new Bitmap(TWidth, THeight,PixelFormat.Format24bppRgb); 
       } 
   }


Map类

public class Map 
{ 
    public int MapHeight = 1;          //地图高度       
    public int MapWidth = 1;           //地图宽度 
    public int TileHeight = 1;         //Tile高度 
    public int TileWidth = 1;         //Tile宽度       
    public List MapTiles;             //List链表
    public Map(int mapwidth, int mapheight, int tilewidth, int tileheight) 
    { 
        MapWidth = mapwidth; 
        MapHeight = mapheight; 
        TileHeight = tileheight; 
        TileWidth = tilewidth; 
        MapTiles = new List(); 
        Tile tempTile = new Tile(TileWidth, TileHeight); 

        for (int i = 0; i < MapHeight; i++) 
        { 
            for (int j = 0; j < MapWidth; j++) 
            { 
                tempTile.x = j; 
                tempTile.y = i; 
                MapTiles.Add(tempTile); 
            } 
        } 
    } 
    /// 
    ///改变地图中的瓷砖 
    ///     
    public bool ChangeTile(int x, int y, Tile tile) 
    { 
        tile.x = x; 
        tile.y = y; 
        bool t = false; 
        Tile otile = new Tile(TileWidth, TileHeight); 
        if ((TileWidth == tile.TWidth) && (TileHeight == tile.THeight) && (x <= MapWidth) && (y <= MapHeight)) 
        { 
            try 
            { 
                foreach (Tile e in MapTiles) 
                { 
                    if (e.x == x && e.y == y) 
                    { 
                        otile = e; 
                        break; 
                    } 
                } 
                MapTiles.Remove(otile); 
                MapTiles.Add(tile); 
                t = true; 
            } 
            catch 
            { 
                MessageBox.Show("改变瓷砖失败"); 
                t = false; 
            } 
        } 
        return t; 
    } 
    //清除指定的Tile
    public bool RemoveTile(int x, int y) 
    { 
        try 
        { 
            foreach (Tile e in MapTiles) 
            { 
                if (x == e.x && y == e.y) 
                { 
                    MapTiles.Remove(e); 
                    return true; 
                } 
            } 
            return false; 
        } 
        catch 
        { 
            MessageBox.Show("删除瓷砖失败"); 
            return false; 
        } 
    } 
    public Tile ReturnTile(int x, int y) 
    { 
        foreach (Tile e in MapTiles) 
        { 
            if (x == e.x && y == e.y) 
            { 
                return e; 
            } 
        } 
        return null; 
    } 
}


定义一个Map类的对象myMap,当Tile的大小不变,Map的高度和宽度发生改变时,执行下面的代码保证地图背景的动态改变。Mapconfig是一个静态类,存储了公共的地图配置信息。

if ((Mapconfig.TileWidth == Mapconfig.TileLastWidth && Mapconfig.TitleHeight==Mapconfig.TitleLastHeight)) 
            { 
                int tempW = mymap.MapWidth;     //原地图宽度 
                int tempH = mymap.MapHeight;      //原地图高度          
                List tempMap = mymap.MapTiles;    //临时链表存储原地图链表 
                mymap = new Map(Mapconfig.MapWidth, Mapconfig.MapHeight, Mapconfig.TileWidth, Mapconfig.TitleHeight);    //创建新地图
               //临时链表中只要是小于新地图尺寸的部分都赋给新地图的地图链表
                foreach (Tile tm in tempMap)
              { 
                    if (tm.x < mymap.MapWidth && tm.y < mymap.MapHeight) 
                    { 
                        mymap.ChangeTile(tm.x, tm.y, tm); 
                    } 
                } 
            }


地图编辑器下载 作者其他源码下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐