Renaming images.
[supertux.git] / src / 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
20 #include <stdexcept>
21 #include <sstream>
22 #include <cmath>
23
24 #include "camera.h"
25 #include "utils/lispwriter.h"
26 #include "player.h"
27 #include "tilemap.h"
28 #include "gameloop.h"
29 #include "app/globals.h"
30 #include "sector.h"
31
32 using namespace SuperTux;
33
34 Camera::Camera(Sector* newsector)
35   : sector(newsector), do_backscrolling(true), scrollchange(NONE),
36     auto_idx(0), auto_t(0)
37 {
38   mode = NORMAL;
39 }
40
41 Camera::~Camera()
42 {
43 }
44
45 const Vector&
46 Camera::get_translation() const
47 {
48   return translation;
49 }
50
51 void
52 Camera::read(LispReader& reader)
53 {
54   std::string modename;
55   
56   reader.read_string("mode", modename);
57   if(modename == "normal") {
58     mode = NORMAL;
59
60     do_backscrolling = true;
61     reader.read_bool("backscrolling", do_backscrolling);
62   } else if(modename == "autoscroll") {
63     mode = AUTOSCROLL;
64     
65     lisp_object_t* cur = 0;
66     reader.read_lisp("path", cur);
67     if(cur == 0) {
68       throw std::runtime_error("No path specified in autoscroll camera.");
69     }
70     float speed = .5;
71     while(!lisp_nil_p(cur)) {
72       if(strcmp(lisp_symbol(lisp_car(lisp_car(cur))), "point") != 0) {
73         std::cerr << "Warning: unknown token in camera path.\n";
74         continue;
75       }
76            
77       LispReader reader(lisp_cdr(lisp_car(cur)));
78
79       ScrollPoint point;
80       if(!reader.read_float("x", point.position.x) ||
81          !reader.read_float("y", point.position.y)) {
82         throw std::runtime_error("x and y missing in point of camerapath");
83       }
84       reader.read_float("speed", speed);
85       point.speed = speed;
86       scrollpoints.push_back(point);
87
88       cur = lisp_cdr(cur);
89     }
90   } else if(modename == "manual") {
91     mode = MANUAL;
92   } else {
93     std::stringstream str;
94     str << "invalid camera mode '" << modename << "'found in worldfile.";
95     throw std::runtime_error(str.str());
96   }
97 }
98
99 void
100 Camera::write(LispWriter& writer)
101 {
102   writer.start_list("camera");
103   
104   if(mode == NORMAL) {
105     writer.write_string("mode", "normal");
106     writer.write_bool("backscrolling", do_backscrolling);
107   } else if(mode == AUTOSCROLL) {
108     writer.write_string("mode", "autoscroll");
109     writer.start_list("path");
110     for(std::vector<ScrollPoint>::iterator i = scrollpoints.begin();
111         i != scrollpoints.end(); ++i) {
112       writer.start_list("point");
113       writer.write_float("x", i->position.x);
114       writer.write_float("y", i->position.y);
115       writer.write_float("speed", i->speed);
116       writer.end_list("point");
117     }
118
119     writer.end_list("path");
120   } else if(mode == MANUAL) {
121     writer.write_string("mode", "manual");
122   }
123                      
124   writer.end_list("camera");
125 }
126
127 void
128 Camera::reset(const Vector& tuxpos)
129 {
130   translation.x = tuxpos.x - screen->w/3 * 2;
131   translation.y = tuxpos.y - screen->h/2;
132   keep_in_bounds();
133 }
134
135 static const float EPSILON = .00001;
136 static const float max_speed_y = 1.4;
137
138 void
139 Camera::action(float elapsed_time)
140 {
141   if(mode == NORMAL)
142     scroll_normal(elapsed_time);
143   else if(mode == AUTOSCROLL)
144     scroll_autoscroll(elapsed_time);
145 }
146
147 void
148 Camera::keep_in_bounds()
149 {
150   float width = sector->solids->get_width() * 32;
151   float height = sector->solids->get_height() * 32;
152
153   // don't scroll before the start or after the level's end
154   if(translation.y > height - screen->h)
155     translation.y = height - screen->h;
156   if(translation.y < 0)                                      
157     translation.y = 0; 
158   if(translation.x > width - screen->w)
159     translation.x = width - screen->w;
160   if(translation.x < 0)
161     translation.x = 0;                                         
162 }
163
164 void
165 Camera::scroll_normal(float elapsed_time)
166 {
167   assert(sector != 0);
168   Player* player = sector->player;
169   
170   // check that we don't have division by zero later
171   if(elapsed_time < EPSILON)
172     return;
173
174   /****** Vertical Scrolling part ******/
175   bool do_y_scrolling = true;
176
177   if(player->dying || sector->solids->get_height() == 19)
178     do_y_scrolling = false;
179
180   if(do_y_scrolling) {
181     // target_y is the high we target our scrolling at. This is not always the
182     // high of the player, but if he is jumping upwards we should use the
183     // position where he last touched the ground.
184     float target_y; 
185     if(player->fall_mode == Player::JUMPING)
186       target_y = player->last_ground_y + player->base.height;
187     else
188       target_y = player->base.y + player->base.height;
189
190     // delta_y is the distance we'd have to travel to directly reach target_y
191     float delta_y = translation.y - (target_y - screen->h/2);
192     // speed is the speed the camera would need to reach target_y in this frame
193     float speed_y = delta_y / elapsed_time;
194
195     // limit the camera speed when jumping upwards
196     if(player->fall_mode != Player::FALLING 
197         && player->fall_mode != Player::TRAMPOLINE_JUMP) {
198       if(speed_y > max_speed_y)
199         speed_y = max_speed_y;
200       else if(speed_y < -max_speed_y)
201         speed_y = -max_speed_y;
202     }
203
204     // finally scroll with calculated speed
205     translation.y -= speed_y * elapsed_time;
206   }
207
208   /****** Horizontal scrolling part *******/
209
210   // our camera is either in leftscrolling, rightscrolling or nonscrollingmode.
211   
212   // when suddenly changing directions while scrolling into the other direction.
213   // abort scrolling, since tux might be going left/right at a relatively small
214   // part of the map (like when jumping upwards)
215   if((player->dir == ::LEFT && scrollchange == RIGHT)
216       || (player->dir == ::RIGHT && scrollchange == LEFT))
217     scrollchange = NONE;
218   // when in left 1/3rd of screen scroll left
219   if(player->base.x < translation.x + screen->w/3 - 16 && do_backscrolling)
220     scrollchange = LEFT;
221   // scroll right when in right 1/3rd of screen
222   else if(player->base.x > translation.x + screen->w/3*2 + 16)
223     scrollchange = RIGHT;
224
225   // calculate our scroll target depending on scroll mode
226   float target_x;
227   if(scrollchange == LEFT)
228     target_x = player->base.x - screen->w/3*2;
229   else if(scrollchange == RIGHT)
230     target_x = player->base.x - screen->w/3;
231   else
232     target_x = translation.x;
233
234   // that's the distance we would have to travel to reach target_x
235   float delta_x = translation.x - target_x;
236   // the speed we'd need to travel to reach target_x in this frame
237   float speed_x = 1.3 * delta_x / elapsed_time;
238
239   // limit our speed
240   float maxv = 1.3 * (1 + fabsf(player->physic.get_velocity_x() * 1.3));
241   if(speed_x > maxv)
242     speed_x = maxv;
243   else if(speed_x < -maxv)
244     speed_x = -maxv;
245  
246   // apply scrolling
247   translation.x -= speed_x * elapsed_time;
248
249   keep_in_bounds();
250 }
251
252 void
253 Camera::scroll_autoscroll(float elapsed_time)
254 {
255   Player* player = sector->player;
256   
257   if(player->dying)
258     return;
259
260   if(auto_t - elapsed_time >= 0) {
261     translation += current_dir * elapsed_time;
262     auto_t -= elapsed_time;
263   } else {
264     // do the rest of the old movement
265     translation += current_dir * auto_t;
266     elapsed_time -= auto_t;
267     auto_t = 0;
268
269     // construct path for next point
270     if(auto_idx+1 >= scrollpoints.size()) {
271       keep_in_bounds();
272       return;
273     }
274     Vector distance = scrollpoints[auto_idx+1].position 
275                       - scrollpoints[auto_idx].position;
276     current_dir = distance.unit() * scrollpoints[auto_idx].speed;
277     auto_t = distance.norm() / scrollpoints[auto_idx].speed;
278
279     // do movement for the remaining time
280     translation += current_dir * elapsed_time;
281     auto_t -= elapsed_time;
282     auto_idx++;
283   }
284
285   keep_in_bounds();
286 }