refactor & add test.new_world

This commit is contained in:
MihailRis 2024-12-10 18:41:39 +03:00
parent 8f37704530
commit 9b4dd8f65e
9 changed files with 57 additions and 8 deletions

View File

@ -0,0 +1,2 @@
test.new_world("demo", "2019", "core:default")
print(world.get_generator())

View File

@ -12,6 +12,7 @@ end
if test then 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
end end
------------------------------------------------ ------------------------------------------------

View File

@ -3,7 +3,9 @@
#include "debug/Logger.hpp" #include "debug/Logger.hpp"
#include "engine.hpp" #include "engine.hpp"
#include "frontend/screens/MenuScreen.hpp" #include "frontend/screens/MenuScreen.hpp"
#include "frontend/screens/LevelScreen.hpp"
#include "window/Window.hpp" #include "window/Window.hpp"
#include "world/Level.hpp"
static debug::Logger logger("mainloop"); static debug::Logger logger("mainloop");
@ -13,6 +15,10 @@ Mainloop::Mainloop(Engine& engine) : engine(engine) {
void Mainloop::run() { void Mainloop::run() {
auto& time = engine.getTime(); auto& time = engine.getTime();
engine.setLevelConsumer([this](auto level) {
engine.setScreen(std::make_shared<LevelScreen>(&engine, std::move(level)));
});
logger.info() << "starting menu screen"; logger.info() << "starting menu screen";
engine.setScreen(std::make_shared<MenuScreen>(&engine)); engine.setScreen(std::make_shared<MenuScreen>(&engine));

View File

@ -1,8 +1,10 @@
#include "TestMainloop.hpp" #include "TestMainloop.hpp"
#include "logic/scripting/scripting.hpp" #include "logic/scripting/scripting.hpp"
#include "logic/LevelController.hpp"
#include "interfaces/Process.hpp" #include "interfaces/Process.hpp"
#include "debug/Logger.hpp" #include "debug/Logger.hpp"
#include "world/Level.hpp"
#include "engine.hpp" #include "engine.hpp"
static debug::Logger logger("mainloop"); static debug::Logger logger("mainloop");
@ -12,6 +14,8 @@ inline constexpr int TPS = 20;
TestMainloop::TestMainloop(Engine& engine) : engine(engine) { TestMainloop::TestMainloop(Engine& engine) : engine(engine) {
} }
TestMainloop::~TestMainloop() = default;
void TestMainloop::run() { void TestMainloop::run() {
const auto& coreParams = engine.getCoreParameters(); const auto& coreParams = engine.getCoreParameters();
auto& time = engine.getTime(); auto& time = engine.getTime();
@ -20,6 +24,9 @@ void TestMainloop::run() {
logger.info() << "nothing to do"; logger.info() << "nothing to do";
return; return;
} }
engine.setLevelConsumer([this](auto level) {
setLevel(std::move(level));
});
logger.info() << "starting test " << coreParams.testFile; logger.info() << "starting test " << coreParams.testFile;
auto process = scripting::start_coroutine(coreParams.testFile); auto process = scripting::start_coroutine(coreParams.testFile);
@ -29,3 +36,7 @@ void TestMainloop::run() {
} }
logger.info() << "test finished"; logger.info() << "test finished";
} }
void TestMainloop::setLevel(std::unique_ptr<Level> level) {
this->controller = std::make_unique<LevelController>(&engine, std::move(level));
}

View File

@ -1,11 +1,19 @@
#pragma once #pragma once
#include <memory>
class Level;
class LevelController;
class Engine; class Engine;
class TestMainloop { class TestMainloop {
Engine& engine; Engine& engine;
std::unique_ptr<LevelController> controller;
public: public:
TestMainloop(Engine& engine); TestMainloop(Engine& engine);
~TestMainloop();
void run(); void run();
void setLevel(std::unique_ptr<Level> level);
}; };

View File

@ -19,6 +19,7 @@
#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/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"
@ -35,6 +36,7 @@
#include "window/Events.hpp" #include "window/Events.hpp"
#include "window/input.hpp" #include "window/input.hpp"
#include "window/Window.hpp" #include "window/Window.hpp"
#include "world/Level.hpp"
#include "Mainloop.hpp" #include "Mainloop.hpp"
#include "TestMainloop.hpp" #include "TestMainloop.hpp"
@ -120,7 +122,7 @@ Engine::Engine(CoreParameters coreParameters)
} }
keepAlive(settings.ui.language.observe([=](auto lang) { keepAlive(settings.ui.language.observe([=](auto lang) {
setLanguage(lang); setLanguage(lang);
}, !langNotSet)); }, true));
scripting::initialize(this); scripting::initialize(this);
basePacks = files::read_list(resdir/fs::path("config/builtins.list")); basePacks = files::read_list(resdir/fs::path("config/builtins.list"));
@ -272,6 +274,10 @@ PacksManager Engine::createPacksManager(const fs::path& worldFolder) {
return manager; return manager;
} }
void Engine::setLevelConsumer(consumer<std::unique_ptr<Level>> levelConsumer) {
this->levelConsumer = std::move(levelConsumer);
}
void Engine::loadAssets() { void Engine::loadAssets() {
logger.info() << "loading assets"; logger.info() << "loading assets";
Shader::preprocessor->setPaths(resPaths.get()); Shader::preprocessor->setPaths(resPaths.get());
@ -380,9 +386,11 @@ void Engine::loadContent() {
ContentLoader::loadScripts(*content); ContentLoader::loadScripts(*content);
langs::setup(resdir, langs::current->getId(), contentPacks); langs::setup(resdir, langs::current->getId(), contentPacks);
if (!isHeadless()) {
loadAssets(); loadAssets();
onAssetsLoaded(); onAssetsLoaded();
} }
}
void Engine::resetContent() { void Engine::resetContent() {
scripting::cleanup(); scripting::cleanup();
@ -445,6 +453,10 @@ void Engine::setLanguage(std::string locale) {
} }
} }
void Engine::onWorldOpen(std::unique_ptr<Level> level) {
levelConsumer(std::move(level));
}
gui::GUI* Engine::getGUI() { gui::GUI* Engine::getGUI() {
return gui.get(); return gui.get();
} }

