add assert.h
[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  * Base class for all game objects. This contains functions for:
29  *  -querying the actual type of the object
30  *  -a flag that indicates if the object wants to be removed. Objects with this
31  *   flag will be removed at the end of each frame. This is alot safer than
32  *   having some uncontrollable "delete this" in the code.
33  *  -an action function that is called once per frame and allows the object to
34  *   update it's state.
35  * 
36  * Most GameObjects will also implement the DrawableObject interface so that
37  * they can actually be drawn on screen.
38  */
39 class GameObject
40 {
41 public:
42   GameObject();
43   virtual ~GameObject();
44
45   /** This function is called once per frame and allows the object to update
46    * it's state. The elapsed_time is the time since the last frame in
47    * seconds and should be the base for all timed calculations (don't use
48    * SDL_GetTicks directly as this will fail in pause mode)
49    */
50   virtual void action(float elapsed_time) = 0;
51
52   /** The GameObject should draw itself onto the provided DrawingContext if this
53    * function is called.
54    */
55   virtual void draw(DrawingContext& context) = 0;
56
57   /** returns true if the object is not scheduled to be removed yet */
58   bool is_valid() const
59     {
60       return !wants_to_die;
61     }
62   /** schedules this object to be removed at the end of the frame */
63   void remove_me()
64   {
65     wants_to_die = true;
66   }
67   /** registers a remove listener which will be called if the object 
68    * gets removed/destroyed
69    */
70   void add_remove_listener(ObjectRemoveListener* listener)
71   {
72     RemoveListenerListEntry* entry = new RemoveListenerListEntry();
73     entry->next = remove_listeners;
74     entry->listener = listener;
75
76     remove_listeners = entry;
77   }
78   
79   // flags
80   enum {
81     /// the tile so you can stand on it
82     FLAG_SOLID       = 0x0001,
83     /// can be used to temporatily disable collision detection
84     FLAG_NO_COLLDET  = 0x0002
85   };                     
86
87   int get_flags() const
88   {
89     return flags;            
90   }
91
92 private:
93   /** this flag indicates if the object should be removed at the end of the
94    * frame
95    */
96   bool wants_to_die;
97
98   struct RemoveListenerListEntry
99   {
100     RemoveListenerListEntry* next;
101     ObjectRemoveListener* listener;
102   };
103   RemoveListenerListEntry* remove_listeners;
104
105 protected:
106   int flags;
107 };
108
109 #endif /*SUPERTUX_GAMEOBJECT_H*/
110