Tried reducing extent of motion sickness induced by Camera algorithm
[supertux.git] / src / object / camera.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 //
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.
19
20 #include <config.h>
21
22 #include <stdexcept>
23 #include <sstream>
24 #include <cmath>
25
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"
31 #include "camera.hpp"
32 #include "player.hpp"
33 #include "tilemap.hpp"
34 #include "game_session.hpp"
35 #include "sector.hpp"
36 #include "main.hpp"
37 #include "object_factory.hpp"
38 #include "log.hpp"
39 #include "path.hpp"
40 #include "path_walker.hpp"
41
42 Camera::Camera(Sector* newsector, std::string name)
43   : mode(NORMAL), sector(newsector), do_backscrolling(true),
44     scrollchange(NONE)
45 {
46   this->name = name;
47 }
48
49 Camera::~Camera()
50 {
51 }
52
53 void
54 Camera::expose(HSQUIRRELVM vm, SQInteger table_idx)
55 {
56   if(name.empty()) return;
57   Scripting::Camera* interface = new Scripting::Camera(this);
58   expose_object(vm, table_idx, interface, name, true);
59 }
60
61 void
62 Camera::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
63 {
64   if(name.empty()) return;
65   Scripting::unexpose_object(vm, table_idx, name);
66 }
67
68 const Vector&
69 Camera::get_translation() const
70 {
71   return translation;
72 }
73
74 void
75 Camera::parse(const lisp::Lisp& reader)
76 {
77   std::string modename;
78
79   reader.get("mode", modename);
80   if(modename == "normal") {
81     mode = NORMAL;
82
83     do_backscrolling = true;
84     reader.get("backscrolling", do_backscrolling);
85   } else if(modename == "autoscroll") {
86     mode = AUTOSCROLL;
87
88     const lisp::Lisp* pathLisp = reader.get_lisp("path");
89     if(pathLisp == NULL)
90       throw std::runtime_error("No path specified in autoscroll camera.");
91
92     autoscroll_path.reset(new Path());
93     autoscroll_path->read(*pathLisp);
94     autoscroll_walker.reset(new PathWalker(autoscroll_path.get()));
95   } else if(modename == "manual") {
96     mode = MANUAL;
97   } else {
98     std::stringstream str;
99     str << "invalid camera mode '" << modename << "'found in worldfile.";
100     throw std::runtime_error(str.str());
101   }
102 }
103
104 void
105 Camera::write(lisp::Writer& writer)
106 {
107   writer.start_list("camera");
108
109   if(mode == NORMAL) {
110     writer.write_string("mode", "normal");
111     writer.write_bool("backscrolling", do_backscrolling);
112   } else if(mode == AUTOSCROLL) {
113     writer.write_string("mode", "autoscroll");
114     autoscroll_path->write(writer);
115   } else if(mode == MANUAL) {
116     writer.write_string("mode", "manual");
117   }
118
119   writer.end_list("camera");
120 }
121
122 void
123 Camera::reset(const Vector& tuxpos)
124 {
125   translation.x = tuxpos.x - SCREEN_WIDTH/3 * 2;
126   translation.y = tuxpos.y - SCREEN_HEIGHT/2;
127   shakespeed = 0;
128   shaketimer.stop();
129   keep_in_bounds(translation);
130 }
131
132 void
133 Camera::shake(float time, float x, float y)
134 {
135   shaketimer.start(time);
136   shakedepth_x = x;
137   shakedepth_y = y;
138   shakespeed = M_PI/2 / time;
139 }
140
141 void
142 Camera::scroll_to(const Vector& goal, float scrolltime)
143 {
144   scroll_from = translation;
145   scroll_goal = goal;
146   keep_in_bounds(scroll_goal);
147
148   scroll_to_pos = 0;
149   scrollspeed = 1.0 / scrolltime;
150   mode = SCROLLTO;
151 }
152
153 static const float EPSILON = .00001;
154 static const float max_speed_y = 140;
155
156 void
157 Camera::update(float elapsed_time)
158 {
159   switch(mode) {
160     case NORMAL:
161       update_scroll_normal(elapsed_time);
162       break;
163     case AUTOSCROLL:
164       update_scroll_autoscroll(elapsed_time);
165       break;
166     case SCROLLTO:
167       update_scroll_to(elapsed_time);
168       break;
169     default:
170       break;
171   }
172 }
173
174 void
175 Camera::keep_in_bounds(Vector& translation)
176 {
177   float width = sector->get_width();
178   float height = sector->get_height();
179
180   // don't scroll before the start or after the level's end
181   if(translation.y > height - SCREEN_HEIGHT)
182     translation.y = height - SCREEN_HEIGHT;
183   if(translation.y < 0)
184     translation.y = 0;
185   if(translation.x > width - SCREEN_WIDTH)
186     translation.x = width - SCREEN_WIDTH;
187   if(translation.x < 0)
188     translation.x = 0;
189 }
190
191 void
192 Camera::shake()
193 {
194   if(shaketimer.started()) {
195     translation.x -= sin(shaketimer.get_timegone() * shakespeed) * shakedepth_x;
196     translation.y -= sin(shaketimer.get_timegone() * shakespeed) * shakedepth_y;
197   }
198 }
199
200 void
201 Camera::update_scroll_normal(float elapsed_time)
202 {
203   assert(sector != 0);
204   Player* player = sector->player;
205
206   // check that we don't have division by zero later
207   if(elapsed_time < EPSILON)
208     return;
209
210   /****** Vertical Scrolling part ******/
211   bool do_y_scrolling = true;
212
213   if(player->is_dying() || sector->get_height() == 19*32)
214     do_y_scrolling = false;
215
216   if(do_y_scrolling) {
217     // target_y is the high we target our scrolling at. This is not always the
218     // high of the player, but if he is jumping upwards we should use the
219     // position where he last touched the ground. (this probably needs
220     // exceptions for trampolines and similar things in the future)
221     float target_y;
222     if(player->fall_mode == Player::JUMPING)
223       target_y = player->last_ground_y + player->get_bbox().get_height();
224     else
225       target_y = player->get_bbox().p2.y;
226
227     // delta_y is the distance we'd have to travel to directly reach target_y
228     float delta_y = translation.y - (target_y - SCREEN_HEIGHT*2/3);
229     // speed is the speed the camera would need to reach target_y in this frame
230     float speed_y = delta_y / elapsed_time;
231
232     // limit the camera speed when jumping upwards
233     if(player->fall_mode != Player::FALLING
234         && player->fall_mode != Player::TRAMPOLINE_JUMP) {
235       if(speed_y > max_speed_y)
236         speed_y = max_speed_y;
237       else if(speed_y < -max_speed_y)
238         speed_y = -max_speed_y;
239     }
240
241     // finally scroll with calculated speed
242     translation.y -= speed_y * elapsed_time;
243
244     // make sure to always keep the player inside the middle 1/6 of the screen
245     translation.y = std::min(player->get_bbox().p1.y - SCREEN_HEIGHT*1/6, translation.y);
246     translation.y = std::max(player->get_bbox().p2.y - SCREEN_HEIGHT*5/6, translation.y);
247   }
248
249   /****** Horizontal scrolling part *******/
250
251   // our camera is either in leftscrolling, rightscrolling or nonscrollingmode.
252
253   // when suddenly changing directions while scrolling into the other direction.
254   // abort scrolling, since tux might be going left/right at a relatively small
255   // part of the map (like when jumping upwards)
256   
257
258   // Find out direction in which the player walks: We want to try and show a 
259   // bit more of what's in front of the player and less of what's behind
260   LeftRightScrollChange walkDirection;  
261   if (player->physic.get_velocity_x() < -EPSILON) walkDirection = LEFT;
262   else if (player->physic.get_velocity_x() > EPSILON) walkDirection = RIGHT;
263   else if (player->dir == ::LEFT) walkDirection = LEFT;
264   else walkDirection = RIGHT;
265
266
267   if((walkDirection == LEFT && scrollchange == RIGHT)
268       || (walkDirection == RIGHT && scrollchange == LEFT))
269     scrollchange = NONE;
270   // when in left 1/3rd of screen scroll left
271   if(player->get_bbox().get_middle().x < translation.x + SCREEN_WIDTH/3 - 16
272       && do_backscrolling)
273     scrollchange = LEFT;
274   // scroll right when in right 1/3rd of screen
275   else if(player->get_bbox().get_middle().x > translation.x + SCREEN_WIDTH/3*2+16)
276     scrollchange = RIGHT;
277
278   // calculate our scroll target depending on scroll mode
279   float target_x;
280   if(scrollchange == LEFT)
281     target_x = player->get_bbox().get_middle().x - SCREEN_WIDTH/3*2;
282   else if(scrollchange == RIGHT)
283     target_x = player->get_bbox().get_middle().x - SCREEN_WIDTH/3;
284   else
285     target_x = translation.x;
286
287   // that's the distance we would have to travel to reach target_x
288   float delta_x = translation.x - target_x;
289   // the speed we'd need to travel to reach target_x in this frame
290   float speed_x = delta_x / elapsed_time;
291
292   // limit our speed
293   float maxv = 130 + (fabsf(player->physic.get_velocity_x() * 1.3));
294   if(speed_x > maxv)
295     speed_x = maxv;
296   else if(speed_x < -maxv)
297     speed_x = -maxv;
298
299   // If player is peeking scroll in that direction. Fast.
300   if( player->peeking_direction() == ::LEFT ){
301         speed_x = maxv;
302   }
303   if( player->peeking_direction() == ::RIGHT ){
304         speed_x = -maxv;
305   }
306
307   // apply scrolling
308   translation.x -= speed_x * elapsed_time;
309
310   // make sure to always keep the player inside the middle 4/6 of the screen
311   translation.x = std::min(player->get_bbox().p1.x - SCREEN_WIDTH*1/6, translation.x);
312   translation.x = std::max(player->get_bbox().p2.x - SCREEN_WIDTH*5/6, translation.x);
313
314   keep_in_bounds(translation);
315   shake();
316 }
317
318 void
319 Camera::update_scroll_autoscroll(float elapsed_time)
320 {
321   Player* player = sector->player;
322   if(player->is_dying())
323     return;
324
325   translation = autoscroll_walker->advance(elapsed_time);
326
327   keep_in_bounds(translation);
328   shake();
329 }
330
331 void
332 Camera::update_scroll_to(float elapsed_time)
333 {
334   scroll_to_pos += elapsed_time * scrollspeed;
335   if(scroll_to_pos >= 1.0) {
336     mode = MANUAL;
337     translation = scroll_goal;
338     return;
339   }
340
341   translation = scroll_from + (scroll_goal - scroll_from) * scroll_to_pos;
342 }