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