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 "lisp/lisp.h"
26 #include "lisp/writer.h"
27 #include "lisp/list_iterator.h"
31 #include "game_session.h"
34 #include "object_factory.h"
36 Camera::Camera(Sector* newsector)
37 : sector(newsector), do_backscrolling(true), scrollchange(NONE),
38 auto_idx(0), auto_t(0)
48 Camera::get_translation() const
54 Camera::parse(const lisp::Lisp& reader)
58 reader.get("mode", modename);
59 if(modename == "normal") {
62 do_backscrolling = true;
63 reader.get("backscrolling", do_backscrolling);
64 } else if(modename == "autoscroll") {
65 printf("autoscroll.\n");
68 const lisp::Lisp* path_lisp = reader.get_lisp("path");
70 throw std::runtime_error("No path specified in autoscroll camera.");
72 lisp::ListIterator iter(path_lisp);
75 if(iter.item() != "point") {
76 std::cerr << "Warning: unknown token '" << iter.item()
77 << "' in camera path.\n";
80 const lisp::Lisp* point_lisp = iter.lisp();
83 if(!point_lisp->get("x", point.position.x) ||
84 !point_lisp->get("y", point.position.y)) {
85 throw std::runtime_error("x and y missing in point of camerapath");
87 point_lisp->get("speed", speed);
89 scrollpoints.push_back(point);
91 } else if(modename == "manual") {
94 std::stringstream str;
95 str << "invalid camera mode '" << modename << "'found in worldfile.";
96 throw std::runtime_error(str.str());
101 Camera::write(lisp::Writer& writer)
103 writer.start_list("camera");
106 writer.write_string("mode", "normal");
107 writer.write_bool("backscrolling", do_backscrolling);
108 } else if(mode == AUTOSCROLL) {
109 writer.write_string("mode", "autoscroll");
110 writer.start_list("path");
111 for(std::vector<ScrollPoint>::iterator i = scrollpoints.begin();
112 i != scrollpoints.end(); ++i) {
113 writer.start_list("point");
114 writer.write_float("x", i->position.x);
115 writer.write_float("y", i->position.y);
116 writer.write_float("speed", i->speed);
117 writer.end_list("point");
120 writer.end_list("path");
121 } else if(mode == MANUAL) {
122 writer.write_string("mode", "manual");
125 writer.end_list("camera");
129 Camera::reset(const Vector& tuxpos)
131 translation.x = tuxpos.x - SCREEN_WIDTH/3 * 2;
132 translation.y = tuxpos.y - SCREEN_HEIGHT/2;
139 Camera::shake(float time, float x, float y)
141 shaketimer.start(time);
144 shakespeed = M_PI/2 / time;
147 static const float EPSILON = .00001;
148 static const float max_speed_y = 140;
151 Camera::action(float elapsed_time)
154 scroll_normal(elapsed_time);
155 else if(mode == AUTOSCROLL)
156 scroll_autoscroll(elapsed_time);
160 Camera::keep_in_bounds()
162 float width = sector->solids->get_width() * 32;
163 float height = sector->solids->get_height() * 32;
165 // don't scroll before the start or after the level's end
166 if(translation.y > height - SCREEN_HEIGHT)
167 translation.y = height - SCREEN_HEIGHT;
168 if(translation.y < 0)
170 if(translation.x > width - SCREEN_WIDTH)
171 translation.x = width - SCREEN_WIDTH;
172 if(translation.x < 0)
179 if(shaketimer.started()) {
180 translation.x -= sin(shaketimer.get_timegone() * shakespeed) * shakedepth_x;
181 translation.y -= sin(shaketimer.get_timegone() * shakespeed) * shakedepth_y;
186 Camera::scroll_normal(float elapsed_time)
189 Player* player = sector->player;
191 // check that we don't have division by zero later
192 if(elapsed_time < EPSILON)
195 /****** Vertical Scrolling part ******/
196 bool do_y_scrolling = true;
198 if(player->is_dying() || sector->solids->get_height() == 19)
199 do_y_scrolling = false;
202 // target_y is the high we target our scrolling at. This is not always the
203 // high of the player, but if he is jumping upwards we should use the
204 // position where he last touched the ground. (this probably needs
205 // exceptions for trampolines and similar things in the future)
207 if(player->fall_mode == Player::JUMPING)
208 target_y = player->last_ground_y + player->get_bbox().get_height();
210 target_y = player->get_bbox().p2.y;
212 // delta_y is the distance we'd have to travel to directly reach target_y
213 float delta_y = translation.y - (target_y - SCREEN_HEIGHT/2);
214 // speed is the speed the camera would need to reach target_y in this frame
215 float speed_y = delta_y / elapsed_time;
217 // limit the camera speed when jumping upwards
218 if(player->fall_mode != Player::FALLING
219 && player->fall_mode != Player::TRAMPOLINE_JUMP) {
220 if(speed_y > max_speed_y)
221 speed_y = max_speed_y;
222 else if(speed_y < -max_speed_y)
223 speed_y = -max_speed_y;
226 // finally scroll with calculated speed
227 translation.y -= speed_y * elapsed_time;
230 /****** Horizontal scrolling part *******/
232 // our camera is either in leftscrolling, rightscrolling or nonscrollingmode.
234 // when suddenly changing directions while scrolling into the other direction.
235 // abort scrolling, since tux might be going left/right at a relatively small
236 // part of the map (like when jumping upwards)
237 if((player->dir == ::LEFT && scrollchange == RIGHT)
238 || (player->dir == ::RIGHT && scrollchange == LEFT))
240 // when in left 1/3rd of screen scroll left
241 if(player->get_bbox().get_middle().x < translation.x + SCREEN_WIDTH/3 - 16
244 // scroll right when in right 1/3rd of screen
245 else if(player->get_bbox().get_middle().x > translation.x + SCREEN_WIDTH/3*2+16)
246 scrollchange = RIGHT;
248 // calculate our scroll target depending on scroll mode
250 if(scrollchange == LEFT)
251 target_x = player->get_bbox().get_middle().x - SCREEN_WIDTH/3*2;
252 else if(scrollchange == RIGHT)
253 target_x = player->get_bbox().get_middle().x - SCREEN_WIDTH/3;
255 target_x = translation.x;
257 // that's the distance we would have to travel to reach target_x
258 float delta_x = translation.x - target_x;
259 // the speed we'd need to travel to reach target_x in this frame
260 float speed_x = delta_x / elapsed_time;
263 float maxv = 130 + (fabsf(player->physic.get_velocity_x() * 1.3));
266 else if(speed_x < -maxv)
270 translation.x -= speed_x * elapsed_time;
277 Camera::scroll_autoscroll(float elapsed_time)
279 Player* player = sector->player;
281 if(player->is_dying())
284 if(auto_t - elapsed_time >= 0) {
285 translation += current_dir * elapsed_time;
286 auto_t -= elapsed_time;
288 // do the rest of the old movement
289 translation += current_dir * auto_t;
290 elapsed_time -= auto_t;
293 // construct path for next point
294 if(auto_idx+1 >= scrollpoints.size()) {
298 Vector distance = scrollpoints[auto_idx+1].position
299 - scrollpoints[auto_idx].position;
300 current_dir = distance.unit() * scrollpoints[auto_idx].speed;
301 auto_t = distance.norm() / scrollpoints[auto_idx].speed;
303 // do movement for the remaining time
304 translation += current_dir * elapsed_time;
305 auto_t -= elapsed_time;