- did some C++ifying. let's try to follow suit :)
[supertux.git] / src / camera.cpp
index c25cc20..b440d0e 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 "camera.h"
 
 #include <stdexcept>
 #include <sstream>
-#include <math.h>
+#include <cmath>
+
+#include "camera.h"
 #include "lispwriter.h"
 #include "player.h"
-#include "level.h"
+#include "tilemap.h"
+#include "gameloop.h"
 #include "globals.h"
+#include "sector.h"
 
-Camera::Camera(Player* newplayer, Level* newlevel)
-  : player(newplayer), level(newlevel), do_backscrolling(true),
-    scrollchange(NONE), auto_idx(0), auto_t(0)
+Camera::Camera(Sector* newsector)
+  : sector(newsector), do_backscrolling(true), scrollchange(NONE),
+    auto_idx(0), auto_t(0)
 {
-  if(!player || !level)
-    mode = MANUAL;
-  else
-    mode = NORMAL;
+  mode = NORMAL;
 }
 
 Camera::~Camera()
 {
 }
 
-void
-Camera::set_translation(const Vector& newtranslation)
+const Vector&
+Camera::get_translation() const
 {
-  translation = newtranslation;
+  return translation;
 }
 
 void
@@ -51,17 +51,17 @@ Camera::read(LispReader& reader)
 {
   std::string modename;
   
-  reader.read_string("mode", &modename);
+  reader.read_string("mode", modename);
   if(modename == "normal") {
     mode = NORMAL;
 
     do_backscrolling = true;
-    reader.read_bool("backscrolling", &do_backscrolling);
+    reader.read_bool("backscrolling", do_backscrolling);
   } else if(modename == "autoscroll") {
     mode = AUTOSCROLL;
     
     lisp_object_t* cur = 0;
-    reader.read_lisp("path", &cur);
+    reader.read_lisp("path", cur);
     if(cur == 0) {
       throw std::runtime_error("No path specified in autoscroll camera.");
     }
@@ -75,11 +75,11 @@ Camera::read(LispReader& reader)
       LispReader reader(lisp_cdr(lisp_car(cur)));
 
       ScrollPoint point;
-      if(!reader.read_float("x", &point.position.x) ||
-         !reader.read_float("y", &point.position.y)) {
+      if(!reader.read_float("x", point.position.x) ||
+         !reader.read_float("y", point.position.y)) {
         throw std::runtime_error("x and y missing in point of camerapath");
       }
-      reader.read_float("speed", &speed);
+      reader.read_float("speed", speed);
       point.speed = speed;
       scrollpoints.push_back(point);
 
@@ -122,6 +122,14 @@ Camera::write(LispWriter& writer)
   writer.end_list("camera");
 }
 
+void
+Camera::reset(const Vector& tuxpos)
+{
+  translation.x = tuxpos.x - screen->w/3 * 2;
+  translation.y = tuxpos.y - screen->h/2;
+  keep_in_bounds();
+}
+
 static const float EPSILON = .00001;
 static const float max_speed_y = 1.4;
 
@@ -137,13 +145,16 @@ Camera::action(float elapsed_time)
 void
 Camera::keep_in_bounds()
 {
+  float width = sector->solids->get_width() * 32;
+  float height = sector->solids->get_height() * 32;
+
   // don't scroll before the start or after the level's end
-  if(translation.y > level->height * 32 - screen->h)
-    translation.y = level->height * 32 - screen->h;
+  if(translation.y > height - screen->h)
+    translation.y = height - screen->h;
   if(translation.y < 0)                                      
     translation.y = 0; 
-  if(translation.x > level->width * 32 - screen->w)
-    translation.x = level->width * 32 - screen->w;
+  if(translation.x > width - screen->w)
+    translation.x = width - screen->w;
   if(translation.x < 0)
     translation.x = 0;                                         
 }
@@ -151,7 +162,8 @@ Camera::keep_in_bounds()
 void
 Camera::scroll_normal(float elapsed_time)
 {
-  assert(level != 0 && player != 0);
+  assert(sector != 0);
+  Player* player = sector->player;
   
   // check that we don't have division by zero later
   if(elapsed_time < EPSILON)
@@ -160,7 +172,7 @@ Camera::scroll_normal(float elapsed_time)
   /****** Vertical Scrolling part ******/
   bool do_y_scrolling = true;
 
-  if(player->dying || level->height == 19)
+  if(player->dying || sector->solids->get_height() == 19)
     do_y_scrolling = false;
 
   if(do_y_scrolling) {
@@ -202,10 +214,10 @@ Camera::scroll_normal(float elapsed_time)
       || (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 && do_backscrolling)
+  if(player->base.x < translation.x + screen->w/3 - 16 && do_backscrolling)
     scrollchange = LEFT;
   // scroll right when in right 1/3rd of screen
-  else if(player->base.x > translation.x + screen->w/3*2)
+  else if(player->base.x > translation.x + screen->w/3*2 + 16)
     scrollchange = RIGHT;
 
   // calculate our scroll target depending on scroll mode
@@ -220,10 +232,10 @@ Camera::scroll_normal(float elapsed_time)
   // 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;
+  float speed_x = 1.3 * delta_x / elapsed_time;
 
   // limit our speed
-  float maxv = 1 + fabsf(player->physic.get_velocity_x() * 1.3);
+  float maxv = 1.3 * (1 + fabsf(player->physic.get_velocity_x() * 1.3));
   if(speed_x > maxv)
     speed_x = maxv;
   else if(speed_x < -maxv)
@@ -238,6 +250,8 @@ Camera::scroll_normal(float elapsed_time)
 void
 Camera::scroll_autoscroll(float elapsed_time)
 {
+  Player* player = sector->player;
+  
   if(player->dying)
     return;