// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <config.h>
-
+#include "log.hpp"
#include "game_object.hpp"
#include "object_remove_listener.hpp"
entry = next;
}
}
+
+
+void
+GameObject::add_remove_listener(ObjectRemoveListener* listener)
+{
+ RemoveListenerListEntry* entry = new RemoveListenerListEntry();
+ entry->next = remove_listeners;
+ entry->listener = listener;
+ remove_listeners = entry;
+}
+
+void
+GameObject::del_remove_listener(ObjectRemoveListener* listener)
+{
+ RemoveListenerListEntry* entry = remove_listeners;
+ if (entry->listener == listener) {
+ remove_listeners = entry->next;
+ delete entry;
+ return;
+ }
+ RemoveListenerListEntry* next = entry->next;
+ while(next != NULL) {
+ if (next->listener == listener) {
+ entry->next = next->next;
+ delete next;
+ break;
+ }
+ entry = next;
+ next = next->next;
+ }
+}
+
/** 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;
- }
+ void add_remove_listener(ObjectRemoveListener* listener);
+
+ /**
+ * unregisters a remove listener, so it will no longer be called if the object
+ * gets removed/destroyed
+ */
+ void del_remove_listener(ObjectRemoveListener* listener);
const std::string& get_name() const
{
#include "trigger_base.hpp"
#include "video/drawing_context.hpp"
#include "object/player.hpp"
+#include "log.hpp"
TriggerBase::TriggerBase()
- : sprite(0), lasthit(false), hit(false), losetouch_listener(0)
+ : sprite(0), lasthit(false), hit(false)
{
set_group(COLGROUP_TOUCHABLE);
}
TriggerBase::~TriggerBase()
{
+ // unregister remove_listener hooks, so nobody will try to call us after we've been destroyed
+ for (std::list<Player*>::iterator i = losetouch_listeners.begin(); i != losetouch_listeners.end(); i++) {
+ Player* p = *i;
+ p->del_remove_listener(this);
+ }
+ losetouch_listeners.clear();
}
void
TriggerBase::update(float )
{
if (lasthit && !hit) {
- if (losetouch_listener) {
- event(*losetouch_listener, EVENT_LOSETOUCH);
- losetouch_listener = 0;
+ for (std::list<Player*>::iterator i = losetouch_listeners.begin(); i != losetouch_listeners.end(); i++) {
+ Player* p = *i;
+ event(*p, EVENT_LOSETOUCH);
+ p->del_remove_listener(this);
}
+ losetouch_listeners.clear();
}
lasthit = hit;
hit = false;
if(player) {
hit = true;
if(!lasthit) {
- losetouch_listener = player;
+ losetouch_listeners.push_back(player);
player->add_remove_listener(this);
event(*player, EVENT_TOUCH);
}
void
TriggerBase::object_removed(GameObject* object)
{
- if (losetouch_listener == object) losetouch_listener = 0;
+ for (std::list<Player*>::iterator i = losetouch_listeners.begin(); i != losetouch_listeners.end(); i++) {
+ Player* p = *i;
+ if (p == object) {
+ losetouch_listeners.erase(i);
+ break;
+ }
+ }
}