/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2008.
     ssss    gggg x   x         Steven Goodwin. All Rights Reserved.
    
This version has been released under the GNU Public License v2.0.

Please see the license files for details.

File: src/sgx/core/gamecode/happening.cpp

*/

#include "sgx/core/core.hpp"

CSGXHappening *CSGXHappening::m_pSingleton = NULL;


/**
CSGXHappening is static class which holds a list of CSGXEngine's. If you want
to call the same method on each engine, then use this. However, this is rarely
useful since if the same object is registerObjected in two engines the update (e.g.)
will get called twice.
*/
CSGXHappening *
CSGXHappening::create()
{
	m_pSingleton = new CSGXHappening();
	// We don't call initialize here, because the engine base class would
	// call us to be registerObjected as an "engine"
	return m_pSingleton;
}

CSGXHappening *CSGXHappening::get()
{
	if (!m_pSingleton) {
		return create();
	}
	return m_pSingleton;
}

void
CSGXHappening::addEngine(CSGXEngine *engine) 
{
	registerObject(engine);
}

void
CSGXHappening::removeEngine(CSGXEngine *engine) 
{
	unregisterObject(engine);
}


tBOOL 
CSGXHappening::registerScenarioObjectToAllEngines(CGameHandler *ptr) {
	tBOOL success = TRUE;
	CSGXEngine *pEngine;

	sgxVector<CGameHandler *>::iterator it = m_EngineObjects.begin();
	for(;it!=m_EngineObjects.end();++it) {
		if ((pEngine = dynamic_cast<CSGXEngine *>(*it))) {
			success &= pEngine->registerObject(ptr);
		}
	}

	return success;
}

tBOOL 
CSGXHappening::unregisterScenarioObjectFromAllEngines(CGameHandler *ptr) {
	tBOOL success = TRUE;
	CSGXEngine *pEngine;

	sgxVector<CGameHandler *>::iterator it = m_EngineObjects.begin();
	for(;it!=m_EngineObjects.end();++it) {
		if ((pEngine = dynamic_cast<CSGXEngine *>(*it))) {
			success &= pEngine->unregisterObject(ptr);
		}
	}

	return success;
}

tBOOL 
CSGXHappening::unregisterScenarioObjectsFromAllEngines() {
	sgxVector<CGameHandler *>::iterator it = m_EngineObjects.begin();
	CSGXEngine *pEngine;
	for(;it!=m_EngineObjects.end();++it) {
		if ((pEngine = dynamic_cast<CSGXEngine *>(*it))) {
			pEngine->unregisterObjects();
		}
	}

	return TRUE;
}

tBOOL 
CSGXHappening::unregisterAllObjectsFromAllEngines() {
	sgxVector<CGameHandler *>::iterator it = m_EngineObjects.begin();
	CSGXEngine *pEngine;
	for(;it!=m_EngineObjects.end();++it) {
		if ((pEngine = dynamic_cast<CSGXEngine *>(*it))) {
			pEngine->unregisterAllObjects();
		}
	}

	return TRUE;
}

tBOOL 
CSGXHappening::releaseAllObjectsFromAllEngines() {
	sgxVector<CGameHandler *>::iterator it = m_EngineObjects.begin();
	CSGXEngine *pEngine;
	for(;it!=m_EngineObjects.end();++it) {
		if ((pEngine = dynamic_cast<CSGXEngine *>(*it))) {
			pEngine->releaseGameObjects();
		}
	}

	return TRUE;
}
