3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2004 Matthias Braun <matze@braunis.de
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include "utils/lispwriter.h"
29 #include "app/globals.h"
32 using namespace SuperTux;
34 Camera::Camera(Sector* newsector)
35 : sector(newsector), do_backscrolling(true), scrollchange(NONE),
36 auto_idx(0), auto_t(0)
46 Camera::get_translation() const
52 Camera::read(LispReader& reader)
56 reader.read_string("mode", modename);
57 if(modename == "normal") {
60 do_backscrolling = true;
61 reader.read_bool("backscrolling", do_backscrolling);
62 } else if(modename == "autoscroll") {
65 lisp_object_t* cur = 0;
66 reader.read_lisp("path", cur);
68 throw std::runtime_error("No path specified in autoscroll camera.");
71 while(!lisp_nil_p(cur)) {
72 if(strcmp(lisp_symbol(lisp_car(lisp_car(cur))), "point") != 0) {
73 std::cerr << "Warning: unknown token in camera path.\n";
77 LispReader reader(lisp_cdr(lisp_car(cur)));
80 if(!reader.read_float("x", point.position.x) ||
81 !reader.read_float("y", point.position.y)) {
82 throw std::runtime_error("x and y missing in point of camerapath");
84 reader.read_float("speed", speed);
86 scrollpoints.push_back(point);
90 } else if(modename == "manual") {
93 std::stringstream str;
94 str << "invalid camera mode '" << modename << "'found in worldfile.";
95 throw std::runtime_error(str.str());
100 Camera::write(LispWriter& writer)
102 writer.start_list("camera");
105 writer.write_string("mode", "normal");
106 writer.write_bool("backscrolling", do_backscrolling);
107 } else if(mode == AUTOSCROLL) {
108 writer.write_string("mode", "autoscroll");
109 writer.start_list("path");
110 for(std::vector<ScrollPoint>::iterator i = scrollpoints.begin();
111 i != scrollpoints.end(); ++i) {
112 writer.start_list("point");
113 writer.write_float("x", i->position.x);
114 writer.write_float("y", i->position.y);
115 writer.write_float("speed", i->speed);
116 writer.end_list("point");
119 writer.end_list("path");
120 } else if(mode == MANUAL) {
121 writer.write_string("mode", "manual");
124 writer.end_list("camera");
128 Camera::reset(const Vector& tuxpos)
130 translation.x = tuxpos.x - screen->w/3 * 2;
131 translation.y = tuxpos.y - screen->h/2;
135 static const float EPSILON = .00001;
136 static const float max_speed_y = 1.4;
139 Camera::action(float elapsed_time)
142 scroll_normal(elapsed_time);
143 else if(mode == AUTOSCROLL)
144 scroll_autoscroll(elapsed_time);
148 Camera::keep_in_bounds()
150 float width = sector->solids->get_width() * 32;
151 float height = sector->solids->get_height() * 32;
153 // don't scroll before the start or after the level's end
154 if(translation.y > height - screen->h)
155 translation.y = height - screen->h;
156 if(translation.y < 0)
158 if(translation.x > width - screen->w)
159 translation.x = width - screen->w;
160 if(translation.x < 0)
165 Camera::scroll_normal(float elapsed_time)
168 Player* player = sector->player;
170 // check that we don't have division by zero later
171 if(elapsed_time < EPSILON)
174 /****** Vertical Scrolling part ******/
175 bool do_y_scrolling = true;
177 if(player->dying || sector->solids->get_height() == 19)
178 do_y_scrolling = false;
181 // target_y is the high we target our scrolling at. This is not always the
182 // high of the player, but if he is jumping upwards we should use the
183 // position where he last touched the ground.
185 if(player->fall_mode == Player::JUMPING)
186 target_y = player->last_ground_y + player->base.height;
188 target_y = player->base.y + player->base.height;
190 // delta_y is the distance we'd have to travel to directly reach target_y
191 float delta_y = translation.y - (target_y - screen->h/2);
192 // speed is the speed the camera would need to reach target_y in this frame
193 float speed_y = delta_y / elapsed_time;
195 // limit the camera speed when jumping upwards
196 if(player->fall_mode != Player::FALLING
197 && player->fall_mode != Player::TRAMPOLINE_JUMP) {
198 if(speed_y > max_speed_y)
199 speed_y = max_speed_y;
200 else if(speed_y < -max_speed_y)
201 speed_y = -max_speed_y;
204 // finally scroll with calculated speed
205 translation.y -= speed_y * elapsed_time;
208 /****** Horizontal scrolling part *******/
210 // our camera is either in leftscrolling, rightscrolling or nonscrollingmode.
212 // when suddenly changing directions while scrolling into the other direction.
213 // abort scrolling, since tux might be going left/right at a relatively small
214 // part of the map (like when jumping upwards)
215 if((player->dir == ::LEFT && scrollchange == RIGHT)
216 || (player->dir == ::RIGHT && scrollchange == LEFT))
218 // when in left 1/3rd of screen scroll left
219 if(player->base.x < translation.x + screen->w/3 - 16 && do_backscrolling)
221 // scroll right when in right 1/3rd of screen
222 else if(player->base.x > translation.x + screen->w/3*2 + 16)
223 scrollchange = RIGHT;
225 // calculate our scroll target depending on scroll mode
227 if(scrollchange == LEFT)
228 target_x = player->base.x - screen->w/3*2;
229 else if(scrollchange == RIGHT)
230 target_x = player->base.x - screen->w/3;
232 target_x = translation.x;
234 // that's the distance we would have to travel to reach target_x
235 float delta_x = translation.x - target_x;
236 // the speed we'd need to travel to reach target_x in this frame
237 float speed_x = 1.3 * delta_x / elapsed_time;
240 float maxv = 1.3 * (1 + fabsf(player->physic.get_velocity_x() * 1.3));
243 else if(speed_x < -maxv)
247 translation.x -= speed_x * elapsed_time;
253 Camera::scroll_autoscroll(float elapsed_time)
255 Player* player = sector->player;
260 if(auto_t - elapsed_time >= 0) {
261 translation += current_dir * elapsed_time;
262 auto_t -= elapsed_time;
264 // do the rest of the old movement
265 translation += current_dir * auto_t;
266 elapsed_time -= auto_t;
269 // construct path for next point
270 if(auto_idx+1 >= scrollpoints.size()) {
274 Vector distance = scrollpoints[auto_idx+1].position
275 - scrollpoints[auto_idx].position;
276 current_dir = distance.unit() * scrollpoints[auto_idx].speed;
277 auto_t = distance.norm() / scrollpoints[auto_idx].speed;
279 // do movement for the remaining time
280 translation += current_dir * elapsed_time;
281 auto_t -= elapsed_time;