add test.close_world(bool)

This commit is contained in:
MihailRis 2024-12-10 19:14:51 +03:00
parent 9b4dd8f65e
commit d3f74b56fa
8 changed files with 30 additions and 11 deletions

View File

@ -1,2 +1,4 @@
test.new_world("demo", "2019", "core:default") test.new_world("demo", "2019", "core:default")
print(world.get_generator()) assert(world.get_generator() == "core:default")
test.close_world(true)
assert(not world.is_open())

View File

@ -13,6 +13,7 @@ if test then
test.sleep = sleep test.sleep = sleep
test.name = __VC_TEST_NAME test.name = __VC_TEST_NAME
test.new_world = core.new_world test.new_world = core.new_world
test.close_world = core.close_world
end end
------------------------------------------------ ------------------------------------------------

View File

@ -16,7 +16,14 @@ void Mainloop::run() {
auto& time = engine.getTime(); auto& time = engine.getTime();
engine.setLevelConsumer([this](auto level) { engine.setLevelConsumer([this](auto level) {
if (level == nullptr) {
// destroy LevelScreen and run quit callbacks
engine.setScreen(nullptr);
// create and go to menu screen
engine.setScreen(std::make_shared<MenuScreen>(&engine));
} else {
engine.setScreen(std::make_shared<LevelScreen>(&engine, std::move(level))); engine.setScreen(std::make_shared<LevelScreen>(&engine, std::move(level)));
}
}); });
logger.info() << "starting menu screen"; logger.info() << "starting menu screen";

View File

@ -38,5 +38,11 @@ void TestMainloop::run() {
} }
void TestMainloop::setLevel(std::unique_ptr<Level> level) { void TestMainloop::setLevel(std::unique_ptr<Level> level) {
this->controller = std::make_unique<LevelController>(&engine, std::move(level)); if (level == nullptr) {
controller->onWorldQuit();
engine.getPaths()->setCurrentWorldFolder(fs::path());
controller = nullptr;
} else {
controller = std::make_unique<LevelController>(&engine, std::move(level));
}
} }

View File

@ -18,8 +18,6 @@
#include "frontend/locale.hpp" #include "frontend/locale.hpp"
#include "frontend/menu.hpp" #include "frontend/menu.hpp"
#include "frontend/screens/Screen.hpp" #include "frontend/screens/Screen.hpp"
#include "frontend/screens/MenuScreen.hpp"
#include "frontend/screens/LevelScreen.hpp"
#include "graphics/render/ModelsGenerator.hpp" #include "graphics/render/ModelsGenerator.hpp"
#include "graphics/core/DrawContext.hpp" #include "graphics/core/DrawContext.hpp"
#include "graphics/core/ImageData.hpp" #include "graphics/core/ImageData.hpp"
@ -454,9 +452,15 @@ void Engine::setLanguage(std::string locale) {
} }
void Engine::onWorldOpen(std::unique_ptr<Level> level) { void Engine::onWorldOpen(std::unique_ptr<Level> level) {
logger.info() << "world open";
levelConsumer(std::move(level)); levelConsumer(std::move(level));
} }
void Engine::onWorldClosed() {
logger.info() << "world closed";
levelConsumer(nullptr);
}
gui::GUI* Engine::getGUI() { gui::GUI* Engine::getGUI() {
return gui.get(); return gui.get();
} }

View File

@ -135,6 +135,7 @@ public:
ResPaths* getResPaths(); ResPaths* getResPaths();
void onWorldOpen(std::unique_ptr<Level> level); void onWorldOpen(std::unique_ptr<Level> level);
void onWorldClosed();
/// @brief Get current Content instance /// @brief Get current Content instance
const Content* getContent() const; const Content* getContent() const;

View File

@ -54,8 +54,9 @@ void LevelController::update(float delta, bool input, bool pause) {
} }
void LevelController::saveWorld() { void LevelController::saveWorld() {
level->getWorld()->wfile->createDirectories(); auto world = level->getWorld();
logger.info() << "writing world"; logger.info() << "writing world '" << world->getName() << "'";
world->wfile->createDirectories();
scripting::on_world_save(); scripting::on_world_save();
level->onSave(); level->onSave();
level->getWorld()->write(level.get()); level->getWorld()->write(level.get());

View File

@ -69,10 +69,7 @@ static int l_close_world(lua::State* L) {
if (save_world) { if (save_world) {
controller->saveWorld(); controller->saveWorld();
} }
// destroy LevelScreen and run quit callbacks engine->onWorldClosed();
engine->setScreen(nullptr);
// create and go to menu screen
engine->setScreen(std::make_shared<MenuScreen>(engine));
return 0; return 0;
} }