diff engine/core/model/structures/instance.cpp @ 209:044d1fead5d2

* Applying icelus' "leader is not around anymore" patch from #350 * Close #350
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 15 Mar 2009 22:20:20 +0000
parents 5816ab527da8
children be246fb3a0df
line wrap: on
line diff
--- a/engine/core/model/structures/instance.cpp	Sun Mar 15 18:59:49 2009 +0000
+++ b/engine/core/model/structures/instance.cpp	Sun Mar 15 22:20:20 2009 +0000
@@ -170,6 +170,17 @@
 	}
 
 	Instance::~Instance() {
+                std::vector<InstanceDeleteListener *>::iterator itor;
+                for(itor = m_deletelisteners.begin();
+                    itor != m_deletelisteners.end();
+                    ++itor) {
+                        (*itor)->onInstanceDeleted(this);
+                }
+                if(m_activity &&
+                   m_activity->m_actioninfo &&
+                   m_activity->m_actioninfo->m_leader) {
+                        m_activity->m_actioninfo->m_leader->removeDeleteListener(this);
+                }
 		delete m_activity;
 		delete m_facinglocation;
 		delete m_visual;
@@ -271,6 +282,7 @@
 		m_activity->m_actioninfo->m_target = new Location(leader->getLocationRef());
 		m_activity->m_actioninfo->m_speed = speed;
 		m_activity->m_actioninfo->m_leader = leader;
+                leader->addDeleteListener(this);
 		setFacingLocation(*m_activity->m_actioninfo->m_target);
 		FL_DBG(_log, LMsg("starting action ") <<  action_name << " from" << m_location << " to " << *m_activity->m_actioninfo->m_target << " with speed " << speed);
 	}
@@ -510,4 +522,25 @@
 		}
 		return TimeManager::instance()->getTime();
 	}
+        void Instance::addDeleteListener(InstanceDeleteListener *listener) const {
+                m_deletelisteners.push_back(listener);
+        }
+        void Instance::removeDeleteListener(InstanceDeleteListener *listener) const {
+                std::vector<InstanceDeleteListener*>::iterator itor;
+                itor = std::find(m_deletelisteners.begin(),
+                                 m_deletelisteners.end(),
+                                 listener);
+                if(itor != m_deletelisteners.end()) {
+                        m_deletelisteners.erase(itor);
+                } else {
+                        FL_WARN(_log, "Cannot remove unknown listener");
+                }
+        }
+        void Instance::onInstanceDeleted(Instance* instance) {
+                if(m_activity && 
+                   m_activity->m_actioninfo && 
+                   m_activity->m_actioninfo->m_leader == instance) {
+                        m_activity->m_actioninfo->m_leader = NULL;
+                }
+        }
 }