📄 cbattle.cpp
字号:
pRenderEffect->CreateTask(true,true,0);
m_pBattle->GetRender()->AddTask(pRenderEffect);
}
}
if(timeGetTime() - pRole->pAttackAni->dwOldTime > pRole->pAttackAni->dwDelay)
{
pRole->pAttackAni->dwOldTime=timeGetTime();
pRole->CurPosX+=pRole->pAttackAni->aXOffsite[pRole->pAttackAni->nCurrentFrame];
pRole->CurPosY+=pRole->pAttackAni->aYOffsite[pRole->pAttackAni->nCurrentFrame];
pRole->pAttackAni->nCurrentFrame++;
if(pRole->pAttackAni->nKeyFrame==pRole->pAttackAni->nCurrentFrame)
{
if(pRole->pAttackAni->pSound)
pRole->pAttackAni->pSound->Play(0,0);
}
if(pRole->pAttackAni->nCurrentFrame>=pRole->pAttackAni->nFrames)
{
pRole->pAttackAni->nCurrentFrame=0;
pRole->nCurrentState=LOA_BATTLE_STATE_WAIT;
pRole->CurPosX=pRole->ShowPosX;
pRole->CurPosY=pRole->ShowPosY;
}
}
}
break;
case LOA_BATTLE_STATE_BE_ATTACK:
{
if(!pRole->pBeAttackedAni)
break;
if(pRole->pBeAttackedAni->nCurrentFrame==0)
{
if(pRole->pBeAttackedAni->pSound)
pRole->pBeAttackedAni->pSound->Play(0,0);
}
m_pGame->GetDisplay()->ColorKeyBlt(pRole->CurPosX,
pRole->CurPosY,
pRole->pBeAttackedAni->pSurface->GetDDrawSurface(),
&pRole->pBeAttackedAni->aRect[pRole->pBeAttackedAni->nCurrentFrame]);
if(pRole->pBeAttackedAni->pEffect)
{
if(pRole->pBeAttackedAni->nKeyFrame==pRole->pBeAttackedAni->nCurrentFrame)
{
CRenderEffect*pRenderEffect=new CRenderEffect;
pRenderEffect->InitEffect(m_pGame,pRole->pBeAttackedAni->xEffectShow,pRole->pBeAttackedAni->yEffectShow,pRole->pBeAttackedAni->pEffect);
pRenderEffect->CreateTask();
m_pBattle->GetRender()->AddTask(pRenderEffect);
}
}
if(timeGetTime() - pRole->pBeAttackedAni->dwOldTime > pRole->pBeAttackedAni->dwDelay)
{
pRole->pBeAttackedAni->dwOldTime=timeGetTime();
pRole->CurPosX+=pRole->pBeAttackedAni->aXOffsite[pRole->pBeAttackedAni->nCurrentFrame];
pRole->CurPosY+=pRole->pBeAttackedAni->aYOffsite[pRole->pBeAttackedAni->nCurrentFrame];
pRole->pBeAttackedAni->nCurrentFrame++;
if(pRole->pBeAttackedAni->nCurrentFrame>=pRole->pBeAttackedAni->nFrames)
{
pRole->pBeAttackedAni->nCurrentFrame=0;
pRole->nCurrentState=LOA_BATTLE_STATE_WAIT;
pRole->CurPosX=pRole->ShowPosX;
pRole->CurPosY=pRole->ShowPosY;
}
}
}
break;
case LOA_BATTLE_STATE_DEFEND:
{
if(!pRole->pDefendAni)
break;
if(pRole->pDefendAni->nCurrentFrame==0)
{
if(pRole->pDefendAni->pSound)
pRole->pDefendAni->pSound->Play(0,0);
}
m_pGame->GetDisplay()->ColorKeyBlt(pRole->CurPosX,
pRole->CurPosY,
pRole->pDefendAni->pSurface->GetDDrawSurface(),
&pRole->pDefendAni->aRect[pRole->pDefendAni->nCurrentFrame]);
if(pRole->pDefendAni->pEffect)
{
if(pRole->pDefendAni->nKeyFrame==pRole->pDefendAni->nCurrentFrame)
{
CRenderEffect*pRenderEffect=new CRenderEffect;
pRenderEffect->InitEffect(m_pGame,pRole->pDefendAni->xEffectShow,pRole->pDefendAni->yEffectShow,pRole->pDefendAni->pEffect);
pRenderEffect->CreateTask();
m_pBattle->GetRender()->AddTask(pRenderEffect);
}
}
if(timeGetTime() - pRole->pDefendAni->dwOldTime > pRole->pDefendAni->dwDelay)
{
pRole->pDefendAni->dwOldTime=timeGetTime();
pRole->CurPosX+=pRole->pDefendAni->aXOffsite[pRole->pDefendAni->nCurrentFrame];
pRole->CurPosY+=pRole->pDefendAni->aYOffsite[pRole->pDefendAni->nCurrentFrame];
pRole->pDefendAni->nCurrentFrame++;
if(pRole->pDefendAni->nCurrentFrame>=pRole->pDefendAni->nFrames)
{
pRole->pDefendAni->nCurrentFrame=0;
pRole->nCurrentState=LOA_BATTLE_STATE_WAIT;
pRole->CurPosX=pRole->ShowPosX;
pRole->CurPosY=pRole->ShowPosY;
}
}
}
break;
case LOA_BATTLE_STATE_DEAD:
{
if(!pRole->pDeadAni)
{
pRole->bShow=false;
break;
}
if(pRole->pDeadAni->nCurrentFrame==0)
{
if(pRole->pDeadAni->pSound)
pRole->pDeadAni->pSound->Play(0,0);
}
m_pGame->GetDisplay()->ColorKeyBlt(pRole->CurPosX,
pRole->CurPosY,
pRole->pDeadAni->pSurface->GetDDrawSurface(),
&pRole->pDeadAni->aRect[pRole->pDeadAni->nCurrentFrame]);
if(pRole->pDeadAni->pEffect)
{
if(pRole->pDeadAni->nKeyFrame==pRole->pDeadAni->nCurrentFrame)
{
CRenderEffect*pRenderEffect=new CRenderEffect;
pRenderEffect->InitEffect(m_pGame,pRole->pDeadAni->xEffectShow,pRole->pDeadAni->yEffectShow,pRole->pDeadAni->pEffect);
pRenderEffect->CreateTask();
m_pBattle->GetRender()->AddTask(pRenderEffect);
}
}
if(timeGetTime() - pRole->pDeadAni->dwOldTime > pRole->pDeadAni->dwDelay)
{
pRole->pDeadAni->dwOldTime=timeGetTime();
pRole->CurPosX+=pRole->pDeadAni->aXOffsite[pRole->pDeadAni->nCurrentFrame];
pRole->CurPosY+=pRole->pDeadAni->aYOffsite[pRole->pDeadAni->nCurrentFrame];
pRole->pDeadAni->nCurrentFrame++;
if(pRole->pDeadAni->nCurrentFrame>=pRole->pDeadAni->nFrames)
{
pRole->pDeadAni->nCurrentFrame=0;
pRole->bShow=false;
pRole->nCurrentState=LOA_BATTLE_STATE_DEAD;
pRole->CurPosX=pRole->ShowPosX;
pRole->CurPosY=pRole->ShowPosY;
}
}
}
break;
}
}
void CRenderBattleRole::Render()
{
RenderRole(m_pPlayer);
for(int i=0;i<LOA_MAX_BATTLE_ENEMY;i++)
{
if(m_pEnemy[i]->bShow)
RenderRole(m_pEnemy[i]);
}
}
int CBattle::GameOver()
{
m_pGame->PlayMedia("music\\ssdead.wma",false);
m_pGame->ShowStaticPictureWithMessage("pic\\system\\gameover.jpg"," ");
return 1;
}
////////////////////////////////////////////////////////////////////////////////////////////
//敌人的AI
////////////////////////////////////////////////////////////////////////////////////////////
void CBattle::EnemyAI(int index)
{
switch(m_pEnemy[index]->pRole->Kidney) //根据角色性格来判断
{
case LOA_KIDNEY_ATTACK: //全力攻击类型
{
m_pEnemy[index]->m_RoleCmd.bToPlayer=true; //攻击玩家
if(0==rand()%6) //只有1/6的几率能使用2倍攻击力
m_pEnemy[index]->m_RoleCmd.iCmd=LOA_CMD_ATTACK_2; //2倍攻击力
else
m_pEnemy[index]->m_RoleCmd.iCmd=LOA_CMD_NORMAL_ATTACK;//普通攻击力
}
break;
case LOA_KIDNEY_DEFEND: //注重防御类型
{
if(m_pEnemy[index]->pRole->Hp< m_pEnemy[index]->pRole->MaxHp/4)//如果hp降到了1/4以内,考虑增加hp
{
int maxAddHp=0; //作为比较哪个东西能增加hp最多的变量
int indexGood=-1; //物品位置索引
for(int i=0;i<m_pEnemy[index]->pRole->GoodsNum;i++) //查找所有物品
{
if(0!=m_pEnemy[index]->pRole->GoodsData[i]) //如果这个位置有物品
{
STGood *pGood;
if(!m_pGame->GetGoodsDataBase()->FindData(m_pEnemy[index]->pRole->GoodsData[i],&pGood))
//如果再物品数据库中找不到物品数据
{
m_sLog.WriteMessage("Can't Get Goods(%d) 's Data In Goods DataBase!!!\n",m_pEnemy[index]->pRole->GoodsData[i]);
m_pEnemy[index]->m_RoleCmd.bToPlayer=true;
m_pEnemy[index]->m_RoleCmd.iCmd=LOA_CMD_NORMAL_ATTACK;
return;
}
if(pGood->AddHp>0) //如果该物品能增加hp,寻找能增加hp最多的物品
{
if(pGood->AddHp>maxAddHp)
{
maxAddHp=pGood->AddHp; //把最大值保留
indexGood=i; //保留索引
m_pEnemy[index]->m_RoleCmd.bToPlayer=false; //不是对玩家使用
m_pEnemy[index]->m_RoleCmd.iCmd=LOA_CMD_USE_GOOD;//指令为使用物品
m_pEnemy[index]->m_RoleCmd.iCmdParam=i; //使用了第i个位置的物品
m_pEnemy[index]->m_RoleCmd.nObject=index; //对自己使用
}
}
}
}
if(index!=-1)
{
if(maxAddHp>0) //如果已经找到合适的物品,策略完成
{
m_pEnemy[index]->pRole->DelGoods(indexGood); //删除此物品
return;
}
}
for(i=0;i<m_pEnemy[index]->pRole->MagicNum;i++) //如果没有合适的物品,尝试使用魔法
{
if(0 != m_pEnemy[index]->pRole->MagicData[i]) //如果这个位置有魔法,看看是否是增加hp的魔法
{
STMagic*pMagic; //这部分同理
if(!m_pGame->GetMagicDataBase()->FindData(m_pEnemy[index]->pRole->MagicData[i],&pMagic))
//如果再物品数据库中找不到魔法数据
{
m_sLog.WriteMessage("Can't Get Magic(%d) 's In From Magic DataBase!!!\n",m_pEnemy[index]->pRole->MagicData[i]);
m_pEnemy[index]->m_RoleCmd.bToPlayer=true;
m_pEnemy[index]->m_RoleCmd.iCmd=LOA_CMD_NORMAL_ATTACK;
return;
}
if(pMagic->nChangeHp>0) //如果该魔法能增加hp,寻找能增加hp最多的魔法
{
if(abs(pMagic->nChangeMp)>m_pEnemy[index]->pRole->Mp)//如果魔法点数不够
continue;
if(pMagic->nChangeHp>maxAddHp)
{
maxAddHp=pMagic->nChangeHp; //把最大值保留
m_pEnemy[index]->m_RoleCmd.bToPlayer=false; //不是对玩家使用
m_pEnemy[index]->m_RoleCmd.iCmd=LOA_CMD_MAGIC;//指令为使用魔法
m_pEnemy[index]->m_RoleCmd.iCmdParam=i; //使用了第i个位置的魔法
m_pEnemy[index]->m_RoleCmd.nObject=index; //对自己使用
}
}//if
}//if
}//for
if(maxAddHp>0) //如果已经找到合适的魔法,策略完成
{
return;
}
}
//如果hp还比较多,或则hp不足,但是找不到办法增加hp,就直接攻击
m_pEnemy[index]->m_RoleCmd.bToPlayer=true; //攻击玩家
if(0==rand()%8) //只有1/8的几率能使用2倍攻击力
m_pEnemy[index]->m_RoleCmd.iCmd=LOA_CMD_ATTACK_2; //2倍攻击力
else
m_pEnemy[index]->m_RoleCmd.iCmd=LOA_CMD_NORMAL_ATTACK;//普通攻击力
}
break;
case LOA_KIDNEY_MAGIC: //善用魔法类型
{
int attackMaxHp=0; //能攻击玩家最大hp的变量
for(int i=0;i<m_pEnemy[index]->pRole->MagicNum;i++) //尝试使用魔法
{
if(0 !=m_pEnemy[index]->pRole->MagicData[i]) //如果这个位置有魔法,看看是否是攻击的魔法
{
STMagic*pMagic; //这部分同理
if(!m_pGame->GetMagicDataBase()->FindData(m_pEnemy[index]->pRole->MagicData[i],&pMagic))
//如果再物品数据库中找不到魔法数据
{
m_sLog.WriteMessage("Can't Get Magic(%d) 's In From Magic DataBase!!!\n",m_pEnemy[index]->pRole->MagicData[i]);
m_pEnemy[index]->m_RoleCmd.bToPlayer=true;
m_pEnemy[index]->m_RoleCmd.iCmd=LOA_CMD_NORMAL_ATTACK;
return;
}
if(pMagic->nChangeHp<0) //如果该魔法能减少hp,寻找能减少hp最多的魔法
{
if(pMagic->nChangeMp>m_pEnemy[index]->pRole->Mp)//如果魔法点数不够
continue;
if(pMagic->nChangeHp<attackMaxHp)
{
attackMaxHp=pMagic->nChangeHp; //把最大(负)值保留
m_pEnemy[index]->m_RoleCmd.bToPlayer=true; //对玩家使用
m_pEnemy[index]->m_RoleCmd.iCmd=LOA_CMD_MAGIC;//指令为使用魔法
m_pEnemy[index]->m_RoleCmd.iCmdParam=i; //使用了第i个位置的魔法
}
}//if
}//if
}//for
if(attackMaxHp<0) //如果找到了策略
{
return;
}
//否则,没办法使用魔法,就直接攻击
m_pEnemy[index]->m_RoleCmd.bToPlayer=true; //攻击玩家
if(0==rand()%8) //只有1/8的几率能使用2倍攻击力
m_pEnemy[index]->m_RoleCmd.iCmd=LOA_CMD_ATTACK_2; //2倍攻击力
else
m_pEnemy[index]->m_RoleCmd.iCmd=LOA_CMD_NORMAL_ATTACK;//普通攻击力
}
break;
case LOA_KIDNEY_ESCAPE: //逃命类型
if(m_pPlayer->pRole->Attack>m_pEnemy[index]->pRole->Hp+m_pEnemy[index]->pRole->Defend)//如果玩家的攻击力比敌人的hp加上敌人的防御还要高,则逃跑
{
m_pEnemy[index]->m_RoleCmd.iCmd=LOA_CMD_ESCAPE;
m_pEnemy[index]->m_RoleCmd.bToPlayer=false;
return;
}
if(m_pEnemy[index]->pRole->Hp< m_pEnemy[index]->pRole->MaxHp/4)//如果hp降到了1/4以内,考虑逃跑
{
m_pEnemy[index]->m_RoleCmd.iCmd=LOA_CMD_ESCAPE;
m_pEnemy[index]->m_RoleCmd.bToPlayer=false;
return;
}
//否则,就直接攻击
m_pEnemy[index]->m_RoleCmd.bToPlayer=true; //攻击玩家
if(0==rand()%8) //只有1/8的几率能使用2倍攻击力
m_pEnemy[index]->m_RoleCmd.iCmd=LOA_CMD_ATTACK_2; //2倍攻击力
else
m_pEnemy[index]->m_RoleCmd.iCmd=LOA_CMD_NORMAL_ATTACK;//普通攻击力
}
}
BOOL CRenderBattleText::InitRender(CGame*pGame,int x,int y,DWORD dwColor,int speed,int showtime,char*txt,...)
{
LOA_ASSERT(pGame);
m_pGame=pGame;
m_pFont=new CFont;
m_pFont->CreateFont(20);
m_nSpeed=speed;
m_nShowTime=showtime;
m_x=x;
m_y=y;
m_dwColor=dwColor;
m_szStr=new char[1024];
memset(m_szStr,0,1024);
va_list vl;
va_start(vl,txt);
wvsprintf(m_szStr,txt,vl);
va_end(vl);
m_dwOldTime=timeGetTime();
return true;
}
BOOL LOA_RENDER_API CRenderBattleText::IsRenderComplete()
{
if(timeGetTime()-m_dwOldTime > (DWORD)m_nShowTime)
return true;
return false;
}
void LOA_RENDER_API CRenderBattleText::Release()
{
if(m_szStr)
{
delete [] m_szStr;
m_szStr=NULL;
}
if(m_pFont)
{
delete m_pFont;
m_pFont=NULL;
}
}
void LOA_RENDER_API CRenderBattleText::Render()
{
HDC hdc;
m_pGame->GetDisplay()->GetBackBuffer()->GetDC(&hdc);
::SelectObject(hdc,m_pFont->GetFont());
::SetBkMode(hdc,TRANSPARENT);
::SetTextColor(hdc,m_dwColor);
::TextOut(hdc,m_x,m_y,m_szStr,strlen(m_szStr));
m_pGame->GetDisplay()->GetBackBuffer()->ReleaseDC(hdc);
m_y-=m_nSpeed;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -