- // Find out direction in which the player walks
- LeftRightScrollChange walkDirection;
- if (player->physic.get_velocity_x() < -EPSILON) walkDirection = LEFT;
- else if (player->physic.get_velocity_x() > EPSILON) walkDirection = RIGHT;
- else if (player->dir == ::LEFT) walkDirection = LEFT;
- else walkDirection = RIGHT;
-
- float LEFTEND = SCREEN_WIDTH * config.sensitive_x;
- float RIGHTEND = SCREEN_WIDTH * (1-config.sensitive_x);
-
- if((walkDirection == LEFT && scrollchange == RIGHT)
- || (walkDirection == RIGHT && scrollchange == LEFT))
- scrollchange = NONE;
- // when in left 1/3rd of screen scroll left
- if(player_pos.x < translation.x + LEFTEND)
- scrollchange = LEFT;
- // scroll right when in right 1/3rd of screen
- else if(player_pos.x > translation.x + RIGHTEND)
- scrollchange = RIGHT;
+ // Find out direction in which the player moves
+ LookaheadMode walkDirection;
+ if (player_delta.x < -EPSILON) walkDirection = LOOKAHEAD_LEFT;
+ else if (player_delta.x > EPSILON) walkDirection = LOOKAHEAD_RIGHT;
+ else if (player->dir == ::LEFT) walkDirection = LOOKAHEAD_LEFT;
+ else walkDirection = LOOKAHEAD_RIGHT;
+
+ float LEFTEND, RIGHTEND;
+ if(config.sensitive_x > 0) {
+ LEFTEND = SCREEN_WIDTH * config.sensitive_x;
+ RIGHTEND = SCREEN_WIDTH * (1-config.sensitive_x);
+ } else {
+ LEFTEND = SCREEN_WIDTH;
+ RIGHTEND = 0;
+ }
+
+ if(lookahead_mode == LOOKAHEAD_NONE) {
+ /* if we're undecided then look if we crossed the left or right
+ * "sensitive" area */
+ if(player_pos.x < cached_translation.x + LEFTEND) {
+ lookahead_mode = LOOKAHEAD_LEFT;
+ } else if(player_pos.x > cached_translation.x + RIGHTEND) {
+ lookahead_mode = LOOKAHEAD_RIGHT;
+ }
+ /* at the ends of a level it's obvious which way we will go */
+ if(player_pos.x < SCREEN_WIDTH*0.5) {
+ lookahead_mode = LOOKAHEAD_RIGHT;
+ } else if(player_pos.x >= sector->get_width() - SCREEN_WIDTH*0.5) {
+ lookahead_mode = LOOKAHEAD_LEFT;
+ }
+
+ changetime = -1;
+ } else if(lookahead_mode != walkDirection) {
+ /* player changed direction while camera was scrolling...
+ * he has to do this for a certain time to add robustness against
+ * sudden changes */
+ if(changetime < 0) {
+ changetime = game_time;
+ } else if(game_time - changetime > config.dirchange_time) {
+ if(lookahead_mode == LOOKAHEAD_LEFT &&
+ player_pos.x > cached_translation.x + RIGHTEND) {
+ lookahead_mode = LOOKAHEAD_RIGHT;
+ } else if(lookahead_mode == LOOKAHEAD_RIGHT &&
+ player_pos.x < cached_translation.x + LEFTEND) {
+ lookahead_mode = LOOKAHEAD_LEFT;
+ } else {
+ lookahead_mode = LOOKAHEAD_NONE;
+ }
+ }
+ } else {
+ changetime = -1;
+ }