⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 map.cpp

📁 成熟的RPG引擎,Flysky发布. 斜视角引擎.
💻 CPP
字号:
#include "../GameH.h"

//       斜视角地图引擎 V1.0
// 横20快
// 竖40快
//32*16
// 0 1 2 3 4 5  // X          20|
// 0/\/\/\/\/\  // Y            |
// 1\/\/\/\/\/\ //              |
// 2/\/\/\/\/\/ //              |
// 3\/\/\/\/\/\ //              |
// 4/\/\/\/\/\/ //              |
// 5\/\/\/\/\/\ //              |
// 6 \/\/\/\/\/ //40            |

int CMap::MapDMX=int(0.5*((ScreenHeight<<1)+ScreenWidth)/(TileWidth>>1))+1; //最大画出点X
int CMap::MapDMY=int(0.5*(ScreenHeight<<1)/(TileWidth>>1))+1;//最大画出点Y
int CMap::MapDSY=int(0.5*(-ScreenWidth)/(TileWidth>>1))-1; //最小画出点Y(特殊)

CMap::~CMap()
{
	if (Width>0 &&Height>0)
	{
		if (Cell!=NULL)
		{
			for (int i=0;i<Width;i++)
			{
				delete [] Cell[i];Cell[i]=NULL;
			}
			delete [] Cell;Cell=NULL;
		}
	}
	//原来是这个地方出的问题,呸呸,7450~~~~~~~~~~~~~~~~~
	//害得我下了N个断点
	//5~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	if (lpDDS_MapBack!=NULL) {lpDDS_MapBack->Release();lpDDS_MapBack=NULL;} //清空临时地图文件
	if (lpDDS_TMouse!=NULL){lpDDS_TMouse->Release();lpDDS_TMouse=NULL;} //清除Tile鼠标
}
bool CMap::ShowMap(LPDIRECTDRAWSURFACE7 MapDDS)
{
int tx,ty; //都是指左上角
if (Width>0&&Height>0)
{
	if (MapBx==MapStartX&&MapBy==MapStartY)	BltFast(MapDDS,0,0,lpDDS_MapBack,NULL,NoKey); //是否有优化纪录
	else
	{
		Clrscr(lpDDS_MapBack,0);
		//高速刷屏
		//Flysky 2005年7月14日
		//100*100 Fps:70
		for (int Y=IntSizeL(MapStartY+MapDSY,0); Y<IntSizeS(MapStartY+MapDMY,Height); Y++)
		{
			for (int X=IntSizeL(MapStartX-1,0); X<IntSizeS(MapStartX+MapDMX,Width); X++)
			{
				MDToMI(X-MapStartX,Y-MapStartY,tx,ty);	
				//地面
				if (Cell[X][Y].GroundPicNum!=-1||Cell[X][Y].IsGround!=false)
				{
					CreateRect(tmpr,Cell[X][Y].GroundPicX*TileWidth,Cell[X][Y].GroundPicY*TileHeight,
					(Cell[X][Y].GroundPicX+1)*TileWidth,(Cell[X][Y].GroundPicY+1)*TileHeight);  //显示地表
					BltFast(lpDDS_MapBack,tx,ty,lpDDS_Tile[Cell[X][Y].GroundPicNum],&tmpr,SrcKey);
				}	
				//物品1
				if (Cell[X][Y].Object1PicNum!=-1||Cell[X][Y].IsObject1!=false)
				{
					CreateRect(tmpr,Cell[X][Y].Object1PicX*TileWidth,Cell[X][Y].Object1PicY*TileHeight,
					(Cell[X][Y].Object1PicX+1)*TileWidth,(Cell[X][Y].Object1PicY+1)*TileHeight);  //显示物品1
					BltFast(lpDDS_MapBack,tx,ty,lpDDS_Tile[Cell[X][Y].Object1PicNum],&tmpr,SrcKey);
				}
				//物品2(天空)
				if (Cell[X][Y].Object2PicNum!=-1||Cell[X][Y].IsObject2!=false)
				{
					CreateRect(tmpr,Cell[X][Y].Object2PicX*TileWidth,Cell[X][Y].Object2PicY*TileHeight,
					(Cell[X][Y].Object2PicX+1)*TileWidth,(Cell[X][Y].Object2PicY+1)*TileHeight);  //显示物品1
					BltFast(lpDDS_MapBack,tx,ty,lpDDS_Tile[Cell[X][Y].Object2PicNum],&tmpr,SrcKey);
				}
			}
		}
		MapBx=MapStartX;MapBy=MapStartY;  //作优化准备
		MapDDS->BltFast(0,0,lpDDS_MapBack,NULL,NoKey); //作优化备份
	}
}
return true;
}
//=================================================================
//函数:CMap::ShowTile(int ZC=2,int x,int y,LPDIRECTDRAWSURFACE7 MapDDS)
//功能:显示一个指定层次的 Tile(无遮挡等关系)
//=================================================================
void CMap::ShowTile(int ZC,int x,int y,LPDIRECTDRAWSURFACE7 MapDDS)
{
		int tx,ty;//临时
		if (x<0||y<0||x>=Width||y>=Height) return; //越界返回
		else
		{
		MDToMI(x-MapStartX,y-MapStartY,tx,ty);
		if (tx<0||ty<0||tx>=ScreenWidth||tx>=ScreenHeight) return;
		if (ZC==2)
		{
			if (Cell[x][y].Object2PicNum!=-1||Cell[x][y].IsObject2!=false)
			{
				CreateRect(tmpr,Cell[x][y].Object2PicX*TileWidth,Cell[x][y].Object2PicY*TileHeight,
				(Cell[x][y].Object2PicX+1)*TileWidth,(Cell[x][y].Object2PicY+1)*TileHeight);  //显示物品1
				BltFast(MapDDS,tx,ty,lpDDS_Tile[Cell[x][y].Object2PicNum],&tmpr,SrcKey);
			}
		}
		else if (ZC==1)
		{
			if (Cell[x][y].Object1PicNum!=-1||Cell[x][y].IsObject1!=false)
			{
				CreateRect(tmpr,Cell[x][y].Object1PicX*TileWidth,Cell[x][y].Object1PicY*TileHeight,
				(Cell[x][y].Object1PicX+1)*TileWidth,(Cell[x][y].Object1PicY+1)*TileHeight);  //显示物品1
				BltFast(MapDDS,tx,ty,lpDDS_Tile[Cell[x][y].Object1PicNum],&tmpr,SrcKey);
			}
		}
		else
		{
			if (Cell[x][y].GroundPicNum!=-1||Cell[x][y].IsGround!=false)
			{
				CreateRect(tmpr,Cell[x][y].GroundPicX*TileWidth,Cell[x][y].GroundPicY*TileHeight,
				(Cell[x][y].GroundPicX+1)*TileWidth,(Cell[x][y].GroundPicY+1)*TileHeight);  //显示地表

				BltFast(MapDDS,tx,ty,lpDDS_Tile[Cell[x][y].GroundPicNum],&tmpr,SrcKey);
			}
		}
		}
}

