[Super-tux-commit] supertux/src camera.cpp,1.4,1.5
Brought to you by:
wkendrick
From: Matze B. <mat...@us...> - 2004-05-24 23:02:44
|
Update of /cvsroot/super-tux/supertux/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv12243 Modified Files: camera.cpp Log Message: added some comments to scrolling code Index: camera.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/camera.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- camera.cpp 24 May 2004 21:52:14 -0000 1.4 +++ camera.cpp 24 May 2004 23:02:34 -0000 1.5 @@ -80,21 +80,28 @@ 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) @@ -103,6 +110,7 @@ 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 @@ -112,14 +120,24 @@ 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; @@ -128,15 +146,19 @@ 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 |