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"
45 // 0 = No, 1 = Fix, 2 = Mario/Yoshi, 3 = Kirby
47 // as above, 4 = super metroid like
49 float kirby_rectsize_x;
50 float kirby_rectsize_y;
51 // where to fix the player (used for Yoshi and Fix camera)
54 // maximum scrolling speed in Y direction
57 // factor to dynamically increase max_speed_x based on player speed
58 float dynamic_max_speed_x;
60 // time the player has to face into the other direction before we assume a
65 // when too change from noscroll mode back to lookahead left/right mode
66 // set to <= 0 to disable noscroll mode
72 float dynamic_speed_sm;
83 kirby_rectsize_x = 0.2f;
84 kirby_rectsize_y = 0.34f;
86 sensitive_x = 1.f/4.f;
87 dynamic_max_speed_x = 1.0;
88 dirchange_time = 0.2f;
89 dynamic_speed_sm = 1.0f;
92 void load(const std::string& filename)
95 const lisp::Lisp* root = parser.parse(filename);
96 const lisp::Lisp* camconfig = root->get_lisp("camera-config");
98 throw std::runtime_error("file is not a camera config file.");
100 camconfig->get("xmode", xmode);
101 camconfig->get("ymode", ymode);
102 camconfig->get("target-x", target_x);
103 camconfig->get("target-y", target_y);
104 camconfig->get("max-speed-x", max_speed_x);
105 camconfig->get("max-speed-y", max_speed_y);
106 camconfig->get("dynamic-max-speed-x", dynamic_max_speed_x);
107 camconfig->get("dirchange-time", dirchange_time);
108 camconfig->get("clamp-x", clamp_x);
109 camconfig->get("clamp-y", clamp_y);
110 camconfig->get("kirby-rectsize-x", kirby_rectsize_x);
111 camconfig->get("kirby-rectsize-y", kirby_rectsize_y);
112 camconfig->get("edge-x", edge_x);
113 camconfig->get("sensitive-x", sensitive_x);
114 camconfig->get("dynamic-speed-sm", dynamic_speed_sm);
118 Camera::Camera(Sector* newsector, std::string name)
119 : mode(NORMAL), sector(newsector), lookahead_mode(LOOKAHEAD_NONE),
123 config = new CameraConfig();
133 Camera::expose(HSQUIRRELVM vm, SQInteger table_idx)
135 if(name.empty()) return;
136 Scripting::Camera* interface = new Scripting::Camera(this);
137 expose_object(vm, table_idx, interface, name, true);
141 Camera::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
143 if(name.empty()) return;
144 Scripting::unexpose_object(vm, table_idx, name);
148 Camera::draw(DrawingContext& )
153 Camera::get_translation() const
159 Camera::parse(const lisp::Lisp& reader)
161 std::string modename;
163 reader.get("mode", modename);
164 if(modename == "normal") {
166 } else if(modename == "autoscroll") {
169 const lisp::Lisp* pathLisp = reader.get_lisp("path");
171 throw std::runtime_error("No path specified in autoscroll camera.");
173 autoscroll_path.reset(new Path());
174 autoscroll_path->read(*pathLisp);
175 autoscroll_walker.reset(new PathWalker(autoscroll_path.get()));
176 } else if(modename == "manual") {
179 std::stringstream str;
180 str << "invalid camera mode '" << modename << "'found in worldfile.";
181 throw std::runtime_error(str.str());
186 Camera::write(lisp::Writer& writer)
188 writer.start_list("camera");
191 writer.write_string("mode", "normal");
192 } else if(mode == AUTOSCROLL) {
193 writer.write_string("mode", "autoscroll");
194 autoscroll_path->write(writer);
195 } else if(mode == MANUAL) {
196 writer.write_string("mode", "manual");
199 writer.end_list("camera");
203 Camera::reset(const Vector& tuxpos)
205 translation.x = tuxpos.x - SCREEN_WIDTH/2;
206 translation.y = tuxpos.y - SCREEN_HEIGHT/2;
209 keep_in_bounds(translation);
213 Camera::shake(float time, float x, float y)
215 shaketimer.start(time);
218 shakespeed = M_PI/2 / time;
222 Camera::scroll_to(const Vector& goal, float scrolltime)
224 scroll_from = translation;
226 keep_in_bounds(scroll_goal);
229 scrollspeed = 1.0 / scrolltime;
233 static const float EPSILON = .00001f;
234 static const float MAX_SPEED_Y = 140;
237 Camera::update(float elapsed_time)
241 update_scroll_normal(elapsed_time);
244 update_scroll_autoscroll(elapsed_time);
247 update_scroll_to(elapsed_time);
256 Camera::reload_config()
258 config->load("camera.cfg");
261 float clamp(float val, float min, float max)
272 Camera::keep_in_bounds(Vector& translation)
274 float width = sector->get_width();
275 float height = sector->get_height();
277 // don't scroll before the start or after the level's end
278 translation.x = clamp(translation.x, 0, width - SCREEN_WIDTH);
279 translation.y = clamp(translation.y, 0, height - SCREEN_HEIGHT);
281 if (height < SCREEN_HEIGHT)
282 translation.y = height/2.0 - SCREEN_HEIGHT/2.0;
283 if (width < SCREEN_WIDTH)
284 translation.x = width/2.0 - SCREEN_WIDTH/2.0;
290 if(shaketimer.started()) {
291 translation.x -= sin(shaketimer.get_timegone() * shakespeed) * shakedepth_x;
292 translation.y -= sin(shaketimer.get_timegone() * shakespeed) * shakedepth_y;
297 Camera::update_scroll_normal(float elapsed_time)
299 const CameraConfig& config = *(this->config);
300 Player* player = sector->player;
301 const Vector& player_pos = player->get_bbox().get_middle();
302 static Vector last_player_pos = player_pos;
303 Vector player_delta = player_pos - last_player_pos;
304 last_player_pos = player_pos;
306 // check that we don't have division by zero later
307 if(elapsed_time < EPSILON)
310 /****** Vertical Scrolling part ******/
311 int xmode = config.xmode;
312 int ymode = config.ymode;
314 if(player->is_dying() || sector->get_height() == 19*32) {
317 if(player->is_dying())
321 translation.y = player_pos.y - SCREEN_HEIGHT * config.target_y;
324 // target_y is the high we target our scrolling at. This is not always the
325 // high of the player, but if he is jumping upwards we should use the
326 // position where he last touched the ground. (this probably needs
327 // exceptions for trampolines and similar things in the future)
329 if(player->fall_mode == Player::JUMPING)
330 target_y = player->last_ground_y + player->get_bbox().get_height();
332 target_y = player->get_bbox().p2.y;
333 target_y -= SCREEN_HEIGHT * config.target_y;
335 // delta_y is the distance we'd have to travel to directly reach target_y
336 float delta_y = translation.y - target_y;
337 // speed is the speed the camera would need to reach target_y in this frame
338 float speed_y = delta_y / elapsed_time;
340 // limit the camera speed when jumping upwards
341 if(player->fall_mode != Player::FALLING
342 && player->fall_mode != Player::TRAMPOLINE_JUMP) {
343 speed_y = clamp(speed_y, -config.max_speed_y, config.max_speed_y);
346 // scroll with calculated speed
347 translation.y -= speed_y * elapsed_time;
350 float halfsize = config.kirby_rectsize_y * 0.5f;
351 translation.y = clamp(translation.y,
352 player_pos.y - SCREEN_HEIGHT * (0.5f + halfsize),
353 player_pos.y - SCREEN_HEIGHT * (0.5f - halfsize));
356 float upperend = SCREEN_WIDTH * config.edge_x;
357 float lowerend = SCREEN_WIDTH * (1 - config.edge_x);
359 if (player_delta.y < -EPSILON) {
361 lookahead_pos -= player_delta.x * config.dynamic_speed_sm;
363 if(lookahead_pos > lowerend) {
364 lookahead_pos = lowerend;
366 } else if (player_delta.y > EPSILON) {
368 lookahead_pos -= player_delta.y * config.dynamic_speed_sm;
369 if(lookahead_pos < upperend) {
370 lookahead_pos = upperend;
374 // adjust for level ends
375 if (player_pos.y < upperend) {
376 lookahead_pos = upperend;
378 if (player_pos.y > sector->get_width() - upperend) {
379 lookahead_pos = lowerend;
382 translation.y = player_pos.y - lookahead_pos;
385 if(ymode != 0 && config.clamp_y > 0) {
386 translation.y = clamp(translation.y,
387 player_pos.y - SCREEN_HEIGHT * (1-config.clamp_y),
388 player_pos.y - SCREEN_HEIGHT * config.clamp_y);
391 /****** Horizontal scrolling part *******/
394 translation.x = player_pos.x - SCREEN_WIDTH * config.target_x;
397 // our camera is either in leftscrolling, rightscrolling or
400 // when suddenly changing directions while scrolling into the other
401 // direction abort scrolling, since tux might be going left/right at a
402 // relatively small part of the map (like when jumping upwards)
404 // Find out direction in which the player moves
405 LookaheadMode walkDirection;
406 if (player_delta.x < -EPSILON) walkDirection = LOOKAHEAD_LEFT;
407 else if (player_delta.x > EPSILON) walkDirection = LOOKAHEAD_RIGHT;
408 else if (player->dir == ::LEFT) walkDirection = LOOKAHEAD_LEFT;
409 else walkDirection = LOOKAHEAD_RIGHT;
411 float LEFTEND, RIGHTEND;
412 if(config.sensitive_x > 0) {
413 LEFTEND = SCREEN_WIDTH * config.sensitive_x;
414 RIGHTEND = SCREEN_WIDTH * (1-config.sensitive_x);
416 LEFTEND = SCREEN_WIDTH;
420 if(lookahead_mode == LOOKAHEAD_NONE) {
421 /* if we're undecided then look if we crossed the left or right
422 * "sensitive" area */
423 if(player_pos.x < translation.x + LEFTEND) {
424 lookahead_mode = LOOKAHEAD_LEFT;
425 } else if(player_pos.x > translation.x + RIGHTEND) {
426 lookahead_mode = LOOKAHEAD_RIGHT;
428 /* at the ends of a level it's obvious which way we will go */
429 if(player_pos.x < SCREEN_WIDTH*0.5) {
430 lookahead_mode = LOOKAHEAD_RIGHT;
431 } else if(player_pos.x >= sector->get_width() - SCREEN_WIDTH*0.5) {
432 lookahead_mode = LOOKAHEAD_LEFT;
436 } else if(lookahead_mode != walkDirection) {
437 /* player changed direction while camera was scrolling...
438 * he has to do this for a certain time to add robustness against
441 changetime = game_time;
442 } else if(game_time - changetime > config.dirchange_time) {
443 if(lookahead_mode == LOOKAHEAD_LEFT &&
444 player_pos.x > translation.x + RIGHTEND) {
445 lookahead_mode = LOOKAHEAD_RIGHT;
446 } else if(lookahead_mode == LOOKAHEAD_RIGHT &&
447 player_pos.x < translation.x + LEFTEND) {
448 lookahead_mode = LOOKAHEAD_LEFT;
450 lookahead_mode = LOOKAHEAD_NONE;
457 LEFTEND = SCREEN_WIDTH * config.edge_x;
458 RIGHTEND = SCREEN_WIDTH * (1-config.edge_x);
460 // calculate our scroll target depending on scroll mode
462 if(lookahead_mode == LOOKAHEAD_LEFT)
463 target_x = player_pos.x - RIGHTEND;
464 else if(lookahead_mode == LOOKAHEAD_RIGHT)
465 target_x = player_pos.x - LEFTEND;
467 target_x = translation.x;
469 // that's the distance we would have to travel to reach target_x
470 float delta_x = translation.x - target_x;
471 // the speed we'd need to travel to reach target_x in this frame
472 float speed_x = delta_x / elapsed_time;
475 float player_speed_x = player_delta.x / elapsed_time;
476 float maxv = config.max_speed_x + (fabsf(player_speed_x * config.dynamic_max_speed_x));
477 speed_x = clamp(speed_x, -maxv, maxv);
479 // If player is peeking scroll in that direction. Fast.
480 if(player->peeking_direction() == ::LEFT) {
481 speed_x = config.max_speed_x;
483 if(player->peeking_direction() == ::RIGHT) {
484 speed_x = -config.max_speed_x;
488 translation.x -= speed_x * elapsed_time;
491 float halfsize = config.kirby_rectsize_x * 0.5f;
492 translation.x = clamp(translation.x,
493 player_pos.x - SCREEN_WIDTH * (0.5f + halfsize),
494 player_pos.x - SCREEN_WIDTH * (0.5f - halfsize));
497 float LEFTEND = SCREEN_WIDTH * config.edge_x;
498 float RIGHTEND = SCREEN_WIDTH * (1 - config.edge_x);
500 if (player_delta.x < -EPSILON) {
502 lookahead_pos -= player_delta.x * config.dynamic_speed_sm;
504 if(lookahead_pos > RIGHTEND) {
505 lookahead_pos = RIGHTEND;
507 } else if (player_delta.x > EPSILON) {
509 lookahead_pos -= player_delta.x * config.dynamic_speed_sm;
510 if(lookahead_pos < LEFTEND) {
511 lookahead_pos = LEFTEND;
515 if(player->peeking_direction() == ::LEFT) {
516 lookahead_pos += config.max_speed_x * elapsed_time * 3.0f;
517 } else if(player->peeking_direction() == ::RIGHT) {
518 lookahead_pos -= config.max_speed_x * elapsed_time * 3.0f;
521 // adjust for level ends
522 if (player_pos.x < LEFTEND) {
523 lookahead_pos = LEFTEND;
525 if (player_pos.x > sector->get_width() - LEFTEND) {
526 lookahead_pos = RIGHTEND;
529 translation.x = player_pos.x - lookahead_pos;
532 if(xmode != 0 && config.clamp_x > 0) {
533 translation.x = clamp(translation.x,
534 player_pos.x - SCREEN_WIDTH * (1-config.clamp_x),
535 player_pos.x - SCREEN_WIDTH * config.clamp_x);
538 keep_in_bounds(translation);
542 Camera::update_scroll_autoscroll(float elapsed_time)
544 Player* player = sector->player;
545 if(player->is_dying())
548 translation = autoscroll_walker->advance(elapsed_time);
550 keep_in_bounds(translation);
554 Camera::update_scroll_to(float elapsed_time)
556 scroll_to_pos += elapsed_time * scrollspeed;
557 if(scroll_to_pos >= 1.0) {
559 translation = scroll_goal;
563 translation = scroll_from + (scroll_goal - scroll_from) * scroll_to_pos;
567 Camera::get_center() const {
568 return translation + Vector(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);