bool CMap::NewMap(char *XName,int XID,int XWidth,int XHeight,char *ScrFNameX)
{
	ID=XID;
	Width=XWidth;
	Height=XHeight;
    _DELETE_ARRAY(Cell);//清空数组
	Cell=new stCell*[Width];
	for (int i=0;i<Width;i++)
	{
		Cell[i]=new stCell[Height];
	}                                  //开辟二微数组
	for (int tx=0;tx<Width;tx++)
	{
	 for (int ty=0;ty<Height;ty++)
	 {
		 Cell[tx][ty].Block=ID_BLOCK_F;
		 Cell[tx][ty].IsGround=true;
		 Cell[tx][ty].IsObject1=false;
		 Cell[tx][ty].IsObject2=false;
		 Cell[tx][ty].GroundPicNum=0;
		 Cell[tx][ty].GroundPicX=0;
		 Cell[tx][ty].GroundPicY=0;
		 Cell[tx][ty].Object1PicNum=-1;
		 Cell[tx][ty].Object1PicX=0;
		 Cell[tx][ty].Object1PicY=0;
		 Cell[tx][ty].Object2PicNum=-1;
		 Cell[tx][ty].Object2PicX=0;
		 Cell[tx][ty].Object2PicY=0;
		 Cell[tx][ty].Hook=ID_HOOK_F;
	 }
	}
	strcpy(Name,XName);
	strcpy(ScrFName,ScrFNameX);
	strcpy(FileName,"tmp.map");
	strcpy(Reserve,"V1.0");
	if (CreateSurface(lpDDS_MapBack,NULL,ScreenWidth,ScreenHeight)==true)
	{
		MapBx=Width+10;MapBy=Height+10;
		return true;
	}
	return false;
}

