Unified Messaging Subsystem
[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 #include <config.h>
20
21 #include <stdexcept>
22 #include <sstream>
23 #include <cmath>
24
25 #include "lisp/lisp.hpp"
26 #include "lisp/writer.hpp"
27 #include "lisp/list_iterator.hpp"
28 #include "camera.hpp"
29 #include "player.hpp"
30 #include "tilemap.hpp"
31 #include "game_session.hpp"
32 #include "sector.hpp"
33 #include "main.hpp"
34 #include "object_factory.hpp"
35 #include "msg.hpp"
36
37 Camera::Camera(Sector* newsector)
38   : sector(newsector), do_backscrolling(true), scrollchange(NONE)
39 {
40   mode = NORMAL;
41 }
42
43 Camera::~Camera()
44 {
45 }
46
47 const Vector&
48 Camera::get_translation() const
49 {
50   return translation;
51 }
52
53 void
54 Camera::parse(const lisp::Lisp& reader)
55 {
56   std::string modename;
57   
58   reader.get("mode", modename);
59   if(modename == "normal") {
60     mode = NORMAL;
61
62     do_backscrolling = true;
63     reader.get("backscrolling", do_backscrolling);
64   } else if(modename == "autoscroll") {
65     mode = AUTOSCROLL;
66     std::string use_path;
67     
68     if (!reader.get("path", use_path)) throw std::runtime_error("No path specified in autoscroll camera.");
69
70     autoscrollPath = Path::GetByName(use_path);
71     if (autoscrollPath == NULL) { 
72       msg_warning("Path for autoscroll camera not found! Make sure that the name is spelled correctly and that the path is initialized before the platform in the level file!");
73     }
74
75   } else if(modename == "manual") {
76     mode = MANUAL;
77   } else {
78     std::stringstream str;
79     str << "invalid camera mode '" << modename << "'found in worldfile.";
80     throw std::runtime_error(str.str());
81   }
82 }
83
84 void
85 Camera::write(lisp::Writer& writer)
86 {
87   writer.start_list("camera");
88   
89   if(mode == NORMAL) {
90     writer.write_string("mode", "normal");
91     writer.write_bool("backscrolling", do_backscrolling);
92   } else if(mode == AUTOSCROLL) {
93     writer.write_string("mode", "autoscroll");
94     writer.write_string("path", autoscrollPath->GetName());
95   } else if(mode == MANUAL) {
96     writer.write_string("mode", "manual");
97   }
98                      
99   writer.end_list("camera");
100 }
101
102 void
103 Camera::reset(const Vector& tuxpos)
104 {
105   translation.x = tuxpos.x - SCREEN_WIDTH/3 * 2;
106   translation.y = tuxpos.y - SCREEN_HEIGHT/2;
107   shakespeed = 0;
108   shaketimer.stop();
109   keep_in_bounds(translation);
110 }
111
112 void
113 Camera::shake(float time, float x, float y)
114 {
115   shaketimer.start(time);
116   shakedepth_x = x;
117   shakedepth_y = y;
118   shakespeed = M_PI/2 / time;
119 }
120
121 void
122 Camera::scroll_to(const Vector& goal, float scrolltime)
123 {
124   scroll_from = translation;
125   scroll_goal = goal;
126   keep_in_bounds(scroll_goal);
127
128   scroll_to_pos = 0;
129   scrollspeed = 1.0 / scrolltime;
130   mode = SCROLLTO;
131 }
132
133 static const float EPSILON = .00001;
134 static const float max_speed_y = 140;
135
136 void
137 Camera::update(float elapsed_time)
138 {
139   switch(mode) {
140     case NORMAL:
141       update_scroll_normal(elapsed_time);
142       break;
143     case AUTOSCROLL:
144       update_scroll_autoscroll(elapsed_time);
145       break;
146     case SCROLLTO:
147       update_scroll_to(elapsed_time);
148       break;
149     default:
150       break;
151   }
152 }
153
154 void
155 Camera::keep_in_bounds(Vector& translation)
156 {
157   float width = sector->solids->get_width() * 32;
158   float height = sector->solids->get_height() * 32;
159
160   // don't scroll before the start or after the level's end
161   if(translation.y > height - SCREEN_HEIGHT)
162     translation.y = height - SCREEN_HEIGHT;
163   if(translation.y < 0)                                      
164     translation.y = 0; 
165   if(translation.x > width - SCREEN_WIDTH)
166     translation.x = width - SCREEN_WIDTH;
167   if(translation.x < 0)
168     translation.x = 0;                                         
169 }
170
171 void
172 Camera::shake()
173 {
174   if(shaketimer.started()) {
175     translation.x -= sin(shaketimer.get_timegone() * shakespeed) * shakedepth_x;
176     translation.y -= sin(shaketimer.get_timegone() * shakespeed) * shakedepth_y;
177   }
178 }
179
180 void
181 Camera::update_scroll_normal(float elapsed_time)
182 {
183   assert(sector != 0);
184   Player* player = sector->player;
185   
186   // check that we don't have division by zero later
187   if(elapsed_time < EPSILON)
188     return;
189
190   /****** Vertical Scrolling part ******/
191   bool do_y_scrolling = true;
192
193   if(player->is_dying() || sector->solids->get_height() == 19)
194     do_y_scrolling = false;
195
196   if(do_y_scrolling) {
197     // target_y is the high we target our scrolling at. This is not always the
198     // high of the player, but if he is jumping upwards we should use the
199     // position where he last touched the ground. (this probably needs
200     // exceptions for trampolines and similar things in the future)
201     float target_y;
202     if(player->fall_mode == Player::JUMPING)
203       target_y = player->last_ground_y + player->get_bbox().get_height();
204     else
205       target_y = player->get_bbox().p2.y;
206
207     // delta_y is the distance we'd have to travel to directly reach target_y
208     float delta_y = translation.y - (target_y - SCREEN_HEIGHT*2/3);
209     // speed is the speed the camera would need to reach target_y in this frame
210     float speed_y = delta_y / elapsed_time;
211
212     // limit the camera speed when jumping upwards
213     if(player->fall_mode != Player::FALLING 
214         && player->fall_mode != Player::TRAMPOLINE_JUMP) {
215       if(speed_y > max_speed_y)
216         speed_y = max_speed_y;
217       else if(speed_y < -max_speed_y)
218         speed_y = -max_speed_y;
219     }
220
221     // finally scroll with calculated speed
222     translation.y -= speed_y * elapsed_time;
223   }
224
225   /****** Horizontal scrolling part *******/
226
227   // our camera is either in leftscrolling, rightscrolling or nonscrollingmode.
228   
229   // when suddenly changing directions while scrolling into the other direction.
230   // abort scrolling, since tux might be going left/right at a relatively small
231   // part of the map (like when jumping upwards)
232   if((player->dir == ::LEFT && scrollchange == RIGHT)
233       || (player->dir == ::RIGHT && scrollchange == LEFT))
234     scrollchange = NONE;
235   // when in left 1/3rd of screen scroll left
236   if(player->get_bbox().get_middle().x < translation.x + SCREEN_WIDTH/3 - 16
237       && do_backscrolling)
238     scrollchange = LEFT;
239   // scroll right when in right 1/3rd of screen
240   else if(player->get_bbox().get_middle().x > translation.x + SCREEN_WIDTH/3*2+16)
241     scrollchange = RIGHT;
242
243   // calculate our scroll target depending on scroll mode
244   float target_x;
245   if(scrollchange == LEFT)
246     target_x = player->get_bbox().get_middle().x - SCREEN_WIDTH/3*2;
247   else if(scrollchange == RIGHT)
248     target_x = player->get_bbox().get_middle().x - SCREEN_WIDTH/3;
249   else
250     target_x = translation.x;
251
252   // that's the distance we would have to travel to reach target_x
253   float delta_x = translation.x - target_x;
254   // the speed we'd need to travel to reach target_x in this frame
255   float speed_x = delta_x / elapsed_time;
256
257   // limit our speed
258   float maxv = 130 + (fabsf(player->physic.get_velocity_x() * 1.3));
259   if(speed_x > maxv)
260     speed_x = maxv;
261   else if(speed_x < -maxv)
262     speed_x = -maxv;
263  
264   // apply scrolling
265   translation.x -= speed_x * elapsed_time;
266
267   keep_in_bounds(translation);
268   shake();
269 }
270
271 void
272 Camera::update_scroll_autoscroll(float elapsed_time)
273 {
274   Player* player = sector->player;
275   
276   if(player->is_dying())
277     return;
278
279   translation = autoscrollPath->GetPosition();
280
281   keep_in_bounds(translation);
282   shake();
283 }
284
285 void
286 Camera::update_scroll_to(float elapsed_time)
287 {
288   scroll_to_pos += elapsed_time * scrollspeed;
289   if(scroll_to_pos >= 1.0) {
290     mode = MANUAL;
291     translation = scroll_goal;
292     return;
293   }
294
295   translation = scroll_from + (scroll_goal - scroll_from) * scroll_to_pos;
296 }
297