📄 cgame.cpp
字号:
case 7:
strcpy(pRole->szName,"羊老肥");
break;
}
wsprintf(pRole->szNpcScriptFile,"pic\\battle\\bad%d.ini",rnd1);
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////
//与BOSS战斗
////////////////////////////////////////////////////////////////////////////////////////////
BOOL CGame::BossBattle(char*szBattleMap,char*szBattleMusic,char*szBossGraphIni,char*szBossRoleIni,char*szBossRoleSegment,BOOL bNeedWin)
{
LOA_ASSERT(szBattleMap);
LOA_ASSERT(szBattleMusic);
LOA_ASSERT(szBossGraphIni);
LOA_ASSERT(szBossRoleIni);
LOA_ASSERT(szBossRoleSegment);
STRenderRole*pBoss;
if(!LoadRole(szBossRoleIni,szBossRoleSegment,&pBoss))
{
MsgBox("Boss Battle:Load Boss Data From File:%s,Segment:%s Failed!",szBossRoleIni,szBossRoleSegment);
return false;
}
FadeOut();
CBattle bat;
BOOL bRet=bat.InitBattle( this,szBattleMap,
szBattleMusic,
m_pPlayer->pRole,
NULL,
NULL,
0,
pBoss->pRole,
szBossGraphIni,
150,
NULL,
NULL,
0,
true,
bNeedWin);
delete pBoss;
return bRet;
}
////////////////////////////////////////////////////////////////////////////////////////////
//与随机敌人战斗
////////////////////////////////////////////////////////////////////////////////////////////
BOOL CGame::RandomBattle()
{
FadeOut();
srand(time(0));
CBattle bat;
CRole enemy1;
CRole enemy2;
CRole enemy3;
RandomEnemy(&enemy1);
RandomEnemy(&enemy2);
RandomEnemy(&enemy3);
char strBattleMap[256];
wsprintf(strBattleMap,"%s.battle.lmp",m_szCurrentMapName);
FILE*fp=fopen(strBattleMap,"rb");
if(fp)
{
fclose(fp);
}
else
{
strcpy(strBattleMap,"map\\battle_1.lmp");
}
return bat.InitBattle(this,strBattleMap,
"music\\battle1.wma",
m_pPlayer->pRole,
&enemy1,
enemy1.szNpcScriptFile,
50,
rand()%4?&enemy2:NULL,
enemy2.szNpcScriptFile,
150,
rand()%3?&enemy3:NULL,
enemy3.szNpcScriptFile,
250,
false,true);
}
////////////////////////////////////////////////////////////////////////////////////////////
//正常状态下的循环
////////////////////////////////////////////////////////////////////////////////////////////
int CGame::NormalStateLoop()
{
STMessage loamsg;
MSG msg;
while(m_bPlayGame)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
{
if(!GetMessage(&msg, NULL,0,0))
return msg.wParam;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else if(m_bActive)
{
//------------------------------------------------------------------//
//------------------------------------------------------------------//
m_pMsg->ExecuteMessage(); //执行消息队列中的消息
//------------------------------------------------------------------//
//------------------------------------------------------------------//
NpcRandomMove(); //npc随机移动
//------------------------------------------------------------------//
//------------------------------------------------------------------//
SortAndShowRole();
//-------------------------------------------------------------------//
//-------------------------------------------------------------------//
ComputeMapStartPos();
//对地图坐标作判断
//-----------------------------------------------------------------//
BYTE key=this->m_pKeyBoard->ReadLastBufferKey(50); //得到输入的键
if(key==DIK_ESCAPE) //如果是退出游戏的消息
{
if(m_pKeyBoard->WaitKey(DIK_ESCAPE,100))
{
m_pSurfScene->BltFast(0,0,m_pDisplay->GetBackBuffer(),NULL,false);
CGameMenu menu;
if(-1==menu.MenuLoop(this,LOA_MENU_STYLE_MAIN_MENU))
{
LoadGame(this->m_nCurrentSaveFileIndex);
continue;
}
}
}
//test
// else if(key==DIK_F1)
// {
// ShowShop("data\\shop.dat","shop1");
// continue;
// }
else if(key==DIK_F4)
{
if(-1==RandomBattle())
ShowBeginMenu();
continue;
}
//end test
else if(key==DIK_RETURN)
{
if(m_pKeyBoard->WaitKey(DIK_RETURN,100))
{
}
}
else if(key==DIK_SPACE)
{
if(m_pKeyBoard->WaitKey(DIK_SPACE,150))
{
if(m_pPlayer->pRole->bOffsiteOk) //如果移动完成
{
int FaceToX=m_pPlayer->pRole->x>>5;
int FaceToY=m_pPlayer->pRole->y>>5;
int nNpcDir=LOA_ROLE_DIR_DOWN;
switch(m_pPlayer->pRole->nDirection)
{
case LOA_ROLE_DIR_UP:
{
FaceToY--;
nNpcDir=LOA_ROLE_DIR_DOWN;
}
break;
case LOA_ROLE_DIR_DOWN:
{
FaceToY++;
nNpcDir=LOA_ROLE_DIR_UP;
}
break;
case LOA_ROLE_DIR_LEFT:
{
FaceToX--;
nNpcDir=LOA_ROLE_DIR_RIGHT;
}
break;
case LOA_ROLE_DIR_RIGHT:
{
FaceToX++;
nNpcDir=LOA_ROLE_DIR_LEFT;
}
break;
}
if(FaceToX>=0 && FaceToX<m_pMap->m_head.Width
&& FaceToY>=0 && FaceToY<m_pMap->m_head.Height) //如果面向的方向的位置合法
{
DWORD dwTempAddr=m_pNpcList->GetCurAddress();
m_pNpcList->ResetToHead();
STRenderRole*pNpcTemp=NULL;
BOOL bTalked=false;
while(m_pNpcList->GetAndMovePointToNext(pNpcTemp))
{
int NpcTileX=pNpcTemp->pRole->x%32 > 16 ? (pNpcTemp->pRole->x>>5)+1:pNpcTemp->pRole->x>>5;
int NpcTileY=pNpcTemp->pRole->y%32 > 16 ? (pNpcTemp->pRole->y>>5)+1:pNpcTemp->pRole->y>>5;
if(FaceToX==NpcTileX && FaceToY==NpcTileY)//如果当前位置有NPC
{
m_pNpcList->SetCurAddress(dwTempAddr);
bTalked=true;
int OldDir=pNpcTemp->pRole->nDirection;
pNpcTemp->pRole->nDirection=nNpcDir;
if(pNpcTemp->pRole->bStandNPC==false)
{
pNpcTemp->pRole->bStandNPC=true;
ExecuteScript(pNpcTemp->pRole->szNpcScriptFile);
pNpcTemp->pRole->bStandNPC=false;
pNpcTemp->pRole->nDirection=OldDir;
}
else
{
ExecuteScript(pNpcTemp->pRole->szNpcScriptFile); //运行NPC脚本文件
}
break;
}
}
if(bTalked)
{
continue;
}
}//if face
}//if offok
}//if waitkey
}//if key==dik_space
else if(key==DIK_UP)
{
int iRev=m_pPlayer->pRole->JudgeAndMoveUp(this);
if(-1==iRev)
{
ShowBeginMenu();
continue;
}
else if(0==iRev)
continue;
//
}
else if(key==DIK_DOWN)
{
int iRev=m_pPlayer->pRole->JudgeAndMoveDown(this);
if(-1==iRev)
{
ShowBeginMenu();
continue;
}
else if(0==iRev)
continue;
}
else if(key==DIK_LEFT)
{
int iRev=m_pPlayer->pRole->JudgeAndMoveLeft(this);
if(-1==iRev)
{
ShowBeginMenu();
continue;
}
else if(0==iRev)
continue;
}
else if(key==DIK_RIGHT)
{
int iRev=m_pPlayer->pRole->JudgeAndMoveRight(this);
if(-1==iRev)
{
ShowBeginMenu();
continue;
}
else if(0==iRev)
continue;
}
else
{
}
/*
BOOL bDown=this->m_pMouse->IsLeftButtonDown();
if(bDown)
{
if(m_pPlayer->pRole->bOffsiteOk)
{
POINT pt;
::GetCursorPos(&pt);
::ScreenToClient(m_hWnd,&pt);
pt.x>>=5;
pt.y>>=5;
m_pPlayer->pRole->bMoving=FALSE;
m_pPlayer->pRole->MoveTo(m_pLog,this,pt.x+(this->m_pRenderMap->m_xStart>>5),pt.y+(this->m_pRenderMap->m_yStart>>5));
}
}
*/
if(m_pPlayer->pRole->bMoving)
{
m_pPlayer->pRole->MoveTo(m_pLog,this,m_pPlayer->pRole->OldDestPosX,m_pPlayer->pRole->OldDestPosY);
}
else
{
if(key!=DIK_RIGHT && key!=DIK_LEFT && key!=DIK_UP && key!=DIK_DOWN)
{
RECT rect={0,0,m_pMap->m_head.Width<<5,m_pMap->m_head.Height<<5};
if(!m_pPlayer->pRole->bOffsiteOk)
m_pPlayer->pRole->MoveRole(&rect,this);
else
m_pPlayer->pRole->MoveAni();
}
}
UpdateScreen();
}
else
WaitMessage();
}
return 1;
}
int CGame::BattleStateLoop()
{
MSG msg;
while(m_nCurrentGameState==LOA_GAME_STATE_BATTLE)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
{
if(!GetMessage(&msg, NULL,0,0))
return msg.wParam;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else if(m_bActive)
{
m_pMsg->ExecuteMessage();
UpdateScreen();
}
else
WaitMessage();
}
return 1;
}
////////////////////////////////////////////////////////////////////////////////////////////
//主循环
////////////////////////////////////////////////////////////////////////////////////////////
int CGame::MainLoop()
{
char szMagicDataFile[]="data\\magic.dat";
if(!m_pGameData->CreateMagicDataBase(this,szMagicDataFile,&m_pdbMagic))
{
m_pLog->WriteMessage("初始化魔法数据库...Failed!\n");
return false;
}
m_pLog->WriteMessage("初始化魔法数据库...OK!\n");
char szEffectDataFile[]="data\\effect.dat";
if(!m_pGameData->CreateEffectDataBase(this,szEffectDataFile,&m_pdbEffect))
{
m_pLog->WriteMessage("初始化特效数据库...Failed!\n");
}
m_pLog->WriteMessage("初始化特效数据库...OK!\n");
m_bPlayGame=true;
SendMessage(m_hWnd,UM_SETGAME,(WPARAM)this,0);
m_bActive=true;
if(!m_pMapSurface->Init(m_pDisplay,m_pLog))
{
m_pLog->WriteMessage("Init Map Surface Failed!\n");
return false;
}
m_pMapSurface->ShowAniTileToAniSurface(); //更新动态图块
m_nCurrentGameState=LOA_GAME_STATE_NORMAL;
ShowBeginMenu();
// LoadMap("map\\lanhome1.lmp");
MSG msg;
while(m_bPlayGame)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
{
if(!GetMessage(&msg, NULL,0,0))
return msg.wParam;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else if(m_bActive)
{
switch(m_nCurrentGameState)
{
case LOA_GAME_STATE_NORMAL:
{
NormalStateLoop();
}
break;
case LOA_GAME_STATE_BATTLE:
{
BattleStateLoop();
}
break;
case LOA_GAME_STATE_LOGO:
{
LogoLoop();
}
break;
case LOA_GAME_STATE_CREDITS:
{
ShowCreditsLoop();
}
break;
}
}
else
WaitMessage();
}
return 1;
}
////////////////////////////////////////////////////////////////////////////////////////////
//释放
////////////////////////////////////////////////////////////////////////////////////////////
void CGame::Release()
{
//游戏退出,交给Windows处理,嘿嘿嘿嘿...
}
////////////////////////////////////////////////////////////////////////////////////////////
//显示FPS
////////////////////////////////////////////////////////////////////////////////////////////
void CGame::ShowFPS()
{
LockFPS();
static DWORD fpsoldTime=timeGetTime();
static int frames=0,fps=0;
if(!m_bShowFPS)
return;
if(timeGetTime()-fpsoldTime<1000)
{
frames++;
}
else
{
fpsoldTime=timeGetTime();
fps=frames;
frames=0;
}
char tmp[32];
memset(tmp,0,32);
wsprintf(tmp,"fps:%d",fps);
HDC hdc;
m_pDisplay->GetBackBuffer()->GetDC(&hdc);
SetTextColor(hdc,RGB(255,255,255));
SetBkMode(hdc,TRANSPARENT);
::TextOut(hdc,10,10,tmp,strlen(tmp));
if(this->m_bRandomBattle)
wsprintf(tmp,"踩地雷模式:开");
else
wsprintf(tmp,"踩地雷模式:关");
::TextOut(hdc,10,30,tmp,strlen(tmp));
m_pDisplay->GetBackBuffer()->ReleaseDC(hdc);
}
///////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -