New Path based on time intervals; see levels/test/platform.stl
[supertux.git] / src / object / camera.cpp
index 1286db9..5afd926 100644 (file)
 //  You should have received a copy of the GNU General Public License
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
-
 #include <config.h>
 
 #include <stdexcept>
 #include <sstream>
 #include <cmath>
 
-#include "lisp/lisp.h"
-#include "lisp/writer.h"
-#include "lisp/list_iterator.h"
-#include "camera.h"
-#include "player.h"
-#include "tilemap.h"
-#include "gameloop.h"
-#include "app/globals.h"
-#include "sector.h"
-#include "object_factory.h"
-
-using namespace SuperTux;
+#include "lisp/lisp.hpp"
+#include "lisp/writer.hpp"
+#include "lisp/list_iterator.hpp"
+#include "camera.hpp"
+#include "player.hpp"
+#include "tilemap.hpp"
+#include "game_session.hpp"
+#include "sector.hpp"
+#include "main.hpp"
+#include "object_factory.hpp"
 
 Camera::Camera(Sector* newsector)
   : sector(newsector), do_backscrolling(true), scrollchange(NONE),
@@ -65,7 +62,6 @@ Camera::parse(const lisp::Lisp& reader)
     do_backscrolling = true;
     reader.get("backscrolling", do_backscrolling);
   } else if(modename == "autoscroll") {
-    printf("autoscroll.\n");
     mode = AUTOSCROLL;
     
     const lisp::Lisp* path_lisp = reader.get_lisp("path");
@@ -135,7 +131,7 @@ Camera::reset(const Vector& tuxpos)
   translation.y = tuxpos.y - SCREEN_HEIGHT/2;
   shakespeed = 0;
   shaketimer.stop();
-  keep_in_bounds();
+  keep_in_bounds(translation);
 }
 
 void
@@ -147,20 +143,41 @@ Camera::shake(float time, float x, float y)
   shakespeed = M_PI/2 / time;
 }
 
+void
+Camera::scroll_to(const Vector& goal, float scrolltime)
+{
+  scroll_from = translation;
+  scroll_goal = goal;
+  keep_in_bounds(scroll_goal);
+
+  scroll_to_pos = 0;
+  scrollspeed = 1.0 / scrolltime;
+  mode = SCROLLTO;
+}
+
 static const float EPSILON = .00001;
 static const float max_speed_y = 140;
 
 void
-Camera::action(float elapsed_time)
+Camera::update(float elapsed_time)
 {
-  if(mode == NORMAL)
-    scroll_normal(elapsed_time);
-  else if(mode == AUTOSCROLL)
-    scroll_autoscroll(elapsed_time);
+  switch(mode) {
+    case NORMAL:
+      update_scroll_normal(elapsed_time);
+      break;
+    case AUTOSCROLL:
+      update_scroll_autoscroll(elapsed_time);
+      break;
+    case SCROLLTO:
+      update_scroll_to(elapsed_time);
+      break;
+    default:
+      break;
+  }
 }
 
 void
-Camera::keep_in_bounds()
+Camera::keep_in_bounds(Vector& translation)
 {
   float width = sector->solids->get_width() * 32;
   float height = sector->solids->get_height() * 32;
@@ -186,7 +203,7 @@ Camera::shake()
 }
 
 void
-Camera::scroll_normal(float elapsed_time)
+Camera::update_scroll_normal(float elapsed_time)
 {
   assert(sector != 0);
   Player* player = sector->player;
@@ -213,7 +230,7 @@ Camera::scroll_normal(float elapsed_time)
       target_y = player->get_bbox().p2.y;
 
     // delta_y is the distance we'd have to travel to directly reach target_y
-    float delta_y = translation.y - (target_y - SCREEN_HEIGHT/2);
+    float delta_y = translation.y - (target_y - SCREEN_HEIGHT*2/3);
     // speed is the speed the camera would need to reach target_y in this frame
     float speed_y = delta_y / elapsed_time;
 
@@ -272,12 +289,12 @@ Camera::scroll_normal(float elapsed_time)
   // apply scrolling
   translation.x -= speed_x * elapsed_time;
 
-  keep_in_bounds();
+  keep_in_bounds(translation);
   shake();
 }
 
 void
-Camera::scroll_autoscroll(float elapsed_time)
+Camera::update_scroll_autoscroll(float elapsed_time)
 {
   Player* player = sector->player;
   
@@ -295,7 +312,7 @@ Camera::scroll_autoscroll(float elapsed_time)
 
     // construct path for next point
     if(auto_idx+1 >= scrollpoints.size()) {
-      keep_in_bounds();
+      keep_in_bounds(translation);
       return;
     }
     Vector distance = scrollpoints[auto_idx+1].position 
@@ -309,7 +326,20 @@ Camera::scroll_autoscroll(float elapsed_time)
     auto_idx++;
   }
 
-  keep_in_bounds();
+  keep_in_bounds(translation);
   shake();
 }
 
+void
+Camera::update_scroll_to(float elapsed_time)
+{
+  scroll_to_pos += elapsed_time * scrollspeed;
+  if(scroll_to_pos >= 1.0) {
+    mode = MANUAL;
+    translation = scroll_goal;
+    return;
+  }
+
+  translation = scroll_from + (scroll_goal - scroll_from) * scroll_to_pos;
+}
+