Tentative checkin of tuxdev's "Object improvement patch, part 1"
[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 #include "lisp/lisp.hpp"
25
26 class DrawingContext;
27 class ObjectRemoveListener;
28
29 /**
30  * This is a base class for all game objects. Each sector of a level will hold a
31  * list of active GameObject while the game is played.
32  *
33  * This class is responsible for:
34  *  - Updating and Drawing the object. This should happen in the update() and
35  *    draw() functions. Both are called once per frame.
36  *  - Providing a safe way to remove the object by calling the remove_me
37  *    functions.
38  *  - a 32bit bitset for flags...
39  */
40 class GameObject : public RefCounter
41 {
42 public:
43   GameObject(std::string name = "");
44   GameObject(const lisp::Lisp& lisp);
45   virtual ~GameObject();
46
47   /** This function is called once per frame and allows the object to update
48    * it's state. The elapsed_time is the time since the last frame in
49    * seconds and should be the base for all timed calculations (don't use
50    * SDL_GetTicks directly as this will fail in pause mode)
51    */
52   virtual void update(float elapsed_time) = 0;
53
54   /** The GameObject should draw itself onto the provided DrawingContext if this
55    * function is called.
56    */
57   virtual void draw(DrawingContext& context) = 0;
58
59   /** returns true if the object is not scheduled to be removed yet */
60   bool is_valid() const
61   {
62     return !wants_to_die;
63   }
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   /** registers a remove listener which will be called if the object
72    * gets removed/destroyed
73    */
74   void add_remove_listener(ObjectRemoveListener* listener)
75   {
76     RemoveListenerListEntry* entry = new RemoveListenerListEntry();
77     entry->next = remove_listeners;
78     entry->listener = listener;
79
80     remove_listeners = entry;
81   }
82
83   std::string get_name() const
84   {
85     return name;
86   }
87   // --- BEGIN METHODS TO EXPOSE TO SQUIRREL --- //
88   //void set_visible(bool visible);
89   //bool is_visible();
90   // --- END METHODS TO EXPOSE TO SQUIRREL --- //
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   std::string name; /**< user-defined name for use in scripts or empty string if not scriptable */
107 };
108
109 #endif /*SUPERTUX_GAMEOBJECT_H*/