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

📄 3dgamemap.cpp

📁 网络游戏魔域源代码 测试可以完整变异
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	char szFileName[_MAX_PATH];
	char szFullFileName[_MAX_PATH];
	GetCurrentDirectory(_MAX_PATH,szCurDir);
	fread(szFileName, sizeof(char), _MAX_PATH, fp);

	sprintf(szFullFileName, "%s\\%s", szCurDir, szFileName);
//	繁体地图目录有问题	
//	sprintf(szFullFileName, "%s",szFileName);
	
	CTerrainLayer* pLayer = this->GetTerrainLayer();
	if(!pLayer)
		return false;
	pLayer->LoadPuzzle(szFullFileName);
	
	CMySize infoSize = {0, 0};
	fread(&infoSize.iWidth, sizeof(UINT), 1, fp);
	fread(&infoSize.iHeight, sizeof(UINT), 1, fp);
	this->SetMapSize(infoSize);
	
	int i, j;
	for(i = 0; i < infoSize.iHeight; i++)
	{
		DWORD dwCheckData = 0;
		for(j = 0; j < infoSize.iWidth; j++)
		{
			CellInfo* pCellInfo = g_objGameMap.GetCell(j, i);
			MYASSERT(pCellInfo);
			LayerInfo* pLayerInfo = &pCellInfo->m_infoLayer;
			
			if(pLayerInfo)
			{
				//int nPosX, nPosY;

				fread(&pLayerInfo->usMask, sizeof(unsigned short), 1, fp);
				fread(&pLayerInfo->usTerrain, sizeof(unsigned short), 1, fp);
				fread(&pLayerInfo->sAltitude, sizeof(short), 1, fp);
				dwCheckData += pLayerInfo->usMask * (pLayerInfo->usTerrain+i+1) + (pLayerInfo->sAltitude+2)*(j+1+pLayerInfo->usTerrain);
				
			}
		}
		DWORD dwMapCheckData;
		fread(&dwMapCheckData, sizeof(DWORD), 1, fp);
		if(dwMapCheckData != dwCheckData)
			return false;
	}
	pLayer = this->GetTerrainLayer();
	if(pLayer)
	{
		pLayer->LoadDataPassage(fp);
		/*if(dwVersion == 1003)
		pLayer->LoadDataRegion(fp);*/
	}
	CInteractiveLayer* pInteractiveLayer = this->GetInteractiveLayer();
	if(pInteractiveLayer)
	{
		pInteractiveLayer->LoadDataLayer(fp);
	}
	
	// other layers
	// read amount ...
	int nAmount;
	fread(&nAmount, sizeof(int), 1, fp);
	for(i = 0; i < nAmount; i++)
	{
		int nIndex, nType;
		fread(&nIndex, sizeof(int), 1, fp);
		fread(&nType, sizeof(int), 1, fp);
		
		switch(nType)
		{
		case CMapLayer::LAYER_SCENE:
			{
				CSceneLayer* pLayer = new CSceneLayer;
				if(!pLayer)
					return false;
				m_setMapLayer.insert(m_setMapLayer.begin()+nIndex,pLayer);
				pLayer->LoadDataLayer(fp);
			}
			break;
		}
	}
	
	fclose(fp);
	m_idDoc =1;
	return true;
}
//---------------------------------------------------------------------------//
int  CGameMap::GetLayerAmount()
{
	return m_setMapLayer.size();
}
//----------------------------------------------------//
CMapLayer* CGameMap::GetLayerByIndex(int nIndex)
{
	int nAmount = m_setMapLayer.size();
	if((nIndex < 0) || (nIndex >= nAmount))
		return NULL;
	return  m_setMapLayer[nIndex];
}
//----------------------------------------------------//
void CGameMap::DelLayer(int nIndex)
{
	int nAmount = this->GetLayerAmount();
	if((nIndex < 0) || (nIndex >= nAmount))
		return;
	CMapLayer* pLayer = this->GetLayerByIndex(nIndex);
	if((pLayer->GetType() == CMapLayer::LAYER_TERRAIN)||
		(pLayer->GetType() == CMapLayer::LAYER_INTERACTIVE)||
		(pLayer->GetType() == CMapLayer::LAYER_SURFACE))
		return;
	SAFE_DELETE(pLayer);
	m_setMapLayer.erase(m_setMapLayer.begin()+nIndex);
}
//----------------------------------------------------//
void CGameMap::LayerUp(int nIndex)
{
	if(nIndex <= 0)
		return;
	CMapLayer* pLayer = this->GetLayerByIndex(nIndex);
	if(!pLayer)
		return;
	if((pLayer->GetType() == CMapLayer::LAYER_TERRAIN)||
		(pLayer->GetType() == CMapLayer::LAYER_INTERACTIVE)||
		(pLayer->GetType() == CMapLayer::LAYER_SURFACE))
		return;
	CMapLayer* pUpLayer = this->GetLayerByIndex(nIndex-1);
	if(!pUpLayer)
		return;
	m_setMapLayer[nIndex] = pUpLayer;
	m_setMapLayer[nIndex-1] = pLayer;
}
//----------------------------------------------------//
void CGameMap::LayerDn(int nIndex)
{
	if(nIndex < 0)
		return;
	int nAmount = m_setMapLayer.size();
	if(nIndex > nAmount)
		return;
	CMapLayer* pLayer = this->GetLayerByIndex(nIndex);
	if(!pLayer)
		return;
	if((pLayer->GetType() == CMapLayer::LAYER_TERRAIN)||
		(pLayer->GetType() == CMapLayer::LAYER_INTERACTIVE)||
		(pLayer->GetType() == CMapLayer::LAYER_SURFACE))
		return;
	
	CMapLayer* pDnLayer = this->GetLayerByIndex(nIndex+1);
	if(!pDnLayer)
		return;
	m_setMapLayer[nIndex] = pDnLayer;
	m_setMapLayer[nIndex+1] = pLayer;
	
}
//----------------------------------------------------//