View File

@ -21,6 +21,7 @@
#include <vector> #include <vector>
#include <mutex> #include <mutex>
class Level;
class Screen; class Screen;
class EnginePaths; class EnginePaths;
class ResPaths; class ResPaths;
@ -71,6 +72,7 @@ class Engine : public util::ObjectsKeeper {
std::vector<std::string> basePacks; std::vector<std::string> basePacks;
std::unique_ptr<gui::GUI> gui; std::unique_ptr<gui::GUI> gui;
Time time; Time time;
consumer<std::unique_ptr<Level>> levelConsumer;
void loadControls(); void loadControls();
void loadSettings(); void loadSettings();
@ -132,6 +134,8 @@ public:
/// @brief Get engine resource paths controller /// @brief Get engine resource paths controller
ResPaths* getResPaths(); ResPaths* getResPaths();
void onWorldOpen(std::unique_ptr<Level> level);
/// @brief Get current Content instance /// @brief Get current Content instance
const Content* getContent() const; const Content* getContent() const;
@ -155,6 +159,8 @@ public:
PacksManager createPacksManager(const fs::path& worldFolder); PacksManager createPacksManager(const fs::path& worldFolder);
void setLevelConsumer(consumer<std::unique_ptr<Level>> levelConsumer);
SettingsHandler& getSettingsHandler(); SettingsHandler& getSettingsHandler();
network::Network& getNetwork(); network::Network& getNetwork();

View File

@ -150,9 +150,7 @@ static void load_world(Engine* engine, const std::shared_ptr<WorldFiles>& worldF
auto& settings = engine->getSettings(); auto& settings = engine->getSettings();
auto level = World::load(worldFiles, settings, content, packs); auto level = World::load(worldFiles, settings, content, packs);
engine->setScreen( engine->onWorldOpen(std::move(level));
std::make_shared<LevelScreen>(engine, std::move(level))
);
} catch (const world_load_error& error) { } catch (const world_load_error& error) {
guiutil::alert( guiutil::alert(
engine->getGUI(), engine->getGUI(),
@ -229,7 +227,10 @@ void EngineController::createWorld(
EnginePaths* paths = engine->getPaths(); EnginePaths* paths = engine->getPaths();
auto folder = paths->getWorldsFolder() / fs::u8path(name); auto folder = paths->getWorldsFolder() / fs::u8path(name);
if (!menus::call(engine, [this, paths, folder]() { if (engine->isHeadless()) {
engine->loadContent();
paths->setCurrentWorldFolder(folder);
} else if (!menus::call(engine, [this, paths, folder]() {
engine->loadContent(); engine->loadContent();
paths->setCurrentWorldFolder(folder); paths->setCurrentWorldFolder(folder);
})) { })) {
@ -244,7 +245,7 @@ void EngineController::createWorld(
engine->getContent(), engine->getContent(),
engine->getContentPacks() engine->getContentPacks()
); );
engine->setScreen(std::make_shared<LevelScreen>(engine, std::move(level))); engine->onWorldOpen(std::move(level));
} }
void EngineController::reopenWorld(World* world) { void EngineController::reopenWorld(World* world) {

View File

@ -95,6 +95,8 @@ std::unique_ptr<Level> World::create(
content, content,
packs packs
); );
logger.info() << "created world '" << name << "' (" << directory.u8string() << ")";
logger.info() << "world seed: " << seed << " generator: " << generator;
auto level = std::make_unique<Level>(std::move(world), content, settings); auto level = std::make_unique<Level>(std::move(world), content, settings);
level->players->create(); level->players->create();
return level; return level;