Esempio n. 1
0
void TFHPBar::updateBar()
{
    if (NULL == getInnerSprite() || NULL == m_pBar)
    {
        return;
    }
    
    if (FLT_EQUAL(m_percentage, 0.f))
    {
        m_pBar->setSpriteVisible(false);
    }
    else
    {
        m_pBar->setSpriteVisible(true);
        
        CCSize sz = m_pBar->getSpriteContentSize();
        sz.width = OrigBarSize_.width * m_percentage;

        m_pBar->setSpritePreferredSize(sz);
    }
}
Esempio n. 2
0
void CMoveOnGridComp::update(float dt)
{
    if (!isEnabled()) return;
    
#ifdef DEBUG
    if (m_ownerRole->getMark())
    {
        CWarriorRoleCompBase::update(dt);
    }
    else
#endif
    {
        CWarriorRoleCompBase::update(dt);
    }
    m_idleSleepTime -= dt;

    
    switch (m_subState)
    {
        case MOVE_SUB_STATE_IDLE:
            m_ownerRole->playAnimation(ROLE_ANIMATION_IDLE);
            m_ownerRole->unlockState();

            if (m_idleSleepTime < 0)
            {
                THINK_AND_BREAK();
                findPathIfNeeded(true);
            }
            break;
        case MOVE_SUB_STATE_PATH_FIND:
        {
            m_ownerRole->playAnimation(ROLE_ANIMATION_IDLE);
            m_ownerRole->unlockState();

            if (m_idleSleepTime < 0)
            {
                THINK_AND_BREAK();
                findPathIfNeeded(false);
            }
            break;
        }
        case MOVE_SUB_STATE_PATH_FINDING:
            m_ownerRole->playAnimation(ROLE_ANIMATION_IDLE);
            break;
        case MOVE_SUB_STATE_PATH_FOUND:
        {
            THINK_AND_BREAK();
            if (!m_ownerRole->getMovetarget().equals(m_moveTarget))
            {
                m_subState = MOVE_SUB_STATE_IDLE;
                break;
            }
            if (m_paths.size() > 0)
            {
                CBackgroundManager* bkg = m_ownerRole->getBackGround();
                CC_ASSERT(bkg);
                const Point& pos = m_paths.back();
                if (pos.equals(m_ownerRole->getLogicGrid()->getGridPos()))
                {
                    m_paths.pop_back();
                    break;
                }

                if (bkg->isRoleCanBePlacedOnPos(m_ownerRole, pos, true))
                {
                    
                    CLogicGrid* pGrid = m_ownerRole->getLogicGrid();
                    const Point& curPos = pGrid->getGridPos();

                    float speed = m_ownerRole->getSpeed();
                    m_moveTotalTime = pos.getDistance(curPos) / speed;
                    CC_ASSERT(!FLT_EQUAL(m_moveTotalTime, 0.f));
                    m_moveElapseTime = 0.f;
                    m_moveFrom = bkg->gridToWorldPoint(curPos);
                    m_moveTo = bkg->gridToWorldPoint(pos);
                    
                    Point diff = m_moveTo - m_moveFrom;
                    if (diff.x > 0)
                    {
                        if (diff.y > 0)
                        {
                            m_ownerRole->setFaceTo(FACE_TO_RIGHT_UP);
                        }
                        else
                        {
                            m_ownerRole->setFaceTo(FACE_TO_RIGHT_DOWN);
                        }
                    }
                    else if (diff.x < 0)
                    {
                        if (diff.y > 0)
                        {
                            m_ownerRole->setFaceTo(FACE_TO_LEFT_UP);
                        }
                        else
                        {
                            m_ownerRole->setFaceTo(FACE_TO_LEFT_DOWN);
                        }
                    }
                    m_ownerRole->playAnimation(ROLE_ANIMATION_MOVE);
                    m_ownerRole->lockState();
                    
                    m_subState = MOVE_SUB_STATE_MOVING;
                }
                else
                {
                    bkg->isRoleCanBePlacedOnPos(m_ownerRole, pos, true);
                    m_subState = MOVE_SUB_STATE_PATH_FIND;
//                    m_idleSleepTime = 2.f;
                }
            }
            else
            {
                m_subState = MOVE_SUB_STATE_IDLE;
            }
            break;
        }
        case MOVE_SUB_STATE_MOVING:
        {
            m_moveElapseTime += dt;
            float alpha = m_moveElapseTime / m_moveTotalTime;
            
            if (FLT_GE(alpha, 1.f))
            {
                alpha = 1.f;
                m_paths.pop_back();
                m_subState = MOVE_SUB_STATE_PATH_FOUND;
                
                m_ownerRole->unlockState();
            }
            else if (FLT_GE(alpha, .5))
            {
                CBackgroundManager* bkg = m_ownerRole->getBackGround();
                CC_ASSERT(bkg);
                if (!bkg->placeRole(m_ownerRole, m_paths.back()))
                {
                    m_subState = MOVE_SUB_STATE_ROLL_BACK;
                }
            }


            Point newPos = m_moveFrom.lerp(m_moveTo, alpha);
            m_ownerRole->setSpritePosition(newPos);

            break;
        }
        case MOVE_SUB_STATE_ROLL_BACK:
        {
            CBackgroundManager* bkg = m_ownerRole->getBackGround();
            CC_ASSERT(bkg);
            bkg->placeRole(m_ownerRole, m_ownerRole->getLogicGrid()->getGridPos());
            m_ownerRole->playAnimation(ROLE_ANIMATION_IDLE);
            m_subState = MOVE_SUB_STATE_PATH_FIND;
            m_ownerRole->unlockState();
            break;
        }
        default:
            break;
    }
}