BOOL CGameMap::IsPosVisible(CMyPos posCell, int nExtendSize/*=8*/)
{
	int nSumPos     = posCell.x+posCell.y;
	int nDeltaPos	= posCell.x-posCell.y;
	int nMyExtendSize = nExtendSize*_NORMAL_SCALE/this->GetScale();
	// get top-left cell point of visible area
	int nStartX, nStartY;
	this->World2Cell(m_posView.x, m_posView.y, nStartX, nStartY);
	
	nStartX	-= nMyExtendSize;
	
	int nSumStartPos	=nStartX+nStartY;
	int nDeltaStartPos	=nStartX-nStartY;
	
	// get width and height of visible area
	int nWidth	=_SCR_WIDTH/_CELL_WIDTH+nMyExtendSize;
	int nHeight	=_SCR_HEIGHT/_CELL_HEIGHT+2*nMyExtendSize;
	
	// judge now
	if(nSumPos >= nSumStartPos && nSumPos <= nSumStartPos+2*nHeight &&
		nDeltaPos >= nDeltaStartPos && nDeltaPos <= nDeltaStartPos+2*nWidth)
		return true;
	else
		return false;	
}
//----------------------------------------------------//
void CGameMap::GetMapObjSize(CMySize& infoSize, CMapObj* pObj)
{
	if(!pObj)
	{
		infoSize.iWidth = 1;
		infoSize.iHeight = 1;
		return;
	}
	
	switch(pObj->GetObjType())
	{
	case MAP_TERRAIN_PART:
		{
			C2DMapTerrainObjPart* pTerrainObjPart =  (C2DMapTerrainObjPart*)pObj;
			if(pTerrainObjPart)
				pTerrainObjPart->GetBase(infoSize);
		}
		break;
	case MAP_COVER:
		{
			C2DMapCoverObj* pCoverObj =  (C2DMapCoverObj*)pObj;
			if(pCoverObj)
				pCoverObj->GetBase(infoSize);
		}
		break;
		
	default:
		{
			infoSize.iWidth = 1;
			infoSize.iHeight = 1;
		}
		break;
	}
}
//----------------------------------------------------//
void CGameMap::ResetViewPos()
{
	m_posViewOffset.x = 0;
	m_posViewOffset.y = 0;
}
//----------------------------------------------------//
void CGameMap::SetViewPos(CMyPos posView, BOOL bShiftViewPos, BOOL bBgLimit)
{
	CMyPos posPos;
	::MouseCheck(posPos.x, posPos.y);
	
	if(bShiftViewPos)
	{
		if(posPos.y <= 120)
		{
			m_posViewOffset.y -= 6 - posPos.y/20;
		}
		if(posPos.y >= _SCR_HEIGHT-170)
		{
			m_posViewOffset.y += 6 - (_SCR_HEIGHT - posPos.y - 50)/20;
		}
		if(posPos.x <= 120)
		{
			m_posViewOffset.x -= 6 - posPos.x/20;
		}
		if(posPos.x >= _SCR_WIDTH-120)
		{
			m_posViewOffset.x += 6 - (_SCR_WIDTH-posPos.x)/20;
		}
	}
	
	// 移动屏幕的限制,保证人在屏幕内
	if(bBgLimit)
	{
		int nData = 356;
		nData = nData *  _NORMAL_SCALE / this->GetScale();
		if(m_posViewOffset.x > nData)
			m_posViewOffset.x = nData;
		if(m_posViewOffset.x < -nData)
			m_posViewOffset.x = -nData;
		
		nData = 192;
		nData = nData *  _NORMAL_SCALE / this->GetScale();
		if(m_posViewOffset.y > nData)
			m_posViewOffset.y = nData;
		nData = 172;
		nData = nData *  _NORMAL_SCALE / this->GetScale();
		if(m_posViewOffset.y < -nData)
			m_posViewOffset.y = -nData;
	}
	
	this->ProcessShakeEffect();
	this->ProcessScaleEffect();
	this->ProcessColorEffect();
	posView.x += m_posViewOffset.x;
	posView.y += m_posViewOffset.y;
	
	int iWorldX = posView.x;
	int iWorldY = posView.y;
	
	CMyPos posLimitBg, posLimitWorld;
	CMySize sizeBg;
	this->GetBgSize(sizeBg);
	posLimitBg.x = sizeBg.iWidth - _SCR_WIDTH + (_SCR_WIDTH - m_nRealScreenWidth)/2;
	posLimitBg.y = sizeBg.iHeight - _SCR_HEIGHT + (_SCR_HEIGHT - m_nRealScreenHeight)/2;
	this->Bg2World(posLimitBg.x, posLimitBg.y, posLimitWorld.x, posLimitWorld.y);
	
	
	UINT uBgWorldX, uBgWorldY;
	this->GetBgWorldPos(uBgWorldX, uBgWorldY);
	if(bBgLimit)
	{
		if(iWorldX > posLimitWorld.x)
			iWorldX = posLimitWorld.x;
		
		if(iWorldY > posLimitWorld.y)
			iWorldY = posLimitWorld.y;
		
		if(iWorldX < (int)uBgWorldX - (_SCR_WIDTH - m_nRealScreenWidth)/2)
			iWorldX =uBgWorldX - (_SCR_WIDTH - m_nRealScreenWidth)/2;
		
		if(iWorldY < (int)uBgWorldY - (_SCR_HEIGHT - m_nRealScreenHeight)/2)
			iWorldY =uBgWorldY - (_SCR_HEIGHT - m_nRealScreenHeight)/2;
	}
	
	m_posView.x = iWorldX;
	m_posView.y = iWorldY;
	this->SetCamera(m_posView);
}
//----------------------------------------------------//
void CGameMap::World2Bg	(int iWorldX, int iWorldY, int& iBgX, int& iBgY)
{
	UINT uBgWorldX, uBgWorldY;
	this->GetBgWorldPos(uBgWorldX, uBgWorldY);
	
	iBgX	=iWorldX-uBgWorldX;
	iBgY	=iWorldY-uBgWorldY;
}

