-Started to move stuff from library back to main game
[supertux.git] / src / object / camera.cpp
index b2f47ed..f355917 100644 (file)
 #include <sstream>
 #include <cmath>
 
+#include "lisp/lisp.h"
+#include "lisp/writer.h"
+#include "lisp/list_iterator.h"
 #include "camera.h"
-#include "utils/lispreader.h"
-#include "utils/lispwriter.h"
 #include "player.h"
 #include "tilemap.h"
-#include "gameloop.h"
+#include "game_session.h"
 #include "app/globals.h"
 #include "sector.h"
+#include "object_factory.h"
 
 using namespace SuperTux;
 
@@ -52,43 +54,42 @@ Camera::get_translation() const
 }
 
 void
-Camera::parse(LispReader& reader)
+Camera::parse(const lisp::Lisp& reader)
 {
   std::string modename;
   
-  reader.read_string("mode", modename);
+  reader.get("mode", modename);
   if(modename == "normal") {
     mode = NORMAL;
 
     do_backscrolling = true;
-    reader.read_bool("backscrolling", do_backscrolling);
+    reader.get("backscrolling", do_backscrolling);
   } else if(modename == "autoscroll") {
+    printf("autoscroll.\n");
     mode = AUTOSCROLL;
     
-    lisp_object_t* cur = 0;
-    reader.read_lisp("path", cur);
-    if(cur == 0) {
+    const lisp::Lisp* path_lisp = reader.get_lisp("path");
+    if(!path_lisp)
       throw std::runtime_error("No path specified in autoscroll camera.");
-    }
-    float speed = 50;
-    while(!lisp_nil_p(cur)) {
-      if(strcmp(lisp_symbol(lisp_car(lisp_car(cur))), "point") != 0) {
-        std::cerr << "Warning: unknown token in camera path.\n";
+
+    lisp::ListIterator iter(path_lisp);
+    float speed = .5;
+    while(iter.next()) {
+      if(iter.item() != "point") {
+        std::cerr << "Warning: unknown token '" << iter.item() 
+          << "' in camera path.\n";
         continue;
       }
-           
-      LispReader reader(lisp_cdr(lisp_car(cur)));
+      const lisp::Lisp* point_lisp = iter.lisp();
 
       ScrollPoint point;
-      if(!reader.read_float("x", point.position.x) ||
-         !reader.read_float("y", point.position.y)) {
+      if(!point_lisp->get("x", point.position.x) ||
+         !point_lisp->get("y", point.position.y)) {
         throw std::runtime_error("x and y missing in point of camerapath");
       }
-      reader.read_float("speed", speed);
+      point_lisp->get("speed", speed);
       point.speed = speed;
       scrollpoints.push_back(point);
-
-      cur = lisp_cdr(cur);
     }
   } else if(modename == "manual") {
     mode = MANUAL;
@@ -100,7 +101,7 @@ Camera::parse(LispReader& reader)
 }
 
 void
-Camera::write(LispWriter& writer)
+Camera::write(lisp::Writer& writer)
 {
   writer.start_list("camera");
   
@@ -130,11 +131,22 @@ Camera::write(LispWriter& writer)
 void
 Camera::reset(const Vector& tuxpos)
 {
-  translation.x = tuxpos.x - screen->w/3 * 2;
-  translation.y = tuxpos.y - screen->h/2;
+  translation.x = tuxpos.x - SCREEN_WIDTH/3 * 2;
+  translation.y = tuxpos.y - SCREEN_HEIGHT/2;
+  shakespeed = 0;
+  shaketimer.stop();
   keep_in_bounds();
 }
 
+void
+Camera::shake(float time, float x, float y)
+{
+  shaketimer.start(time);
+  shakedepth_x = x;
+  shakedepth_y = y;
+  shakespeed = M_PI/2 / time;
+}
+
 static const float EPSILON = .00001;
 static const float max_speed_y = 140;
 
@@ -154,17 +166,26 @@ Camera::keep_in_bounds()
   float height = sector->solids->get_height() * 32;
 
   // don't scroll before the start or after the level's end
-  if(translation.y > height - screen->h)
-    translation.y = height - screen->h;
+  if(translation.y > height - SCREEN_HEIGHT)
+    translation.y = height - SCREEN_HEIGHT;
   if(translation.y < 0)                                      
     translation.y = 0; 
-  if(translation.x > width - screen->w)
-    translation.x = width - screen->w;
+  if(translation.x > width - SCREEN_WIDTH)
+    translation.x = width - SCREEN_WIDTH;
   if(translation.x < 0)
     translation.x = 0;                                         
 }
 
 void
+Camera::shake()
+{
+  if(shaketimer.started()) {
+    translation.x -= sin(shaketimer.get_timegone() * shakespeed) * shakedepth_x;
+    translation.y -= sin(shaketimer.get_timegone() * shakespeed) * shakedepth_y;
+  }
+}
+
+void
 Camera::scroll_normal(float elapsed_time)
 {
   assert(sector != 0);
@@ -177,7 +198,7 @@ Camera::scroll_normal(float elapsed_time)
   /****** Vertical Scrolling part ******/
   bool do_y_scrolling = true;
 
-  if(player->dying || sector->solids->get_height() == 19)
+  if(player->is_dying() || sector->solids->get_height() == 19)
     do_y_scrolling = false;
 
   if(do_y_scrolling) {
@@ -192,7 +213,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->h/2);
+    float delta_y = translation.y - (target_y - SCREEN_HEIGHT/2);
     // speed is the speed the camera would need to reach target_y in this frame
     float speed_y = delta_y / elapsed_time;
 
@@ -220,19 +241,19 @@ Camera::scroll_normal(float elapsed_time)
       || (player->dir == ::RIGHT && scrollchange == LEFT))
     scrollchange = NONE;
   // when in left 1/3rd of screen scroll left
-  if(player->get_bbox().get_middle().x < translation.x + screen->w/3 - 16
+  if(player->get_bbox().get_middle().x < translation.x + SCREEN_WIDTH/3 - 16
       && do_backscrolling)
     scrollchange = LEFT;
   // scroll right when in right 1/3rd of screen
-  else if(player->get_bbox().get_middle().x > translation.x + screen->w/3*2+16)
+  else if(player->get_bbox().get_middle().x > translation.x + SCREEN_WIDTH/3*2+16)
     scrollchange = RIGHT;
 
   // calculate our scroll target depending on scroll mode
   float target_x;
   if(scrollchange == LEFT)
-    target_x = player->get_bbox().get_middle().x - screen->w/3*2;
+    target_x = player->get_bbox().get_middle().x - SCREEN_WIDTH/3*2;
   else if(scrollchange == RIGHT)
-    target_x = player->get_bbox().get_middle().x - screen->w/3;
+    target_x = player->get_bbox().get_middle().x - SCREEN_WIDTH/3;
   else
     target_x = translation.x;
 
@@ -252,6 +273,7 @@ Camera::scroll_normal(float elapsed_time)
   translation.x -= speed_x * elapsed_time;
 
   keep_in_bounds();
+  shake();
 }
 
 void
@@ -259,7 +281,7 @@ Camera::scroll_autoscroll(float elapsed_time)
 {
   Player* player = sector->player;
   
-  if(player->dying)
+  if(player->is_dying())
     return;
 
   if(auto_t - elapsed_time >= 0) {
@@ -288,4 +310,6 @@ Camera::scroll_autoscroll(float elapsed_time)
   }
 
   keep_in_bounds();
+  shake();
 }
+