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.
28 #include "lisp/lisp.hpp"
29 #include "lisp/writer.hpp"
30 #include "lisp/list_iterator.hpp"
31 #include "lisp/parser.hpp"
32 #include "scripting/camera.hpp"
33 #include "scripting/squirrel_util.hpp"
35 #include "tilemap.hpp"
36 #include "game_session.hpp"
39 #include "object_factory.hpp"
42 #include "path_walker.hpp"
44 /* this is the fractional distance toward the peek
45 position to move each frame; lower is slower,
46 0 is never get there, 1 is instant */
47 static const float PEEK_ARRIVE_RATIO = 0.1;
52 // 0 = No, 1 = Fix, 2 = Mario/Yoshi, 3 = Kirby, 4 = Super Metroid-like
56 float kirby_rectsize_x;
57 float kirby_rectsize_y;
58 // where to fix the player (used for Yoshi and Fix camera)
61 // maximum scrolling speed in Y direction
64 // factor to dynamically increase max_speed_x based on player speed
65 float dynamic_max_speed_x;
67 // time the player has to face into the other direction before we assume a
72 // when too change from noscroll mode back to lookahead left/right mode
73 // set to <= 0 to disable noscroll mode
79 float dynamic_speed_sm;
90 kirby_rectsize_x = 0.2f;
91 kirby_rectsize_y = 0.34f;
94 dynamic_max_speed_x = 1.0;
95 dirchange_time = 0.2f;
96 dynamic_speed_sm = 0.8f;
99 void load(const std::string& filename)
102 const lisp::Lisp* root = parser.parse(filename);
103 const lisp::Lisp* camconfig = root->get_lisp("camera-config");
104 if(camconfig == NULL)
105 throw std::runtime_error("file is not a camera config file.");
107 camconfig->get("xmode", xmode);
108 camconfig->get("ymode", ymode);
109 camconfig->get("target-x", target_x);
110 camconfig->get("target-y", target_y);
111 camconfig->get("max-speed-x", max_speed_x);
112 camconfig->get("max-speed-y", max_speed_y);
113 camconfig->get("dynamic-max-speed-x", dynamic_max_speed_x);
114 camconfig->get("dirchange-time", dirchange_time);
115 camconfig->get("clamp-x", clamp_x);
116 camconfig->get("clamp-y", clamp_y);
117 camconfig->get("kirby-rectsize-x", kirby_rectsize_x);
118 camconfig->get("kirby-rectsize-y", kirby_rectsize_y);
119 camconfig->get("edge-x", edge_x);
120 camconfig->get("sensitive-x", sensitive_x);
121 camconfig->get("dynamic-speed-sm", dynamic_speed_sm);
125 Camera::Camera(Sector* newsector, std::string name)
126 : mode(NORMAL), sector(newsector), lookahead_mode(LOOKAHEAD_NONE)
129 config = new CameraConfig();
139 Camera::expose(HSQUIRRELVM vm, SQInteger table_idx)
141 if(name.empty()) return;
142 Scripting::Camera* interface = new Scripting::Camera(this);
143 expose_object(vm, table_idx, interface, name, true);
147 Camera::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
149 if(name.empty()) return;
150 Scripting::unexpose_object(vm, table_idx, name);
154 Camera::draw(DrawingContext& )
159 Camera::get_translation() const
165 Camera::parse(const lisp::Lisp& reader)
167 std::string modename;
169 reader.get("mode", modename);
170 if(modename == "normal") {
172 } else if(modename == "autoscroll") {
175 const lisp::Lisp* pathLisp = reader.get_lisp("path");
177 throw std::runtime_error("No path specified in autoscroll camera.");
179 autoscroll_path.reset(new Path());
180 autoscroll_path->read(*pathLisp);
181 autoscroll_walker.reset(new PathWalker(autoscroll_path.get()));
182 } else if(modename == "manual") {
185 std::stringstream str;
186 str << "invalid camera mode '" << modename << "'found in worldfile.";
187 throw std::runtime_error(str.str());
192 Camera::write(lisp::Writer& writer)
194 writer.start_list("camera");
197 writer.write("mode", "normal");
198 } else if(mode == AUTOSCROLL) {
199 writer.write("mode", "autoscroll");
200 autoscroll_path->write(writer);
201 } else if(mode == MANUAL) {
202 writer.write("mode", "manual");
205 writer.end_list("camera");
209 Camera::reset(const Vector& tuxpos)
211 translation.x = tuxpos.x - SCREEN_WIDTH/2;
212 translation.y = tuxpos.y - SCREEN_HEIGHT/2;
216 keep_in_bounds(translation);
218 cached_translation = translation;
222 Camera::shake(float time, float x, float y)
224 shaketimer.start(time);
227 shakespeed = M_PI/2 / time;
231 Camera::scroll_to(const Vector& goal, float scrolltime)
233 scroll_from = translation;
235 keep_in_bounds(scroll_goal);
238 scrollspeed = 1.0 / scrolltime;
242 static const float EPSILON = .00001f;
243 static const float MAX_SPEED_Y = 140;
246 Camera::update(float elapsed_time)
250 update_scroll_normal(elapsed_time);
253 update_scroll_autoscroll(elapsed_time);
256 update_scroll_to(elapsed_time);
265 Camera::reload_config()
267 if(PHYSFS_exists("camera.cfg")) {
269 config->load("camera.cfg");
270 log_info << "Loaded camera.cfg." << std::endl;
271 } catch(std::exception &e) {
272 log_debug << "Couldn't load camera.cfg, using defaults ("
273 << e.what() << ")" << std::endl;
278 float clamp(float val, float min, float max)
289 Camera::keep_in_bounds(Vector& translation)
291 float width = sector->get_width();
292 float height = sector->get_height();
294 // don't scroll before the start or after the level's end
295 translation.x = clamp(translation.x, 0, width - SCREEN_WIDTH);
296 translation.y = clamp(translation.y, 0, height - SCREEN_HEIGHT);
298 if (height < SCREEN_HEIGHT)
299 translation.y = height/2.0 - SCREEN_HEIGHT/2.0;
300 if (width < SCREEN_WIDTH)
301 translation.x = width/2.0 - SCREEN_WIDTH/2.0;
307 if(shaketimer.started()) {
308 translation.x -= sin(shaketimer.get_timegone() * shakespeed) * shakedepth_x;
309 translation.y -= sin(shaketimer.get_timegone() * shakespeed) * shakedepth_y;
314 Camera::update_scroll_normal(float elapsed_time)
316 const CameraConfig& config = *(this->config);
317 Player* player = sector->player;
318 const Vector& player_pos = Vector(player->get_bbox().get_middle().x,
319 player->get_bbox().get_bottom());
320 static Vector last_player_pos = player_pos;
321 Vector player_delta = player_pos - last_player_pos;
322 last_player_pos = player_pos;
324 // check that we don't have division by zero later
325 if(elapsed_time < EPSILON)
328 /****** Vertical Scrolling part ******/
329 int ymode = config.ymode;
331 if(player->is_dying() || sector->get_height() == 19*32) {
335 cached_translation.y = player_pos.y - SCREEN_HEIGHT * config.target_y;
338 // target_y is the high we target our scrolling at. This is not always the
339 // high of the player, but if he is jumping upwards we should use the
340 // position where he last touched the ground. (this probably needs
341 // exceptions for trampolines and similar things in the future)
343 if(player->fall_mode == Player::JUMPING)
344 target_y = player->last_ground_y + player->get_bbox().get_height();
346 target_y = player->get_bbox().p2.y;
347 target_y -= SCREEN_HEIGHT * config.target_y;
349 // delta_y is the distance we'd have to travel to directly reach target_y
350 float delta_y = cached_translation.y - target_y;
351 // speed is the speed the camera would need to reach target_y in this frame
352 float speed_y = delta_y / elapsed_time;
354 // limit the camera speed when jumping upwards
355 if(player->fall_mode != Player::FALLING
356 && player->fall_mode != Player::TRAMPOLINE_JUMP) {
357 speed_y = clamp(speed_y, -config.max_speed_y, config.max_speed_y);
360 // scroll with calculated speed
361 cached_translation.y -= speed_y * elapsed_time;
364 float halfsize = config.kirby_rectsize_y * 0.5f;
365 cached_translation.y = clamp(cached_translation.y,
366 player_pos.y - SCREEN_HEIGHT * (0.5f + halfsize),
367 player_pos.y - SCREEN_HEIGHT * (0.5f - halfsize));
370 float upperend = SCREEN_HEIGHT * config.edge_x;
371 float lowerend = SCREEN_HEIGHT * (1 - config.edge_x);
373 if (player_delta.y < -EPSILON) {
375 lookahead_pos.y -= player_delta.y * config.dynamic_speed_sm;
377 if(lookahead_pos.y > lowerend) {
378 lookahead_pos.y = lowerend;
380 } else if (player_delta.y > EPSILON) {
382 lookahead_pos.y -= player_delta.y * config.dynamic_speed_sm;
383 if(lookahead_pos.y < upperend) {
384 lookahead_pos.y = upperend;
388 // adjust for level ends
389 if (player_pos.y < upperend) {
390 lookahead_pos.y = upperend;
392 if (player_pos.y > sector->get_width() - upperend) {
393 lookahead_pos.y = lowerend;
396 cached_translation.y = player_pos.y - lookahead_pos.y;
399 translation.y = cached_translation.y;
402 float top_edge, bottom_edge;
403 if(config.clamp_y <= 0) {
405 bottom_edge = SCREEN_HEIGHT;
407 top_edge = SCREEN_HEIGHT*config.clamp_y;
408 bottom_edge = SCREEN_HEIGHT*(1-config.clamp_y);
412 float translation_compensation = player_pos.y - translation.y;
414 if(player->peeking_direction_y() == ::UP) {
415 peek_to = bottom_edge - translation_compensation;
416 } else if(player->peeking_direction_y() == ::DOWN) {
417 peek_to = top_edge - translation_compensation;
420 float peek_move = (peek_to - peek_pos.y) * PEEK_ARRIVE_RATIO;
421 if(fabs(peek_move) < 1.0) {
425 peek_pos.y += peek_move;
427 translation.y -= peek_pos.y;
429 if(config.clamp_y > 0) {
430 translation.y = clamp(translation.y,
431 player_pos.y - SCREEN_HEIGHT * (1-config.clamp_y),
432 player_pos.y - SCREEN_HEIGHT * config.clamp_y);
433 cached_translation.y = clamp(cached_translation.y,
434 player_pos.y - SCREEN_HEIGHT * (1-config.clamp_y),
435 player_pos.y - SCREEN_HEIGHT * config.clamp_y);
439 /****** Horizontal scrolling part *******/
440 int xmode = config.xmode;
442 if(player->is_dying())
446 cached_translation.x = player_pos.x - SCREEN_WIDTH * config.target_x;
449 // our camera is either in leftscrolling, rightscrolling or
452 // when suddenly changing directions while scrolling into the other
453 // direction abort scrolling, since tux might be going left/right at a
454 // relatively small part of the map (like when jumping upwards)
456 // Find out direction in which the player moves
457 LookaheadMode walkDirection;
458 if (player_delta.x < -EPSILON) walkDirection = LOOKAHEAD_LEFT;
459 else if (player_delta.x > EPSILON) walkDirection = LOOKAHEAD_RIGHT;
460 else if (player->dir == ::LEFT) walkDirection = LOOKAHEAD_LEFT;
461 else walkDirection = LOOKAHEAD_RIGHT;
463 float LEFTEND, RIGHTEND;
464 if(config.sensitive_x > 0) {
465 LEFTEND = SCREEN_WIDTH * config.sensitive_x;
466 RIGHTEND = SCREEN_WIDTH * (1-config.sensitive_x);
468 LEFTEND = SCREEN_WIDTH;
472 if(lookahead_mode == LOOKAHEAD_NONE) {
473 /* if we're undecided then look if we crossed the left or right
474 * "sensitive" area */
475 if(player_pos.x < cached_translation.x + LEFTEND) {
476 lookahead_mode = LOOKAHEAD_LEFT;
477 } else if(player_pos.x > cached_translation.x + RIGHTEND) {
478 lookahead_mode = LOOKAHEAD_RIGHT;
480 /* at the ends of a level it's obvious which way we will go */
481 if(player_pos.x < SCREEN_WIDTH*0.5) {
482 lookahead_mode = LOOKAHEAD_RIGHT;
483 } else if(player_pos.x >= sector->get_width() - SCREEN_WIDTH*0.5) {
484 lookahead_mode = LOOKAHEAD_LEFT;
488 } else if(lookahead_mode != walkDirection) {
489 /* player changed direction while camera was scrolling...
490 * he has to do this for a certain time to add robustness against
493 changetime = game_time;
494 } else if(game_time - changetime > config.dirchange_time) {
495 if(lookahead_mode == LOOKAHEAD_LEFT &&
496 player_pos.x > cached_translation.x + RIGHTEND) {
497 lookahead_mode = LOOKAHEAD_RIGHT;
498 } else if(lookahead_mode == LOOKAHEAD_RIGHT &&
499 player_pos.x < cached_translation.x + LEFTEND) {
500 lookahead_mode = LOOKAHEAD_LEFT;
502 lookahead_mode = LOOKAHEAD_NONE;
509 LEFTEND = SCREEN_WIDTH * config.edge_x;
510 RIGHTEND = SCREEN_WIDTH * (1-config.edge_x);
512 // calculate our scroll target depending on scroll mode
514 if(lookahead_mode == LOOKAHEAD_LEFT)
515 target_x = player_pos.x - RIGHTEND;
516 else if(lookahead_mode == LOOKAHEAD_RIGHT)
517 target_x = player_pos.x - LEFTEND;
519 target_x = cached_translation.x;
521 // that's the distance we would have to travel to reach target_x
522 float delta_x = cached_translation.x - target_x;
523 // the speed we'd need to travel to reach target_x in this frame
524 float speed_x = delta_x / elapsed_time;
527 float player_speed_x = player_delta.x / elapsed_time;
528 float maxv = config.max_speed_x + (fabsf(player_speed_x * config.dynamic_max_speed_x));
529 speed_x = clamp(speed_x, -maxv, maxv);
532 cached_translation.x -= speed_x * elapsed_time;
535 float halfsize = config.kirby_rectsize_x * 0.5f;
536 cached_translation.x = clamp(cached_translation.x,
537 player_pos.x - SCREEN_WIDTH * (0.5f + halfsize),
538 player_pos.x - SCREEN_WIDTH * (0.5f - halfsize));
541 float LEFTEND = SCREEN_WIDTH * config.edge_x;
542 float RIGHTEND = SCREEN_WIDTH * (1 - config.edge_x);
544 if (player_delta.x < -EPSILON) {
546 lookahead_pos.x -= player_delta.x * config.dynamic_speed_sm;
547 if(lookahead_pos.x > RIGHTEND) {
548 lookahead_pos.x = RIGHTEND;
551 } else if (player_delta.x > EPSILON) {
553 lookahead_pos.x -= player_delta.x * config.dynamic_speed_sm;
554 if(lookahead_pos.x < LEFTEND) {
555 lookahead_pos.x = LEFTEND;
559 // adjust for level ends
560 if (player_pos.x < LEFTEND) {
561 lookahead_pos.x = LEFTEND;
563 if (player_pos.x > sector->get_width() - LEFTEND) {
564 lookahead_pos.x = RIGHTEND;
567 cached_translation.x = player_pos.x - lookahead_pos.x;
570 translation.x = cached_translation.x;
573 float left_edge, right_edge;
574 if(config.clamp_x <= 0) {
576 right_edge = SCREEN_WIDTH;
578 left_edge = SCREEN_WIDTH*config.clamp_x;
579 right_edge = SCREEN_WIDTH*(1-config.clamp_x);
583 float translation_compensation = player_pos.x - translation.x;
585 if(player->peeking_direction_x() == ::LEFT) {
586 peek_to = right_edge - translation_compensation;
587 } else if(player->peeking_direction_x() == ::RIGHT) {
588 peek_to = left_edge - translation_compensation;
591 float peek_move = (peek_to - peek_pos.x) * PEEK_ARRIVE_RATIO;
592 if(fabs(peek_move) < 1.0) {
596 peek_pos.x += peek_move;
598 translation.x -= peek_pos.x;
600 if(config.clamp_x > 0) {
601 translation.x = clamp(translation.x,
602 player_pos.x - SCREEN_WIDTH * (1-config.clamp_x),
603 player_pos.x - SCREEN_WIDTH * config.clamp_x);
605 cached_translation.x = clamp(cached_translation.x,
606 player_pos.x - SCREEN_WIDTH * (1-config.clamp_x),
607 player_pos.x - SCREEN_WIDTH * config.clamp_x);
611 keep_in_bounds(translation);
612 keep_in_bounds(cached_translation);
616 Camera::update_scroll_autoscroll(float elapsed_time)
618 Player* player = sector->player;
619 if(player->is_dying())
622 translation = autoscroll_walker->advance(elapsed_time);
624 keep_in_bounds(translation);
628 Camera::update_scroll_to(float elapsed_time)
630 scroll_to_pos += elapsed_time * scrollspeed;
631 if(scroll_to_pos >= 1.0) {
633 translation = scroll_goal;
637 translation = scroll_from + (scroll_goal - scroll_from) * scroll_to_pos;
641 Camera::get_center() const {
642 return translation + Vector(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);