-Added new object system and converted some GameObjects to it.
[supertux.git] / src / gameobjs.h
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
5 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 // 
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 //  02111-1307, USA.
21
22 #ifndef SUPERTUX_GAMEOBJS_H
23 #define SUPERTUX_GAMEOBJS_H
24
25 #include "type.h"
26 #include "texture.h"
27 #include "timer.h"
28 #include "scene.h"
29 #include "physic.h"
30 #include "collision.h"
31 #include "game_object.h"
32 #include "drawable.h"
33
34 enum ObjectType { OBJ_NONE, OBJ_BADGUY, OBJ_TRAMPOLINE };
35
36 template <class T>
37 struct ObjectData
38 {
39   int x;
40   int y;
41   ObjectType type;
42   T type_specific;
43
44   ObjectData(ObjectData* pobject)
45     : x((int)pobject->x), y((int)pobject->y), type(pobject->type), type_specific(pobject->type_specific) {};
46   ObjectData(int x_, int y_, ObjectType type_, T type_specific_) 
47     : x(x_), y(y_), type(type_), type_specific(type_specific_) {};
48
49   ObjectData()
50     : x(0), y(0), type(OBJ_NONE), type_specific() {};
51 };
52
53 /* Bounciness of distros: */
54 #define NO_BOUNCE 0
55 #define BOUNCE 1
56
57 class BouncyDistro : public _GameObject, public Drawable
58 {
59 public:
60   BouncyDistro(DisplayManager& displaymanager, const Vector& pos);
61   virtual void action(float elapsed_time);
62   virtual void draw(ViewPort& viewport, int layer);
63   virtual std::string type() const
64   { return "BouncyDistro"; };
65
66 private:
67   Vector position;
68   float ym;
69 };
70
71 extern Surface* img_distro[4];
72
73 #define BOUNCY_BRICK_MAX_OFFSET 8
74 #define BOUNCY_BRICK_SPEED 0.9
75
76 class Tile;
77
78 class BrokenBrick : public _GameObject, public Drawable
79 {
80 public:
81   BrokenBrick(DisplayManager& displaymanager, Tile* tile,
82       const Vector& pos, const Vector& movement);
83
84   virtual void action(float elapsed_time);
85   virtual void draw(ViewPort& viewport, int layer);
86
87   virtual std::string type() const
88   { return "BrokenBrick"; };
89
90 private:
91   Timer timer;
92   Tile* tile;
93   Vector position;
94   Vector movement;
95 };
96
97 class BouncyBrick : public _GameObject, public Drawable
98 {
99 public:
100   BouncyBrick(DisplayManager& displaymanager, const Vector& pos);
101   virtual void action(float elapsed_time);
102   virtual void draw(ViewPort& viewport, int layer);
103   
104   virtual std::string type() const
105   { return "BouncyBrick"; };
106
107 private:
108   Vector position;
109   float offset;   
110   float offset_m;
111   int shape;      
112 };
113
114 class FloatingScore : public _GameObject, public Drawable
115 {
116 public:
117   FloatingScore(DisplayManager& displaymanager, const Vector& pos, int s);
118   
119   virtual void action(float elapsed_time);
120   virtual void draw(ViewPort& viewport, int layer);
121   virtual std::string type() const
122   { return "FloatingScore"; };
123
124 private:
125   Vector position;
126   char str[10];
127   Timer timer;  
128 };
129
130
131 /* Trampoline */
132 struct TrampolineData
133 {
134   float power;
135 };
136
137 class Trampoline : public GameObject
138 {
139  public:
140   void init(float x, float y);
141   void action(double frame_ratio);
142   void draw();
143   std::string type() { return "Trampoline"; };
144
145   Trampoline(ObjectData<TrampolineData> data)
146   {
147     power = data.type_specific.power;
148
149     init(data.x, data.y);
150   };
151
152   void collision(void *p_c_object, int c_object, CollisionType type);
153
154   Physic physic;
155   enum { M_NORMAL, M_HELD } mode;
156
157  private:
158   float power;
159   unsigned int frame;
160 };
161
162 void load_object_gfx();
163
164
165 class ObjectManager
166 {
167  private:
168   ObjectManager();
169   ~ObjectManager();
170
171   static ObjectManager* instance_;
172
173   // XXX Objects will have to be split up into two categories:
174   //  - Drawn before tux
175   //  - Drawn after tux
176   // Eventually Player should be a part of ObjectManager
177
178   std::vector<BadGuy*> badguys;
179   std::vector<Trampoline> trampolines;
180   std::vector<BouncyDistro> bouncy_distros;
181   std::vector<BrokenBrick> broken_bricks;
182   std::vector<BouncyBrick> bouncy_bricks;
183   //std::vector<Upgrade> upgrades;
184   //std::vector<Bullet> bullets;
185
186   void load_badguys(std::string filename);
187
188  public:
189   static ObjectManager* instance() { return instance_ ? instance_ : instance_ = new ObjectManager(); }
190   static void destroy_instance() { delete instance_; instance_ = 0; }
191
192   void draw_bg();
193   void draw_fg();
194   void actions();
195   
196 /*  Object* get(unsigned int id) {
197     if(id < badguys.size())
198       {
199         return badguys[id]; 
200       }
201     else
202       {
203         // Never return 0, but return the 0th tile instead so that
204         // user code doesn't have to check for NULL pointers all over
205         // the place
206         return badguys[0]; 
207       } 
208   }
209 */
210 };
211
212 #endif 
213
214 /* Local Variables: */
215 /* mode:c++ */
216 /* End: */