📄 3dgamemap-back.cpp
字号:
return;
}
//---------------------------------------------------------------------------//
void CGameMap::Cell2Map( int nCellX, int nCellY, int &nMapX, int &nMapY )
{
int nWorldX, nWorldY, nBgX, nBgY ;
g_objGameMap.Cell2World( nCellX, nCellY, nWorldX, nWorldY ) ;
g_objGameMap.World2Bg( nWorldX, nWorldY, nBgX, nBgY ) ;
CMySize BgSize ;
g_objGameMap.GetBgSize( BgSize ) ;
nMapX = nBgX ;
nMapY = BgSize.iHeight - nBgY ;
nMapY /= 32 ;
nMapX /= 64 ;
}
//---------------------------------------------------------------------------//
void CGameMap::Map2Cell( int nMapX, int nMapY, int &nCellX, int &nCellY )
{
int nWorldX, nWorldY, nBgX, nBgY ;
nBgX = nMapX * 64 ;
nBgY = nMapY * 32 ;
CMySize BgSize ;
g_objGameMap.GetBgSize( BgSize ) ;
nBgY = BgSize.iHeight - nBgY ;
g_objGameMap.Bg2World( nBgX, nBgY, nWorldX, nWorldY ) ;
g_objGameMap.World2Cell( nWorldX, nWorldY, nCellX, nCellY ) ;
}
//---------------------------------------------------------------------------//
void CGameMap::GetMapSize(CMySize& infoSize)
{
infoSize.iWidth =m_sizeMap.iWidth;
infoSize.iHeight =m_sizeMap.iHeight;
}
//---------------------------------------------------------------------------//
void CGameMap::SetMapSize(CMySize infoSize)
{
// set info ...
m_sizeMap.iWidth =infoSize.iWidth;
m_sizeMap.iHeight =infoSize.iHeight;
m_posOrigin.x =_CELL_WIDTH*m_sizeMap.iWidth/2;
m_posOrigin.y =_CELL_HEIGHT/2;
CMySize sizeBmp;
this->GetMapSize(sizeBmp);
CMyPos posCenter;
posCenter.x =m_sizeMap.iWidth*_CELL_WIDTH/2;
posCenter.y =m_sizeMap.iHeight*_CELL_HEIGHT/2;
m_szieWorld.iWidth = m_sizeMap.iWidth * _CELL_WIDTH;
m_szieWorld.iHeight = m_sizeMap.iHeight * _CELL_HEIGHT;
this->CreateCell();
m_setCellAStar.clear();
this->ClearNodeSet();
}
//---------------------------------------------------------------------------//
void CGameMap::GetWorldSize(CMySize& infoSize)
{
infoSize = m_szieWorld;
}
//---------------------------------------------------------------------------//
void CGameMap::GetViewPos(CMyPos& posShow)
{
posShow = m_posView;
}
//---------------------------------------------------------------------------//
CellInfo* CGameMap::GetCell(int iPosX,int iPosY)
{
if(iPosX >= m_sizeMap.iWidth)
return NULL;
if(iPosY >= m_sizeMap.iHeight)
return NULL;
if(iPosX < 0)
return NULL;
if(iPosY < 0)
return NULL;
/*
if(m_sizeMap.iWidth*iPosY+iPosX < 0)
return NULL;
if(m_sizeMap.iWidth*iPosY+iPosX >= m_sizeMap.iWidth*m_sizeMap.iHeight)
return NULL;
*/
if(!m_pCellInfo)
return NULL;
return m_pCellInfo+m_sizeMap.iWidth*iPosY+iPosX;
}
//---------------------------------------------------------------------------//
int CGameMap::GetExitIndex(CMyPos posCell)
{
CTerrainLayer* pLayer = this->GetTerrainLayer();
if(pLayer)
{
return pLayer->GetExitIndex(posCell);
}
return -1;
}
//---------------------------------------------------------------------------//
BOOL CGameMap::Create(DWORD idDoc)
{
// load map
char szSection[256] = "";
sprintf(szSection, "Map%u", idDoc);
char szMapFile[_MAX_FNAME] = "";
MYASSERT (::IniStrGet("ini/GameMap.ini", szSection, "File", szMapFile));
MYASSERT( ::IniStrGet("ini/GameMap.ini", szSection, "Name", m_szMapName ) ) ;
MYASSERT( ::IniDataGet("ini/GameMap.ini", szSection, "Snow", m_bSnow ) ) ;
int dwAlpha=255, dwRed=255, dwGreen=255, dwBlue=255;
::IniDataGet("ini/GameMap.ini", szSection, "Alpha", dwAlpha);
::IniDataGet("ini/GameMap.ini", szSection, "Red", dwRed);
::IniDataGet("ini/GameMap.ini", szSection, "Green", dwGreen);
::IniDataGet("ini/GameMap.ini", szSection, "Blue", dwBlue);
DWORD dwColor = (dwAlpha << 24) + (dwRed << 16) + (dwGreen << 8) + dwBlue;
g_objGameMap.SetARGB(dwColor);
if (!this->LoadDataMap(szMapFile))
{
::ErrorMsg("game map %s load failed.", szMapFile);
return false;
}
g_objPlayerSet.DelAllPlayer(true);
g_objHero.m_objBoothManager.Close();
this->AddInteractiveObj(&g_objHero);
int nAmount = g_objPlayerSet.GetPlayerAmount();
for(int i = 0; i < nAmount; i ++)
{
CPlayer* pPlayer = g_objPlayerSet.GetPlayerByIndex(i);
if(pPlayer)
this->AddInteractiveObj(pPlayer);
}
m_idDoc = idDoc;
::PostCmd(CMD_CHANGE_MAP);
g_objHero.LoadMusicInfo();
return true;
}
//---------------------------------------------------------------------------//
void CGameMap::SaveTextMap(char* pszFileName)
{
if(!pszFileName)
return;
FILE* fp = fopen(pszFileName, "w");
if(!fp)
return;
char szCurDir[_MAX_PATH];
char szFileName[_MAX_PATH];
GetCurrentDirectory(_MAX_PATH,szCurDir);
CTerrainLayer* pTerrainLayer = this->GetTerrainLayer();
MYASSERT(pTerrainLayer);
char szPuzzleFile[_MAX_PATH] = "";
strcpy(szPuzzleFile, pTerrainLayer->GetPuzleFileName());
int nLen = strlen(szCurDir);
for(int k = 0; k < _MAX_PATH-nLen-1; k++)
{
szFileName[k] = szPuzzleFile[nLen+k+1];
}
// save puzzle file ...
fprintf(fp, "PuzzleFile=%s\n", szFileName);
// save map size ...
CMySize infoSize;
g_objGameMap.GetMapSize(infoSize);
fprintf(fp, "Width=%d\n", infoSize.iWidth);
fprintf(fp, "Height=%d\n", infoSize.iHeight);
int i, j;
// save mask ...
// save terrain ...
// save altitude ..
for(i = 0; i < infoSize.iHeight; i++)
{
char szTemp[1024];
for(j = 0; j < infoSize.iWidth; j++)
{
CellInfo* pCellInfo = g_objGameMap.GetCell(j, i);
LayerInfo* pLayerInfo = &pCellInfo->m_infoLayer;
if(pLayerInfo)
{
sprintf(szTemp, "Cell[%d,%d]=[%u,%d,%d]",
j, i, pLayerInfo->usMask, pLayerInfo->usTerrain,
pLayerInfo->sAltitude);
fprintf(fp, "%s\n", szTemp);
}
}
}
pTerrainLayer->SaveTextPassage(fp);
CInteractiveLayer* pInteractiveLayer = this->GetInteractiveLayer();
if(pInteractiveLayer)
{
pInteractiveLayer->SaveTextLayer(fp);
}
// save others ...
int nAmount = this->GetLayerAmount();
// write amount ...
fprintf(fp, "========Other Layers Amount=%d========\n", this->GetLayerAmount()-3);
for(i = 0; i < nAmount; i++)
{
CMapLayer* pLayer = this->GetLayerByIndex(i);
if(!pLayer)
continue;
switch(pLayer->GetType())
{
case CMapLayer::LAYER_SCENE:
{
// save index type
fprintf(fp, "========Index=%d, Type=%d========\n", i, pLayer->GetType());
CSceneLayer* pSceneLayer = (CSceneLayer*)pLayer;
pSceneLayer->SaveTextLayer(fp);
}
break;
}
}
//pTerrainLayer->SaveTextRegion(fp);
fclose(fp);
}
//---------------------------------------------------------------------------//
void CGameMap::SaveDataMap(char* pszFileName)
{
if(!pszFileName)
return;
FILE* fp = fopen(pszFileName, "wb");
if(!fp)
return;
// header ...
DWORD dwVersion = 1004;
DWORD dwData = 0;
fwrite(&dwVersion, sizeof(DWORD), 1, fp);
fwrite(&dwData, sizeof(DWORD), 1, fp);
// save puzzle file
char szCurDir[_MAX_PATH];
char szFileName[_MAX_PATH];
GetCurrentDirectory(_MAX_PATH,szCurDir);
CTerrainLayer* pTerrainLayer = this->GetTerrainLayer();
MYASSERT(pTerrainLayer);
char szPuzzleFile[_MAX_PATH] = "";
strcpy(szPuzzleFile, pTerrainLayer->GetPuzleFileName());
int nLen = strlen(szCurDir);
for(int k = 0; k < _MAX_PATH-nLen-1; k++)
{
szFileName[k] = szPuzzleFile[nLen+k+1];
}
fwrite(szFileName, sizeof(char), _MAX_PATH, fp);
// save map size ...
fwrite(&m_sizeMap.iWidth, sizeof(int), 1, fp);
fwrite(&m_sizeMap.iHeight, sizeof(int), 1, fp);
int i, j;
// save mask ...
// save terrain ...
// save altitude ..
for(i = 0; i < m_sizeMap.iHeight; i++)
{
DWORD dwCheckData = 0;
for(j = 0; j < m_sizeMap.iWidth; j++)
{
CellInfo* pCellInfo = g_objGameMap.GetCell(j, i);
LayerInfo* pLayerInfo = &pCellInfo->m_infoLayer;
if(pLayerInfo)
{
unsigned short usData = pLayerInfo->usMask;
unsigned short usTerrain = pLayerInfo->usTerrain;
short sAltiute = pLayerInfo->sAltitude;
if(pLayerInfo->sAltitude > 0)
int kkk = 0;
fwrite(&usData, sizeof(unsigned short), 1, fp);
fwrite(&usTerrain, sizeof(unsigned short), 1, fp);
fwrite(&sAltiute, sizeof(short), 1, fp);
dwCheckData += pLayerInfo->usMask * (pLayerInfo->usTerrain+i+1) + (pLayerInfo->sAltitude+2)*(j+1+pLayerInfo->usTerrain);
}
}
fwrite(&dwCheckData, sizeof(DWORD), 1, fp);
}
pTerrainLayer->SaveDataPassage(fp);
//pTerrainLayer->SaveDataRegion(fp);
CInteractiveLayer* pInteractiveLayer = this->GetInteractiveLayer();
if(pInteractiveLayer)
{
pInteractiveLayer->SaveDataLayer(fp);
}
// save others ...
int nAmount = this->GetLayerAmount();
// save Other Amount ...
int nOtherAmount = nAmount-3;
fwrite(&nOtherAmount, sizeof(int), 1, fp);
for(i = 0; i < nAmount; i++)
{
CMapLayer* pLayer = this->GetLayerByIndex(i);
if(!pLayer)
continue;
switch(pLayer->GetType())
{
case CMapLayer::LAYER_SCENE:
{
// save index
fwrite(&i, sizeof(int), 1, fp);
// save type
int nType = pLayer->GetType();
fwrite(&nType, sizeof(int), 1, fp);
CSceneLayer* pSceneLayer = (CSceneLayer*)pLayer;
pSceneLayer->SaveDataLayer(fp);
}
break;
}
}
fclose(fp);
}
//---------------------------------------------------------------------------//
BOOL CGameMap::LoadTextMap(char* pszFileName)
{
if(!pszFileName)
return false;
FILE* fp = fopen(pszFileName, "r");
if(!fp)
return false;
this->Reset();
this->DestroyCell();
// get puzzle file name ...
char szCurDir[_MAX_PATH];
char szFileName[_MAX_PATH];
char szFullFileName[_MAX_PATH];
GetCurrentDirectory(_MAX_PATH,szCurDir);
// begin read ...
fscanf(fp, "PuzzleFile=%s\n", szFileName);
sprintf(szFullFileName, "%s\\%s", szCurDir, szFileName);
CTerrainLayer* pLayer = this->GetTerrainLayer();
if(!pLayer)
return false;
pLayer->LoadPuzzle(szFullFileName);
CMySize infoSize = {0, 0};
fscanf(fp, "Width=%i\n", &infoSize.iWidth);
fscanf(fp, "Height=%i\n", &infoSize.iHeight);
this->SetMapSize(infoSize);
int i, j;
for(i = 0; i < infoSize.iHeight; i++)
{
for(j = 0; j < infoSize.iWidth; j++)
{
CellInfo* pCellInfo = g_objGameMap.GetCell(j, i);
LayerInfo* pLayerInfo = &pCellInfo->m_infoLayer;
if(pLayer)
{
int nPosX, nPosY;
DWORD dwMask;
int nTer, nAltitude;
fscanf(fp, "Cell[%d,%d]=[%u,%d,%d]\n",
&nPosX, &nPosY, &dwMask, &nTer, &nAltitude);
pLayerInfo->usMask = dwMask;
pLayerInfo->usTerrain = nTer;
pLayerInfo->sAltitude = nAltitude;
}
}
}
pLayer = this->GetTerrainLayer();
if(pLayer)
{
pLayer->LoadTextPassage(fp);
}
CInteractiveLayer* pInteractiveLayer = this->GetInteractiveLayer();
if(pInteractiveLayer)
{
pInteractiveLayer->LoadTextLayer(fp);
}
// other layers
// read amount ...
int nAmount;
fscanf(fp, "========Other Layers Amount=%d========\n", &nAmount);
for(i = 0; i < nAmount; i++)
{
int nIndex, nType;
fscanf(fp, "========Index=%d, Type=%d========\n", &nIndex, &nType);
switch(nType)
{
case CMapLayer::LAYER_SCENE:
{
CSceneLayer* pLayer = new CSceneLayer;
if(!pLayer)
return false;
m_setMapLayer.insert(m_setMapLayer.begin()+nIndex,pLayer);
pLayer->LoadTextLayer(fp);
}
break;
}
}
/*pLayer = this->GetTerrainLayer();
if(pLayer)
{
pLayer->LoadTextRegion(fp);
}
*/
fclose(fp);
return true;
}
//---------------------------------------------------------------------------//
BOOL CGameMap::LoadDataMap(char* pszFileName)
{
if(!pszFileName)
return false;
FILE* fp = fopen(pszFileName, "rb");
if(!fp)
return false;
this->Reset();
this->DestroyCell();
DWORD dwVersion;
DWORD dwData;
fread(&dwVersion, sizeof(DWORD), 1, fp);
fread(&dwData, sizeof(DWORD), 1, fp);
// get puzzle file name ...
char szCurDir[_MAX_PATH];
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);
// strcpy(szFullFileName,szFileName);
CTerrainLayer* pLayer = this->GetTerrainLayer();
if(!pLayer)
return false;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -