- Yet another try in the endless quest for perfect collision detection.
[supertux.git] / src / object / camera.hpp
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 #ifndef SUPERTUX_CAMERA_H
20 #define SUPERTUX_CAMERA_H
21
22 #include <vector>
23 #include <cassert>
24 #include <memory>
25
26 #include "math/vector.hpp"
27 #include "game_object.hpp"
28 #include "video/drawing_context.hpp"
29 #include "serializable.hpp"
30 #include "timer.hpp"
31
32 namespace lisp {
33 class Lisp;
34 }
35
36 class Sector;
37 class Path;
38 class PathWalker;
39
40 class Camera : public GameObject, public Serializable
41 {
42 public:
43   Camera(Sector* sector);
44   virtual ~Camera();
45
46   /// parse camera mode from lisp file
47   void parse(const lisp::Lisp& reader);
48   /// write camera mode to a lisp file
49   virtual void write(lisp::Writer& writer);
50
51   /// reset camera postion
52   virtual void reset(const Vector& tuxpos);
53
54   /** return camera position */
55   const Vector& get_translation() const;
56
57   virtual void update(float elapsed_time);
58
59   virtual void draw(DrawingContext& )
60   {
61   }
62
63   // shake camera in a direction 1 time
64   void shake(float speed, float x, float y);
65
66   void set_scrolling(int scroll_x, int scroll_y)
67   {
68     translation.x = scroll_x;
69     translation.y = scroll_y;
70   }
71
72   /**
73    * scroll the upper left edge of the camera in scrolltime seconds
74    * to the position goal
75    */
76   void scroll_to(const Vector& goal, float scrolltime);
77
78   enum CameraMode
79   {
80     NORMAL, AUTOSCROLL, SCROLLTO, MANUAL
81   };
82   CameraMode mode;
83
84 private:
85   void update_scroll_normal(float elapsed_time);
86   void update_scroll_autoscroll(float elapsed_time);
87   void update_scroll_to(float elapsed_time);
88   void keep_in_bounds(Vector& vector);
89   void shake();
90
91   enum LeftRightScrollChange
92   {
93     NONE, LEFT, RIGHT
94   };
95
96   Vector translation;
97
98   Sector* sector;
99
100   // normal mode
101   bool do_backscrolling;
102   LeftRightScrollChange scrollchange;
103
104   // autoscroll mode
105   std::auto_ptr<Path> autoscroll_path;
106   std::auto_ptr<PathWalker> autoscroll_walker;
107
108   // shaking
109   Timer shaketimer;
110   float shakespeed;
111   float shakedepth_x;
112   float shakedepth_y;
113
114   // scrollto mode
115   Vector scroll_from;
116   Vector scroll_goal;
117   float scroll_to_pos;
118   float scrollspeed;
119 };
120
121 #endif /*SUPERTUX_CAMERA_H*/
122