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.
25 #include "lisp/lisp.hpp"
26 #include "lisp/writer.hpp"
27 #include "lisp/list_iterator.hpp"
28 #include "lisp/parser.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"
42 /* this is the fractional distance toward the peek
43 position to move each frame; lower is slower,
44 0 is never get there, 1 is instant */
45 static const float PEEK_ARRIVE_RATIO = 0.1;
50 // 0 = No, 1 = Fix, 2 = Mario/Yoshi, 3 = Kirby
52 // as above, 4 = super metroid like
54 float kirby_rectsize_x;
55 float kirby_rectsize_y;
56 // where to fix the player (used for Yoshi and Fix camera)
59 // maximum scrolling speed in Y direction
62 // factor to dynamically increase max_speed_x based on player speed
63 float dynamic_max_speed_x;
65 // time the player has to face into the other direction before we assume a
70 // when too change from noscroll mode back to lookahead left/right mode
71 // set to <= 0 to disable noscroll mode
77 float dynamic_speed_sm;
88 kirby_rectsize_x = 0.2f;
89 kirby_rectsize_y = 0.34f;
92 dynamic_max_speed_x = 1.0;
93 dirchange_time = 0.2f;
94 dynamic_speed_sm = 0.8f;
97 void load(const std::string& filename)
100 const lisp::Lisp* root = parser.parse(filename);
101 const lisp::Lisp* camconfig = root->get_lisp("camera-config");
102 if(camconfig == NULL)
103 throw std::runtime_error("file is not a camera config file.");
105 camconfig->get("xmode", xmode);
106 camconfig->get("ymode", ymode);
107 camconfig->get("target-x", target_x);
108 camconfig->get("target-y", target_y);
109 camconfig->get("max-speed-x", max_speed_x);
110 camconfig->get("max-speed-y", max_speed_y);
111 camconfig->get("dynamic-max-speed-x", dynamic_max_speed_x);
112 camconfig->get("dirchange-time", dirchange_time);
113 camconfig->get("clamp-x", clamp_x);
114 camconfig->get("clamp-y", clamp_y);
115 camconfig->get("kirby-rectsize-x", kirby_rectsize_x);
116 camconfig->get("kirby-rectsize-y", kirby_rectsize_y);
117 camconfig->get("edge-x", edge_x);
118 camconfig->get("sensitive-x", sensitive_x);
119 camconfig->get("dynamic-speed-sm", dynamic_speed_sm);
123 Camera::Camera(Sector* newsector, std::string name)
124 : mode(NORMAL), sector(newsector), lookahead_mode(LOOKAHEAD_NONE)
127 config = new CameraConfig();
137 Camera::expose(HSQUIRRELVM vm, SQInteger table_idx)
139 if(name.empty()) return;
140 Scripting::Camera* interface = new Scripting::Camera(this);
141 expose_object(vm, table_idx, interface, name, true);
145 Camera::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
147 if(name.empty()) return;
148 Scripting::unexpose_object(vm, table_idx, name);
152 Camera::draw(DrawingContext& )
157 Camera::get_translation() const
163 Camera::parse(const lisp::Lisp& reader)
165 std::string modename;
167 reader.get("mode", modename);
168 if(modename == "normal") {
170 } else if(modename == "autoscroll") {
173 const lisp::Lisp* pathLisp = reader.get_lisp("path");
175 throw std::runtime_error("No path specified in autoscroll camera.");
177 autoscroll_path.reset(new Path());
178 autoscroll_path->read(*pathLisp);
179 autoscroll_walker.reset(new PathWalker(autoscroll_path.get()));
180 } else if(modename == "manual") {
183 std::stringstream str;
184 str << "invalid camera mode '" << modename << "'found in worldfile.";
185 throw std::runtime_error(str.str());
190 Camera::write(lisp::Writer& writer)
192 writer.start_list("camera");
195 writer.write_string("mode", "normal");
196 } else if(mode == AUTOSCROLL) {
197 writer.write_string("mode", "autoscroll");
198 autoscroll_path->write(writer);
199 } else if(mode == MANUAL) {
200 writer.write_string("mode", "manual");
203 writer.end_list("camera");
207 Camera::reset(const Vector& tuxpos)
209 translation.x = tuxpos.x - SCREEN_WIDTH/2;
210 translation.y = tuxpos.y - SCREEN_HEIGHT/2;
214 keep_in_bounds(translation);
216 cached_translation = translation;
220 Camera::shake(float time, float x, float y)
222 shaketimer.start(time);
225 shakespeed = M_PI/2 / time;
229 Camera::scroll_to(const Vector& goal, float scrolltime)
231 scroll_from = translation;
233 keep_in_bounds(scroll_goal);
236 scrollspeed = 1.0 / scrolltime;
240 static const float EPSILON = .00001f;
241 static const float MAX_SPEED_Y = 140;
244 Camera::update(float elapsed_time)
248 update_scroll_normal(elapsed_time);
251 update_scroll_autoscroll(elapsed_time);
254 update_scroll_to(elapsed_time);
263 Camera::reload_config()
266 config->load("camera.cfg");
267 } catch(std::exception &e) {
268 log_debug << "Couldn't load camera.cfg, using defaults ("
269 << e.what() << ")" << std::endl;
273 float clamp(float val, float min, float max)
284 Camera::keep_in_bounds(Vector& translation)
286 float width = sector->get_width();
287 float height = sector->get_height();
289 // don't scroll before the start or after the level's end
290 translation.x = clamp(translation.x, 0, width - SCREEN_WIDTH);
291 translation.y = clamp(translation.y, 0, height - SCREEN_HEIGHT);
293 if (height < SCREEN_HEIGHT)
294 translation.y = height/2.0 - SCREEN_HEIGHT/2.0;
295 if (width < SCREEN_WIDTH)
296 translation.x = width/2.0 - SCREEN_WIDTH/2.0;
302 if(shaketimer.started()) {
303 translation.x -= sin(shaketimer.get_timegone() * shakespeed) * shakedepth_x;
304 translation.y -= sin(shaketimer.get_timegone() * shakespeed) * shakedepth_y;
309 Camera::update_scroll_normal(float elapsed_time)
311 const CameraConfig& config = *(this->config);
312 Player* player = sector->player;
313 const Vector& player_pos = Vector(player->get_bbox().get_middle().x,
314 player->get_bbox().get_bottom());
315 static Vector last_player_pos = player_pos;
316 Vector player_delta = player_pos - last_player_pos;
317 last_player_pos = player_pos;
319 // check that we don't have division by zero later
320 if(elapsed_time < EPSILON)
323 /****** Vertical Scrolling part ******/
324 int xmode = config.xmode;
325 int ymode = config.ymode;
327 if(player->is_dying() || sector->get_height() == 19*32) {
330 if(player->is_dying())
334 cached_translation.y = player_pos.y - SCREEN_HEIGHT * config.target_y;
337 // target_y is the high we target our scrolling at. This is not always the
338 // high of the player, but if he is jumping upwards we should use the
339 // position where he last touched the ground. (this probably needs
340 // exceptions for trampolines and similar things in the future)
342 if(player->fall_mode == Player::JUMPING)
343 target_y = player->last_ground_y + player->get_bbox().get_height();
345 target_y = player->get_bbox().p2.y;
346 target_y -= SCREEN_HEIGHT * config.target_y;
348 // delta_y is the distance we'd have to travel to directly reach target_y
349 float delta_y = cached_translation.y - target_y;
350 // speed is the speed the camera would need to reach target_y in this frame
351 float speed_y = delta_y / elapsed_time;
353 // limit the camera speed when jumping upwards
354 if(player->fall_mode != Player::FALLING
355 && player->fall_mode != Player::TRAMPOLINE_JUMP) {
356 speed_y = clamp(speed_y, -config.max_speed_y, config.max_speed_y);
359 // scroll with calculated speed
360 cached_translation.y -= speed_y * elapsed_time;
363 float halfsize = config.kirby_rectsize_y * 0.5f;
364 cached_translation.y = clamp(cached_translation.y,
365 player_pos.y - SCREEN_HEIGHT * (0.5f + halfsize),
366 player_pos.y - SCREEN_HEIGHT * (0.5f - halfsize));
369 float upperend = SCREEN_HEIGHT * config.edge_x;
370 float lowerend = SCREEN_HEIGHT * (1 - config.edge_x);
372 if (player_delta.y < -EPSILON) {
374 lookahead_pos.y -= player_delta.y * config.dynamic_speed_sm;
376 if(lookahead_pos.y > lowerend) {
377 lookahead_pos.y = lowerend;
379 } else if (player_delta.y > EPSILON) {
381 lookahead_pos.y -= player_delta.y * config.dynamic_speed_sm;
382 if(lookahead_pos.y < upperend) {
383 lookahead_pos.y = upperend;
387 // adjust for level ends
388 if (player_pos.y < upperend) {
389 lookahead_pos.y = upperend;
391 if (player_pos.y > sector->get_width() - upperend) {
392 lookahead_pos.y = lowerend;
395 cached_translation.y = player_pos.y - lookahead_pos.y;
398 translation.y = cached_translation.y;
401 float top_edge, bottom_edge;
402 if(config.clamp_y <= 0) {
404 bottom_edge = SCREEN_HEIGHT;
406 top_edge = SCREEN_HEIGHT*config.clamp_y;
407 bottom_edge = SCREEN_HEIGHT*(1-config.clamp_y);
411 float translation_compensation = player_pos.y - translation.y;
413 if(player->peeking_direction_y() == ::UP) {
414 peek_to = bottom_edge - translation_compensation;
415 } else if(player->peeking_direction_y() == ::DOWN) {
416 peek_to = top_edge - translation_compensation;
419 float peek_move = (peek_to - peek_pos.y) * PEEK_ARRIVE_RATIO;
420 if(fabs(peek_move) < 1.0) {
424 peek_pos.y += peek_move;
426 translation.y -= peek_pos.y;
428 if(config.clamp_y > 0) {
429 translation.y = clamp(translation.y,
430 player_pos.y - SCREEN_HEIGHT * (1-config.clamp_y),
431 player_pos.y - SCREEN_HEIGHT * config.clamp_y);
432 cached_translation.y = clamp(cached_translation.y,
433 player_pos.y - SCREEN_HEIGHT * (1-config.clamp_y),
434 player_pos.y - SCREEN_HEIGHT * config.clamp_y);
438 /****** Horizontal scrolling part *******/
441 cached_translation.x = player_pos.x - SCREEN_WIDTH * config.target_x;
444 // our camera is either in leftscrolling, rightscrolling or
447 // when suddenly changing directions while scrolling into the other
448 // direction abort scrolling, since tux might be going left/right at a
449 // relatively small part of the map (like when jumping upwards)
451 // Find out direction in which the player moves
452 LookaheadMode walkDirection;
453 if (player_delta.x < -EPSILON) walkDirection = LOOKAHEAD_LEFT;
454 else if (player_delta.x > EPSILON) walkDirection = LOOKAHEAD_RIGHT;
455 else if (player->dir == ::LEFT) walkDirection = LOOKAHEAD_LEFT;
456 else walkDirection = LOOKAHEAD_RIGHT;
458 float LEFTEND, RIGHTEND;
459 if(config.sensitive_x > 0) {
460 LEFTEND = SCREEN_WIDTH * config.sensitive_x;
461 RIGHTEND = SCREEN_WIDTH * (1-config.sensitive_x);
463 LEFTEND = SCREEN_WIDTH;
467 if(lookahead_mode == LOOKAHEAD_NONE) {
468 /* if we're undecided then look if we crossed the left or right
469 * "sensitive" area */
470 if(player_pos.x < cached_translation.x + LEFTEND) {
471 lookahead_mode = LOOKAHEAD_LEFT;
472 } else if(player_pos.x > cached_translation.x + RIGHTEND) {
473 lookahead_mode = LOOKAHEAD_RIGHT;
475 /* at the ends of a level it's obvious which way we will go */
476 if(player_pos.x < SCREEN_WIDTH*0.5) {
477 lookahead_mode = LOOKAHEAD_RIGHT;
478 } else if(player_pos.x >= sector->get_width() - SCREEN_WIDTH*0.5) {
479 lookahead_mode = LOOKAHEAD_LEFT;
483 } else if(lookahead_mode != walkDirection) {
484 /* player changed direction while camera was scrolling...
485 * he has to do this for a certain time to add robustness against
488 changetime = game_time;
489 } else if(game_time - changetime > config.dirchange_time) {
490 if(lookahead_mode == LOOKAHEAD_LEFT &&
491 player_pos.x > cached_translation.x + RIGHTEND) {
492 lookahead_mode = LOOKAHEAD_RIGHT;
493 } else if(lookahead_mode == LOOKAHEAD_RIGHT &&
494 player_pos.x < cached_translation.x + LEFTEND) {
495 lookahead_mode = LOOKAHEAD_LEFT;
497 lookahead_mode = LOOKAHEAD_NONE;
504 LEFTEND = SCREEN_WIDTH * config.edge_x;
505 RIGHTEND = SCREEN_WIDTH * (1-config.edge_x);
507 // calculate our scroll target depending on scroll mode
509 if(lookahead_mode == LOOKAHEAD_LEFT)
510 target_x = player_pos.x - RIGHTEND;
511 else if(lookahead_mode == LOOKAHEAD_RIGHT)
512 target_x = player_pos.x - LEFTEND;
514 target_x = cached_translation.x;
516 // that's the distance we would have to travel to reach target_x
517 float delta_x = cached_translation.x - target_x;
518 // the speed we'd need to travel to reach target_x in this frame
519 float speed_x = delta_x / elapsed_time;
522 float player_speed_x = player_delta.x / elapsed_time;
523 float maxv = config.max_speed_x + (fabsf(player_speed_x * config.dynamic_max_speed_x));
524 speed_x = clamp(speed_x, -maxv, maxv);
527 cached_translation.x -= speed_x * elapsed_time;
530 float halfsize = config.kirby_rectsize_x * 0.5f;
531 cached_translation.x = clamp(cached_translation.x,
532 player_pos.x - SCREEN_WIDTH * (0.5f + halfsize),
533 player_pos.x - SCREEN_WIDTH * (0.5f - halfsize));
536 float LEFTEND = SCREEN_WIDTH * config.edge_x;
537 float RIGHTEND = SCREEN_WIDTH * (1 - config.edge_x);
539 if (player_delta.x < -EPSILON) {
541 lookahead_pos.x -= player_delta.x * config.dynamic_speed_sm;
542 if(lookahead_pos.x > RIGHTEND) {
543 lookahead_pos.x = RIGHTEND;
546 } else if (player_delta.x > EPSILON) {
548 lookahead_pos.x -= player_delta.x * config.dynamic_speed_sm;
549 if(lookahead_pos.x < LEFTEND) {
550 lookahead_pos.x = LEFTEND;
554 // adjust for level ends
555 if (player_pos.x < LEFTEND) {
556 lookahead_pos.x = LEFTEND;
558 if (player_pos.x > sector->get_width() - LEFTEND) {
559 lookahead_pos.x = RIGHTEND;
562 cached_translation.x = player_pos.x - lookahead_pos.x;
565 translation.x = cached_translation.x;
568 float left_edge, right_edge;
569 if(config.clamp_x <= 0) {
571 right_edge = SCREEN_WIDTH;
573 left_edge = SCREEN_WIDTH*config.clamp_x;
574 right_edge = SCREEN_WIDTH*(1-config.clamp_x);
578 float translation_compensation = player_pos.x - translation.x;
580 if(player->peeking_direction_x() == ::LEFT) {
581 peek_to = right_edge - translation_compensation;
582 } else if(player->peeking_direction_x() == ::RIGHT) {
583 peek_to = left_edge - translation_compensation;
586 float peek_move = (peek_to - peek_pos.x) * PEEK_ARRIVE_RATIO;
587 if(fabs(peek_move) < 1.0) {
591 peek_pos.x += peek_move;
593 translation.x -= peek_pos.x;
595 if(config.clamp_x > 0) {
596 translation.x = clamp(translation.x,
597 player_pos.x - SCREEN_WIDTH * (1-config.clamp_x),
598 player_pos.x - SCREEN_WIDTH * config.clamp_x);
600 cached_translation.x = clamp(cached_translation.x,
601 player_pos.x - SCREEN_WIDTH * (1-config.clamp_x),
602 player_pos.x - SCREEN_WIDTH * config.clamp_x);
606 keep_in_bounds(translation);
607 keep_in_bounds(cached_translation);
611 Camera::update_scroll_autoscroll(float elapsed_time)
613 Player* player = sector->player;
614 if(player->is_dying())
617 translation = autoscroll_walker->advance(elapsed_time);
619 keep_in_bounds(translation);
623 Camera::update_scroll_to(float elapsed_time)
625 scroll_to_pos += elapsed_time * scrollspeed;
626 if(scroll_to_pos >= 1.0) {
628 translation = scroll_goal;
632 translation = scroll_from + (scroll_goal - scroll_from) * scroll_to_pos;
636 Camera::get_center() const {
637 return translation + Vector(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);