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