Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / object / camera.hpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #ifndef HEADER_SUPERTUX_OBJECT_CAMERA_HPP
18 #define HEADER_SUPERTUX_OBJECT_CAMERA_HPP
19
20 #include <memory>
21
22 #include "math/vector.hpp"
23 #include "supertux/game_object.hpp"
24 #include "supertux/script_interface.hpp"
25 #include "supertux/timer.hpp"
26 #include "util/reader_fwd.hpp"
27
28 class Sector;
29 class Path;
30 class PathWalker;
31 class CameraConfig;
32
33 class Camera : public GameObject, 
34                public ScriptInterface
35 {
36 public:
37   Camera(Sector* sector, std::string name = "");
38   virtual ~Camera();
39
40   /// parse camera mode from lisp file
41   void parse(const Reader& reader);
42     
43   /// reset camera position
44   void reset(const Vector& tuxpos);
45
46   /** return camera position */
47   const Vector& get_translation() const;
48
49   virtual void update(float elapsed_time);
50
51   virtual void draw(DrawingContext& );
52
53   virtual void expose(HSQUIRRELVM vm, SQInteger table_idx);
54   virtual void unexpose(HSQUIRRELVM vm, SQInteger table_idx);
55
56   // shake camera in a direction 1 time
57   void shake(float speed, float x, float y);
58
59   void set_scrolling(int scroll_x, int scroll_y)
60   {
61     translation.x = scroll_x;
62     translation.y = scroll_y;
63   }
64
65   /**
66    * scroll the upper left edge of the camera in scrolltime seconds
67    * to the position goal
68    */
69   void scroll_to(const Vector& goal, float scrolltime);
70
71   void reload_config();
72
73   enum CameraMode
74   {
75     NORMAL, AUTOSCROLL, SCROLLTO, MANUAL
76   };
77   CameraMode mode;
78
79   /**
80    * get the coordinates of the point directly in the center of this camera
81    */
82   Vector get_center() const;
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   /**
92    * The camera basically provides lookahead on the left or right side
93    * or is undecided.
94    */
95   enum LookaheadMode {
96     LOOKAHEAD_NONE, LOOKAHEAD_LEFT, LOOKAHEAD_RIGHT
97   };
98
99   Vector translation;
100
101   Sector* sector;
102
103   // normal mode
104   LookaheadMode lookahead_mode;
105   float changetime;
106   Vector lookahead_pos;
107   Vector peek_pos;
108   Vector cached_translation;
109
110   // autoscroll mode
111   std::auto_ptr<Path> autoscroll_path;
112   std::auto_ptr<PathWalker> autoscroll_walker;
113
114   // shaking
115   Timer shaketimer;
116   float shakespeed;
117   float shakedepth_x;
118   float shakedepth_y;
119
120   // scrollto mode
121   Vector scroll_from;
122   Vector scroll_goal;
123   float scroll_to_pos;
124   float scrollspeed;
125
126   CameraConfig *config;
127
128 private:
129   Camera(const Camera&);
130   Camera& operator=(const Camera&);
131 };
132
133 #endif /*SUPERTUX_CAMERA_H*/
134
135 /* EOF */