/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2009.
     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: sgx/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) 
{
	registerScenarioObject(engine);
}

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


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

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

	return success;
}

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

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

	return success;
}

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

	return TRUE;
}

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

	return TRUE;
}

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

	return TRUE;
}
