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