【cocos2d-x IOS游戏开发-捕鱼达人13】完善鱼群效果

尊重开发者的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/17150301


上节回顾

【cocos2d-x IOS游戏开发-捕鱼达人12】鱼群效果


1、  鱼群相关问题解决和完善

需要处理的问题
何时出现鱼群
鱼群出来时,其它鱼如何散场
鱼群走后,其它鱼如何回到场景

看具体的实现:

#include "GameLogic.h"
#include "../config/FishGroup.h"
namespace fishingjoy
{
	namespace logic
	{
		CGameObject* CGameLogic::createGameObject()
		{
			int idx = CCRANDOM_0_1()*m_vecPercentPool.size();
			idx = m_vecPercentPool[idx];
			const std::vector<config::SFishData>& rFishs = config::CFishConfig::getFishData();
			const config::SFishData& rData = rFishs[idx];

			scene::ISceneObject* pS = m_pGameScene->createObject(rData.AniID);
			CGameObject* pObj = new CGameObject(pS,&rData);

			int maxw = config::CGameConfig::getExtendSize()+480;
			int maxh = config::CGameConfig::getExtendSize()+320;
			pObj->setDuration(360*CCRANDOM_0_1());
			pObj->setPosition(CCRANDOM_0_1()*maxw,CCRANDOM_0_1()*maxh);
			pObj->randomTarget();
			m_vecObjectsList.push_back(pObj);
			return pObj;
		}

		CGameObject* CGameLogic::createGameObject( int fid,int x,int y,int speed,int tx,int ty)
		{
			const std::vector<config::SFishData>& rFishs = config::CFishConfig::getFishData();
			const config::SFishData* pData = NULL;
			for(int i = 0; i < rFishs.size(); ++i){
				if(rFishs[i].FID == fid){
					pData = &rFishs[i];
				}
			}
			if(pData==NULL)
				return NULL;

			scene::ISceneObject* pS = m_pGameScene->createObject(pData->AniID);
			CGameObject* pObj = new CGameObject(pS,pData);

			int maxw = config::CGameConfig::getExtendSize()+480;
			int maxh = config::CGameConfig::getExtendSize()+320;
			pObj->setPosition(x,y);
			pObj->moveToAndDestroy(tx,ty);
			m_vecObjectsList.push_back(pObj);
			return pObj;
		}

		void CGameLogic::destroyGameObject( CGameObject* pObj )
		{
			if(pObj){
				std::vector<CGameObject*>::iterator it= m_vecObjectsList.begin();
				for(;it!=m_vecObjectsList.end();++it){
					if(*it == pObj){
						m_vecObjectsList.erase(it);
						delete (pObj);
						return;
					}
				}
			}
		}

		bool CGameLogic::init( CCScene* pScene )
		{
			const std::vector<config::SFishData>& rFishs = config::CFishConfig::getFishData();
			for(size_t i = 0; i < rFishs.size(); ++i){
				const config::SFishData& rData = rFishs[i];
				for(int x = 0; x < rData.Percent; ++x){
					m_vecPercentPool.push_back(i);
				}
			}

			m_eGameState = EGS_BEGIN_MENU;
			if(pScene==NULL)
				return false;
			m_pGameScene = scene::createScene();
			if(m_pGameScene==NULL)
				return false;


			cocos2d::CCLayer* pSceneRoot = cocos2d::CCLayer::create();
			pScene->addChild(pSceneRoot);
			m_pGameScene->attachTo(pSceneRoot);

			const char* szBeginSceneBg = config::CGameConfig::getBeginSceneBg();
			m_pGameScene->setBackground(szBeginSceneBg);

			cocos2d::CCLayer* pUIRoot = cocos2d::CCLayer::create();
			pScene->addChild(pUIRoot);
			mUIMgr.init(this,pUIRoot);

			return true;
		}

