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