?? map.cpp
字號:
#include "map.h"
#include <algorithm>
CMap::CMap()
: m_pbmGround(NULL)
, m_pbmExplosion(NULL)
{
ASLFileLoader loader;
loader.SetDirApp("Pic");
m_pbmExplosion = new ASLBitmap();
m_pbmExplosion->LoadBMP(loader.Load("Explosion.bmp"));
m_pbmExplosion->SetBlock(14, 5);
m_pbmExplosion->SetColorKey(clFuchsia);
for (int i = 0; i < ROLE_NUM; ++i)
{
m_vRoleSeq[i] = i;
}
}
CMap::~CMap()
{
ReleaseMap();
SAFE_DELETE(m_pbmGround);
SAFE_DELETE(m_pbmExplosion);
}
CMap& CMap::Instance(void)
{
static CMap instance;
return instance;
}
bool Greater(int r1, int r2)
{
if (ROLE[r1].GetY() != ROLE[r2].GetY())
{
return ROLE[r1].GetY() < ROLE[r2].GetY();
}
else
{
return ROLE[r1].GetOffsetY() < ROLE[r2].GetOffsetY();
}
}
void CMap::Update(float fDelta)
{
// 更新活動物體
for (int i = 0; i < CELL_NUM; ++i)
{
if (m_vCell[i].pItem != NULL)
{
m_vCell[i].pItem->Update(fDelta);
}
if (m_vCell[i].pBlindage != NULL)
{
m_vCell[i].pBlindage->Update(fDelta);
}
}
// 對角色繪制順序排序
std::sort(&m_vRoleSeq[0], &m_vRoleSeq[ROLE_NUM], Greater);
// 更新爆炸效果
#define EXP m_vCell[i].Explosion
for (int i = 0; i < CELL_NUM; ++i)
{
if (EXP.Status == 1)
{
EXP.Timer--;
if (EXP.Timer == 0)
{
EXP.Status++;
}
}
else if (EXP.Status > 1)
{
if (EXP.Dr == drMiddle && EXP.Status >= 12)
{
EXP.Status = 0;
}
else if (EXP.Status >= 20)
{
EXP.Status = 0;
if (EXP.Gift != 0)
{
m_vCell[i].pItem = FACTORY.MakeFixedItem(EXP.Gift);
m_vCell[i].pItem->SetPosition(i / CELL_X_ALL, i % CELL_X_ALL);
}
}
else
{
EXP.Status++;
}
}
}
#undef EXP
}
void CMap::Draw(void) const
{
#define CELL m_vCell[i * CELL_X_ALL + j]
// 畫地面
DrawGround();
for (int i = 1; i <= CELL_Y; ++i)
{
// 畫普通物體
for (int j = CELL_X; j >= 1; --j)
{
if (CELL.Type >= itFirm && CELL.Type <= itBox && CELL.Blind == btNone)
{
if (CELL.pItem != NULL)
{
CELL.pItem->Draw();
}
}
else if (CELL.Blind >= btBush)
{
ASSERT(CELL.pBlindage != NULL);
CELL.pBlindage->Draw();
}
}
// 畫寶物和泡泡
for (int j = 1; j <= CELL_X; ++j)
{
if (CELL.Type == itGift || CELL.Type == itPopo && CELL.Blind == btNone)
{
ASSERT(CELL.pItem != NULL);
CELL.pItem->Draw();
}
}
// 爆炸
for (int j = 1; j <= CELL_X; ++j)
{
if (MAP[i][j].Explosion.Status > 1 && MAP[i][j].Blind != btTent)
{
DrawExplosion(i, j);
}
}
// 畫主角
for (int j = 0; j < ROLE_NUM; ++j)
{
int nX = ROLE[m_vRoleSeq[j]].GetX();
int nY = ROLE[m_vRoleSeq[j]].GetY();
if (nY == i && m_vCell[i * CELL_X_ALL + nX].Blind == btNone)
{
ROLE[m_vRoleSeq[j]].Draw();
}
}
}
#undef CELL
}
void CMap::LoadMap(ASLFile *pFile)
{
ASLFileLoader loader;
ASLIni ini;
int nMapType;
char szBuffer[80];
Cell *pCell = m_vCell;
ASSERT(pFile != NULL);
// 加載地圖原型
pFile->Read(&nMapType, sizeof(int));
sprintf(szBuffer, "MapType%d.ini", nMapType);
loader.SetDirApp("Ini");
FACTORY.LoadMapPrototype(loader.Load(szBuffer));
// 加載地面圖片
ini.Load(loader.Load(szBuffer));
ini.SetSection("Setup");
loader.SetDirApp("Pic");
SAFE_DELETE(m_pbmGround);
m_pbmGround = new ASLBitmap();
m_pbmGround->LoadBMP(loader.Load(ini.SafeReadString("Ground").c_str()));
m_pbmGround->SetBlock(ini.SafeReadInteger("GroundBlock"), 1);
// 釋放原地圖塊內容
ReleaseMap();
// 讀取地圖的每塊內容
for (int i = 0; i < CELL_NUM; ++i)
{
// 讀取屬性
pFile->Read(pCell, 4);
// 加載物體
if (pCell->Blind >= btBush)
{
pCell->pBlindage = (CBlindage*)FACTORY.MakeMapItem(pCell->ID);
pCell->pBlindage->SetPosition(i / CELL_X_ALL, i % CELL_X_ALL);
}
else if (pCell->ID > 0)
{
pCell->pItem = FACTORY.MakeMapItem(pCell->ID);
pCell->pItem->SetPosition(i / CELL_X_ALL, i % CELL_X_ALL);
}
// 初始化爆炸效果
pCell->Explosion.Status = 0;
// 進入下一格
++pCell;
}
SAFE_DELETE(pFile);
}
void CMap::ReleaseMap(void)
{
for (int i = 0; i < CELL_NUM; ++i)
{
SAFE_DELETE(m_vCell[i].pItem);
SAFE_DELETE(m_vCell[i].pBlindage);
}
}
void CMap::DrawGround(void) const
{
int x, y;
if (m_pbmGround == NULL)
{
return;
}
for (int i = 1; i <= CELL_Y; ++i)
{
for (int j = 1; j <= CELL_X; ++j)
{
x = (j-1) * CELL_SIZE + SCENE_LEFT;
y = (i-1) * CELL_SIZE + SCENE_TOP;
m_pbmGround->Draw(SCREEN, x, y, m_vCell[i * CELL_X_ALL + j].Ground);
}
}
}
void CMap::DrawExplosion(int nCellY, int nCellX) const
{
#define EXP m_vCell[nCellY * CELL_X_ALL + nCellX].Explosion
int col;
int x = (nCellX-1) * CELL_SIZE + SCENE_LEFT;
int y = (nCellY-1) * CELL_SIZE + SCENE_TOP;
if (EXP.Dr == drMiddle)
{
col = EXP.Status % 4;
}
else
{
if (EXP.IsHead == 0)
{
if (EXP.Status == 2)
{
col = 0;
}
else if (EXP.Status < EXP.Dead) // Dead min = 12
{
col = (EXP.Status - 3) / 3 % 2 + 3;
}
else
{
col = EXP.Status - 7;
}
}
else
{
if (EXP.Status <= 11)
{
col = (EXP.Status - 2) / 3;
}
else
{
col = EXP.Status - 7;
}
}
}
m_pbmExplosion->Draw(SCREEN, x, y, EXP.Dr, col);
#undef EXP
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -