Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

SCA_LogicManager.cpp

Go to the documentation of this file.
00001 
00002 // this manager lets controllers register themself to sensor(s)
00003 // Each frame, out of the collection of activated sensors,
00004 // a collection of triggered controllers is build.
00005 // These triggered controllers are executed afterwards.
00006 
00007 
00008 #include "Value.h"
00009 #include "SCA_LogicManager.h"
00010 
00011 #include "SCA_ISensor.h"
00012 #include "SCA_IController.h"
00013 #include "SCA_IActuator.h"
00014 #include "SCA_EventManager.h"
00015 
00016 #include <set>
00017 
00018 SCA_LogicManager::~SCA_LogicManager()

00019 {
00020         int i;
00021         for (vector<SCA_EventManager*>::iterator it = m_eventmanagers.begin();!(it==m_eventmanagers.end());it++)
00022         {
00023                 delete (*it);
00024         }
00025         m_eventmanagers.clear();
00026         m_sensorcontrollermapje.clear();
00027 
00028         int numgameobj = m_mapStringToGameObjects.size();
00029         for (i=0;i<numgameobj;i++)
00030         {
00031                 CValue** gameobjptr = m_mapStringToGameObjects.at(i);
00032                 assert(gameobjptr);
00033                 if (gameobjptr)
00034                         (*gameobjptr)->Release();
00035 
00036         }
00037 
00038         /*for (int i=0;i<m_sensorcontrollermap.size();i++)

00039         {

00040                 vector<SCA_IController*>* controllerarray = *(m_sensorcontrollermap[i]);

00041                 delete controllerarray;

00042         }

00043         */
00044 
00045 }
00046 
00047 void SCA_LogicManager::RemoveSensor(SCA_ISensor* sensor)

00048 {
00049     m_sensorcontrollermapje.erase(sensor);
00050         
00051         for (vector<SCA_EventManager*>::const_iterator ie=m_eventmanagers.begin();
00052         !(ie==m_eventmanagers.end());ie++)
00053         {
00054                 (*ie)->RemoveSensor(sensor);
00055         }
00056 }
00057 void SCA_LogicManager::RegisterToSensor(SCA_IController* controller,SCA_ISensor* sensor)

00058 {
00059     m_sensorcontrollermapje[sensor].push_back(controller);
00060         controller->LinkToSensor(sensor);
00061 }
00062 
00063 void SCA_LogicManager::RegisterToActuator(SCA_IController* controller,SCA_IActuator* actua)

00064 {
00065         controller->LinkToActuator(actua);
00066 
00067 }
00068 
00069 
00070 void    SCA_LogicManager::BeginFrame(double curtime,double deltatime)

00071 {
00072         
00073         for (vector<SCA_EventManager*>::const_iterator ie=m_eventmanagers.begin();
00074         !(ie==m_eventmanagers.end());ie++)
00075         {
00076                 (*ie)->NextFrame(curtime,deltatime);
00077         }
00078 
00079         // for this frame, look up for activated sensors, and build the collection of triggered controllers
00080         int numsensors = this->m_activatedsensors.size();
00081 
00082         set<SCA_IController*,SCACompare> triggeredControllerSet;
00083 
00084         for (vector<SCA_ISensor*>::const_iterator is=m_activatedsensors.begin();
00085         !(is==m_activatedsensors.end());is++)
00086         {
00087                 SCA_ISensor* sensor = *is;
00088                 controllerlist contlist = m_sensorcontrollermapje[sensor];
00089                 for (list<SCA_IController*>::const_iterator c= contlist.begin();
00090                         !(c==contlist.end());c++)
00091                 {
00092                                 SCA_IController* contr = *c;//controllerarray->at(c);
00093                                 triggeredControllerSet.insert(contr);
00094                 }
00095                 //sensor->SetActive(false);
00096         }
00097 
00098         
00099         int numtriggered = triggeredControllerSet.size();
00100         for (set<SCA_IController*,SCACompare>::iterator tit=triggeredControllerSet.begin();
00101         !(tit==triggeredControllerSet.end());tit++)
00102         {
00103                 SCA_IController* contr = *tit;
00104                 contr->Trigger(this);
00105         }
00106         triggeredControllerSet.clear();
00107 
00108 
00109 };
00110 void SCA_LogicManager::UpdateFrame(double curtime,double deltatime)

00111 {
00112 
00113         vector<SCA_IActuator*> removedActuators;
00114         
00115         for (set<SCA_IActuator*,SCACompare>::iterator ia = m_activeActuators.begin();!(ia==m_activeActuators.end());ia++)
00116         {
00117                 SCA_IActuator* actua = *ia;
00118                 if (!actua->Update(curtime,deltatime))
00119                 {
00120                         //*ia = m_activeactuators.back();
00121                         removedActuators.push_back(*ia);
00122                         
00123                         actua->SetActive(false);
00124                         //m_activeactuators.pop_back();
00125                 }
00126         }
00127 
00128         for ( vector<SCA_IActuator*>::iterator ra = removedActuators.begin();
00129         !(ra == removedActuators.end());ra++)
00130         {
00131                 m_activeActuators.erase(*ra);
00132         }
00133         removedActuators.clear();
00134         
00135 }
00136 
00137 void    SCA_LogicManager::EndFrame()

00138 {
00139         for (vector<SCA_ISensor*>::const_iterator is=m_activatedsensors.begin();
00140         !(is==m_activatedsensors.end());is++)
00141         {
00142                 SCA_ISensor* sensor = *is;
00143                 sensor->SetActive(false);
00144         }
00145         m_activatedsensors.clear();
00146 
00147         for (vector<SCA_EventManager*>::const_iterator ie=m_eventmanagers.begin();
00148         !(ie==m_eventmanagers.end());ie++)
00149         {
00150                 (*ie)->EndFrame();
00151         }
00152 
00153 }
00154 void    SCA_LogicManager::AddActivatedSensor(SCA_ISensor* sensor)

00155 {
00156         // each frame, only add sensor once, and to avoid a seek, or bloated container
00157         // hold a flag in each sensor, with the 'framenr'
00158         if (!sensor->IsActive())
00159         {
00160                 sensor->SetActive(true);
00161                 m_activatedsensors.push_back(sensor);
00162         }
00163                 
00164 }
00165 
00166 void    SCA_LogicManager::AddActiveActuator(SCA_IActuator* actua,CValue* event)

00167 {
00168         if (!actua->IsActive())
00169         {
00170                 actua->SetActive(true);
00171                 m_activeActuators.insert(actua);
00172         }
00173         actua->AddEvent(event->AddRef());
00174 }
00175 
00176 SCA_EventManager*       SCA_LogicManager::FindEventManager(int eventmgrtype)

00177 {
00178         // find an eventmanager of a certain type
00179         SCA_EventManager* eventmgr = NULL;
00180 
00181         for (vector<SCA_EventManager*>::const_iterator i=
00182         m_eventmanagers.begin();!(i==m_eventmanagers.end());i++)
00183         {
00184                 SCA_EventManager* emgr = *i;
00185                 if (emgr->GetType() == eventmgrtype)
00186                 {
00187                         eventmgr = emgr;
00188                         break;
00189                 }
00190 
00191         }
00192         
00193         return eventmgr;
00194 }

Generated at Thu Feb 1 13:03:10 2001 for Ketsji Game Engine by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000