4 // Copyright (C) 2006 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.
26 #include "lisp/lisp.hpp"
27 #include "lisp/writer.hpp"
28 #include "lisp/list_iterator.hpp"
29 #include "scripting/camera.hpp"
30 #include "scripting/squirrel_util.hpp"
33 #include "tilemap.hpp"
34 #include "game_session.hpp"
37 #include "object_factory.hpp"
40 #include "path_walker.hpp"
43 enum CameraStyle { CameraStyleYI, CameraStyleKD };
44 const CameraStyle cameraStyle = CameraStyleKD;
47 Camera::Camera(Sector* newsector, std::string name)
48 : mode(NORMAL), sector(newsector), do_backscrolling(true),
59 Camera::expose(HSQUIRRELVM vm, SQInteger table_idx)
61 if(name.empty()) return;
62 Scripting::Camera* interface = new Scripting::Camera(this);
63 expose_object(vm, table_idx, interface, name, true);
67 Camera::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
69 if(name.empty()) return;
70 Scripting::unexpose_object(vm, table_idx, name);
74 Camera::get_translation() const
80 Camera::parse(const lisp::Lisp& reader)
84 reader.get("mode", modename);
85 if(modename == "normal") {
88 do_backscrolling = true;
89 reader.get("backscrolling", do_backscrolling);
90 } else if(modename == "autoscroll") {
93 const lisp::Lisp* pathLisp = reader.get_lisp("path");
95 throw std::runtime_error("No path specified in autoscroll camera.");
97 autoscroll_path.reset(new Path());
98 autoscroll_path->read(*pathLisp);
99 autoscroll_walker.reset(new PathWalker(autoscroll_path.get()));
100 } else if(modename == "manual") {
103 std::stringstream str;
104 str << "invalid camera mode '" << modename << "'found in worldfile.";
105 throw std::runtime_error(str.str());
110 Camera::write(lisp::Writer& writer)
112 writer.start_list("camera");
115 writer.write_string("mode", "normal");
116 writer.write_bool("backscrolling", do_backscrolling);
117 } else if(mode == AUTOSCROLL) {
118 writer.write_string("mode", "autoscroll");
119 autoscroll_path->write(writer);
120 } else if(mode == MANUAL) {
121 writer.write_string("mode", "manual");
124 writer.end_list("camera");
128 Camera::reset_kd(const Vector& tuxpos)
130 translation.x = tuxpos.x - (SCREEN_WIDTH * 0.5);
131 translation.y = tuxpos.y - (SCREEN_HEIGHT * 0.5);
135 keep_in_bounds(translation);
140 Camera::reset(const Vector& tuxpos)
142 if (cameraStyle == CameraStyleKD) {
147 translation.x = tuxpos.x - SCREEN_WIDTH/3 * 2;
148 translation.y = tuxpos.y - SCREEN_HEIGHT/2;
151 keep_in_bounds(translation);
155 Camera::shake(float time, float x, float y)
157 shaketimer.start(time);
160 shakespeed = M_PI/2 / time;
164 Camera::scroll_to(const Vector& goal, float scrolltime)
166 scroll_from = translation;
168 keep_in_bounds(scroll_goal);
171 scrollspeed = 1.0 / scrolltime;
175 static const float EPSILON = .00001f;
176 static const float max_speed_y = 140;
179 Camera::update(float elapsed_time)
183 update_scroll_normal(elapsed_time);
186 update_scroll_autoscroll(elapsed_time);
189 update_scroll_to(elapsed_time);
197 Camera::keep_in_bounds(Vector& translation)
199 float width = sector->get_width();
200 float height = sector->get_height();
202 // don't scroll before the start or after the level's end
203 if(translation.y > height - SCREEN_HEIGHT)
204 translation.y = height - SCREEN_HEIGHT;
205 if(translation.y < 0)
207 if (height < SCREEN_HEIGHT)
208 translation.y = height/2.0 - SCREEN_HEIGHT/2.0;
209 if(translation.x > width - SCREEN_WIDTH)
210 translation.x = width - SCREEN_WIDTH;
211 if(translation.x < 0)
213 if (width < SCREEN_WIDTH)
214 translation.x = width/2.0 - SCREEN_WIDTH/2.0;
220 if(shaketimer.started()) {
221 translation.x -= sin(shaketimer.get_timegone() * shakespeed) * shakedepth_x;
222 translation.y -= sin(shaketimer.get_timegone() * shakespeed) * shakedepth_y;
227 Camera::update_scroll_normal_kd(float elapsed_time)
229 // make sure some time has actually passed
230 if(elapsed_time < EPSILON) return;
232 // make sure we have an active player
234 Player* player = sector->player;
235 Vector playerCenter = player->get_bbox().get_middle();
237 // If player is peeking, scroll in that direction
238 if (player->peeking_direction() == ::LEFT) {
239 translation.x -= elapsed_time * 128.0f;
241 else if (player->peeking_direction() == ::RIGHT) {
242 translation.x += elapsed_time * 128.0f;
245 // keep player within a small box, centered on the screen (vertical part)
246 bool do_y_scrolling = true;
247 if (player->is_dying() || sector->get_height() == 19*32) do_y_scrolling = false;
248 if (do_y_scrolling) {
249 translation.y = std::min(player->get_bbox().p1.y - SCREEN_HEIGHT * (0.5f - 0.17f), translation.y);
250 translation.y = std::max(player->get_bbox().p2.y - SCREEN_HEIGHT * (0.5f + 0.17f), translation.y);
253 // keep player within a small box, centered on the screen (horizontal part)
254 translation.x = std::min(player->get_bbox().p1.x - SCREEN_WIDTH * (0.5f - 0.1f), translation.x);
255 translation.x = std::max(player->get_bbox().p2.x - SCREEN_WIDTH * (0.5f + 0.1f), translation.x);
257 // make sure camera doesn't point outside level borders
258 keep_in_bounds(translation);
260 // handle shaking of camera (if applicable)
265 Camera::update_scroll_normal(float elapsed_time)
267 if (cameraStyle == CameraStyleKD) {
268 update_scroll_normal_kd(elapsed_time);
273 Player* player = sector->player;
275 // check that we don't have division by zero later
276 if(elapsed_time < EPSILON)
279 /****** Vertical Scrolling part ******/
280 bool do_y_scrolling = true;
282 if(player->is_dying() || sector->get_height() == 19*32)
283 do_y_scrolling = false;
286 // target_y is the high we target our scrolling at. This is not always the
287 // high of the player, but if he is jumping upwards we should use the
288 // position where he last touched the ground. (this probably needs
289 // exceptions for trampolines and similar things in the future)
291 if(player->fall_mode == Player::JUMPING)
292 target_y = player->last_ground_y + player->get_bbox().get_height();
294 target_y = player->get_bbox().p2.y;
296 // delta_y is the distance we'd have to travel to directly reach target_y
297 float delta_y = translation.y - (target_y - SCREEN_HEIGHT*2/3);
298 // speed is the speed the camera would need to reach target_y in this frame
299 float speed_y = delta_y / elapsed_time;
301 // limit the camera speed when jumping upwards
302 if(player->fall_mode != Player::FALLING
303 && player->fall_mode != Player::TRAMPOLINE_JUMP) {
304 if(speed_y > max_speed_y)
305 speed_y = max_speed_y;
306 else if(speed_y < -max_speed_y)
307 speed_y = -max_speed_y;
310 // finally scroll with calculated speed
311 translation.y -= speed_y * elapsed_time;
313 // make sure to always keep the player inside the middle 1/6 of the screen
314 translation.y = std::min(player->get_bbox().p1.y - SCREEN_HEIGHT*1/6, translation.y);
315 translation.y = std::max(player->get_bbox().p2.y - SCREEN_HEIGHT*5/6, translation.y);
318 /****** Horizontal scrolling part *******/
320 // our camera is either in leftscrolling, rightscrolling or nonscrollingmode.
322 // when suddenly changing directions while scrolling into the other direction.
323 // abort scrolling, since tux might be going left/right at a relatively small
324 // part of the map (like when jumping upwards)
327 // Find out direction in which the player walks: We want to try and show a
328 // bit more of what's in front of the player and less of what's behind
329 LeftRightScrollChange walkDirection;
330 if (player->physic.get_velocity_x() < -EPSILON) walkDirection = LEFT;
331 else if (player->physic.get_velocity_x() > EPSILON) walkDirection = RIGHT;
332 else if (player->dir == ::LEFT) walkDirection = LEFT;
333 else walkDirection = RIGHT;
336 if((walkDirection == LEFT && scrollchange == RIGHT)
337 || (walkDirection == RIGHT && scrollchange == LEFT))
339 // when in left 1/3rd of screen scroll left
340 if(player->get_bbox().get_middle().x < translation.x + SCREEN_WIDTH/3 - 16
343 // scroll right when in right 1/3rd of screen
344 else if(player->get_bbox().get_middle().x > translation.x + SCREEN_WIDTH/3*2+16)
345 scrollchange = RIGHT;
347 // calculate our scroll target depending on scroll mode
349 if(scrollchange == LEFT)
350 target_x = player->get_bbox().get_middle().x - SCREEN_WIDTH/3*2;
351 else if(scrollchange == RIGHT)
352 target_x = player->get_bbox().get_middle().x - SCREEN_WIDTH/3;
354 target_x = translation.x;
356 // that's the distance we would have to travel to reach target_x
357 float delta_x = translation.x - target_x;
358 // the speed we'd need to travel to reach target_x in this frame
359 float speed_x = delta_x / elapsed_time;
362 float maxv = 130 + (fabsf(player->physic.get_velocity_x() * 1.3));
365 else if(speed_x < -maxv)
368 // If player is peeking scroll in that direction. Fast.
369 if( player->peeking_direction() == ::LEFT ){
372 if( player->peeking_direction() == ::RIGHT ){
377 translation.x -= speed_x * elapsed_time;
379 // make sure to always keep the player inside the middle 4/6 of the screen
380 translation.x = std::min(player->get_bbox().p1.x - SCREEN_WIDTH*1/6, translation.x);
381 translation.x = std::max(player->get_bbox().p2.x - SCREEN_WIDTH*5/6, translation.x);
383 keep_in_bounds(translation);
388 Camera::update_scroll_autoscroll(float elapsed_time)
390 Player* player = sector->player;
391 if(player->is_dying())
394 translation = autoscroll_walker->advance(elapsed_time);
396 keep_in_bounds(translation);
401 Camera::update_scroll_to(float elapsed_time)
403 scroll_to_pos += elapsed_time * scrollspeed;
404 if(scroll_to_pos >= 1.0) {
406 translation = scroll_goal;
410 translation = scroll_from + (scroll_goal - scroll_from) * scroll_to_pos;