Tentative checkin of tuxdev's "Object improvement patch, part 1"
[supertux.git] / src / object / camera.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 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.hpp"
27 #include "lisp/writer.hpp"
28 #include "lisp/list_iterator.hpp"
29 #include "scripting/camera.hpp"
30 #include "scripting/squirrel_util.hpp"
31 #include "camera.hpp"
32 #include "player.hpp"
33 #include "tilemap.hpp"
34 #include "game_session.hpp"
35 #include "sector.hpp"
36 #include "main.hpp"
37 #include "object_factory.hpp"
38 #include "log.hpp"
39 #include "path.hpp"
40 #include "path_walker.hpp"
41
42 Camera::Camera(Sector* newsector, std::string name) :
43   GameObject(name), mode(NORMAL), sector(newsector), do_backscrolling(true),
44   scrollchange(NONE)
45 {
46 }
47
48 Camera::~Camera()
49 {
50 }
51
52 void
53 Camera::expose(HSQUIRRELVM vm, SQInteger table_idx)
54 {
55   if(name.empty()) return;
56   Scripting::Camera* interface = new Scripting::Camera(this);
57   expose_object(vm, table_idx, interface, name, true);
58 }
59
60 void
61 Camera::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
62 {
63   if(name.empty()) return;
64   Scripting::unexpose_object(vm, table_idx, name);
65 }
66
67 const Vector&
68 Camera::get_translation() const
69 {
70   return translation;
71 }
72
73 void
74 Camera::parse(const lisp::Lisp& reader)
75 {
76   std::string modename;
77
78   reader.get("mode", modename);
79   if(modename == "normal") {
80     mode = NORMAL;
81
82     do_backscrolling = true;
83     reader.get("backscrolling", do_backscrolling);
84   } else if(modename == "autoscroll") {
85     mode = AUTOSCROLL;
86
87     const lisp::Lisp* pathLisp = reader.get_lisp("path");
88     if(pathLisp == NULL)
89       throw std::runtime_error("No path specified in autoscroll camera.");
90
91     autoscroll_path.reset(new Path());
92     autoscroll_path->read(*pathLisp);
93     autoscroll_walker.reset(new PathWalker(autoscroll_path.get()));
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     autoscroll_path->write(writer);
114   } else if(mode == MANUAL) {
115     writer.write_string("mode", "manual");
116   }
117
118   writer.end_list("camera");
119 }
120
121 void
122 Camera::reset(const Vector& tuxpos)
123 {
124   translation.x = tuxpos.x - SCREEN_WIDTH/3 * 2;
125   translation.y = tuxpos.y - SCREEN_HEIGHT/2;
126   shakespeed = 0;
127   shaketimer.stop();
128   keep_in_bounds(translation);
129 }
130
131 void
132 Camera::shake(float time, float x, float y)
133 {
134   shaketimer.start(time);
135   shakedepth_x = x;
136   shakedepth_y = y;
137   shakespeed = M_PI/2 / time;
138 }
139
140 void
141 Camera::scroll_to(const Vector& goal, float scrolltime)
142 {
143   scroll_from = translation;
144   scroll_goal = goal;
145   keep_in_bounds(scroll_goal);
146
147   scroll_to_pos = 0;
148   scrollspeed = 1.0 / scrolltime;
149   mode = SCROLLTO;
150 }
151
152 static const float EPSILON = .00001;
153 static const float max_speed_y = 140;
154
155 void
156 Camera::update(float elapsed_time)
157 {
158   switch(mode) {
159     case NORMAL:
160       update_scroll_normal(elapsed_time);
161       break;
162     case AUTOSCROLL:
163       update_scroll_autoscroll(elapsed_time);
164       break;
165     case SCROLLTO:
166       update_scroll_to(elapsed_time);
167       break;
168     default:
169       break;
170   }
171 }
172
173 void
174 Camera::keep_in_bounds(Vector& translation)
175 {
176   float width = sector->get_width();
177   float height = sector->get_height();
178
179   // don't scroll before the start or after the level's end
180   if(translation.y > height - SCREEN_HEIGHT)
181     translation.y = height - SCREEN_HEIGHT;
182   if(translation.y < 0)
183     translation.y = 0;
184   if(translation.x > width - SCREEN_WIDTH)
185     translation.x = width - SCREEN_WIDTH;
186   if(translation.x < 0)
187     translation.x = 0;
188 }
189
190 void
191 Camera::shake()
192 {
193   if(shaketimer.started()) {
194     translation.x -= sin(shaketimer.get_timegone() * shakespeed) * shakedepth_x;
195     translation.y -= sin(shaketimer.get_timegone() * shakespeed) * shakedepth_y;
196   }
197 }
198
199 void
200 Camera::update_scroll_normal(float elapsed_time)
201 {
202   assert(sector != 0);
203   Player* player = sector->player;
204
205   // check that we don't have division by zero later
206   if(elapsed_time < EPSILON)
207     return;
208
209   /****** Vertical Scrolling part ******/
210   bool do_y_scrolling = true;
211
212   if(player->is_dying() || sector->get_height() == 19*32)
213     do_y_scrolling = false;
214
215   if(do_y_scrolling) {
216     // target_y is the high we target our scrolling at. This is not always the
217     // high of the player, but if he is jumping upwards we should use the
218     // position where he last touched the ground. (this probably needs
219     // exceptions for trampolines and similar things in the future)
220     float target_y;
221     if(player->fall_mode == Player::JUMPING)
222       target_y = player->last_ground_y + player->get_bbox().get_height();
223     else
224       target_y = player->get_bbox().p2.y;
225
226     // delta_y is the distance we'd have to travel to directly reach target_y
227     float delta_y = translation.y - (target_y - SCREEN_HEIGHT*2/3);
228     // speed is the speed the camera would need to reach target_y in this frame
229     float speed_y = delta_y / elapsed_time;
230
231     // limit the camera speed when jumping upwards
232     if(player->fall_mode != Player::FALLING
233         && player->fall_mode != Player::TRAMPOLINE_JUMP) {
234       if(speed_y > max_speed_y)
235         speed_y = max_speed_y;
236       else if(speed_y < -max_speed_y)
237         speed_y = -max_speed_y;
238     }
239
240     // finally scroll with calculated speed
241     translation.y -= speed_y * elapsed_time;
242
243     // make sure to always keep the player inside the middle 1/6 of the screen
244     translation.y = std::min(player->get_bbox().p1.y - SCREEN_HEIGHT*1/6, translation.y);
245     translation.y = std::max(player->get_bbox().p2.y - SCREEN_HEIGHT*5/6, translation.y);
246   }
247
248   /****** Horizontal scrolling part *******/
249
250   // our camera is either in leftscrolling, rightscrolling or nonscrollingmode.
251
252   // when suddenly changing directions while scrolling into the other direction.
253   // abort scrolling, since tux might be going left/right at a relatively small
254   // part of the map (like when jumping upwards)
255   if((player->dir == ::LEFT && scrollchange == RIGHT)
256       || (player->dir == ::RIGHT && scrollchange == LEFT))
257     scrollchange = NONE;
258   // when in left 1/3rd of screen scroll left
259   if(player->get_bbox().get_middle().x < translation.x + SCREEN_WIDTH/3 - 16
260       && do_backscrolling)
261     scrollchange = LEFT;
262   // scroll right when in right 1/3rd of screen
263   else if(player->get_bbox().get_middle().x > translation.x + SCREEN_WIDTH/3*2+16)
264     scrollchange = RIGHT;
265
266   // calculate our scroll target depending on scroll mode
267   float target_x;
268   if(scrollchange == LEFT)
269     target_x = player->get_bbox().get_middle().x - SCREEN_WIDTH/3*2;
270   else if(scrollchange == RIGHT)
271     target_x = player->get_bbox().get_middle().x - SCREEN_WIDTH/3;
272   else
273     target_x = translation.x;
274
275   // that's the distance we would have to travel to reach target_x
276   float delta_x = translation.x - target_x;
277   // the speed we'd need to travel to reach target_x in this frame
278   float speed_x = delta_x / elapsed_time;
279
280   // limit our speed
281   float maxv = 130 + (fabsf(player->physic.get_velocity_x() * 1.3));
282   if(speed_x > maxv)
283     speed_x = maxv;
284   else if(speed_x < -maxv)
285     speed_x = -maxv;
286
287   // If player is peeking scroll in that direction. Fast.
288   if( player->peeking_direction() == ::LEFT ){
289         speed_x = maxv;
290   }
291   if( player->peeking_direction() == ::RIGHT ){
292         speed_x = -maxv;
293   }
294
295   // apply scrolling
296   translation.x -= speed_x * elapsed_time;
297
298   // make sure to always keep the player inside the middle 4/6 of the screen
299   translation.x = std::min(player->get_bbox().p1.x - SCREEN_WIDTH*1/6, translation.x);
300   translation.x = std::max(player->get_bbox().p2.x - SCREEN_WIDTH*5/6, translation.x);
301
302   keep_in_bounds(translation);
303   shake();
304 }
305
306 void
307 Camera::update_scroll_autoscroll(float elapsed_time)
308 {
309   Player* player = sector->player;
310   if(player->is_dying())
311     return;
312
313   translation = autoscroll_walker->advance(elapsed_time);
314
315   keep_in_bounds(translation);
316   shake();
317 }
318
319 void
320 Camera::update_scroll_to(float elapsed_time)
321 {
322   scroll_to_pos += elapsed_time * scrollspeed;
323   if(scroll_to_pos >= 1.0) {
324     mode = MANUAL;
325     translation = scroll_goal;
326     return;
327   }
328
329   translation = scroll_from + (scroll_goal - scroll_from) * scroll_to_pos;
330 }