		void CGameLogic::update( float dt )
		{
			if(m_eGameState != EGS_GAME_PLAY)
				return;
			m_fTime += dt;

			switch(m_iSubState)
			{
			case EGPS_NORMAL:
				{
					static float total = 0;
					total+=dt;
					if(total > 3){
						if(m_vecObjectsList.size()< config::CGameConfig::getMaxFishNum()){
							createGameObject();
						}
						total = 0;
					}

					const std::vector<fishingjoy::config::SFishGroupData>& rGroupData = fishingjoy::config::CFishGroup::getGroupData();

					CCLOG("m_fTime = %f,rGroupData[0].BeginTime = %d",m_fTime, rGroupData[0].BeginTime);

					//每隔rGroupData[0].BeginTime秒出现一次鱼群
					for(size_t i = 0; i < rGroupData.size(); ++i){
						//当m_fTime接近于rGroupData[0].BeginTime秒时出现鱼群
						if(abs(m_fTime - rGroupData[i].BeginTime)<1){
							printf("change state to EGPS_GROUP_COME\n");
							m_iSubState = EGPS_GROUP_COME;

							int extend = config::CGameConfig::getExtendSize();

							float xs[] = {-extend,480+extend,	480+extend,-extend};
							float ys[] = {-extend,-extend,		320+extend,320+extend};

							std::vector<CGameObject*>::iterator it = m_vecObjectsList.begin();
							std::vector<CGameObject*>::iterator end = m_vecObjectsList.end();
							for(;it!=end;++it){
								int randx = CCRANDOM_0_1()*4;
								int randy = CCRANDOM_0_1()*4;
								(*it)->moveToAndStop(xs[randx],ys[randy]);
							}
							m_pGroupData = &rGroupData[i];
							m_fGroupTime = m_fTime;
							break;
						}
					}
				}
				break;
				//鱼群来临了
			case EGPS_GROUP_COME:
				{
					//等待GROUP_COME_TIME出现
					if(m_fTime > m_fGroupTime + GROUP_COME_TIME){
						printf("change state to EGPS_GROUP\n");
						m_iSubState = EGPS_GROUP;

						for(int i = 0; i < m_pGroupData->Fishs.size(); ++i){
							const config::SFishInGroup& rInGroup = m_pGroupData->Fishs[i];
							//创建鱼群对象
							createGameObject(rInGroup.FID,rInGroup.OffsetX + m_pGroupData->X + m_pGroupData->AnchorX,
								rInGroup.OffsetY + m_pGroupData->Y + m_pGroupData->AnchorY,m_pGroupData->Speed
								,m_pGroupData->TargetX + m_pGroupData->AnchorX,
								m_pGroupData->TargetY + m_pGroupData->AnchorY);
						}
						m_fGroupTime = m_fTime;
						break;
					}
				}
				break;
			case EGPS_GROUP:
				{
					//鱼群持续了m_pGroupData->LastTime
					if(m_fTime >  m_fGroupTime + m_pGroupData->LastTime){
						printf("change state to EGPS_GROUP_GO\n");
						//即将结束
						m_iSubState = EGPS_GROUP_GO;
						m_fGroupTime = m_fTime;
					}
				}
				break;
			case EGPS_GROUP_GO:
				{
					//GROUP_GO_TIME后鱼群结束
					if(m_fTime > m_fGroupTime+ GROUP_GO_TIME){
						std::vector<CGameObject*>::iterator it = m_vecObjectsList.begin();
						std::vector<CGameObject*>::iterator end = m_vecObjectsList.end();
						for(;it!=end;++it){
							(*it)->randomTarget();
						}
						printf("change state to EGPS_NORMAL\n");
						m_iSubState = EGPS_NORMAL;
						m_fTime = 0;
					}
				}
				break;
			}

			if(m_pGameScene)
				m_pGameScene->update(dt);

			std::vector<CGameObject*>::iterator it = m_vecObjectsList.begin();
			std::vector<CGameObject*>::iterator end = m_vecObjectsList.end();
			for(;it!=end;){
				if((*it)->needDestroyed()){
					scene::ISceneObject* pObj = (*it)->getSceneObj();
					m_pGameScene->destroyObject(pObj);
					it = m_vecObjectsList.erase(it); 
					end = m_vecObjectsList.end();
					continue;
				}
				(*it)->update(dt);
				++it;
			}
		}

	}
}

看实现:

【cocos2d-x IOS游戏开发-捕鱼达人13】完善鱼群效果_第1张图片

本节源代码与工程文件:【资源引用页】


大家可以继续实现更复杂的鱼群效果,加入更多的控制因子。

你可能感兴趣的:(object,数据,对象,cocos2d-x,捕鱼达人)