diff --git a/src/TestMainloop.cpp b/src/TestMainloop.cpp index ddf0ca171..334870591 100644 --- a/src/TestMainloop.cpp +++ b/src/TestMainloop.cpp @@ -37,7 +37,7 @@ void TestMainloop::run() { if (controller) { float delta = time.getDelta(); controller->getLevel()->getWorld()->updateTimers(delta); - controller->update(glm::min(delta, 0.2f), false, false); + controller->update(glm::min(delta, 0.2f), false); } } logger.info() << "test finished"; diff --git a/src/frontend/screens/LevelScreen.cpp b/src/frontend/screens/LevelScreen.cpp index 4436cf488..98b216133 100644 --- a/src/frontend/screens/LevelScreen.cpp +++ b/src/frontend/screens/LevelScreen.cpp @@ -1,27 +1,30 @@ #include "LevelScreen.hpp" -#include "core_defs.hpp" -#include "frontend/hud.hpp" -#include "frontend/LevelFrontend.hpp" #include "audio/audio.hpp" #include "coders/imageio.hpp" +#include "content/Content.hpp" +#include "core_defs.hpp" #include "debug/Logger.hpp" #include "engine.hpp" #include "files/files.hpp" -#include "content/Content.hpp" +#include "frontend/LevelFrontend.hpp" +#include "frontend/hud.hpp" #include "graphics/core/DrawContext.hpp" #include "graphics/core/ImageData.hpp" #include "graphics/core/PostProcessing.hpp" #include "graphics/core/Viewport.hpp" -#include "graphics/render/WorldRenderer.hpp" #include "graphics/render/Decorator.hpp" -#include "graphics/ui/elements/Menu.hpp" +#include "graphics/render/WorldRenderer.hpp" #include "graphics/ui/GUI.hpp" +#include "graphics/ui/elements/Menu.hpp" #include "logic/LevelController.hpp" +#include "logic/PlayerController.hpp" #include "logic/scripting/scripting.hpp" #include "logic/scripting/scripting_hud.hpp" -#include "util/stringutil.hpp" +#include "maths/voxmaths.hpp" +#include "objects/Players.hpp" #include "physics/Hitbox.hpp" +#include "util/stringutil.hpp" #include "voxels/Chunks.hpp" #include "window/Camera.hpp" #include "window/Events.hpp" @@ -42,16 +45,25 @@ LevelScreen::LevelScreen(Engine* engine, std::unique_ptr levelPtr) menu->reset(); controller = std::make_unique(engine, std::move(levelPtr)); + + auto player = level->players->get(0); + playerController = std::make_unique( + settings, + level, + player, + controller->getBlocksController() + ); + frontend = std::make_unique( - controller->getPlayer(), controller.get(), assets + player, controller.get(), assets ); worldRenderer = std::make_unique( - engine, *frontend, controller->getPlayer() + engine, *frontend, player ); - hud = std::make_unique(engine, *frontend, controller->getPlayer()); + hud = std::make_unique(engine, *frontend, player); decorator = std::make_unique( - *engine, *controller, *worldRenderer, assets + *engine, *controller, *worldRenderer, assets, *player ); keepAlive(settings.graphics.backlight.observe([=](bool) { @@ -59,7 +71,7 @@ LevelScreen::LevelScreen(Engine* engine, std::unique_ptr levelPtr) worldRenderer->clear(); })); keepAlive(settings.camera.fov.observe([=](double value) { - controller->getPlayer()->fpCamera->setFov(glm::radians(value)); + player->fpCamera->setFov(glm::radians(value)); })); keepAlive(Events::getBinding(BIND_CHUNKS_RELOAD).onactived.add([=](){ controller->getLevel()->chunks->saveAndClear(); @@ -106,7 +118,7 @@ void LevelScreen::saveWorldPreview() { try { logger.info() << "saving world preview"; auto paths = engine->getPaths(); - auto player = controller->getPlayer(); + auto player = playerController->getPlayer(); auto& settings = engine->getSettings(); int previewSize = settings.ui.worldPreviewSize.get(); @@ -129,6 +141,7 @@ void LevelScreen::saveWorldPreview() { } void LevelScreen::updateHotkeys() { + auto player = playerController->getPlayer(); auto& settings = engine->getSettings(); if (Events::jpressed(keycode::O)) { settings.graphics.frustumCulling.toggle(); @@ -137,7 +150,7 @@ void LevelScreen::updateHotkeys() { hudVisible = !hudVisible; } if (Events::jpressed(keycode::F3)) { - controller->getPlayer()->debug = !controller->getPlayer()->debug; + player->debug = !player->debug; } } @@ -151,7 +164,7 @@ void LevelScreen::update(float delta) { updateHotkeys(); } - auto player = controller->getPlayer(); + auto player = playerController->getPlayer(); auto camera = player->currentCamera; bool paused = hud->isPause(); @@ -167,18 +180,36 @@ void LevelScreen::update(float delta) { camera->dir, glm::vec3(0, 1, 0) ); + auto level = controller->getLevel(); + const auto& settings = engine->getSettings(); if (!hud->isPause()) { - controller->getLevel()->getWorld()->updateTimers(delta); + level->getWorld()->updateTimers(delta); animator->update(delta); } - controller->update(glm::min(delta, 0.2f), !inputLocked, hud->isPause()); + + glm::vec3 position = player->getPosition(); + level->loadMatrix( + position.x, + position.z, + settings.chunks.loadDistance.get() + settings.chunks.padding.get() * 2 + ); + controller->getChunksController()->update( + settings.chunks.loadSpeed.get(), settings.chunks.loadDistance.get(), + floordiv(position.x, CHUNK_W), floordiv(position.z, CHUNK_D) + ); + if (!hud->isPause()) { + playerController->update(delta, !inputLocked); + } + controller->update(glm::min(delta, 0.2f), hud->isPause()); + playerController->postUpdate(delta, !inputLocked, hud->isPause()); + hud->update(hudVisible); decorator->update(delta, *camera); } void LevelScreen::draw(float delta) { - auto camera = controller->getPlayer()->currentCamera; + auto camera = playerController->getPlayer()->currentCamera; Viewport viewport(Window::width, Window::height); DrawContext ctx(nullptr, viewport, batch.get()); diff --git a/src/frontend/screens/LevelScreen.hpp b/src/frontend/screens/LevelScreen.hpp index db0f04ff4..1810359b0 100644 --- a/src/frontend/screens/LevelScreen.hpp +++ b/src/frontend/screens/LevelScreen.hpp @@ -8,6 +8,7 @@ class Engine; class LevelFrontend; class Hud; class LevelController; +class PlayerController; class WorldRenderer; class TextureAnimator; class PostProcessing; @@ -18,6 +19,7 @@ class Level; class LevelScreen : public Screen { std::unique_ptr frontend; std::unique_ptr controller; + std::unique_ptr playerController; std::unique_ptr worldRenderer; std::unique_ptr animator; std::unique_ptr postProcessing; diff --git a/src/graphics/render/Decorator.cpp b/src/graphics/render/Decorator.cpp index b7bd42188..2f01955fb 100644 --- a/src/graphics/render/Decorator.cpp +++ b/src/graphics/render/Decorator.cpp @@ -10,6 +10,7 @@ #include "voxels/Block.hpp" #include "world/Level.hpp" #include "window/Camera.hpp" +#include "objects/Player.hpp" #include "objects/Players.hpp" #include "logic/LevelController.hpp" #include "util/stringutil.hpp" @@ -29,13 +30,17 @@ inline constexpr int ITERATIONS = 512; inline constexpr int BIG_PRIME = 666667; Decorator::Decorator( - Engine& engine, LevelController& controller, WorldRenderer& renderer, const Assets& assets + Engine& engine, + LevelController& controller, + WorldRenderer& renderer, + const Assets& assets, + Player& player ) : engine(engine), level(*controller.getLevel()), renderer(renderer), assets(assets), - player(*controller.getPlayer()) { + player(player) { controller.getBlocksController()->listenBlockInteraction( [this](auto player, const auto& pos, const auto& def, BlockInteraction type) { if (type == BlockInteraction::placing && def.particles) { @@ -43,7 +48,7 @@ Decorator::Decorator( } }); for (const auto& [id, player] : *level.players) { - if (id == controller.getPlayer()->getId()) { + if (id == this->player.getId()) { continue; } playerTexts[id] = renderer.texts->add(std::make_unique( diff --git a/src/graphics/render/Decorator.hpp b/src/graphics/render/Decorator.hpp index fbc196aa9..d19ed45f8 100644 --- a/src/graphics/render/Decorator.hpp +++ b/src/graphics/render/Decorator.hpp @@ -39,7 +39,8 @@ class Decorator { Engine& engine, LevelController& level, WorldRenderer& renderer, - const Assets& assets + const Assets& assets, + Player& player ); void update(float delta, const Camera& camera); diff --git a/src/logic/LevelController.cpp b/src/logic/LevelController.cpp index 87c2b6ad8..9e30db489 100644 --- a/src/logic/LevelController.cpp +++ b/src/logic/LevelController.cpp @@ -5,14 +5,14 @@ #include "debug/Logger.hpp" #include "engine.hpp" #include "files/WorldFiles.hpp" +#include "maths/voxmaths.hpp" #include "objects/Entities.hpp" #include "objects/Players.hpp" #include "physics/Hitbox.hpp" +#include "scripting/scripting.hpp" #include "settings.hpp" #include "world/Level.hpp" #include "world/World.hpp" -#include "maths/voxmaths.hpp" -#include "scripting/scripting.hpp" static debug::Logger logger("level-control"); @@ -25,41 +25,17 @@ LevelController::LevelController(Engine* engine, std::unique_ptr levelPtr chunks(std::make_unique( *level, settings.chunks.padding.get() )) { - if (!engine->isHeadless()) { - player = std::make_unique( - settings, level.get(), level->players->get(0), blocks.get() - ); - } scripting::on_world_load(this); } -void LevelController::update(float delta, bool input, bool pause) { - if (player) { - glm::vec3 position = player->getPlayer()->getPosition(); - level->loadMatrix( - position.x, - position.z, - settings.chunks.loadDistance.get() + settings.chunks.padding.get() * 2 - ); - chunks->update( - settings.chunks.loadSpeed.get(), settings.chunks.loadDistance.get(), - floordiv(position.x, CHUNK_W), floordiv(position.z, CHUNK_D) - ); - } - +void LevelController::update(float delta, bool pause) { if (!pause) { // update all objects that needed blocks->update(delta); - if (player) { - player->update(delta, input); - } level->entities->updatePhysics(delta); level->entities->update(delta); } level->entities->clean(); - if (player) { - player->postUpdate(delta, input, pause); - } } void LevelController::saveWorld() { @@ -79,10 +55,6 @@ Level* LevelController::getLevel() { return level.get(); } -Player* LevelController::getPlayer() { - return player->getPlayer(); -} - BlocksController* LevelController::getBlocksController() { return blocks.get(); } @@ -90,7 +62,3 @@ BlocksController* LevelController::getBlocksController() { ChunksController* LevelController::getChunksController() { return chunks.get(); } - -PlayerController* LevelController::getPlayerController() { - return player.get(); -} diff --git a/src/logic/LevelController.hpp b/src/logic/LevelController.hpp index 94e0ac2c5..6cfcf8167 100644 --- a/src/logic/LevelController.hpp +++ b/src/logic/LevelController.hpp @@ -4,7 +4,6 @@ #include "BlocksController.hpp" #include "ChunksController.hpp" -#include "PlayerController.hpp" class Engine; class Level; @@ -18,23 +17,19 @@ class LevelController { // Sub-controllers std::unique_ptr blocks; std::unique_ptr chunks; - std::unique_ptr player; public: LevelController(Engine* engine, std::unique_ptr level); /// @param delta time elapsed since the last update - /// @param input is user input allowed to be handled /// @param pause is world and player simulation paused - void update(float delta, bool input, bool pause); + void update(float delta, bool pause); void saveWorld(); void onWorldQuit(); Level* getLevel(); - Player* getPlayer(); BlocksController* getBlocksController(); ChunksController* getChunksController(); - PlayerController* getPlayerController(); };