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