也许是一个英雄,也可以是一个隐藏的对象。也就是下文种的 GridCenter
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;
        }
    }
    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;
    }
    for(auto hero : m_MainScene->heroList)
    {
        if(!hero->getIsAlly() && !hero->getIsDead())
        {
            Vec2 curVec = slotRelativeVec[hero->getSlotIndex()];
            Point des = GridCenter + curVec;
            hero->setDestinationPoint(des);
        }
    }
....
}
再根据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));
}
void GameControlManager::updateGridDirection()
{
            Point slot = getSlotPosByIndex(hero->getSlotIndex());
            if(hero->getCenterPoint().distance(slot) > getElasticRange())
            {
                hero->moveToward(slot);
            }
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
基于cocos2dx的RPG简单实用算法之3 - 多角色跟随阵型移动
原文地址:http://blog.csdn.net/goodeveningbaby/article/details/48065925