using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.IO; using Color = System.Drawing.Color; using Rectangle = System.Drawing.Rectangle; using RectangleF = System.Drawing.RectangleF; using Point = System.Drawing.Point; using PointF = System.Drawing.PointF; using Size = System.Drawing.Size; using SizeF = System.Drawing.SizeF; using SlimDX.Direct3D9; using SlimDX; namespace MG { /// <summary> /// 地图 /// </summary> public class Map : IDisposable { public Map() { for (int i = 0; i < 8; i++) { m_bAniTileFrame[i] = new byte[16]; } } /// <summary> /// 读取地图 /// </summary> /// <param name="filename">地图文件名</param> /// <returns>MapFile</returns> public static MapFile ReadMapFile(string filename) { MapFile MF; MapFileHeader mapheader; MapGround[,] mglist; using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) { using (BinaryReader br = new BinaryReader(fs)) { br.BaseStream.Seek(20, SeekOrigin.Begin); mapheader.shVer = br.ReadUInt16(); mapheader.shWidth = br.ReadUInt16(); mapheader.shHeight = br.ReadUInt16(); mapheader.cEventFileIdx = (char)br.ReadByte(); mapheader.cFogColor = (char)br.ReadByte(); ushort groundwidth = (ushort)(mapheader.shWidth / 2); ushort groundheight = (ushort)(mapheader.shHeight / 2); MF.MapHeader = mapheader; mglist = new MapGround[groundwidth, groundheight]; //读取小地图(背景)所有瓷砖的结构(构造地面最底层的) MapGround mg; for (int w = 0; w < groundwidth; w++) { for (int h = 0; h < groundheight; h++) { mg.cFileIdx = (char)br.ReadByte(); mg.shTileIdx = br.ReadUInt16(); mglist[w, h] = mg; } } MF.MGList = mglist; //读取大地图(单元格信息,地面物件对象)的结构(构造基于地面的建筑,树木,动画等的) CellInfor[,] CellInfors = new CellInfor[mapheader.shWidth, mapheader.shHeight]; CellInfor ci; for (int w = 0; w < mapheader.shWidth; w++) { for (int h = 0; h < mapheader.shHeight; h++) { ci.bFlag = br.ReadByte(); ci.bObj1Ani = br.ReadByte(); ci.bObj2Ani = br.ReadByte(); ci.bFileIdx = br.ReadInt16(); ci.wObj1 = br.ReadUInt16(); ci.wObj2 = br.ReadUInt16(); ci.bDoorIdx = br.ReadUInt16(); ci.bDoorOffset = br.ReadByte(); ci.wLigntNEvent = br.ReadUInt16(); CellInfors[w, h] = ci; } } MF.CellInfors = CellInfors; br.Close(); } } return MF; } int dwLastTime, dwTime, dwDelay = 0; //用于纪录最后时间,时间,延时等数据的全局变量初值为0 /// <summary> /// 画出地图 /// </summary> /// <param name="nX">横坐标</param> /// <param name="nY">从坐标</param> /// <param name="brush2d">画笔</param> public void DrawMap(int nX, int nY, Brush brush2d) { dwTime = DateTime.Now.Millisecond; //获取系统当前消息循环的滴答数 dwDelay = dwTime - dwLastTime; //当前消息循环滴答数减去上一次的消息循环的滴答数为两次消息处理的延时间隔。 dwLastTime = dwTime; //保存当前滴答数为最近一次消息循环的时间//渲染地图上的物体 //if (dwDelay != 0) //{ SetAniTileFrame(dwDelay); //设置地图上的动画帧 DrawGround(nX, nY, brush2d); DrawMapCell(nX, nY, brush2d); //绘制开门 //DrawOpenDoor(nX, nY); //} } /// <summary> /// 地表元素 /// </summary> public class MapElement : FaceImage { public MapElement(int x, int y, int width, int height) : base(Game.Current.dev, width, height, Format.R5G6B5) { X = x; Y = y; } /// <summary> /// 标识地图上的坐标点 /// </summary> public int X, Y; } /// <summary> /// 地面对象 /// </summary> public class MapObjects : FaceImage { public MapObjects(int x, int y, byte layerIndex, int width, int height) : base(Game.Current.dev, width, height, Format.A8R8G8B8) { X = x; Y = y; LayerIndex = layerIndex; } public byte LayerIndex; /// <summary> /// 标识地图上的坐标点 /// </summary> public int X, Y; } /// <summary> /// 移动像素数目列表 /// </summary> Point[] MovePixelArray = new Point[] { new Point(8, 5), new Point(16, 11), new Point(24, 16), new Point(32, 21), new Point(40, 27), new Point(48, 32) }; /// <summary> /// 视图区的地图(地表)块索引 /// </summary> List<MapElement> GroundViewBuffer = new List<MapElement>(); /// <summary> /// 视图区的地图(物体)的索引 /// </summary> List<MapObjects> ObjectViewBuffer = new List<MapObjects>(); //public ushort[,] ViewBuffer = new ushort[11, 12]; //public ushort[,] ViewMap=new ushort //public FaceImage MapImage = new FaceImage(Game.Current.dev, 1024, 1024) { Location_Draw = new Point(96, 64) }; //public static implicit operator Texture(Map map) //{ // return map.MapImage; //} //public Rectangle DrawRectangle { get { return new Rectangle(MapImage.Location_Draw, Game.Window.ClientSize); } } //Originaldata // //1008width 21 //736height 23 //BufferData //1024width //1024height /// <summary> /// 当前的地图文件 /// </summary> public static MapFile Current { get; set; } /// <summary> /// 删除地表图片的临时存储区 /// </summary> public List<MapElement> TempDeleteGroundViewBuffer = new List<MapElement>(); /// <summary> /// 删除地面对象的临时缓冲区 /// </summary> public List<MapObjects> TempDeleteObjectViewBuffer = new List<MapObjects>(); /// <summary> /// 画出地表元素 /// </summary> /// <param name="nX">横坐标</param> /// <param name="nY">纵坐标</param> /// <param name="brush2d">2D画笔</param> public void DrawGround(int nX, int nY, Brush brush2d) { //是否查过了地图边界 if (nX > Current.MapHeader.shWidth - _showWidthCellCount || nX < 0 || nY > Current.MapHeader.shHeight - _showHeightCellCount || nY < 0) { return; } //过滤超出缓冲范围的图像数据 foreach (MapElement grd in GroundViewBuffer) { if (grd.X < nX / 2 || grd.X > (nX + _bufferWidthCellCount) / 2 || grd.Y < nY / 2f || grd.Y > (nY + _bufferHeightCellCount) / 2) { TempDeleteGroundViewBuffer.Add(grd); } } foreach (MapElement grd in TempDeleteGroundViewBuffer) { GroundViewBuffer.Remove(grd); grd.Dispose(); } if (TempDeleteGroundViewBuffer.Count > 0) TempDeleteGroundViewBuffer.Clear(); for (int y = 0; y < _showHeightCellCount; y++) { for (int x = 0; x < _showWidthCellCount; x++) { int _GrdX = (nX + x); int _GrdY = (nY + y); if (_Grd