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