www.pudn.com > GameEngine_src.rar > CMagicFactory.cpp
#include "CMagicFactory.h" #include "CAliveSprite.h" #include "CEPKFile.h" #include "CMap.h" #include "CHero.h" #include "normal.h" #include#include extern PEASYAUDIO g_pEasyAudio; extern CMap theMap; extern CHero theHero; //魔法状态动画分类:上,中,下 enum MSE_TYPE { MSE_UP = 0, MSE_MIDDLE = 1, MSE_DOWN = 31, }; ////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////// CMagicFactory::CMagicFactory() { } CMagicFactory::~CMagicFactory() { } ////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////// bool CMagicFactory::Init() { return m_MagicSpritePool.Init( 64 ); } ////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////// void CMagicFactory::Free() { m_MagicSpritePool.Free(); m_MagicArray.Free(); m_MSEInfoArray.Free(); } ////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////// void CMagicFactory::OnFreeLvl() { m_MagicArray.Clear(); m_MSEInfoArray.Clear(); } ////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////// void CMagicFactory::OnLoadSI( int id ) { if ( m_MagicIDArray.IsContain( id ) == false ) m_MagicIDArray.Add( id ); } ////////////////////////////////////////////////////////////////////// //载入MGC文件,创建魔法对象,载入EPG动画 //mgc文件,魔法动画,魔法状态动画,全部存在magic.epk中 ////////////////////////////////////////////////////////////////////// void CMagicFactory::OnLoadLvl() { CEPKFile epk; if ( false == epk.Open( "..\\data\\magic.epk", EPK_IN ) ) assert( false ); for ( int i = 0; i < m_MagicIDArray.GetLength(); ++i ) { char sz[8]; wsprintf( sz, "%d.mgc", m_MagicIDArray[i] ); BYTE *buf = epk.Read( sz ); //读取MGC文件 debug_assert( buf != NULL ); //读取文件头 MAGIC_FILE_HEADER mfh = READ_MEMORY( buf, MAGIC_FILE_HEADER ); buf += sizeof(mfh); CMagic *pMagic = m_MagicArray.CreateOneElement(); pMagic->Init( mfh, (char*)buf ); //创建并初始化魔法对象 if ( mfh.mgcEPGName[0] != '\0' ) { buf = epk.Read( mfh.mgcEPGName ); //载入魔法动画 debug_assert( buf != NULL ); pMagic->GetMagicEPG()->LoadEPG( (char*)buf, false, 0, 0, 0 ); } if ( mfh.mgcEffectID > 0 ) { wsprintf( sz, "%d.epg", mfh.mgcEffectID ); buf = epk.Read( sz ); MSE_INFO *pMSE = m_MSEInfoArray.CreateOneElement(); pMSE->mseID = mfh.mgcEffectID; //载入魔法状态动画 pMSE->msePG.LoadEPG( (char*)buf, false, 0, 0, 0 ); } if ( mfh.mgcWAVName[0] != '\0' ) { buf = epk.Read( mfh.mgcWAVName ); debug_assert( buf != NULL ); PEASYSOUND sound = g_pEasyAudio->CreateSoundBuffer( (char *)buf, false, 4 ); pMagic->SetSound( sound ); } } epk.Close(); } ////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////// void CMagicFactory::OnInitLvl() { } ////////////////////////////////////////////////////////////////////// //按魔法ID查找一项魔法,返回魔法指针 ////////////////////////////////////////////////////////////////////// CMagic * CMagicFactory::FindMagic( int id ) { CMagic *re = NULL; for ( int i = 0; i < m_MagicArray.GetLength(); ++i ) { CMagic *p = &m_MagicArray[i]; if ( id == p->GetID() ) { re = p; } } debug_assert( re != NULL ); return re; } ////////////////////////////////////////////////////////////////////// //设置魔法的一项属性 ////////////////////////////////////////////////////////////////////// void CMagicFactory::SetMagicInfo( int index, int value ) { debug_assert( index >= MI_END && index <= MI_BEGIN ); m_MagicInfo[(-index)+MI_BEGIN] = value; } ////////////////////////////////////////////////////////////////////// //给目标加上一个魔法状态 ////////////////////////////////////////////////////////////////////// void CMagicFactory::AddTargetMS( int effectIndex, int time ) { MAGIC_STATE ms; ms.effectIndex = (short)effectIndex; ms.effectValue = m_EffectValue; ms.endTime = timeGetTime() + time; m_pTarget->AddInfo( effectIndex, m_EffectValue ); m_pTarget->AddMagicState( ms ); } ////////////////////////////////////////////////////////////////////// //给目标加上一个魔法状态动画 ////////////////////////////////////////////////////////////////////// void CMagicFactory::AddTargetMSE( int id, int time ) { MSE_INFO mi; mi.mseID = id; int i = m_MSEInfoArray.Find( mi ); if ( i < 0 ) return; MAGIC_STATE_EPG mse; mse.pEPG = &m_MSEInfoArray[i].msePG; if ( time == 0 ) { time = 100 * mse.pEPG->GetNumFrame(); } mse.endTime = timeGetTime() + time; i = id / 10; mse.drawType = MSE_UP; //动画ID,10 ~ 19为上, 20 ~ 29 为中, 30 ~ 39为下 if ( i == 2 ) mse.drawType = MSE_MIDDLE; else if ( i == 3 ) mse.drawType = MSE_DOWN; mse.curFrame = 0; m_pTarget->AddMSE( mse ); } const SPRITE_TYPE TYPE_ARRAY[3] = { ST_MAGE_3, ST_MAGE_2, ST_MAGE_1 }; ////////////////////////////////////////////////////////////////////// //产生魔法精灵 //type 为精灵类型:1,天上; 2,中间; 3,地表 //life 为精灵存活时间 ////////////////////////////////////////////////////////////////////// void CMagicFactory::CreateMagicSprite( int type, int life ) { CMagicSprite *sprite = m_MagicSpritePool.Alloc(); if ( sprite != NULL ) { POINT base = m_SpellPoint; if ( m_pCurMagic->IsTarget() ) base = m_pMagician->GetBasePoint(); sprite->Init( m_pCurMagic, base, m_pMagician->GetDirection(), TYPE_ARRAY[type-1], life, m_pMagician, m_pTarget, m_MagicDamage ); if ( theMap.AddMagicSprite( sprite ) == false ) m_MagicSpritePool.Free( sprite ); } } ////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////// void CMagicFactory::DeleteMagicSprite( CMagicSprite *p ) { m_MagicSpritePool.Free( p ); } ////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////// void CMagicFactory::MoveHero() { theHero.MoveHeroTo( m_SpellPoint ); }