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