基於cocos2dx的RPG簡單實用演算法之3 - 多角色跟隨陣型移動

來源:互聯網
上載者:User

標籤:cocos2dx   陣型   



1. 確定到一個陣型中心對象。

也許是一個英雄,也可以是一個隱藏的對象。也就是下文種的 GridCenter

2. 預先計算號每個陣型“槽” 相對中心對象的 向量。

void GameControlManager::startGridMode()
{
    if(m_MainScene->heroList.empty())
        return;
    m_IsStartGridMode = true;
    Point GridCenter = findGridCenter();
    if(memberNumber == 1)
        return;
    else if(memberNumber == 2)
    {
        //     0
        //     1
        originRelativeVec[0] = Vec2(0,Grid_Slot_Radius);
        originRelativeVec[1] = Vec2(0,-Grid_Slot_Radius);
    }
    else if(memberNumber == 3)
    {
        Point firstPos = GridCenter + Vec2(0,Grid_Slot_Radius);
        Point secondPos = firstPos.rotateByAngle(GridCenter, CC_DEGREES_TO_RADIANS(-120));
        Point thirdPos = secondPos.rotateByAngle(GridCenter, CC_DEGREES_TO_RADIANS(-120));
        
        //      0
        //
        //  1       2
        originRelativeVec[0] = firstPos - GridCenter;
        originRelativeVec[1] = secondPos - GridCenter;
        originRelativeVec[2] = thirdPos - GridCenter;
    }
    else if(memberNumber == 4)
    {
        Point firstPos = GridCenter + Vec2(0,Grid_Slot_Radius);
        Point secondPos = firstPos.rotateByAngle(GridCenter, CC_DEGREES_TO_RADIANS(-90));
        Point thirdPos = secondPos.rotateByAngle(GridCenter, CC_DEGREES_TO_RADIANS(-90));
        Point fourthPos = thirdPos.rotateByAngle(GridCenter, CC_DEGREES_TO_RADIANS(-90));
        
        //     0          戰士
        //  1     2     獵人  法師
        //     3          牧師
        originRelativeVec[0] = firstPos - GridCenter;
        originRelativeVec[1] = fourthPos - GridCenter;
        originRelativeVec[2] = secondPos - GridCenter;
        originRelativeVec[3] = thirdPos - GridCenter;
    }

    
    //認領slot位置
    int slotIndex = 0;
    int minSpeed = 999;
    for(auto hero : m_MainScene->heroList) //已經排序
    {
        if(!hero->getIsAlly() && !hero->getIsDead())
        {
            hero->setSlotIndex(slotIndex);
            slotIndex ++;
            
            auto actorInfo = GameData::getActorInfoFromMap(hero->getUnitID());
            if(actorInfo->speed < minSpeed)
                minSpeed = actorInfo->speed;
        }
    }

3. 根據一號英雄相對中心對象的方向來確定陣型初始朝向

    Point firstmanPos = m_MainScene->heroList.front()->getCenterPoint();
//    crossover_point(firstmanPos, GridCenter, )
    
    Vec2 heroVec = firstmanPos - GridCenter;
    heroVec.normalize();
    m_GridAngle = getDirectionByChief(heroVec);
    
    for(int index = 0; index < memberNumber; index++)
    {
        Vec2 cur = originRelativeVec[index];
        Point curPoint = cur + GridCenter;
        curPoint = curPoint.rotateByAngle(GridCenter, m_GridAngle);
        cur = curPoint - GridCenter;
        slotRelativeVec[index] = cur;
    }

4. 所有英雄各就各位

    for(auto hero : m_MainScene->heroList)
    {
        if(!hero->getIsAlly() && !hero->getIsDead())
        {
            Vec2 curVec = slotRelativeVec[hero->getSlotIndex()];
            Point des = GridCenter + curVec;
            hero->setDestinationPoint(des);
        }
    }
....
}


5.  當陣型移動,根據 ”中心對象“相對目的地位置 targetPos來更新陣型朝向角度GridAngle

再根據GridAngle重新整理 每個槽的相對向量 cur

void GameControlManager::setGridDirection(Point targetPos)
{
    //更新陣型朝向
    auto GridCenter = getGridCenter();
    Vec2 chiefVec = targetPos - GridCenter;
    chiefVec.normalize();
    m_GridAngle = getDirectionByChief(chiefVec);
    for(int index = 0; index < memberNumber; index++)
    {
        Vec2 cur = originRelativeVec[index];
        Point curPoint = cur + m_gridObject->getPosition();
        curPoint = curPoint.rotateByAngle(m_gridObject->getPosition(),m_GridAngle); //在原基礎上旋轉
        cur = curPoint - m_gridObject->getPosition();
        slotRelativeVec[index] = cur;
    }
    m_gridObject->setRotation(CC_RADIANS_TO_DEGREES(-m_GridAngle));
}


6. 每一幀讓角色移動到自己對應的槽

void GameControlManager::updateGridDirection()
{
            Point slot = getSlotPosByIndex(hero->getSlotIndex());
            if(hero->getCenterPoint().distance(slot) > getElasticRange())
            {
                hero->moveToward(slot);
            }
}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

基於cocos2dx的RPG簡單實用演算法之3 - 多角色跟隨陣型移動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.