void MDToMI(int Dx,int Dy,int &Ix,int &Iy)
{
		Ix=(TileWidth>>1)*(Dx-Dy);//转换为绝对坐标x
		Iy=(TileHeight>>1)*(Dx+Dy);//转换为绝对坐标y      大菱形
    //Ix=Dx*TileWidth+(Dy&1)*(TileWidth>>1); //转换为绝对坐标x
    //Iy=Dy*TileHeight>>1; //转换为绝对坐标y
}
void MIToMD(int Ix,int Iy,int &Dx,int &Dy)
{
//X=(1/2)*(Y0*2-X0)/a  Y=(1/2)*(Y0*2+X0)/a
	Dx=int(0.5*((Iy<<1)+Ix)/(TileWidth>>1));
	Dy=int(0.5*((Iy<<1)-Ix)/(TileWidth>>1));
//	Dx=(1>>1)*((Iy<<1)-Ix)/(TileWidth>>1);
	//Dy=(1>>1)*((Iy<<1)+Ix)/(TileHeight>>1);
}

bool CMap::LoadMap(char *Filename)
{
		int PX=int(0.5*(ScreenHeight-(TileHeight>>1)+(ScreenWidth>>1)-(TileWidth>>1))/(TileWidth>>1));
	int PY=int(0.5*(ScreenHeight-(ScreenWidth>>1))/(TileWidth>>1));
		int tj=open(Filename,O_RDONLY);
	if (tj<= 0) return false;
	close(tj);
	FILE *fp;
	fp=fopen(Filename,"rb");
	//文件头判断
	fseek(fp,long(strlen(MAP_HEAD)),0);
	fread(Name,32,1,fp);
	//新版本则不用
	//fread(&MAX_NC,4,1,fp);
	fread(ScrFName,32,1,fp);   //读取脚本文件名
	fread(&Width,4,1,fp);
	fread(&Height,4,1,fp);
	//MIToMD(SX,SY,ROLEFX,ROLEFY);

	if (Cell!=NULL)
	{
		for (int i=0;i<Width;i++)
		{
			delete [] Cell[i];Cell[i]=NULL;
		}
		delete [] Cell;Cell=NULL;
	}

	Cell=new stCell*[Width];
		for (int i=0;i<Width;i++)
		{
			Cell[i]=new stCell[Height];
		}                                  //开辟二微数组
		for (int tx=0;tx<Width;tx++)
		{
		for (int ty=0;ty<Height;ty++)
		{
			fread(&Cell[tx][ty], sizeof(stCell), 1, fp);
		}
		}
		//if (InitNPC()!=true) return false;  //建立ROLE数组
	fread(Reserve,4,1,fp);
	fclose(fp);
	if (CreateSurface(lpDDS_MapBack,NULL,ScreenWidth,ScreenHeight)==true) //建立快速页面
	{
		MapBx=Width+10;MapBy=Height+10;
		return true;
	}
	return false;
}

