unsigned int DispenserControl::moveToPosition ( const unsigned int position )
{
    unsigned int stepsTaken = 0;
    if ( !isInMotion () )
    {
        goToHome ();
        stepsTaken = moveDispenserHead ( position, END );

        if ( listener != NULL )
        {
            listener->reachedToPosition ( position, stepsTaken );
        }
    }

    return stepsTaken;
}
Exemple #2
0
void Box::moveToTargetArea()
{
    // Move only if we have a target
    if(hasActiveTargetArea)
    {
        repeatAttack = false;

        // Get center and calculate directions
        float centerX = body->GetPosition().x * SCALE;
        float centerY = body->GetPosition().y * SCALE;
        float cSpeed = speed;
        float cJumpHeight = jumpHeight;

        if(centerX > target.position.x)
        {
            cSpeed = -cSpeed;
        }

        if(centerY < target.position.y)
        {
            cJumpHeight = 0.0f;
        }

        // STOP CONDITION: We are within target boundaries
        if(centerX <= target.position.x + target.radius && centerX >= target.position.x - target.radius && centerY <= target.position.y + target.radius && centerY >= target.position.y - target.radius)
        {
            hasActiveTargetArea = false;
            //dampenedJump();
            body->SetLinearVelocity(b2Vec2(0.0f, 0.0f));
        }
        else
        {
            // Can jump again if we touch a static box
            if(contactStaticList.size() > 0)
            {
                isJumping = false;
            }
            else
            {
                // Can jump if we are not in motion anylonger
                // Prevents getting stuck on dynamic boxes
                if(!isInMotion())
                {
                    isJumping = false;
                }
            }

            // If either of our dynamic contacts is a float box
            // we can jump again
            for(unsigned int i = 0; i < contactDynamicList.size(); i++)
            {
                if(contactDynamicList.at(i)->type == "floatbox" && ((team == TEAM_ALLY && contactDynamicList.at(i)->team != TEAM_ENEMY) || (team == TEAM_ENEMY && contactDynamicList.at(i)->team != TEAM_ALLY)))
                {
                    isJumping = false;
                }
            }

            // We can only jump if we are not currently jumping
            if(!isJumping)
            {
                // If we are at the same horizontal position from before the last jump, we are probably stuck.
                // In this case use normal jump height instead of 0 to enable climbing
                if((int)preJumpPosition.x == (int)(centerX))
                {
                    cJumpHeight = jumpHeight;
                }

                preJumpPosition = b2Vec2(body->GetPosition().x * SCALE, body->GetPosition().y * SCALE);
                jump(cJumpHeight, cSpeed);
                isJumping = true;
            }

            // If we have reached our goal on the horizontal plane, terminate movement
            if(centerX >= target.position.x - target.radius && centerX <= target.position.x + target.radius)
            {
                hasActiveTargetArea = false;
                dampenedJump();
            }
        }
    }
}