7d4b05f713c1cc4b6f3c795776ae0e1ade390d5d
[supertux.git] / src / game_object.hpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 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 #include "refcounter.hpp"
24
25 class DrawingContext;
26 class ObjectRemoveListener;
27
28 /**
29  * This is a base class for all game objects. Each sector of a level will hold a
30  * list of active GameObject while the game is played.
31  *
32  * This class is responsible for:
33  *  - Updating and Drawing the object. This should happen in the update() and
34  *    draw() functions. Both are called once per frame.
35  *  - Providing a safe way to remove the object by calling the remove_me
36  *    functions.
37  *  - a 32bit bitset for flags...
38  */
39 class GameObject : public RefCounter
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 update(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   
63   /** schedules this object to be removed at the end of the frame */
64   void remove_me()
65   {
66     wants_to_die = true;
67   }
68   
69   /** registers a remove listener which will be called if the object 
70    * gets removed/destroyed
71    */
72   void add_remove_listener(ObjectRemoveListener* listener)
73   {
74     RemoveListenerListEntry* entry = new RemoveListenerListEntry();
75     entry->next = remove_listeners;
76     entry->listener = listener;
77
78     remove_listeners = entry;
79   }
80   
81   // flags
82   enum {
83     /// the tile so you can stand on it
84     FLAG_SOLID       = (1 << 0),
85     /// the object can be carried around (inherits from Portable)
86     FLAG_PORTABLE    = (1 << 1)
87   };                     
88
89   int get_flags() const
90   {
91     return flags;            
92   }
93
94 private:
95   /** this flag indicates if the object should be removed at the end of the
96    * frame
97    */
98   bool wants_to_die;
99
100   struct RemoveListenerListEntry
101   {
102     RemoveListenerListEntry* next;
103     ObjectRemoveListener* listener;
104   };
105   RemoveListenerListEntry* remove_listeners;
106
107 protected:
108   int flags;
109 };
110
111 #endif /*SUPERTUX_GAMEOBJECT_H*/
112