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