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.
27 #include "utils/lispreader.h"
28 #include "utils/lispwriter.h"
32 #include "app/globals.h"
35 using namespace SuperTux;
37 Camera::Camera(Sector* newsector)
38 : sector(newsector), do_backscrolling(true), scrollchange(NONE),
39 auto_idx(0), auto_t(0)
49 Camera::get_translation() const
55 Camera::read(LispReader& reader)
59 reader.read_string("mode", modename);
60 if(modename == "normal") {
63 do_backscrolling = true;
64 reader.read_bool("backscrolling", do_backscrolling);
65 } else if(modename == "autoscroll") {
68 lisp_object_t* cur = 0;
69 reader.read_lisp("path", cur);
71 throw std::runtime_error("No path specified in autoscroll camera.");
74 while(!lisp_nil_p(cur)) {
75 if(strcmp(lisp_symbol(lisp_car(lisp_car(cur))), "point") != 0) {
76 std::cerr << "Warning: unknown token in camera path.\n";
80 LispReader reader(lisp_cdr(lisp_car(cur)));
83 if(!reader.read_float("x", point.position.x) ||
84 !reader.read_float("y", point.position.y)) {
85 throw std::runtime_error("x and y missing in point of camerapath");
87 reader.read_float("speed", speed);
89 scrollpoints.push_back(point);
93 } else if(modename == "manual") {
96 std::stringstream str;
97 str << "invalid camera mode '" << modename << "'found in worldfile.";
98 throw std::runtime_error(str.str());
103 Camera::write(LispWriter& writer)
105 writer.start_list("camera");
108 writer.write_string("mode", "normal");
109 writer.write_bool("backscrolling", do_backscrolling);
110 } else if(mode == AUTOSCROLL) {
111 writer.write_string("mode", "autoscroll");
112 writer.start_list("path");
113 for(std::vector<ScrollPoint>::iterator i = scrollpoints.begin();
114 i != scrollpoints.end(); ++i) {
115 writer.start_list("point");
116 writer.write_float("x", i->position.x);
117 writer.write_float("y", i->position.y);
118 writer.write_float("speed", i->speed);
119 writer.end_list("point");
122 writer.end_list("path");
123 } else if(mode == MANUAL) {
124 writer.write_string("mode", "manual");
127 writer.end_list("camera");
131 Camera::reset(const Vector& tuxpos)
133 translation.x = tuxpos.x - screen->w/3 * 2;
134 translation.y = tuxpos.y - screen->h/2;
138 static const float EPSILON = .00001;
139 static const float max_speed_y = 140;
142 Camera::action(float elapsed_time)
145 scroll_normal(elapsed_time);
146 else if(mode == AUTOSCROLL)
147 scroll_autoscroll(elapsed_time);
151 Camera::keep_in_bounds()
153 float width = sector->solids->get_width() * 32;
154 float height = sector->solids->get_height() * 32;
156 // don't scroll before the start or after the level's end
157 if(translation.y > height - screen->h)
158 translation.y = height - screen->h;
159 if(translation.y < 0)
161 if(translation.x > width - screen->w)
162 translation.x = width - screen->w;
163 if(translation.x < 0)
168 Camera::scroll_normal(float elapsed_time)
171 Player* player = sector->player;
173 // check that we don't have division by zero later
174 if(elapsed_time < EPSILON)
177 /****** Vertical Scrolling part ******/
178 bool do_y_scrolling = true;
180 if(player->dying || sector->solids->get_height() == 19)
181 do_y_scrolling = false;
184 // target_y is the high we target our scrolling at. This is not always the
185 // high of the player, but if he is jumping upwards we should use the
186 // position where he last touched the ground. (this probably needs
187 // exceptions for trampolines and similar things in the future)
189 if(player->fall_mode == Player::JUMPING)
190 target_y = player->last_ground_y + player->get_bbox().get_height();
192 target_y = player->get_bbox().p2.y;
194 // delta_y is the distance we'd have to travel to directly reach target_y
195 float delta_y = translation.y - (target_y - screen->h/2);
196 // speed is the speed the camera would need to reach target_y in this frame
197 float speed_y = delta_y / elapsed_time;
199 // limit the camera speed when jumping upwards
200 if(player->fall_mode != Player::FALLING
201 && player->fall_mode != Player::TRAMPOLINE_JUMP) {
202 if(speed_y > max_speed_y)
203 speed_y = max_speed_y;
204 else if(speed_y < -max_speed_y)
205 speed_y = -max_speed_y;
208 // finally scroll with calculated speed
209 translation.y -= speed_y * elapsed_time;
212 /****** Horizontal scrolling part *******/
214 // our camera is either in leftscrolling, rightscrolling or nonscrollingmode.
216 // when suddenly changing directions while scrolling into the other direction.
217 // abort scrolling, since tux might be going left/right at a relatively small
218 // part of the map (like when jumping upwards)
219 if((player->dir == ::LEFT && scrollchange == RIGHT)
220 || (player->dir == ::RIGHT && scrollchange == LEFT))
222 // when in left 1/3rd of screen scroll left
223 if(player->get_bbox().get_middle().x < translation.x + screen->w/3 - 16
226 // scroll right when in right 1/3rd of screen
227 else if(player->get_bbox().get_middle().x > translation.x + screen->w/3*2+16)
228 scrollchange = RIGHT;
230 // calculate our scroll target depending on scroll mode
232 if(scrollchange == LEFT)
233 target_x = player->get_bbox().get_middle().x - screen->w/3*2;
234 else if(scrollchange == RIGHT)
235 target_x = player->get_bbox().get_middle().x - screen->w/3;
237 target_x = translation.x;
239 // that's the distance we would have to travel to reach target_x
240 float delta_x = translation.x - target_x;
241 // the speed we'd need to travel to reach target_x in this frame
242 float speed_x = delta_x / elapsed_time;
245 float maxv = 130 + (fabsf(player->physic.get_velocity_x() * 1.3));
248 else if(speed_x < -maxv)
252 translation.x -= speed_x * elapsed_time;
258 Camera::scroll_autoscroll(float elapsed_time)
260 Player* player = sector->player;
265 if(auto_t - elapsed_time >= 0) {
266 translation += current_dir * elapsed_time;
267 auto_t -= elapsed_time;
269 // do the rest of the old movement
270 translation += current_dir * auto_t;
271 elapsed_time -= auto_t;
274 // construct path for next point
275 if(auto_idx+1 >= scrollpoints.size()) {
279 Vector distance = scrollpoints[auto_idx+1].position
280 - scrollpoints[auto_idx].position;
281 current_dir = distance.unit() * scrollpoints[auto_idx].speed;
282 auto_t = distance.norm() / scrollpoints[auto_idx].speed;
284 // do movement for the remaining time
285 translation += current_dir * elapsed_time;
286 auto_t -= elapsed_time;