changed worldmap format a bit to be more consistent with level format
[supertux.git] / src / game_object.h
1 //  $Id: game_object.h 2293 2005-03-25 20:39:56Z matzebraun $
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_GAMEOBJECT_H
20 #define SUPERTUX_GAMEOBJECT_H
21
22 #include <string>
23
24 class DrawingContext;
25 class ObjectRemoveListener;
26
27 /**
28  * This is a base class for all game objects. Each sector of a level will hold a
29  * list of active GameObject while the game is played.
30  *
31  * This class is responsible for:
32  *  - Updating and Drawing the object. This should happen in the update() and
33  *    draw() functions. Both are called once per frame.
34  *  - Providing a safe way to remove the object by calling the remove_me
35  *    functions.
36  *  - a 32bit bitset for flags...
37  */
38 class GameObject
39 {
40 public:
41   GameObject();
42   virtual ~GameObject();
43
44   /** This function is called once per frame and allows the object to update
45    * it's state. The elapsed_time is the time since the last frame in
46    * seconds and should be the base for all timed calculations (don't use
47    * SDL_GetTicks directly as this will fail in pause mode)
48    */
49   virtual void update(float elapsed_time) = 0;
50
51   /** The GameObject should draw itself onto the provided DrawingContext if this
52    * function is called.
53    */
54   virtual void draw(DrawingContext& context) = 0;
55
56   /** returns true if the object is not scheduled to be removed yet */
57   bool is_valid() const
58     {
59       return !wants_to_die;
60     }
61   /** schedules this object to be removed at the end of the frame */
62   void remove_me()
63   {
64     wants_to_die = true;
65   }
66   /** registers a remove listener which will be called if the object 
67    * gets removed/destroyed
68    */
69   void add_remove_listener(ObjectRemoveListener* listener)
70   {
71     RemoveListenerListEntry* entry = new RemoveListenerListEntry();
72     entry->next = remove_listeners;
73     entry->listener = listener;
74
75     remove_listeners = entry;
76   }
77   
78   // flags
79   enum {
80     /// the tile so you can stand on it
81     FLAG_SOLID       = 0x0001,
82     /// can be used to temporatily disable collision detection
83     FLAG_NO_COLLDET  = 0x0002
84   };                     
85
86   int get_flags() const
87   {
88     return flags;            
89   }
90
91 private:
92   /** this flag indicates if the object should be removed at the end of the
93    * frame
94    */
95   bool wants_to_die;
96
97   struct RemoveListenerListEntry
98   {
99     RemoveListenerListEntry* next;
100     ObjectRemoveListener* listener;
101   };
102   RemoveListenerListEntry* remove_listeners;
103
104 protected:
105   int flags;
106 };
107
108 #endif /*SUPERTUX_GAMEOBJECT_H*/
109