if(elapsed_time < EPSILON)
return;
+ /****** Vertical Scrolling part ******/
bool do_y_scrolling = true;
if(player->dying || level->height == 19)
do_y_scrolling = false;
if(do_y_scrolling) {
- float target_y;
+ // target_y is the high we target our scrolling at. This is not always the
+ // high of the player, but if he is jumping upwards we should use the
+ // position where he last touched the ground.
+ float target_y;
if(player->fall_mode == Player::JUMPING)
target_y = player->last_ground_y + player->base.height;
else
target_y = player->base.y + player->base.height;
+ // delta_y is the distance we'd have to travel to directly reach target_y
float delta_y = translation.y - (target_y - screen->h/2);
+ // speed is the speed the camera would need to reach target_y in this frame
float speed_y = delta_y / elapsed_time;
+ // limit the camera speed when jumping upwards
if(player->fall_mode != Player::FALLING
&& player->fall_mode != Player::TRAMPOLINE_JUMP) {
if(speed_y > max_speed_y)
speed_y = -max_speed_y;
}
+ // finally scroll with calculated speed
translation.y -= speed_y * elapsed_time;
// don't scroll before the start or after the level's end
translation.y = 0;
}
+ /****** Horizontal scrolling part *******/
+
+ // our camera is either in leftscrolling, rightscrolling or nonscrollingmode.
+
+ // when suddenly changing directions while scrolling into the other direction.
+ // abort scrolling, since tux might be going left/right at a relatively small
+ // part of the map (like when jumping upwards)
if((player->dir == ::LEFT && scrollchange == RIGHT)
|| (player->dir == ::RIGHT && scrollchange == LEFT))
scrollchange = NONE;
+ // when in left 1/3rd of screen scroll left
if(player->base.x < translation.x + screen->w/3 && level->back_scrolling)
scrollchange = LEFT;
+ // scroll right when in right 1/3rd of screen
else if(player->base.x > translation.x + screen->w/3*2)
scrollchange = RIGHT;
+ // calculate our scroll target depending on scroll mode
float target_x;
if(scrollchange == LEFT)
target_x = player->base.x - screen->w/3*2;
else
target_x = translation.x;
+ // that's the distance we would have to travel to reach target_x
float delta_x = translation.x - target_x;
+ // the speed we'd need to travel to reach target_x in this frame
float speed_x = delta_x / elapsed_time;
+ // limit our speed
float maxv = 1 + fabsf(player->physic.get_velocity_x() * 1.3);
if(speed_x > maxv)
speed_x = maxv;
else if(speed_x < -maxv)
speed_x = -maxv;
-
+
+ // apply scrolling
translation.x -= speed_x * elapsed_time;
// don't scroll before the start or after the level's end