X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fobject%2Fcamera.cpp;h=476ca412362b1cd7fdc24e2a575931fa15e83f93;hb=ddc3b7ef2077567d368e244a292b41187311e8e3;hp=246207f559a33699f1eba309bec27c095bd5b428;hpb=e3bb6e46812f108f093e9ad0751a945c34b18cd3;p=supertux.git diff --git a/src/object/camera.cpp b/src/object/camera.cpp index 246207f55..476ca4123 100644 --- a/src/object/camera.cpp +++ b/src/object/camera.cpp @@ -16,7 +16,6 @@ // 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 #include @@ -29,13 +28,11 @@ #include "camera.h" #include "player.h" #include "tilemap.h" -#include "gameloop.h" -#include "app/globals.h" +#include "game_session.h" #include "sector.h" +#include "main.h" #include "object_factory.h" -using namespace SuperTux; - Camera::Camera(Sector* newsector) : sector(newsector), do_backscrolling(true), scrollchange(NONE), auto_idx(0), auto_t(0) @@ -131,11 +128,22 @@ Camera::write(lisp::Writer& 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; @@ -155,17 +163,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); @@ -178,7 +195,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) { @@ -193,7 +210,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; @@ -221,19 +238,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; @@ -253,6 +270,7 @@ Camera::scroll_normal(float elapsed_time) translation.x -= speed_x * elapsed_time; keep_in_bounds(); + shake(); } void @@ -260,7 +278,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) { @@ -289,5 +307,6 @@ Camera::scroll_autoscroll(float elapsed_time) } keep_in_bounds(); + shake(); }