added object remove_listener so that you can get a message if some objects are remove...
authorMatthias Braun <matze@braunis.de>
Sun, 16 Jan 2005 12:11:22 +0000 (12:11 +0000)
committerMatthias Braun <matze@braunis.de>
Sun, 16 Jan 2005 12:11:22 +0000 (12:11 +0000)
SVN-Revision: 2277

lib/special/game_object.cpp
lib/special/game_object.h
lib/special/object_remove_listener.h [new file with mode: 0644]

index c3fd98d..7a9db2b 100644 (file)
 
 #include <config.h>
 
-#include "special/game_object.h"
+#include "game_object.h"
+#include "object_remove_listener.h"
 
 namespace SuperTux
 {
 
 GameObject::GameObject()
-  : wants_to_die(false), flags(0)
+  : wants_to_die(false), remove_listeners(0), flags(0)
 {
 }
 
 GameObject::~GameObject()
 {
+  // call remove listeners (and remove them from the list)
+  RemoveListenerListEntry* entry = remove_listeners;
+  while(entry != 0) {
+    RemoveListenerListEntry* next = entry->next;
+    entry->listener->object_removed(this);
+    delete entry;
+    entry = next;
+  }
 }
 
 }
index 88e1ccb..b235c01 100644 (file)
@@ -27,6 +27,8 @@ namespace SuperTux
 
   class DrawingContext;
 
+  class ObjectRemoveListener;
+
   /**
    * Base class for all game objects. This contains functions for:
    *  -querying the actual type of the object
@@ -67,6 +69,18 @@ namespace SuperTux
       {
         wants_to_die = true;
       }
+      /** registers a remove listener which will be called if the object 
+       * gets removed/destroyed
+       */
+      void add_remove_listener(ObjectRemoveListener* listener)
+      {
+        RemoveListenerListEntry* entry = new RemoveListenerListEntry();
+        entry->next = remove_listeners;
+        entry->listener = listener;
+
+        remove_listeners = entry;
+      }
+        
 
       // flags
       enum {
@@ -87,6 +101,13 @@ namespace SuperTux
        */
       bool wants_to_die;
 
+      struct RemoveListenerListEntry
+      {
+        RemoveListenerListEntry* next;
+        ObjectRemoveListener* listener;
+      };
+      RemoveListenerListEntry* remove_listeners;
+
     protected:
       int flags;
     };
diff --git a/lib/special/object_remove_listener.h b/lib/special/object_remove_listener.h
new file mode 100644 (file)
index 0000000..fdcb769
--- /dev/null
@@ -0,0 +1,18 @@
+#ifndef __OBJECT_REMOVE_LISTENER_H__
+#define __OBJECT_REMOVE_LISTENER_H__
+
+namespace SuperTux
+{
+
+class GameObject;
+
+class ObjectRemoveListener
+{
+public:
+  virtual void object_removed(GameObject* object) = 0;
+};
+
+}
+
+#endif
+