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.
24 #include "lispwriter.h"
29 Camera::Camera(Player* newplayer, Level* newlevel)
30 : player(newplayer), level(newlevel), do_backscrolling(true),
31 scrollchange(NONE), auto_idx(0), auto_t(0)
44 Camera::set_translation(const Vector& newtranslation)
46 translation = newtranslation;
50 Camera::read(LispReader& reader)
54 reader.read_string("mode", &modename);
55 if(modename == "normal") {
58 do_backscrolling = true;
59 reader.read_bool("backscrolling", &do_backscrolling);
60 } else if(modename == "autoscroll") {
63 lisp_object_t* cur = 0;
64 reader.read_lisp("path", &cur);
66 throw std::runtime_error("No path specified in autoscroll camera.");
69 while(!lisp_nil_p(cur)) {
70 if(strcmp(lisp_symbol(lisp_car(lisp_car(cur))), "point") != 0) {
71 std::cerr << "Warning: unknown token in camera path.\n";
75 LispReader reader(lisp_cdr(lisp_car(cur)));
78 if(!reader.read_float("x", &point.position.x) ||
79 !reader.read_float("y", &point.position.y)) {
80 throw std::runtime_error("x and y missing in point of camerapath");
82 reader.read_float("speed", &speed);
84 scrollpoints.push_back(point);
88 } else if(modename == "manual") {
91 std::stringstream str;
92 str << "invalid camera mode '" << modename << "'found in worldfile.";
93 throw std::runtime_error(str.str());
98 Camera::write(LispWriter& writer)
100 writer.start_list("camera");
103 writer.write_string("mode", "normal");
104 writer.write_bool("backscrolling", do_backscrolling);
105 } else if(mode == AUTOSCROLL) {
106 writer.write_string("mode", "autoscroll");
107 writer.start_list("path");
108 for(std::vector<ScrollPoint>::iterator i = scrollpoints.begin();
109 i != scrollpoints.end(); ++i) {
110 writer.start_list("point");
111 writer.write_float("x", i->position.x);
112 writer.write_float("y", i->position.y);
113 writer.write_float("speed", i->speed);
114 writer.end_list("point");
117 writer.end_list("path");
118 } else if(mode == MANUAL) {
119 writer.write_string("mode", "manual");
122 writer.end_list("camera");
125 static const float EPSILON = .00001;
126 static const float max_speed_y = 1.4;
129 Camera::action(float elapsed_time)
132 scroll_normal(elapsed_time);
133 else if(mode == AUTOSCROLL)
134 scroll_autoscroll(elapsed_time);
138 Camera::keep_in_bounds()
140 // don't scroll before the start or after the level's end
141 if(translation.y > level->height * 32 - screen->h)
142 translation.y = level->height * 32 - screen->h;
143 if(translation.y < 0)
145 if(translation.x > level->width * 32 - screen->w)
146 translation.x = level->width * 32 - screen->w;
147 if(translation.x < 0)
152 Camera::scroll_normal(float elapsed_time)
154 assert(level != 0 && player != 0);
156 // check that we don't have division by zero later
157 if(elapsed_time < EPSILON)
160 /****** Vertical Scrolling part ******/
161 bool do_y_scrolling = true;
163 if(player->dying || level->height == 19)
164 do_y_scrolling = false;
167 // target_y is the high we target our scrolling at. This is not always the
168 // high of the player, but if he is jumping upwards we should use the
169 // position where he last touched the ground.
171 if(player->fall_mode == Player::JUMPING)
172 target_y = player->last_ground_y + player->base.height;
174 target_y = player->base.y + player->base.height;
176 // delta_y is the distance we'd have to travel to directly reach target_y
177 float delta_y = translation.y - (target_y - screen->h/2);
178 // speed is the speed the camera would need to reach target_y in this frame
179 float speed_y = delta_y / elapsed_time;
181 // limit the camera speed when jumping upwards
182 if(player->fall_mode != Player::FALLING
183 && player->fall_mode != Player::TRAMPOLINE_JUMP) {
184 if(speed_y > max_speed_y)
185 speed_y = max_speed_y;
186 else if(speed_y < -max_speed_y)
187 speed_y = -max_speed_y;
190 // finally scroll with calculated speed
191 translation.y -= speed_y * elapsed_time;
194 /****** Horizontal scrolling part *******/
196 // our camera is either in leftscrolling, rightscrolling or nonscrollingmode.
198 // when suddenly changing directions while scrolling into the other direction.
199 // abort scrolling, since tux might be going left/right at a relatively small
200 // part of the map (like when jumping upwards)
201 if((player->dir == ::LEFT && scrollchange == RIGHT)
202 || (player->dir == ::RIGHT && scrollchange == LEFT))
204 // when in left 1/3rd of screen scroll left
205 if(player->base.x < translation.x + screen->w/3 - 16 && do_backscrolling)
207 // scroll right when in right 1/3rd of screen
208 else if(player->base.x > translation.x + screen->w/3*2 + 16)
209 scrollchange = RIGHT;
211 // calculate our scroll target depending on scroll mode
213 if(scrollchange == LEFT)
214 target_x = player->base.x - screen->w/3*2;
215 else if(scrollchange == RIGHT)
216 target_x = player->base.x - screen->w/3;
218 target_x = translation.x;
220 // that's the distance we would have to travel to reach target_x
221 float delta_x = translation.x - target_x;
222 // the speed we'd need to travel to reach target_x in this frame
223 float speed_x = 1.3 * delta_x / elapsed_time;
226 float maxv = 1.3 * (1 + fabsf(player->physic.get_velocity_x() * 1.3));
229 else if(speed_x < -maxv)
233 translation.x -= speed_x * elapsed_time;
239 Camera::scroll_autoscroll(float elapsed_time)
244 if(auto_t - elapsed_time >= 0) {
245 translation += current_dir * elapsed_time;
246 auto_t -= elapsed_time;
248 // do the rest of the old movement
249 translation += current_dir * auto_t;
250 elapsed_time -= auto_t;
253 // construct path for next point
254 if(auto_idx+1 >= scrollpoints.size()) {
258 Vector distance = scrollpoints[auto_idx+1].position
259 - scrollpoints[auto_idx].position;
260 current_dir = distance.unit() * scrollpoints[auto_idx].speed;
261 auto_t = distance.norm() / scrollpoints[auto_idx].speed;
263 // do movement for the remaining time
264 translation += current_dir * elapsed_time;
265 auto_t -= elapsed_time;