NANCY: Change where states are entered and exited

Moved the calls to onStateEnter() and onStateExit() inside the main game
loop.
This commit is contained in:
Kaloyan Chehlarski 2023-04-05 00:14:27 +03:00
parent 846bee430b
commit 4b3b81eb52
2 changed files with 26 additions and 20 deletions

View File

@ -219,8 +219,6 @@ void NancyEngine::setState(NancyState::NancyState state, NancyState::NancyState
break;
}
_graphicsManager->clearObjects();
if (overridePrevious != NancyState::kNone) {
_gameFlow.prevState = overridePrevious;
} else {
@ -228,22 +226,7 @@ void NancyEngine::setState(NancyState::NancyState state, NancyState::NancyState
}
_gameFlow.curState = state;
bool shouldDestroyLastState = false;
State::State *s = getStateObject(_gameFlow.prevState);
if (s) {
shouldDestroyLastState = s->onStateExit(_gameFlow.curState);
}
s = getStateObject(_gameFlow.curState);
if (s) {
s->onStateEnter(_gameFlow.prevState);
}
if (shouldDestroyLastState) {
destroyState(_gameFlow.prevState);
}
_gameFlow.changingState = true;
}
void NancyEngine::setToPreviousState() {
@ -274,13 +257,35 @@ Common::Error NancyEngine::run() {
_cursorManager->setCursorType(CursorManager::kNormalArrow);
_input->processEvents();
State::State *s = getStateObject(_gameFlow.curState);
State::State *s;
if (_gameFlow.changingState) {
s = getStateObject(_gameFlow.curState);
if (s) {
s->onStateEnter(_gameFlow.curState);
}
_gameFlow.changingState = false;
}
s = getStateObject(_gameFlow.curState);
if (s) {
s->process();
}
_graphicsManager->draw();
if (_gameFlow.changingState) {
_graphicsManager->clearObjects();
s = getStateObject(_gameFlow.prevState);
if (s) {
if(s->onStateExit(_gameFlow.prevState)) {
destroyState(_gameFlow.prevState);
}
}
}
_system->updateScreen();
_system->delayMillis(16);
}

View File

@ -133,6 +133,7 @@ private:
struct GameFlow {
NancyState::NancyState curState = NancyState::kNone;
NancyState::NancyState prevState = NancyState::kNone;
bool changingState = true;
};
void bootGameEngine();