//--------------------------------------------------------------
void CGameMap::Bg2World	(int iBgX, int iBgY, int& iWorldX, int& iWorldY)
{
	UINT uBgWorldX, uBgWorldY;
	this->GetBgWorldPos(uBgWorldX, uBgWorldY);
	
	iWorldX	=uBgWorldX+iBgX;
	iWorldY	=uBgWorldY+iBgY;
}
//--------------------------------------------------------------
void CGameMap::GetBgWorldPos(UINT &uWorldX,UINT &uWorldY)
{
	CMySize sizePuzzle;
	this->GetBgSize(sizePuzzle);
	
	uWorldX	=m_posOrigin.x-sizePuzzle.iWidth/2;
	uWorldY	=m_posOrigin.y+_CELL_HEIGHT*m_sizeMap.iHeight/2-sizePuzzle.iHeight/2;
	uWorldY	-=((m_sizeMap.iHeight+1)%2)*_CELL_HEIGHT/2;
}

//--------------------------------------------------------------
void CGameMap::GetBgViewport(int& nViewportX, int& nViewportY)
{
	UINT uBgWorldX, uBgWorldY;
	this->GetBgWorldPos(uBgWorldX, uBgWorldY);
	
	nViewportX	=m_posView.x-uBgWorldX;
	nViewportY	=m_posView.y-uBgWorldY;
}

