Ejemplo n.º 1
0
float GetNearestMonster( SpriteSeer* pRole )
{
    float fret = 0.0f;
    SpriteMonster* pRet = NULL;

    float minDis = 100000.0f;

    minDis *= minDis;

    const map<int, SpriteMonster*>& allMonster = SpriteMonsterMgr::Get()->GetAllMonster();
    for(map<int, SpriteMonster*>::const_iterator iter = allMonster.begin(); iter != allMonster.end(); iter++)
    {
        float dis = ccpDistanceSQ(iter->second->getPosition(), pRole->getPosition());
        if( dis <= minDis)
        {
            pRet = iter->second;
            minDis = dis;

        }
    }

    if(pRet)
    {
        fret = ccpDistance(pRet->getPosition(), pRole->getPosition());
    }
    return fret;
}
Ejemplo n.º 2
0
void MoveAction::update(float dt) {
    CCPoint pos = m_pAgent->getPosition();
    
    double distSQ = ccpDistanceSQ(m_uTargetPos, pos);
    if (distSQ < 5) {
        m_bActivated = false;
    }
    else {
        m_bActivated = true;
    }
    
    if (m_bActivated) {
        CCPoint direction = ccpSub(m_uTargetPos, pos);
        CCPoint n = ccpNormalize(direction);
        
        double kSpeedTweaker = 0.2;
        double k = m_iSpeedLev * kSpeedTweaker;
        
        n.x *= k;
        n.y *= k;
        
        CCPoint goTo = ccpAdd(pos, n);
        m_pAgent->setPosition(goTo);
    }
}
Ejemplo n.º 3
0
void GameLayer::ccTouchesBegan(CCSet* pTouch, CCEvent* pEvent){
	
	//check if the backIcon is pressed
	CCTouch *touch = (CCTouch*)pTouch->anyObject();
	CCPoint location = touch->getLocation();
	CCDirector* pDirector = CCDirector::sharedDirector();
	CCSize winSize = pDirector->getWinSize();
	float icon_radius = backIcon->getContentSize().height / 2;
	//distance from icon target to center of the sprite
	float distanceSQ = ccpDistanceSQ(location, backIcon->getPosition());
	if (distanceSQ <= icon_radius * icon_radius){
		//change the icon to pressed
		//MapLayer::getMap()->unlockNextLevel();
		pDirector->popScene();
		return;
	}
	//back button checking ended
	
	
	//gesture detection initiate
	ccp_last = touch->getLocationInView();
	ccp_last = pDirector->convertToGL(ccp_last);

	b_click = true;
	b_rect = false;
	b_circle = false;
	count_order = 0;
	gd_direction = kGDirectionNo;

	for (int i = 0; i < 4; i++){
		cache_deirectionshape[i] = false;
	}
	//gesture detection ended
}
Ejemplo n.º 4
0
SpriteMonster* ElfAimDirectionHandler::GetNearestMonster( void )
{
	SpriteMonster* pRet = NULL;
	float minDis = 0.0f;
	minDis = m_pSkill->longDistanceAttackRange;

	if(minDis <= 0.0f)
		minDis = 320.0f;

	minDis *= minDis;

	const map<int, SpriteMonster*>& allMonster = SpriteMonsterMgr::Get()->GetAllMonster();
	for(map<int, SpriteMonster*>::const_iterator iter = allMonster.begin(); iter != allMonster.end(); iter++)
	{
		float dis = ccpDistanceSQ(iter->second->getPosition(), m_pElf->getPosition());
		if( dis <= minDis)
		{
			pRet = iter->second;
			minDis = dis;

		}
	}

	return pRet;
}
Ejemplo n.º 5
0
bool TouchComponent::isTouchInside(CCTouch *pTouch) {
    CCPoint location = pTouch->getLocation();   // GL coord
    
    double distSQ = ccpDistanceSQ(m_pAgent->getPosition(), location);
    if (distSQ < m_dRadius * m_dRadius) {
        return true;
    }
    return false;
}
Ejemplo n.º 6
0
bool NDUIXBoardHitProxy::UITouchEnd(NDTouch* pkTouch)
{
	assert(m_pkOwner && pkTouch);
	//if (!hitTest( touch->GetLocation())) return;

	NDUILayer* pkLayer = 0;

	if (!canProcessTouch(pkLayer))
	{
		if (pkLayer)
		{
			int nZ = pkLayer->GetZOrder();
			bool ret = pkLayer->UITouchEnd(pkTouch);

			return ret;
		}
	}

	bool bResult = false;
	for (int i = 0; i < m_pkOwner->getViewCount(); i++)
	{
		NDUIXView* pkView = m_pkOwner->getViewAt(i);

		if (pkView && pkView->GetEnabled()
			&& (pkView->GetScreenRect().containsPoint(
			pkTouch->GetLocation()) || pkView->getCurrentDragNode()))
		{
			//如果都没点击到控件,则视为点击到view
			if (m_pkTouchDownView)
			{
				if (ccpDistanceSQ(pkTouch->GetLocation(),
					m_kTouchDownLocalPos) < VIEW_CLICK_ERR * VIEW_CLICK_ERR || !m_bEnableMove)
				{
					pkView->UITouchEnd(pkTouch);
					bResult = true;
				}
			}
			break;
		}
	}

	m_kTouchDownLocalPos = CCPointZero;
	m_pkTouchDownView = NULL;

	m_pkOwner->getBoard()->autoBounceBack();
	m_pkOwner->getBoard()->autoScrollView();

	if (m_bIsMoving)
	{
		m_bIsMoving = false;
	}

	//NDUILayer::UITouchEnd(touch);
	return bResult;
}
Ejemplo n.º 7
0
bool RightDPad::ccTouchBegan(CCTouch* pTouch, CCEvent* pEvent)
{
	CCPoint location=pTouch->getLocation();
	float distanceSQ=ccpDistanceSQ(location,this->getPosition());
	if(distanceSQ<=_radius*_radius)
	{
		this->updateRDirectionForTouchLocation(location);
		_isHelding=true;
		return true;
	}
	return false;
}
Ejemplo n.º 8
0
bool ButtonB::onTouchBegan(Touch* touch, Event* event){
	Point location = touch->getLocation();
	//log("%f, %f", touch->getLocation().x, touch->getLocation().y);

	float distanceSQ = ccpDistanceSQ(location, this->getPosition());
	if (distanceSQ <= 32 * 32){
		setSpriteFrame("*****@*****.**");
		m_hero->jump();
	//	log("B");
		return true;
	}
	return false;
}
Ejemplo n.º 9
0
    HitTestResult* hitTestRectStrip(RectBodyNode* a, StripBodyNode* b)
    {
        Point Atop[4];
        Point Btop[4];
        a->getTops(Atop, Atop + 1, Atop + 2, Atop + 3);
        b->getTops(Btop, Btop + 1, Btop + 2, Btop + 3);
        int cpcount = 0;
        Point cp[12];
        bool hit = isRectCrossRect(Atop[0], Atop[1], Atop[2], Atop[3],
            Btop[0], Btop[1], Btop[2], Btop[3], &cpcount, cp);

        if(hit)
        {
            if(cpcount < 0)
                cpcount = 0;

            Point ca = a->getCenter();
            float wa = a->getWidth();
            float ha = a->getHeight();
            Vector2 aa = vector2ForRotation(a->getAngle());
            for(int i = 0; i < 4; i++)
            {
                if(isPointInRect(Btop[i], ca, wa, ha, aa))
                {
                    cp[cpcount++] = Btop[i];
                }
            }

            // use dot result for comparing directly
            // get true distance when receive a minimun dot result
            int iMin = 0;
            float dMin = 0;
            Vector2 direction = vector2ForRotation(a->getAngle());
            for(int i = 0; i < cpcount; i++)
            {
                float d = ccpDistanceSQ(ca, cp[i]);
                if( i == 0 && d < dMin)
                {
                    dMin = d;
                    iMin = i;
                }
            }

            dMin = sq(dMin);
            return HitTestResult::create(HTRT_CROSS, cp[iMin], dMin);
        }
        else
        {
            return HitTestResult::create(HTRT_NONE);
        }
    }
Ejemplo n.º 10
0
void GameLayer::ccTouchesMoved(CCSet* pTouch, CCEvent* pEvent){
	//pattern detection
	CCTouch* touch = (CCTouch*)pTouch->anyObject();
	ccp_now = touch->getLocationInView();
	ccp_now = CCDirector::sharedDirector()->convertToGL(ccp_now);
	float distanceSQ = ccpDistanceSQ(ccp_now, ccp_last);
	float adsx = ccp_now.x - ccp_last.x;
	float adsy = ccp_now.y - ccp_last.y;

	if (abs(adsx) > 10 || abs(adsy) > 10){
		b_click = false;
	}


	if (abs(adsx) > abs(adsy)){
		if (adsx < 0){
			cache_deirectionshape[0] = 1;
			gd_direction = kGDirectionLeft;
		}
		else{
			cache_deirectionshape[1] = 1;
			gd_direction = kGDirectionRight;
		}
	}
	else{
		if (adsy < 0){
			cache_deirectionshape[2] = 1;
			gd_direction = kGDirectionDown;
		}
		else{
			cache_deirectionshape[3] = 1;
			gd_direction = kGDirectionUp;
		}
	}
	
	int x = 0;
	for (int i = 0; i < 4; i++){
		if (cache_deirectionshape[i]){
			x++;
		}
	}

	if (x >= 3){
		b_circle = true;
	}
	
}
Ejemplo n.º 11
0
void Bullet::Attack()
{    
    TGameObjectList objectList;
    if (GameObjectManager::IsSingletonCreated())
        GameObjectManager::Singleton().GetGameObjectList(eGOT_MainCharacter, objectList);
    for (TGameObjectList::iterator iter = objectList.begin(); iter != objectList.end(); ++iter)
    {
        float maxCollisionDis = (*iter)->GetCollisionRadius() + GetCollisionRadius();
        float distanceSQ = ccpDistanceSQ((*iter)->getPosition(), getPosition());

        if (distanceSQ < maxCollisionDis * maxCollisionDis)
        {
            MainPlayerLogic::Singleton().ReduceHp(1);
            GetFsm().SwitchState(STATE(ArrivedBottomSafe));
        }
    }
}
void TapDragPinchInput::ccTouchMoved(CCTouch *touch, CCEvent *pEvent)
{
   switch(_state)
   {
      case DPT_DRAG:
         if(_points[1].ID == touch->getID())
         {
            StoreTouchData(touch, &_points[1]);
            _target->TapDragPinchInputDragContinue(_points[0], _points[1]);
         }
         break;
      case DPT_FINGER_DOWN:
         if(touch->getID()== _points[0].ID)
         {
            float distSq = ccpDistanceSQ(_points[0].pos, touch->getLocation());
            if(distSq >= DRAG_RADIUS_SQ)
            {  // Starting a drag.
               StoreTouchData(touch, &_points[1]);
               _target->TapDragPinchInputDragBegin(_points[0], _points[1]);
               _state = DPT_DRAG;
            }
         }
         else
         {  // Should not happen.
            _state = DPT_IDLE;
         }
         break;
      case DPT_IDLE:
         // You can get here if you were doing a pinch and lifted
         // only one finger.
         break;
      case DPT_PINCH:
         if(touch->getID() == _points[0].ID)
         {  // Point 0 moved.
            StoreTouchData(touch, &_points[0]);
         }
         if(touch->getID() == _points[1].ID)
         {  // Point 1 moved.
            StoreTouchData(touch, &_points[1]);
         }
         _target->TapDragPinchInputPinchContinue(_points[0], _points[1]);
         break;
   }
   
}
Ejemplo n.º 13
0
void WPScene::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
    if (_currentScrollView) {
        _currentScrollView -> ccTouchMoved(pTouch, pEvent);
        CCPoint sp = pTouch -> getStartLocationInView();
        CCPoint ep = pTouch -> getLocationInView();
        float dis = 10;
        bool s = ccpDistanceSQ(sp, ep) > dis * dis;
        if (_currentMenu && s) {
            _currentMenu -> ccTouchCancelled(pTouch, pEvent);
            _currentMenu = NULL;
        }
    }
    else {
        if (_currentMenu) {
            _currentMenu -> ccTouchMoved(pTouch, pEvent);
        }
    }
    
}
Ejemplo n.º 14
0
void ElfAttackTrigger::Update( float dt )
{
	ElfBaseState::Update(dt);

	if(m_pRole->getTag() != MAP_TAG_SEER)
		return;

	m_cd -= dt;
	if(m_cd <= 0.0f)
	{
		float dis = ccpDistanceSQ(m_pRole->getPosition(), m_pElf->getPosition());
		if(dis < m_outerCircleRadius && !StoryInstanceDirector::Get()->IsStart())
		{
			if(GetNearestMonster() || m_pSkill->aim == FAIRY_SKILL_INFO::E_HERO)
			{
				m_pSkillStateMachine->Remove(ToElfCloseTo);

				m_cd = m_pSkill->ownCD;
				m_pSkillStateMachine->Push(m_pStateFactory->Create(ToElfSkillAttack, m_pRole, m_actionId, m_pEvt->m_pAttachData));
			}
		}
		
	}
}
Ejemplo n.º 15
0
 inline bool exclude(BodyNode* a, BodyNode* b)
 {
     return ccpDistanceSQ(a->getCenter(), b->getCenter()) > sq(a->getRadius() + b->getRadius());
 }
Ejemplo n.º 16
0
cocos2d::CCNode* ElfAttackTrigger::GetNearestMonster( void )
{
	cocos2d::CCNode* pRet = NULL;
	float minDis = 0.0f;
	minDis = m_pSkill->longDistanceAttackRange;

	if(minDis <= 0.0f)
		minDis = 320.0f;

	minDis *= minDis;
	if(LevelManager::sShareInstance()->isCurrentPVPLevel())
	{
		if(LevelManager::sShareInstance()->getCurrentLevelLayer())
		{
			USERINFO* pHeroInfo = UserData::Get()->GetUserInfo(m_pRole->GetID());

			std::map<unsigned int, OthersPlayerInfo>& otherPlayer = LevelManager::sShareInstance()->getCurrentLevelLayer()->m_othersPlayerInfo;
			for(std::map<unsigned int, OthersPlayerInfo>::const_iterator iter = otherPlayer.begin(); iter != otherPlayer.end(); iter++)
			{
				SpriteSeer* pOhterHero = iter->second.seer; 

				if(m_pRole == pOhterHero || pOhterHero == NULL)
					continue;

				USERINFO* pOtherInfo = UserData::Get()->GetUserInfo(pOhterHero->GetID());
				if(pHeroInfo && pOhterHero)
				{
					if(pHeroInfo->battleSide % 2 == pOtherInfo->battleSide % 2)
						continue;
				}

				float dis = ccpDistanceSQ(pOhterHero->getPosition(), m_pElf->getPosition());
				if( dis <= minDis)
				{
					pRet = pOhterHero;
					minDis = dis;

				}
			}

			
		}
	}
	else
	{
		const map<int, SpriteMonster*>& allMonster = SpriteMonsterMgr::Get()->GetAllMonster();
		for(map<int, SpriteMonster*>::const_iterator iter = allMonster.begin(); iter != allMonster.end(); iter++)
		{
			float dis = ccpDistanceSQ(iter->second->getPosition(), m_pElf->getPosition());
			if( dis <= minDis)
			{
				pRet = iter->second;
				minDis = dis;

			}
		}
	}
	

	return pRet;
}
Ejemplo n.º 17
0
bool Planet::containsPoint(const cocos2d::CCPoint &p)
{
    return ccpDistanceSQ(p, getPosition()) < _radius * _radius;
}
Ejemplo n.º 18
0
void CCMotionStreak::update(float delta)
{
    if (!m_bStartingPositionInitialized)
    {
        return;
    }
    
    delta *= m_fFadeDelta;

    unsigned int newIdx, newIdx2, i, i2;
    unsigned int mov = 0;

    // Update current points
    for(i = 0; i<m_uNuPoints; i++)
    {
        m_pPointState[i]-=delta;

        if(m_pPointState[i] <= 0)
            mov++;
        else
        {
            newIdx = i-mov;

            if(mov>0)
            {
                // Move data
                m_pPointState[newIdx] = m_pPointState[i];

                // Move point
                m_pPointVertexes[newIdx] = m_pPointVertexes[i];

                // Move vertices
                i2 = i*2;
                newIdx2 = newIdx*2;
                m_pVertices[newIdx2] = m_pVertices[i2];
                m_pVertices[newIdx2+1] = m_pVertices[i2+1];

                // Move color
                i2 *= 4;
                newIdx2 *= 4;
                m_pColorPointer[newIdx2+0] = m_pColorPointer[i2+0];
                m_pColorPointer[newIdx2+1] = m_pColorPointer[i2+1];
                m_pColorPointer[newIdx2+2] = m_pColorPointer[i2+2];
                m_pColorPointer[newIdx2+4] = m_pColorPointer[i2+4];
                m_pColorPointer[newIdx2+5] = m_pColorPointer[i2+5];
                m_pColorPointer[newIdx2+6] = m_pColorPointer[i2+6];
            }else
                newIdx2 = newIdx*8;

            const GLubyte op = (GLubyte)(m_pPointState[newIdx] * 255.0f);
            m_pColorPointer[newIdx2+3] = op;
            m_pColorPointer[newIdx2+7] = op;
        }
    }
    m_uNuPoints-=mov;

    // Append new point
    bool appendNewPoint = true;
    if(m_uNuPoints >= m_uMaxPoints)
    {
        appendNewPoint = false;
    }

    else if(m_uNuPoints>0)
    {
        bool a1 = ccpDistanceSQ(m_pPointVertexes[m_uNuPoints-1], m_tPositionR) < m_fMinSeg;
        bool a2 = (m_uNuPoints == 1) ? false : (ccpDistanceSQ(m_pPointVertexes[m_uNuPoints-2], m_tPositionR) < (m_fMinSeg * 2.0f));
        if(a1 || a2)
        {
            appendNewPoint = false;
        }
    }

    if(appendNewPoint)
    {
        m_pPointVertexes[m_uNuPoints] = m_tPositionR;
        m_pPointState[m_uNuPoints] = 1.0f;

        // Color assignment
        const unsigned int offset = m_uNuPoints*8;
        *((ccColor3B*)(m_pColorPointer + offset)) = _displayedColor;
        *((ccColor3B*)(m_pColorPointer + offset+4)) = _displayedColor;

        // Opacity
        m_pColorPointer[offset+3] = 255;
        m_pColorPointer[offset+7] = 255;

        // Generate polygon
        if(m_uNuPoints > 0 && m_bFastMode )
        {
            if(m_uNuPoints > 1)
            {
                ccVertexLineToPolygon(m_pPointVertexes, m_fStroke, m_pVertices, m_uNuPoints, 1);
            }
            else
            {
                ccVertexLineToPolygon(m_pPointVertexes, m_fStroke, m_pVertices, 0, 2);
            }
        }

        m_uNuPoints ++;
    }

    if( ! m_bFastMode )
    {
        ccVertexLineToPolygon(m_pPointVertexes, m_fStroke, m_pVertices, 0, m_uNuPoints);
    }

    // Updated Tex Coords only if they are different than previous step
    if( m_uNuPoints  && m_uPreviousNuPoints != m_uNuPoints ) {
        float texDelta = 1.0f / m_uNuPoints;
        for( i=0; i < m_uNuPoints; i++ ) {
            m_pTexCoords[i*2] = tex2(0, texDelta*i);
            m_pTexCoords[i*2+1] = tex2(1, texDelta*i);
        }

        m_uPreviousNuPoints = m_uNuPoints;
    }
}
Ejemplo n.º 19
0
bool AITargetWithinRange::canFire(){
    if (target && target->getTarget() && ccpDistanceSQ(target->getPosition(), target->getTarget()->getPosition()) <= range*range) {
        return true;
    }
    return false;
}
Ejemplo n.º 20
0
cocos2d::CCPoint ElfAimDirectionHandler::GetAimDirection()
{
	CCPoint ret = m_pElf->getDirectionVector();
	CCPoint rolePos = m_pElf->getPosition();

	
	if(LevelManager::sShareInstance()->isCurrentPVPLevel())
	{
		if(LevelManager::sShareInstance()->getCurrentLevelLayer())
		{
			float minDis = m_pSkill->longDistanceAttackRange;

			if(minDis <= 0.0f)
				minDis = 320.0f;

			minDis *= minDis;

			SpriteSeer* pNearestHero = NULL;

			std::map<unsigned int, OthersPlayerInfo>& otherPlayer = LevelManager::sShareInstance()->getCurrentLevelLayer()->m_othersPlayerInfo;

			for(std::map<unsigned int, OthersPlayerInfo>::const_iterator iter = otherPlayer.begin(); iter != otherPlayer.end(); iter++)
			{
				SpriteSeer* pOhterHero = iter->second.seer; 
				if(m_pRole == pOhterHero || pOhterHero == NULL)
					continue;

				float dis = ccpDistanceSQ(pOhterHero->getPosition(), m_pElf->getPosition());
				if( dis <= minDis)
				{
					pNearestHero = pOhterHero;
					minDis = dis;

				}
			}

			if(pNearestHero)
			{
				m_bAimMonster = true;
				m_nearestMonsterPos = pNearestHero->getPosition();
				ret = ccpSub(m_nearestMonsterPos, rolePos);
			}
		}
	}
	else
	{
		SpriteMonster* pNearestMonster = GetNearestMonster();

		if(pNearestMonster)
		{
			m_bAimMonster = true;
			m_nearestMonsterPos = pNearestMonster->getPosition();
			ret = ccpSub(m_nearestMonsterPos, rolePos);

		}
	}

	

	ret = ccpNormalize(ret);

	return ret;
}
Ejemplo n.º 21
0
 bool operator() (CCPoint i,CCPoint j) {return ccpDistanceSQ(i, m_startPosition) < ccpDistanceSQ(j, m_startPosition); }