Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Moopli committed Jul 1, 2014
2 parents 5af8a3f + a710b5a commit fcfadbd
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 8 deletions.
1 change: 1 addition & 0 deletions scripts/microbe_stage/microbe_replacement.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ end
function MicrobeReplacementSystem:activate()
activeCreatureId = Engine:playerData():activeCreature()
if Engine:playerData():isBoolSet("edited_microbe") then
Engine:playerData():setBool("edited_microbe", false);
workingMicrobe = Microbe(Entity(activeCreatureId, GameState.MICROBE_EDITOR))
if workingMicrobe:getCompoundAmount(CompoundRegistry.getCompoundId("atp")) == 0 then
workingMicrobe:storeCompound(CompoundRegistry.getCompoundId("atp"), 10)
Expand Down
45 changes: 44 additions & 1 deletion src/engine/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#include <OgreWindowEventUtilities.h>
#include <OISInputManager.h>
#include <OISMouse.h>
#include <map>
#include <random>
#include <set>
#include <stdlib.h>
Expand All @@ -83,7 +84,9 @@ struct Engine::Implementation : public Ogre::WindowEventListener {
Engine& engine
) : m_engine(engine),
m_rng(),
m_playerData("player")
m_playerData("player"),
m_nextShutdownSystems(new std::map<System*, int>),
m_prevShutdownSystems(new std::map<System*, int>)
{
}

Expand Down Expand Up @@ -413,6 +416,9 @@ struct Engine::Implementation : public Ogre::WindowEventListener {

bool m_quitRequested = false;

std::map<System*, int>* m_nextShutdownSystems;
std::map<System*, int>* m_prevShutdownSystems;

struct Graphics {

std::unique_ptr<Ogre::Root> root;
Expand Down Expand Up @@ -488,6 +494,8 @@ Engine::luaBindings() {
.def("load", &Engine::load)
.def("save", &Engine::save)
.def("quit", &Engine::quit)
.def("timedSystemShutdown", &Engine::timedSystemShutdown)
.def("isSystemTimedShutdown", &Engine::isSystemTimedShutdown)
.property("componentFactory", &Engine::componentFactory)
.property("keyboard", &Engine::keyboard)
.property("mouse", &Engine::mouse)
Expand Down Expand Up @@ -640,6 +648,13 @@ Engine::setCurrentGameState(
) {
assert(gameState != nullptr && "GameState must not be null");
m_impl->m_nextGameState = gameState;
for (auto& pair : *m_impl->m_prevShutdownSystems){
//Make sure systems are deactivated before any potential reactivations
pair.first->deactivate();
}
m_impl->m_prevShutdownSystems = m_impl->m_nextShutdownSystems;
m_impl->m_nextShutdownSystems = m_impl->m_prevShutdownSystems;
m_impl->m_nextShutdownSystems->clear();
}

PlayerData&
Expand Down Expand Up @@ -710,8 +725,36 @@ Engine::update(
}
assert(m_impl->m_currentGameState != nullptr);
m_impl->m_currentGameState->update(milliseconds);
// Update any timed shutdown systems
auto itr = m_impl->m_prevShutdownSystems->begin();
while (itr != m_impl->m_prevShutdownSystems->end()) {
int updateTime = std::min(itr->second, milliseconds);
itr->first->update(updateTime);
itr->second = itr->second - updateTime;
if (itr->second == 0) {
// Remove systems that had timed out
itr->first->deactivate();
m_impl->m_prevShutdownSystems->erase(itr++);
} else {
++itr;
}
}
if (not m_impl->m_serialization.loadFile.empty()) {
m_impl->loadSavegame();
}
}

void
Engine::timedSystemShutdown(
System& system,
int milliseconds
) {
(*m_impl->m_nextShutdownSystems)[&system] = milliseconds;
}

bool
Engine::isSystemTimedShutdown(
System& system
) const {
return m_impl->m_prevShutdownSystems->find(&system) != m_impl->m_prevShutdownSystems->end();
}
39 changes: 39 additions & 0 deletions src/engine/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class Engine {
* - Engine::load()
* - Engine::save()
* - Engine::quit()
* - Engine::timedSystemShutdown()
* - Engine::isSystemTimedShutdown()
* - Engine::componentFactory() (as property)
* - Engine::keyboard() (as property)
* - Engine::mouse() (as property)
Expand Down Expand Up @@ -270,11 +272,48 @@ class Engine {
int milliseconds
);

/**
* @brief Keeps a system alive after being shut down for a specified amount of time
*
* Note that this causes update to be called for the specified duration so be careful
* to ensure that the system is not enabled or it will get update calls twice.
*
* @param system
* The system to keep updated
*
* @param milliseconds
* The number of milliseconds to keep the system updated for
*/
void
timedSystemShutdown(
System& system,
int milliseconds
);

/**
* @brief Returns whether the specified system has already been set for a timed shutdown
*
* @param system
* The system to check for
*
* @return
*/
bool
isSystemTimedShutdown(
System& system
) const;

/**
* @brief Transfers an entity from one gamestate to another
*
* @param oldEntityId
* The id of the entity to transfer in the old entitymanager
*
* @param oldEntityManager
* The old entitymanager which is currently handling the entity
*
* @param newGameState
* The new gamestate to transfer the entity to
*/
EntityId
transferEntityGameState(
Expand Down
5 changes: 5 additions & 0 deletions src/engine/entity_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,11 @@ EntityFilter<ComponentTypes...>::removedEntities() {
return m_impl->m_removedEntities;
}

template<typename... ComponentTypes>
EntityManager*
EntityFilter<ComponentTypes...>::entityManager() {
return m_impl->m_entityManager;
}

template<typename... ComponentTypes>
void
Expand Down
8 changes: 8 additions & 0 deletions src/engine/entity_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ class EntityFilter {
std::unordered_set<EntityId>&
removedEntities();

/**
* @brief Gets the current entiyManager
*
* @return
*/
EntityManager*
entityManager();

/**
* @brief Sets the entity manager this filter applies to
*
Expand Down
17 changes: 10 additions & 7 deletions src/sound/sound_source_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ struct SoundSourceSystem::Implementation {
removeSoundsForEntity(
EntityId entityId
) {
EntityManager& entityManager = Game::instance().engine().currentGameState()->entityManager();
EntityManager& entityManager = *m_entities.entityManager();
SoundSourceComponent* soundSource = static_cast<SoundSourceComponent*>(entityManager.getComponent(entityId, SoundSourceComponent::TYPE_ID));
for (const auto& pair : soundSource->m_sounds) {
Sound* sound = pair.second.get();
Expand Down Expand Up @@ -430,13 +430,16 @@ SoundSourceSystem::activate() {

void
SoundSourceSystem::deactivate() {
System::deactivate();
auto& soundManager = OgreOggSound::OgreOggSoundManager::getSingleton();
m_impl->removeAllSounds();
for (auto& value : m_impl->m_entities) {
std::get<0>(value.second)->m_ambientSoundCountdown = 0;
if (this->engine()->isSystemTimedShutdown(*this)) {
System::deactivate();
m_impl->removeAllSounds();
}
else {
for (auto& value : m_impl->m_entities) {
std::get<0>(value.second)->m_ambientSoundCountdown = 1500;
}
this->engine()->timedSystemShutdown(*this, 1500);
}
soundManager.setSceneManager(nullptr);
}


Expand Down

0 comments on commit fcfadbd

Please sign in to comment.