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