//--------------------------------------------------------------
void CGameMap::GetBgSize(CMySize& infoSize)
{
	CTerrainLayer* pLayer = this->GetTerrainLayer();
	if(pLayer)
		pLayer->GetPuzzleSize(infoSize);
	infoSize.iWidth = infoSize.iWidth * pLayer->GetPuzzleGrideSize() / CPuzzleBmp::GetNormalGrideSize();
	infoSize.iHeight = infoSize.iHeight * pLayer->GetPuzzleGrideSize() / CPuzzleBmp::GetNormalGrideSize();
	
}
//--------------------------------------------------------------
int CGameMap::GetDirection(int x0,int y0,int x1,int y1)
{
	int Dir;
	
	long Tan[4]	={-241,-41,41,241};
	long DeltaX	=x1-x0;
	long DeltaY	=y1-y0;
	
	if(DeltaX==0)
	{
		if(DeltaY>0)
			Dir	=0;
		else
			Dir	=4;
	}
	else if(DeltaY==0)
	{
		if(DeltaX>0)
			Dir	=6;
		else
			Dir	=2;
	}
	else
	{
		int Flag=abs(DeltaX)/DeltaX;
		
		DeltaY	*=(100*Flag);
		int i;
		for(i=0;i<4;i++)
			Tan[i]	*=abs(DeltaX);
		
		for(i=0;i<3;i++)
			if(DeltaY>=Tan[i] && DeltaY<Tan[i+1])
				break;
			
			//** note :
			//   i=0    ------- -241 -- -41
			//   i=1    ------- -41  --  41
			//   i=2    -------  41  -- 241
			//   i=3    -------  241 -- -241
			
			DeltaX=x1-x0;
			DeltaY=y1-y0;
			
			if(DeltaX>0)
			{
				if(i==0)			Dir	=5;
				else if(i==1)		Dir	=6;
				else if(i==2)		Dir	=7;
				else if(i==3)
				{
					if(DeltaY>0)	Dir	=0;
					else			Dir	=4;
				}
			}
			else
			{
				if(i==0)			Dir	=1;
				else if(i==1)		Dir	=2;
				else if(i==2)		Dir	=3;
				else if(i==3)
				{
					if(DeltaY>0)	Dir	=0;
					else			Dir	=4;
				}
			}
	}
	
	Dir	=(Dir+7)%8;
	return Dir;
}

//--------------------------------------------------------------
double CGameMap::GetPreciseDistance(int x0,int y0,int x1,int y1)
{
	long Value=(x0-x1)*(x0-x1)+(y0-y1)*(y0-y1);
	return sqrt(Value);
}

//--------------------------------------------------------------
//--- get distance with the size of role
int CGameMap::GetDistance(int x0,int y0,int x1,int y1,int size)
{
	// adjust caculate point
	if(size==2)
	{
		int Dir=GetDirection(x0,y0,x1,y1);
		MYASSERT (Dir>=0 && Dir<_MAX_DIRECTION);
		switch(Dir)
		{
		case 6:
		case 7:
		case 0:
			break;	//** no change
		case 1:
		case 2:

⌨️ 快捷键说明

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