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

📄 cbattle.cpp

📁 一个个人开发的rpg游戏<亚特兰蒂斯传奇>的源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
//--------------------------------------------------------------------------------------------------------
//                        游戏战斗模块
//CBattle.cpp
//游戏引擎中的战斗部分
//作者:吴振华(kylinx)(中国科大01级11系)
//E-mail:game-diy@163.com
//创建于:2003/6/17 by Kylinx
//修改于2003/7/28 by Kylinx
//--------------------------------------------------------------------------------------------------------
#include<time.h>
#include<stdio.h>
#include"CBattle.h"
#include"CGoods.h"
#include"CGame.h"
#include"CMacro.h"
#include"CCursor.h"
#include"CMenu.h"
#include"CError.h"
#include"CFont.h"
#include"CMessageBox.h"
#include"CBattleMenu.h"
STBattleRole::~STBattleRole()
{
	if(this->pAttackAni)
	{
		if(pAttackAni->aRect)
		{
			delete [] pAttackAni->aRect;
			pAttackAni->aRect=NULL;
		}
		if(pAttackAni->aXOffsite)
		{
			delete [] pAttackAni->aXOffsite;
			pAttackAni->aXOffsite=NULL;
		}
		if(pAttackAni->aYOffsite)
		{
			delete [] pAttackAni->aYOffsite;
			pAttackAni->aYOffsite=NULL;
		}
		if(pAttackAni->pSound!=this->pSoundAttack)
		{
			delete pAttackAni->pSound;
			pAttackAni->pSound=NULL;
		}
		if(pAttackAni->pSurface!=this->pSurfMain)
		{
			delete pAttackAni->pSurface;
			pAttackAni->pSurface=NULL;
		}
		delete pAttackAni;
		pAttackAni=NULL;
	}
	if(this->pBeAttackedAni)
	{
		if(pBeAttackedAni->aRect)
		{
			delete [] pBeAttackedAni->aRect;
			pBeAttackedAni->aRect=NULL;
		}
		if(pBeAttackedAni->aXOffsite)
		{
			delete [] pBeAttackedAni->aXOffsite;
			pBeAttackedAni->aXOffsite=NULL;
		}
		if(pBeAttackedAni->aYOffsite)
		{
			delete [] pBeAttackedAni->aYOffsite;
			pBeAttackedAni->aYOffsite=NULL;
		}
		if(pBeAttackedAni->pSound!=this->pSoundBeAttack)
		{
			delete pBeAttackedAni->pSound;
			pBeAttackedAni->pSound=NULL;
		}
		if(pBeAttackedAni->pSurface!=this->pSurfMain)
		{
			delete pBeAttackedAni->pSurface;
			pBeAttackedAni->pSurface=NULL;
		}
		delete pBeAttackedAni;
		pBeAttackedAni=NULL;
	}
	if(this->pDeadAni)
	{
		if(pDeadAni->aRect)
		{
			delete [] pDeadAni->aRect;
			pDeadAni->aRect=NULL;
		}
		if(pDeadAni->aXOffsite)
		{
			delete [] pDeadAni->aXOffsite;
			pDeadAni->aXOffsite=NULL;
		}
		if(pDeadAni->aYOffsite)
		{
			delete [] pDeadAni->aYOffsite;
			pDeadAni->aYOffsite=NULL;
		}
		if(pDeadAni->pSound!=this->pSoundDead)
		{
			delete pDeadAni->pSound;
			pDeadAni->pSound=NULL;
		}
		if(pDeadAni->pSurface!=this->pSurfMain)
		{
			delete pDeadAni->pSurface;
			pDeadAni->pSurface=NULL;
		}
		delete pDeadAni;
		pDeadAni=NULL;
	}
	if(this->pDefendAni)
	{
		if(pDefendAni->aRect)
		{
			delete [] pDefendAni->aRect;
			pDefendAni->aRect=NULL;
		}
		if(pDefendAni->aXOffsite)
		{
			delete [] pDefendAni->aXOffsite;
			pDefendAni->aXOffsite=NULL;
		}
		if(pDefendAni->aYOffsite)
		{
			delete [] pDefendAni->aYOffsite;
			pDefendAni->aYOffsite=NULL;
		}
		if(pDefendAni->pSound!=this->pSoundDefend)
		{
			delete pDefendAni->pSound;
			pDefendAni->pSound=NULL;
		}
		if(pDefendAni->pSurface!=this->pSurfMain)
		{
			delete pDefendAni->pSurface;
			pDefendAni->pSurface=NULL;
		}
		delete pDefendAni;
		pDefendAni=NULL;
	}
	if(this->pStayAni)
	{
		if(pStayAni->aRect)
		{
			delete [] pStayAni->aRect;
			pStayAni->aRect=NULL;
		}
		if(pStayAni->aXOffsite)
		{
			delete [] pStayAni->aXOffsite;
			pStayAni->aXOffsite=NULL;
		}
		if(pStayAni->aYOffsite)
		{
			delete [] pStayAni->aYOffsite;
			pStayAni->aYOffsite=NULL;
		}
		if(pStayAni->pSound)
		{
			delete pStayAni->pSound;
			pStayAni->pSound=NULL;
		}
		if(pStayAni->pSurface!=this->pSurfMain)
		{
			delete pStayAni->pSurface;
			pStayAni->pSurface=NULL;
		}
		delete pStayAni;
		pStayAni=NULL;
	}
	if(this->pSoundAttack)
	{
		delete pSoundAttack;
		pSoundAttack=NULL;
	}
	if(this->pSoundBeAttack)
	{
		delete pSoundBeAttack;
		pSoundBeAttack=NULL;
	}
	if(this->pSoundDead)
	{
		delete pSoundDead;
		pSoundDead=NULL;
	}
	if(this->pSoundDefend)
	{
		delete pSoundDefend;
		pSoundDefend=NULL;
	}
	if(this->pSurfMain)
	{
		delete pSurfMain;
		pSurfMain=NULL;
	}
}
CLog CBattle::m_sLog("BattleLog.txt");								//类中的静态变量声明,并初始化战斗日志
BOOL CBattle::LoadRoleAni(char*szFile,char*szSegment,CSurface*pSurfDefault,CSound*pSoundDefault,STBattleRoleAni*pRoleAni)
{
	LOA_ASSERT(szFile);
	LOA_ASSERT(szSegment);
	LOA_ASSERT(pRoleAni);
	
	
	char szString[256];
	memset(szString,0,256);
	if(::GetPrivateProfileString(szSegment,"picture","",szString,256,szFile))
	{
		if(FAILED(m_pGame->GetDisplay()->CreateSurfaceFromBitmap(&pRoleAni->pSurface,szString,0,0)))
		{
			m_sLog.WriteMessage("Create Battle Role AttackAni Surface<%s> Failed!",szString);
			return false;
		}
		memset(szString,0,256);
		::GetPrivateProfileString(szSegment,"colorkey","0,0,0",szString,256,szFile);
		int r,g,b;
		sscanf(szString,"%d,%d,%d",&r,&g,&b);
		pRoleAni->pSurface->SetColorKey(RGB(r,g,b));
	}
	else
	{
		pRoleAni->pSurface=pSurfDefault;
	}
	memset(szString,0,256);
	if(::GetPrivateProfileString(szSegment,"sound","",szString,256,szFile))
	{
		if(FAILED(m_pGame->GetSoundPlayer()->Create(&pRoleAni->pSound,szString)))
		{
			m_sLog.WriteMessage("Create Sound <%s> Failed!",szString);
		}
	}
	else
	{
		pRoleAni->pSound=pSoundDefault;
	}
	pRoleAni->nCurrentFrame=0;
	pRoleAni->dwOldTime=0;
	pRoleAni->nKeyFrame=::GetPrivateProfileInt(szSegment,"KeyFrame",-1,szFile);
	pRoleAni->xEffectShow=::GetPrivateProfileInt(szSegment,"ShowEffectX",0,szFile);
	pRoleAni->yEffectShow=::GetPrivateProfileInt(szSegment,"ShowEffectY",0,szFile);
	int ID=::GetPrivateProfileInt(szSegment,"EffectID",-1,szFile);
	if(!m_pGame->GetEffectDataBase()->FindData(ID,&pRoleAni->pEffect))
	{
		pRoleAni->pEffect=NULL;
	}
	pRoleAni->dwDelay=::GetPrivateProfileInt(szSegment,"Delay",0,szFile);

	pRoleAni->nFrames=::GetPrivateProfileInt(szSegment,"Frames",0,szFile);
	if(pRoleAni->nFrames<=0)
	{
		m_sLog.WriteMessage("Error Segment<%s> To Get Key:Frames!",szSegment);
		return false;
	}
	pRoleAni->aRect=new RECT[pRoleAni->nFrames];
	if(!pRoleAni->aRect)
	{
		m_sLog.WriteMessage("Out Of Memory while New Rect[%d]",pRoleAni->nFrames);
		return false;
	}
	pRoleAni->aXOffsite=new int[pRoleAni->nFrames];
	pRoleAni->aYOffsite=new int[pRoleAni->nFrames];

	char szKey[256];
	RECT rect;
	for(int i=0;i<pRoleAni->nFrames;i++)
	{
		wsprintf(szKey,"rect_%d",i+1);
		memset(szString,0,256);
		::GetPrivateProfileString(szSegment,szKey,"",szString,256,szFile);
		sscanf(szString,"%d,%d,%d,%d",&rect.left,&rect.top,&rect.right,&rect.bottom);
		pRoleAni->aRect[i]=rect;

		wsprintf(szKey,"xOffsite_%d",i+1);
		pRoleAni->aXOffsite[i]=::GetPrivateProfileInt(szSegment,szKey,0,szFile);
		
		wsprintf(szKey,"yOffsite_%d",i+1);
		pRoleAni->aYOffsite[i]=::GetPrivateProfileInt(szSegment,szKey,0,szFile);
	}
	pRoleAni->Height=pRoleAni->aRect[0].bottom-pRoleAni->aRect[0].top;
	return true;
}
BOOL CBattle::LoadBattleRoleFromFile(char*szFile,STBattleRole*pRole)
{
	char szString[256];
	memset(szString,0,256);
	::GetPrivateProfileString("Basic","picture","",szString,256,szFile);
	if(strcmp(szString,"")==0)
	{
		m_sLog.WriteMessage("Failed To Get Picture Info From BattleRole Ini File<%s>",szFile);
		return false;
	}
	
	if(FAILED(m_pGame->GetDisplay()->CreateSurfaceFromBitmap(&pRole->pSurfMain,szString,0,0)))
	{
		m_sLog.WriteMessage("Create Battle Role Main Surface<%s> Failed!",szString);
		return false;
	}
	memset(szString,0,256);
	::GetPrivateProfileString("Basic","colorkey","0,0,0",szString,256,szFile);
	int r,g,b;
	sscanf(szString,"%d,%d,%d",&r,&g,&b);
	pRole->pSurfMain->SetColorKey(RGB(r,g,b));

	m_pGame->GetSoundPlayer()->Create(&pRole->pSoundAttack,"wav\\attack.wav");
	m_pGame->GetSoundPlayer()->Create(&pRole->pSoundDead,"wav\\dead.wav");
	m_pGame->GetSoundPlayer()->Create(&pRole->pSoundBeAttack,"wav\\beattack.wav");
	m_pGame->GetSoundPlayer()->Create(&pRole->pSoundDefend,"wav\\defend.wav");

	pRole->pStayAni=new STBattleRoleAni;
	pRole->pAttackAni=new STBattleRoleAni;
	pRole->pBeAttackedAni=new STBattleRoleAni;
	pRole->pDefendAni=new STBattleRoleAni;
	

	if(!LoadRoleAni(szFile,"StayAni",pRole->pSurfMain,NULL,pRole->pStayAni))
		return false;
	if(!LoadRoleAni(szFile,"AttackAni",pRole->pSurfMain,pRole->pSoundAttack,pRole->pAttackAni))
		return false;
	if(!LoadRoleAni(szFile,"BeAttackAni",pRole->pSurfMain,pRole->pSoundBeAttack,pRole->pBeAttackedAni))
		return false;
	if(!LoadRoleAni(szFile,"DefendAni",pRole->pSurfMain,pRole->pSoundDefend,pRole->pDefendAni))
		return false;
	STBattleRoleAni*pDeadAni=new STBattleRoleAni;
	if(LoadRoleAni(szFile,"DeadAni",pRole->pSurfMain,pRole->pSoundDead,pDeadAni))
	{
		pRole->pDeadAni=pDeadAni;
	}
	else
	{
		delete pDeadAni;
	}
	return true;
}
////////////////////////////////////////////////////////////////////////////////////////////
//初始化战斗
////////////////////////////////////////////////////////////////////////////////////////////
BOOL CBattle::InitBattle(CGame*pGame,
						char*szBattleMap,
						char*szBattleMusic,
						CRole*pPlayer,								//玩家的数据
						CRole*pEnemy1,								//第一个敌人数据(若为NULL表示这个位置没有敌人)
						char*szBattleFile1,
						int posy1,
						CRole*pEnemy2,								//第二个敌人数据(若为NULL表示这个位置没有敌人)
						char*szBattleFile2,
						int posy2,
						CRole*pEnemy3,								//第三个敌人数据(若为NULL表示这个位置没有敌人)
						char*szBattleFile3,
						int posy3,
						BOOL bFightBoss,
						BOOL bNeedWin)								//是否必须胜利
{
	LOA_ASSERT(pGame);
	LOA_ASSERT(szBattleMap);
	LOA_ASSERT(szBattleMusic);
	LOA_ASSERT(pPlayer);

	
	m_pGame=pGame;

	char szCurMusic[128];
	strcpy(szCurMusic,m_pGame->GetCurrentMusicFile());

	m_pGame->PlayMedia(szBattleMusic);

	m_pBattleMap=new CMap(m_pGame->GetLog());
	if(!m_pBattleMap->LoadMap(szBattleMap))
	{
		m_sLog.WriteMessage("Load Battle map<%s> Failed!",szBattleMap);
		m_pGame->PlayMedia(szCurMusic);
		return false;
	}
	m_pRender=new CRender;
	m_pRenderBattleMap=new CRenderMap;
	m_pRenderBattleMap->InitRender(pGame->GetDisplay(),pGame->GetMapSurface(),m_pBattleMap);
	m_pRenderBattleMap->m_xStart=0;
	m_pRenderBattleMap->m_yStart=0;
	m_pRenderBattleMap->CreateTask(true,true,32);

	m_pRender->AddTask(m_pRenderBattleMap);


	m_bIsBattle=true;

	srand(unsigned(time(0)));										//初始化随机数种子
	
	m_pPlayer=new STBattleRole();
	m_pPlayer->pRole=pPlayer;
	m_pPlayer->ShowPosX=450;
	m_pPlayer->ShowPosY=180;
	m_pPlayer->CurPosX=m_pPlayer->ShowPosX;
	m_pPlayer->CurPosY=m_pPlayer->ShowPosY;
	m_pPlayer->Pos=1;
	m_pPlayer->m_RoleCmd.bToPlayer=false;

	if(!LoadBattleRoleFromFile("pic\\battle\\player.ini",m_pPlayer))
	{
		m_pGame->PlayMedia(szCurMusic);
		return false;
	}
	m_nPosY[0]=posy1;
	m_nPosY[1]=posy2;
	m_nPosY[2]=posy3;

	for(int i=0;i<LOA_MAX_BATTLE_ENEMY;i++)
	{
		m_pEnemy[i]=new STBattleRole();
		m_pEnemy[i]->ShowPosX=100;
		m_pEnemy[i]->ShowPosY=m_nPosY[i];
		m_pEnemy[i]->Pos=i;
		m_pEnemy[i]->bShow=false;
		m_pEnemy[i]->m_RoleCmd.bToPlayer=true;
	}
	
	if(pEnemy1)
	{
		m_pEnemy[0]->bShow=true;
		m_pEnemy[0]->pRole=pEnemy1;

⌨️ 快捷键说明

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