【cocos2d-x IOS游戏开发-捕鱼达人17】炮弹攻击判定

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


上节回顾

【cocos2d-x IOS游戏开发-捕鱼达人16】炮弹攻击效果2


1、  炮弹攻击判定方式

判定规则
鱼网会有一个自己的包围盒
鱼会有一个自己的包围盒

若两包围盒若相交,表示鱼被捕

包围盒的计算方式
包围盒是一个正方形(避免因为旋转带来的麻烦)
以单位的宽和高的最小值作为边长

 2、 代码实现的关键点

矩形的相交算法
使用内置函数CCRect::intersectsRect或自己封装
RenderObject需要提供包围盒
鱼被攻击后,死亡判定和动画播放


具体实现:

#include "GameObject.h"
#include "GameLogic.h"
namespace fishingjoy
{
	namespace logic
	{
		void CFishObject::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){
				if(m_bMoveToAndStop){
					m_eGameObjectState = EGOS_IDLE;
					m_bMoveToAndStop = false;
					return;
				}
				if(m_bMoveToAndDestroy){
					m_eGameObjectState = EGOS_IDLE;
					m_bNeedDestroyed = true;
					m_bMoveToAndDestroy = false;
					return;
				}
				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);
				if(m_bMoveToAndStop){
					m_eGameObjectState = EGOS_IDLE;
					m_bMoveToAndStop = false;
					return;
				}
				if(m_bMoveToAndDestroy){
					m_eGameObjectState = EGOS_IDLE;
					m_bNeedDestroyed = true;
					m_bMoveToAndDestroy = false;
					return;
				}
				randomTarget();
			}
			else{
				setPosition(newX,newY);
			}
		}

		void CFishObject::onHit( int atk )
		{
			if(m_iHp <= atk){
				m_eGameObjectState = EGOS_DIE;
				m_pSceneObject->setAnimation("die");
				m_Time = 0;
				((CGameLogic*)m_pGameLogic)->addMark(m_pFishData->Mark);
			}
			else{
				m_iHp -= atk;
			}
		}
		//判断是否碰撞
		bool CBulletObject::isRectCollision (CCRect rect1, CCRect rect2){
			float x1 = rect1.origin.x;
			float y1 = rect1.origin.y;
			float w1 = rect1.size.width;
			float h1 = rect1.size.height;
			float x2 = rect2.origin.x;
			float y2 = rect2.origin.y;
			float w2 = rect2.size.width;
			float h2 = rect2.size.height;

			if (x1+w1*0.5<x2-w2*0.5)  
				return false;
			else if (x1-w1*0.5>x2+w2*0.5)
				return false;
			else if (y1+h1*0.5<y2-h2*0.5)
				return false;
			else if (y1-h1*0.5>y2+h2*0.5)
				return false;

			return true;
		}

		void CBulletObject::updateNormal( float dt )
		{
			float angle = (90-m_Dir)/180*3.1416926;
			float s = sin(angle);
			float c = cos(angle);
			float dtx = m_Speed*c*dt;
			float dty = m_Speed*s*dt;
			m_CurrentX += dtx;
			m_CurrentY += dty;
			m_pSceneObject->setPosition(m_CurrentX,m_CurrentY);

			dtx = m_CurrentX - m_OrgX;
			dty = m_CurrentY - m_OrgY;

			bool over = false;
			CGameLogic* pLogic = (CGameLogic*)m_pGameLogic;
			std::vector<CFishObject*>& rFishs = pLogic->getFishs();
			std::vector<CFishObject*>::iterator it = rFishs.begin();
			std::vector<CFishObject*>::iterator end = rFishs.end();
			for(;it!=end;++it){
				CFishObject* pFish = *it;
				if(pFish->isDead()){
					continue;
				}
				float fishW = pFish->getSceneObj()->getWidth();
				float fishH = pFish->getSceneObj()->getHeight();
				float fShort = fishW < fishH? fishW:fishH;
				CCRect rectFish = CCRectMake(pFish->getPosX()+fShort/2
					,pFish->getPosY()+fShort/2
					,fShort,fShort);

				float bulletW = m_pSceneObject->getWidth();
				float bulletH = m_pSceneObject->getHeight();

				fShort = bulletW < bulletH? bulletW:bulletH;
				CCRect rectBullet = CCRectMake(m_CurrentX+fShort/2,m_CurrentY+fShort/2
					,fShort,fShort);

				//在cocos2d-x 的2.0.4 版本中,CCRectEqualToRect 、CCRectContainsPoint、CCRectIntersectsRect已不再推荐使用,
				//取而代之的是 equals、containsPoint、intersectsRect。
				//bool intersect = CCRect::CCRectIntersectsRect(rectBullet,rectFish);

				//新版本可以使用精灵调用intersectsRect
				//if (sp1->boundingBox().intersectsRect(sp2->boundingBox()))
				//自己封装
				
				bool intersect = this->isRectCollision(rectBullet,rectFish);
				if(intersect){
				
					over = true;
					pFish->onHit(m_iAtk);
					printf("hit!!!!!!\n");
				}
			}

			if((dtx*dtx+dty*dty > m_Distance*m_Distance)|| over){
				m_Time = 0;
				m_eButtletObjectState = EBOS_EXPLODE;
				m_pSceneObject->setAnimation("net");
			}
		}

		void CBulletObject::update( float dt )
		{
			m_Time+=dt;
			switch(m_eButtletObjectState)
			{
			case EBOS_NORMAL:
				updateNormal(dt);
				break;
			case EBOS_EXPLODE:
				if(m_Time > EXPLODE_TIME){
					m_bNeedDestroyed = true;
				}
				break;
			}
		}

	}
}



可以看到效果图:

【cocos2d-x IOS游戏开发-捕鱼达人17】炮弹攻击判定_第1张图片

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

你可能感兴趣的:(namespace,iterator,cocos2d-x,捕鱼达人)