尊重开发者的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/17148421
上节回顾
【cocos2d-x IOS游戏开发-捕鱼达人10】鱼的产生和移动
1、鱼的产生完善
概率
不同的鱼都有不同的概率
每种鱼的出现,受概率影响
每次判定,都能生产鱼
概率的实现方案
概率精确度1%
所有相加得到的值得到一个总和 sum
在0~sum 之间随机取值,得到相应的鱼
2、 鱼的移动完善
移动实现
选定目标点
将状态设置为移动
判定当前位置,目标位置,下一位置之间的关系,判定是否移动成功。
鱼的状态
转向状态
移动状态
受攻击死亡状态
具体的代码实现:
#include "GameLogic.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()+960; int maxh = config::CGameConfig::getExtendSize()+640; //设置方向 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; } 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; static float total = 0; //每隔三秒中查看已生成的鱼是否大于20,小于则新加鱼对象 total+=dt; CCLOG("dt = %f, total =%f",dt, total); if(total > 3.0f){ if(m_vecObjectsList.size()< config::CGameConfig::getMaxFishNum()){ createGameObject(); } total = 0; } //更新m_pGameScene 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;++it){ (*it)->update(dt); } } } }对象鱼的状态改变:
#ifndef __GAME_OBJECT_H__ #define __GAME_OBJECT_H__ #include "../include/IGameScene.h" #include "../config/FishConfig.h" #include "../config/GameConfig.h" namespace fishingjoy { namespace logic { enum EGameObjectState { EGOS_MOVE,//移动 EGOS_ROTATE,//转向 EGOS_DIE,//死亡 }; class CGameObject { scene::ISceneObject* m_pSceneObject; const config::SFishData* m_pFishData; EGameObjectState m_eGameObjectState; float m_TargetX; float m_TargetY; float m_CurrentX; float m_CurrentY; public: CGameObject(scene::ISceneObject* pObj,const config::SFishData* pData) :m_pSceneObject(pObj) ,m_pFishData(pData) ,m_eGameObjectState(EGOS_MOVE) { } public: void update(float dt) { switch(m_eGameObjectState) { case EGOS_MOVE: updateMove(dt); break; case EGOS_ROTATE: //转向就不再移动了 break; case EGOS_DIE: break; default: break; } } void setPosition(int x,int y) { m_CurrentX = x; m_CurrentY = y; m_pSceneObject->setPosition(x,y); } void randomTarget() { m_TargetX = CCRANDOM_0_1()*(config::CGameConfig::getExtendSize()+960); m_TargetY = CCRANDOM_0_1()*(config::CGameConfig::getExtendSize()+640); float dx = m_TargetX - m_CurrentX; float dy = m_TargetY - m_CurrentY; float angle = atan(dy/dx); float degree = angle*180/3.1415926; if(dx<0){ degree += 180; } setDuration(degree); } void setDuration(int degree) { m_pSceneObject->setDuration(180-degree); } private: void updateMove(float dt); }; } } #endif
#include "GameObject.h" namespace fishingjoy { namespace logic { void CGameObject::updateMove( float dt ) { float dx = m_TargetX - m_CurrentX; float dy = m_TargetY - m_CurrentY; float len = sqrt(dx*dx+dy*dy); if(len<0.001){ randomTarget(); return; } dx/=len; dy/=len; float newX = m_CurrentX + dx*dt*m_pFishData->NormalSpeed; float newY = m_CurrentY + dy*dt*m_pFishData->NormalSpeed; float dx2 = newX - m_CurrentX; float dy2 = newY - m_CurrentY; float dx3 = m_TargetX - m_CurrentX; float dy3 = m_TargetY - m_CurrentY; if(dx2*dx2+dy2*dy2 >= dx3*dx3+dy3*dy3){ setPosition(m_TargetX,m_TargetY); randomTarget(); } else{ setPosition(newX,newY); } } } }
新的实现结果:
本节源代码与工程文件:【资源引用页】
下一节:实现鱼群效果