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