SuperMarioOdysseyOnline/source/server/gamemode/GameModeManager.cpp

95 lines
2.7 KiB
C++
Raw Normal View History

2022-08-07 21:42:56 +00:00
#include "server/gamemode/GameModeManager.hpp"
#include <cstring>
#include <heap/seadExpHeap.h>
#include <basis/seadNew.h>
#include <heap/seadHeapMgr.h>
2022-08-07 21:42:56 +00:00
#include "al/util.hpp"
#include "logger.hpp"
2022-08-07 21:42:56 +00:00
#include "server/gamemode/GameModeBase.hpp"
#include "server/gamemode/GameModeFactory.hpp"
#include "server/gamemode/modifiers/ModeModifierBase.hpp"
#include "server/gamemode/modifiers/ModifierFactory.hpp"
2022-08-07 21:42:56 +00:00
SEAD_SINGLETON_DISPOSER_IMPL(GameModeManager)
GameModeManager::GameModeManager() {
mHeap = sead::ExpHeap::create(0x50000, "GameModeHeap", al::getSequenceHeap(), 8,
2022-08-07 21:42:56 +00:00
sead::Heap::HeapDirection::cHeapDirection_Reverse, false);
setMode(GameMode::HIDEANDSEEK);
}
void GameModeManager::begin() {
if (mCurModeBase) {
sead::ScopedCurrentHeapSetter heapSetter(mHeap);
mCurModeBase->begin();
}
}
void GameModeManager::end() {
if (mCurModeBase) {
sead::ScopedCurrentHeapSetter heapSetter(mHeap);
mCurModeBase->end();
}
}
void GameModeManager::toggleActive() {
mActive = !mActive;
}
void GameModeManager::setPaused(bool paused) {
mPaused = paused;
}
void GameModeManager::setMode(GameMode mode) {
mCurMode = mode;
mWasSetMode = true; // recreate in initScene
}
void GameModeManager::update() {
if (!mCurModeBase) return;
bool inScene = al::getSceneHeap() != nullptr;
if ((mActive && inScene && !mPaused && !mCurModeBase->isModeActive()) || mWasSceneTrans) begin();
if ((!mActive || mPaused || !inScene) && mCurModeBase->isModeActive()) end();
mWasSceneTrans = false;
if (mCurModeBase && mCurModeBase->isModeActive()) {
sead::ScopedCurrentHeapSetter heapSetter(mHeap);
mCurModeBase->update();
}
}
void GameModeManager::initScene(const GameModeInitInfo& info) {
sead::ScopedCurrentHeapSetter heapSetter(mHeap);
2022-08-10 21:10:28 +00:00
if (mCurModeBase != nullptr && mWasSetMode) {
2022-08-07 21:42:56 +00:00
delete mCurModeBase;
2022-08-10 21:10:28 +00:00
mCurModeBase = nullptr;
2022-08-07 21:42:56 +00:00
}
if (mLastInitInfo != nullptr) {
delete mLastInitInfo;
}
if (mCurMode == GameMode::NONE) {
mCurModeBase = nullptr;
2022-08-10 21:10:28 +00:00
delete mModeInfo;
2022-08-07 21:42:56 +00:00
mModeInfo = nullptr;
return;
}
mLastInitInfo = new GameModeInitInfo(info);
2022-08-10 21:10:28 +00:00
if (mWasSetMode) {
2022-08-07 21:42:56 +00:00
GameModeFactory factory("GameModeFactory");
const char* name = factory.getModeString(mCurMode);
mCurModeBase = factory.getCreator(name)(name);
mWasSetMode = false;
}
if (mCurModeBase) {
sead::ScopedCurrentHeapSetter heapSetter(GameModeManager::getSceneHeap());
2022-08-07 21:42:56 +00:00
mCurModeBase->init(*mLastInitInfo);
if (mCurModeBase->isModeActive())
mWasSceneTrans = true;
}
}