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"
30 Camera::Camera(Player* newplayer, Level* newlevel)
31 : player(newplayer), level(newlevel), do_backscrolling(true),
32 scrollchange(NONE), auto_idx(0), auto_t(0)
45 Camera::get_translation() const
47 return World::current()->context.get_translation();
51 Camera::read(LispReader& reader)
55 reader.read_string("mode", &modename);
56 if(modename == "normal") {
59 do_backscrolling = true;
60 reader.read_bool("backscrolling", &do_backscrolling);
61 } else if(modename == "autoscroll") {
64 lisp_object_t* cur = 0;
65 reader.read_lisp("path", &cur);
67 throw std::runtime_error("No path specified in autoscroll camera.");
70 while(!lisp_nil_p(cur)) {
71 if(strcmp(lisp_symbol(lisp_car(lisp_car(cur))), "point") != 0) {
72 std::cerr << "Warning: unknown token in camera path.\n";
76 LispReader reader(lisp_cdr(lisp_car(cur)));
79 if(!reader.read_float("x", &point.position.x) ||
80 !reader.read_float("y", &point.position.y)) {
81 throw std::runtime_error("x and y missing in point of camerapath");
83 reader.read_float("speed", &speed);
85 scrollpoints.push_back(point);
89 } else if(modename == "manual") {
92 std::stringstream str;
93 str << "invalid camera mode '" << modename << "'found in worldfile.";
94 throw std::runtime_error(str.str());
99 Camera::write(LispWriter& writer)
101 writer.start_list("camera");
104 writer.write_string("mode", "normal");
105 writer.write_bool("backscrolling", do_backscrolling);
106 } else if(mode == AUTOSCROLL) {
107 writer.write_string("mode", "autoscroll");
108 writer.start_list("path");
109 for(std::vector<ScrollPoint>::iterator i = scrollpoints.begin();
110 i != scrollpoints.end(); ++i) {
111 writer.start_list("point");
112 writer.write_float("x", i->position.x);
113 writer.write_float("y", i->position.y);
114 writer.write_float("speed", i->speed);
115 writer.end_list("point");
118 writer.end_list("path");
119 } else if(mode == MANUAL) {
120 writer.write_string("mode", "manual");
123 writer.end_list("camera");
126 static const float EPSILON = .00001;
127 static const float max_speed_y = 1.4;
130 Camera::action(float elapsed_time)
132 translation = World::current()->context.get_translation();
134 scroll_normal(elapsed_time);
135 else if(mode == AUTOSCROLL)
136 scroll_autoscroll(elapsed_time);
137 World::current()->context.set_translation(translation);
141 Camera::keep_in_bounds()
143 // don't scroll before the start or after the level's end
144 if(translation.y > level->height * 32 - screen->h)
145 translation.y = level->height * 32 - screen->h;
146 if(translation.y < 0)
148 if(translation.x > level->width * 32 - screen->w)
149 translation.x = level->width * 32 - screen->w;
150 if(translation.x < 0)
155 Camera::scroll_normal(float elapsed_time)
157 assert(level != 0 && player != 0);
159 // check that we don't have division by zero later
160 if(elapsed_time < EPSILON)
163 /****** Vertical Scrolling part ******/
164 bool do_y_scrolling = true;
166 if(player->dying || level->height == 19)
167 do_y_scrolling = false;
170 // target_y is the high we target our scrolling at. This is not always the
171 // high of the player, but if he is jumping upwards we should use the
172 // position where he last touched the ground.
174 if(player->fall_mode == Player::JUMPING)
175 target_y = player->last_ground_y + player->base.height;
177 target_y = player->base.y + player->base.height;
179 // delta_y is the distance we'd have to travel to directly reach target_y
180 float delta_y = translation.y - (target_y - screen->h/2);
181 // speed is the speed the camera would need to reach target_y in this frame
182 float speed_y = delta_y / elapsed_time;
184 // limit the camera speed when jumping upwards
185 if(player->fall_mode != Player::FALLING
186 && player->fall_mode != Player::TRAMPOLINE_JUMP) {
187 if(speed_y > max_speed_y)
188 speed_y = max_speed_y;
189 else if(speed_y < -max_speed_y)
190 speed_y = -max_speed_y;
193 // finally scroll with calculated speed
194 translation.y -= speed_y * elapsed_time;
197 /****** Horizontal scrolling part *******/
199 // our camera is either in leftscrolling, rightscrolling or nonscrollingmode.
201 // when suddenly changing directions while scrolling into the other direction.
202 // abort scrolling, since tux might be going left/right at a relatively small
203 // part of the map (like when jumping upwards)
204 if((player->dir == ::LEFT && scrollchange == RIGHT)
205 || (player->dir == ::RIGHT && scrollchange == LEFT))
207 // when in left 1/3rd of screen scroll left
208 if(player->base.x < translation.x + screen->w/3 - 16 && do_backscrolling)
210 // scroll right when in right 1/3rd of screen
211 else if(player->base.x > translation.x + screen->w/3*2 + 16)
212 scrollchange = RIGHT;
214 // calculate our scroll target depending on scroll mode
216 if(scrollchange == LEFT)
217 target_x = player->base.x - screen->w/3*2;
218 else if(scrollchange == RIGHT)
219 target_x = player->base.x - screen->w/3;
221 target_x = translation.x;
223 // that's the distance we would have to travel to reach target_x
224 float delta_x = translation.x - target_x;
225 // the speed we'd need to travel to reach target_x in this frame
226 float speed_x = 1.3 * delta_x / elapsed_time;
229 float maxv = 1.3 * (1 + fabsf(player->physic.get_velocity_x() * 1.3));
232 else if(speed_x < -maxv)
236 translation.x -= speed_x * elapsed_time;
242 Camera::scroll_autoscroll(float elapsed_time)
247 if(auto_t - elapsed_time >= 0) {
248 translation += current_dir * elapsed_time;
249 auto_t -= elapsed_time;
251 // do the rest of the old movement
252 translation += current_dir * auto_t;
253 elapsed_time -= auto_t;
256 // construct path for next point
257 if(auto_idx+1 >= scrollpoints.size()) {
261 Vector distance = scrollpoints[auto_idx+1].position
262 - scrollpoints[auto_idx].position;
263 current_dir = distance.unit() * scrollpoints[auto_idx].speed;
264 auto_t = distance.norm() / scrollpoints[auto_idx].speed;
266 // do movement for the remaining time
267 translation += current_dir * elapsed_time;
268 auto_t -= elapsed_time;