Fixed check for Block killing Badguy from below
[supertux.git] / src / object / camera.cpp
index 5153dff..049ea88 100644 (file)
@@ -42,8 +42,9 @@
 class CameraConfig
 {
 public:
-  // 0 = No, 1 = Fix, 2 = Mario/Yoshi, 3 = Kirby, 4 = inverse rubber
+  // 0 = No, 1 = Fix, 2 = Mario/Yoshi, 3 = Kirby
   int ymode;
+  // as above, 4 = super metroid like
   int xmode;
   float kirby_rectsize_x;
   float kirby_rectsize_y;
@@ -68,6 +69,8 @@ public:
   float clamp_y;
   float clamp_x;
 
+  float dynamic_speed_sm;
+
   CameraConfig() {
     xmode = 1;
     ymode = 1;
@@ -83,6 +86,7 @@ public:
     sensitive_x = 1.f/4.f;
     dynamic_max_speed_x = 1.0;
     dirchange_time = 0.2f;
+    dynamic_speed_sm = 1.0f;
   }
 
   void load(const std::string& filename)
@@ -107,11 +111,13 @@ public:
     camconfig->get("kirby-rectsize-y", kirby_rectsize_y);
     camconfig->get("edge-x", edge_x);
     camconfig->get("sensitive-x", sensitive_x);
+    camconfig->get("dynamic-speed-sm", dynamic_speed_sm);
   }
 };
 
 Camera::Camera(Sector* newsector, std::string name)
-  : mode(NORMAL), sector(newsector), lookahead_mode(LOOKAHEAD_NONE)
+  : mode(NORMAL), sector(newsector), lookahead_mode(LOOKAHEAD_NONE),
+    lookahead_pos(0)
 {
   this->name = name;
   config = new CameraConfig();
@@ -196,7 +202,7 @@ Camera::write(lisp::Writer& writer)
 void
 Camera::reset(const Vector& tuxpos)
 {
-  translation.x = tuxpos.x - SCREEN_WIDTH/3 * 2;
+  translation.x = tuxpos.x - SCREEN_WIDTH/2;
   translation.y = tuxpos.y - SCREEN_HEIGHT/2;
   shakespeed = 0;
   shaketimer.stop();
@@ -307,8 +313,9 @@ Camera::update_scroll_normal(float elapsed_time)
 
   if(player->is_dying() || sector->get_height() == 19*32) {
     ymode = 0;
-    xmode = 0;
   }
+  if(player->is_dying())
+    xmode = 0;
 
   if(ymode == 1) {
     translation.y = player_pos.y - SCREEN_HEIGHT * config.target_y;
@@ -319,14 +326,10 @@ Camera::update_scroll_normal(float elapsed_time)
     // position where he last touched the ground. (this probably needs
     // exceptions for trampolines and similar things in the future)
     float target_y;
-#if 0
     if(player->fall_mode == Player::JUMPING)
       target_y = player->last_ground_y + player->get_bbox().get_height();
     else
       target_y = player->get_bbox().p2.y;
-#endif
-    target_y = player->last_ground_y;
-
     target_y -= SCREEN_HEIGHT * config.target_y;
 
     // delta_y is the distance we'd have to travel to directly reach target_y
@@ -350,7 +353,33 @@ Camera::update_scroll_normal(float elapsed_time)
         player_pos.y - SCREEN_HEIGHT * (0.5f - halfsize));
   }
   if(ymode == 4) {
-    // TODO...
+    float upperend = SCREEN_WIDTH * config.edge_x;
+    float lowerend = SCREEN_WIDTH * (1 - config.edge_x);
+
+    if (player_delta.y < -EPSILON) {
+      // walking left
+      lookahead_pos -= player_delta.x * config.dynamic_speed_sm;
+
+      if(lookahead_pos > lowerend) {
+        lookahead_pos = lowerend;
+      }
+    } else if (player_delta.y > EPSILON) {
+      // walking right
+      lookahead_pos -= player_delta.y * config.dynamic_speed_sm;
+      if(lookahead_pos < upperend) {
+        lookahead_pos = upperend;
+      }
+    }
+
+    // adjust for level ends
+    if (player_pos.y < upperend) {
+      lookahead_pos = upperend;
+    }
+    if (player_pos.y > sector->get_width() - upperend) {
+      lookahead_pos = lowerend;
+    }
+
+    translation.y = player_pos.y - lookahead_pos;
   }
 
   if(ymode != 0 && config.clamp_y > 0) {
@@ -458,14 +487,46 @@ Camera::update_scroll_normal(float elapsed_time)
     // apply scrolling
     translation.x -= speed_x * elapsed_time;
   }
-  if(mode == 3) {
+  if(xmode == 3) {
     float halfsize = config.kirby_rectsize_x * 0.5f;
     translation.x = clamp(translation.x,
         player_pos.x - SCREEN_WIDTH * (0.5f + halfsize),
         player_pos.x - SCREEN_WIDTH * (0.5f - halfsize));
   }
   if(xmode == 4) {
-    // TODO...
+    float LEFTEND = SCREEN_WIDTH * config.edge_x;
+    float RIGHTEND = SCREEN_WIDTH * (1 - config.edge_x);
+
+    if (player_delta.x < -EPSILON) {
+      // walking left
+      lookahead_pos -= player_delta.x * config.dynamic_speed_sm;
+
+      if(lookahead_pos > RIGHTEND) {
+        lookahead_pos = RIGHTEND;
+      }
+    } else if (player_delta.x > EPSILON) {
+      // walking right
+      lookahead_pos -= player_delta.x * config.dynamic_speed_sm;
+      if(lookahead_pos < LEFTEND) {
+        lookahead_pos = LEFTEND;
+      }
+    }
+
+    if(player->peeking_direction() == ::LEFT) {
+      lookahead_pos += config.max_speed_x * elapsed_time * 3.0f;
+    } else if(player->peeking_direction() == ::RIGHT) {
+      lookahead_pos -= config.max_speed_x * elapsed_time * 3.0f;
+    }
+
+    // adjust for level ends
+    if (player_pos.x < LEFTEND) {
+      lookahead_pos = LEFTEND;
+    }
+    if (player_pos.x > sector->get_width() - LEFTEND) {
+      lookahead_pos = RIGHTEND;
+    }
+
+    translation.x = player_pos.x - lookahead_pos;
   }
 
   if(xmode != 0 && config.clamp_x > 0) {
@@ -501,3 +562,9 @@ Camera::update_scroll_to(float elapsed_time)
 
   translation = scroll_from + (scroll_goal - scroll_from) * scroll_to_pos;
 }
+
+Vector
+Camera::get_center() const {
+  return translation + Vector(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
+}
+