move PlayerController to LevelScreen

This commit is contained in:
MihailRis 2024-12-11 23:42:59 +03:00
parent 9adaf6d527
commit 863146cf6b
7 changed files with 66 additions and 64 deletions

View File

@ -37,7 +37,7 @@ void TestMainloop::run() {
if (controller) { if (controller) {
float delta = time.getDelta(); float delta = time.getDelta();
controller->getLevel()->getWorld()->updateTimers(delta); 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"; logger.info() << "test finished";

View File

@ -1,27 +1,30 @@
#include "LevelScreen.hpp" #include "LevelScreen.hpp"
#include "core_defs.hpp"
#include "frontend/hud.hpp"
#include "frontend/LevelFrontend.hpp"
#include "audio/audio.hpp" #include "audio/audio.hpp"
#include "coders/imageio.hpp" #include "coders/imageio.hpp"
#include "content/Content.hpp"
#include "core_defs.hpp"
#include "debug/Logger.hpp" #include "debug/Logger.hpp"
#include "engine.hpp" #include "engine.hpp"
#include "files/files.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/DrawContext.hpp"
#include "graphics/core/ImageData.hpp" #include "graphics/core/ImageData.hpp"
#include "graphics/core/PostProcessing.hpp" #include "graphics/core/PostProcessing.hpp"
#include "graphics/core/Viewport.hpp" #include "graphics/core/Viewport.hpp"
#include "graphics/render/WorldRenderer.hpp"
#include "graphics/render/Decorator.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/GUI.hpp"
#include "graphics/ui/elements/Menu.hpp"
#include "logic/LevelController.hpp" #include "logic/LevelController.hpp"
#include "logic/PlayerController.hpp"
#include "logic/scripting/scripting.hpp" #include "logic/scripting/scripting.hpp"
#include "logic/scripting/scripting_hud.hpp" #include "logic/scripting/scripting_hud.hpp"
#include "util/stringutil.hpp" #include "maths/voxmaths.hpp"
#include "objects/Players.hpp"
#include "physics/Hitbox.hpp" #include "physics/Hitbox.hpp"
#include "util/stringutil.hpp"
#include "voxels/Chunks.hpp" #include "voxels/Chunks.hpp"
#include "window/Camera.hpp" #include "window/Camera.hpp"
#include "window/Events.hpp" #include "window/Events.hpp"
@ -42,16 +45,25 @@ LevelScreen::LevelScreen(Engine* engine, std::unique_ptr<Level> levelPtr)
menu->reset(); menu->reset();
controller = std::make_unique<LevelController>(engine, std::move(levelPtr)); controller = std::make_unique<LevelController>(engine, std::move(levelPtr));
auto player = level->players->get(0);
playerController = std::make_unique<PlayerController>(
settings,
level,
player,
controller->getBlocksController()
);
frontend = std::make_unique<LevelFrontend>( frontend = std::make_unique<LevelFrontend>(
controller->getPlayer(), controller.get(), assets player, controller.get(), assets
); );
worldRenderer = std::make_unique<WorldRenderer>( worldRenderer = std::make_unique<WorldRenderer>(
engine, *frontend, controller->getPlayer() engine, *frontend, player
); );
hud = std::make_unique<Hud>(engine, *frontend, controller->getPlayer()); hud = std::make_unique<Hud>(engine, *frontend, player);
decorator = std::make_unique<Decorator>( decorator = std::make_unique<Decorator>(
*engine, *controller, *worldRenderer, assets *engine, *controller, *worldRenderer, assets, *player
); );
keepAlive(settings.graphics.backlight.observe([=](bool) { keepAlive(settings.graphics.backlight.observe([=](bool) {
@ -59,7 +71,7 @@ LevelScreen::LevelScreen(Engine* engine, std::unique_ptr<Level> levelPtr)
worldRenderer->clear(); worldRenderer->clear();
})); }));
keepAlive(settings.camera.fov.observe([=](double value) { 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([=](){ keepAlive(Events::getBinding(BIND_CHUNKS_RELOAD).onactived.add([=](){
controller->getLevel()->chunks->saveAndClear(); controller->getLevel()->chunks->saveAndClear();
@ -106,7 +118,7 @@ void LevelScreen::saveWorldPreview() {
try { try {
logger.info() << "saving world preview"; logger.info() << "saving world preview";
auto paths = engine->getPaths(); auto paths = engine->getPaths();
auto player = controller->getPlayer(); auto player = playerController->getPlayer();
auto& settings = engine->getSettings(); auto& settings = engine->getSettings();
int previewSize = settings.ui.worldPreviewSize.get(); int previewSize = settings.ui.worldPreviewSize.get();
@ -129,6 +141,7 @@ void LevelScreen::saveWorldPreview() {
} }
void LevelScreen::updateHotkeys() { void LevelScreen::updateHotkeys() {
auto player = playerController->getPlayer();
auto& settings = engine->getSettings(); auto& settings = engine->getSettings();
if (Events::jpressed(keycode::O)) { if (Events::jpressed(keycode::O)) {
settings.graphics.frustumCulling.toggle(); settings.graphics.frustumCulling.toggle();
@ -137,7 +150,7 @@ void LevelScreen::updateHotkeys() {
hudVisible = !hudVisible; hudVisible = !hudVisible;
} }
if (Events::jpressed(keycode::F3)) { 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(); updateHotkeys();
} }
auto player = controller->getPlayer(); auto player = playerController->getPlayer();
auto camera = player->currentCamera; auto camera = player->currentCamera;
bool paused = hud->isPause(); bool paused = hud->isPause();
@ -167,18 +180,36 @@ void LevelScreen::update(float delta) {
camera->dir, camera->dir,
glm::vec3(0, 1, 0) glm::vec3(0, 1, 0)
); );
auto level = controller->getLevel();
const auto& settings = engine->getSettings();
if (!hud->isPause()) { if (!hud->isPause()) {
controller->getLevel()->getWorld()->updateTimers(delta); level->getWorld()->updateTimers(delta);
animator->update(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); hud->update(hudVisible);
decorator->update(delta, *camera); decorator->update(delta, *camera);
} }
void LevelScreen::draw(float delta) { void LevelScreen::draw(float delta) {
auto camera = controller->getPlayer()->currentCamera; auto camera = playerController->getPlayer()->currentCamera;
Viewport viewport(Window::width, Window::height); Viewport viewport(Window::width, Window::height);
DrawContext ctx(nullptr, viewport, batch.get()); DrawContext ctx(nullptr, viewport, batch.get());

View File

@ -8,6 +8,7 @@ class Engine;
class LevelFrontend; class LevelFrontend;
class Hud; class Hud;
class LevelController; class LevelController;
class PlayerController;
class WorldRenderer; class WorldRenderer;
class TextureAnimator; class TextureAnimator;
class PostProcessing; class PostProcessing;
@ -18,6 +19,7 @@ class Level;
class LevelScreen : public Screen { class LevelScreen : public Screen {
std::unique_ptr<LevelFrontend> frontend; std::unique_ptr<LevelFrontend> frontend;
std::unique_ptr<LevelController> controller; std::unique_ptr<LevelController> controller;
std::unique_ptr<PlayerController> playerController;
std::unique_ptr<WorldRenderer> worldRenderer; std::unique_ptr<WorldRenderer> worldRenderer;
std::unique_ptr<TextureAnimator> animator; std::unique_ptr<TextureAnimator> animator;
std::unique_ptr<PostProcessing> postProcessing; std::unique_ptr<PostProcessing> postProcessing;

View File

@ -10,6 +10,7 @@
#include "voxels/Block.hpp" #include "voxels/Block.hpp"
#include "world/Level.hpp" #include "world/Level.hpp"
#include "window/Camera.hpp" #include "window/Camera.hpp"
#include "objects/Player.hpp"
#include "objects/Players.hpp" #include "objects/Players.hpp"
#include "logic/LevelController.hpp" #include "logic/LevelController.hpp"
#include "util/stringutil.hpp" #include "util/stringutil.hpp"
@ -29,13 +30,17 @@ inline constexpr int ITERATIONS = 512;
inline constexpr int BIG_PRIME = 666667; inline constexpr int BIG_PRIME = 666667;
Decorator::Decorator( Decorator::Decorator(
Engine& engine, LevelController& controller, WorldRenderer& renderer, const Assets& assets Engine& engine,
LevelController& controller,
WorldRenderer& renderer,
const Assets& assets,
Player& player
) )
: engine(engine), : engine(engine),
level(*controller.getLevel()), level(*controller.getLevel()),
renderer(renderer), renderer(renderer),
assets(assets), assets(assets),
player(*controller.getPlayer()) { player(player) {
controller.getBlocksController()->listenBlockInteraction( controller.getBlocksController()->listenBlockInteraction(
[this](auto player, const auto& pos, const auto& def, BlockInteraction type) { [this](auto player, const auto& pos, const auto& def, BlockInteraction type) {
if (type == BlockInteraction::placing && def.particles) { if (type == BlockInteraction::placing && def.particles) {
@ -43,7 +48,7 @@ Decorator::Decorator(
} }
}); });
for (const auto& [id, player] : *level.players) { for (const auto& [id, player] : *level.players) {
if (id == controller.getPlayer()->getId()) { if (id == this->player.getId()) {
continue; continue;
} }
playerTexts[id] = renderer.texts->add(std::make_unique<TextNote>( playerTexts[id] = renderer.texts->add(std::make_unique<TextNote>(

View File

@ -39,7 +39,8 @@ public:
Engine& engine, Engine& engine,
LevelController& level, LevelController& level,
WorldRenderer& renderer, WorldRenderer& renderer,
const Assets& assets const Assets& assets,
Player& player
); );
void update(float delta, const Camera& camera); void update(float delta, const Camera& camera);

View File

@ -5,14 +5,14 @@
#include "debug/Logger.hpp" #include "debug/Logger.hpp"
#include "engine.hpp" #include "engine.hpp"
#include "files/WorldFiles.hpp" #include "files/WorldFiles.hpp"
#include "maths/voxmaths.hpp"
#include "objects/Entities.hpp" #include "objects/Entities.hpp"
#include "objects/Players.hpp" #include "objects/Players.hpp"
#include "physics/Hitbox.hpp" #include "physics/Hitbox.hpp"
#include "scripting/scripting.hpp"
#include "settings.hpp" #include "settings.hpp"
#include "world/Level.hpp" #include "world/Level.hpp"
#include "world/World.hpp" #include "world/World.hpp"
#include "maths/voxmaths.hpp"
#include "scripting/scripting.hpp"
static debug::Logger logger("level-control"); static debug::Logger logger("level-control");
@ -25,41 +25,17 @@ LevelController::LevelController(Engine* engine, std::unique_ptr<Level> levelPtr
chunks(std::make_unique<ChunksController>( chunks(std::make_unique<ChunksController>(
*level, settings.chunks.padding.get() *level, settings.chunks.padding.get()
)) { )) {
if (!engine->isHeadless()) {
player = std::make_unique<PlayerController>(
settings, level.get(), level->players->get(0), blocks.get()
);
}
scripting::on_world_load(this); scripting::on_world_load(this);
} }
void LevelController::update(float delta, bool input, bool pause) { void LevelController::update(float delta, 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)
);
}
if (!pause) { if (!pause) {
// update all objects that needed // update all objects that needed
blocks->update(delta); blocks->update(delta);
if (player) {
player->update(delta, input);
}
level->entities->updatePhysics(delta); level->entities->updatePhysics(delta);
level->entities->update(delta); level->entities->update(delta);
} }
level->entities->clean(); level->entities->clean();
if (player) {
player->postUpdate(delta, input, pause);
}
} }
void LevelController::saveWorld() { void LevelController::saveWorld() {
@ -79,10 +55,6 @@ Level* LevelController::getLevel() {
return level.get(); return level.get();
} }
Player* LevelController::getPlayer() {
return player->getPlayer();
}
BlocksController* LevelController::getBlocksController() { BlocksController* LevelController::getBlocksController() {
return blocks.get(); return blocks.get();
} }
@ -90,7 +62,3 @@ BlocksController* LevelController::getBlocksController() {
ChunksController* LevelController::getChunksController() { ChunksController* LevelController::getChunksController() {
return chunks.get(); return chunks.get();
} }
PlayerController* LevelController::getPlayerController() {
return player.get();
}

View File

@ -4,7 +4,6 @@
#include "BlocksController.hpp" #include "BlocksController.hpp"
#include "ChunksController.hpp" #include "ChunksController.hpp"
#include "PlayerController.hpp"
class Engine; class Engine;
class Level; class Level;
@ -18,23 +17,19 @@ class LevelController {
// Sub-controllers // Sub-controllers
std::unique_ptr<BlocksController> blocks; std::unique_ptr<BlocksController> blocks;
std::unique_ptr<ChunksController> chunks; std::unique_ptr<ChunksController> chunks;
std::unique_ptr<PlayerController> player;
public: public:
LevelController(Engine* engine, std::unique_ptr<Level> level); LevelController(Engine* engine, std::unique_ptr<Level> level);
/// @param delta time elapsed since the last update /// @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 /// @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 saveWorld();
void onWorldQuit(); void onWorldQuit();
Level* getLevel(); Level* getLevel();
Player* getPlayer();
BlocksController* getBlocksController(); BlocksController* getBlocksController();
ChunksController* getChunksController(); ChunksController* getChunksController();
PlayerController* getPlayerController();
}; };