bool CMap::SaveMap(char *Filename)
{
FILE *fp;
fp=fopen(Filename,"wb");
fwrite(MAP_HEAD,strlen(MAP_HEAD),1,fp);
fwrite(Name,32,1,fp);
//fwrite(&MAX_NC,4,1,fp);
fwrite(ScrFName,32,1,fp);   //读取脚本文件名
fwrite(&Width,4,1,fp);
fwrite(&Height,4,1,fp);
	for (int tx=0;tx<Width;tx++)
	{
	 for (int ty=0;ty<Height;ty++)
	 {
		 fwrite(&Cell[tx][ty], sizeof(stCell), 1, fp);
	 }
	}
	//if (InitRole(MAX_NC)!=true) return false;  //建立ROLE数组
fwrite(Reserve,4,1,fp);
fclose(fp);
return true;
}

bool CMap::IsBlock(int x,int y)
{
	if (x<0||y<0||x>=Width||y>=Height) return false;
	else
	{
	if (Cell[x][y].Block==ID_BLOCK_F) return false;
	else return true;
	}
	return false;
}

void CMap::CBlockOn(bool FT,int x,int y)
{
	if (x<0||y<0||x>=Width||y>=Height) return;
	else
	{
	if (FT==true) Cell[x][y].Block=ID_BLOCK_T;
	else Cell[x][y].Block=ID_BLOCK_F;
	}
}

bool CMap::IsHook(int x,int y)
{
	if (x<0||y<0||x>=Width||y>=Height) return false;
	else
	{
	if (Cell[x][y].Hook==ID_HOOK_F) return false;
	else return true;
	}
	return false;
}

void CMap::CHookOn(bool FT,char SName[32],int x,int y)
{
	if (x<0||y<0||x>=Width||y>=Height) return;
	else
	{
		if (FT==true) 
		{
			Cell[x][y].Hook=ID_HOOK_T;
			strcpy(Cell[x][y].HookScriptName,SName);
		}
		else Cell[x][y].Hook=ID_HOOK_F;
	}

}

void CMap::CreateAPic(int Wei,int x,int y,int PicI,int x1,int y1)
{
if (Wei==0)	{Cell[x][y].GroundPicNum=PicI;Cell[x][y].GroundPicX=x1;Cell[x][y].GroundPicY=y1;}
if (Wei==1)	{Cell[x][y].Object1PicNum=PicI;Cell[x][y].Object1PicX=x1;Cell[x][y].Object1PicY=y1;}
if (Wei==2)	{Cell[x][y].Object2PicNum=PicI;Cell[x][y].Object2PicX=x1;Cell[x][y].Object2PicY=y1;}
}
POINT CMap::GetStartXY(int *x,int *y)
{
   if (x!=NULL) *x=MapStartX;
   if (y!=NULL) *y=MapStartY;
   POINT tp={MapStartX,MapStartY};
   return tp;
}
BOOL CMap::SetStartXY(int x,int y)
{
	if ((MapStartX=x)&&(MapStartY=y)) return TRUE;
	return FALSE;
}

void CMap::ShowTileCPic(LPDIRECTDRAWSURFACE7 DDS,int CurposX,int CurposY)
{
	if (lpDDS_TMouse!=NULL) //如果 Mouse 已被初始化
	{
		int sx=0,sy=0;
		MIToMD(CurposX,CurposY,sx,sy);//坐标转换
		if(sx>MapDMX) sx=MapDMX-1;
		if(sy>MapDMY) sy=MapDMY-1;
		MDToMI(sx,sy,CurposX,CurposY); //再一个坐标转换
		BltFast(DDS,CurposX,CurposY,lpDDS_TMouse,NULL,SrcKey);
	}
}
bool CMap::SetTileMouse(char *FileName) //设定鼠标Tile
{
	if (lpDDS_TMouse!=NULL){lpDDS_TMouse->Release();lpDDS_TMouse=NULL;} //清除原来的
	if (CreateSurface(lpDDS_TMouse,FileName,0,0)!=false) return true; //重新建立页面
	return false;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -