move PlayerController to LevelScreen
This commit is contained in:
parent
9adaf6d527
commit
863146cf6b
@ -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";
|
||||
|
||||
@ -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<Level> levelPtr)
|
||||
menu->reset();
|
||||
|
||||
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>(
|
||||
controller->getPlayer(), controller.get(), assets
|
||||
player, controller.get(), assets
|
||||
);
|
||||
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>(
|
||||
*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<Level> 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());
|
||||
|
||||
@ -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<LevelFrontend> frontend;
|
||||
std::unique_ptr<LevelController> controller;
|
||||
std::unique_ptr<PlayerController> playerController;
|
||||
std::unique_ptr<WorldRenderer> worldRenderer;
|
||||
std::unique_ptr<TextureAnimator> animator;
|
||||
std::unique_ptr<PostProcessing> postProcessing;
|
||||
|
||||
@ -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<TextNote>(
|
||||
|
||||
@ -39,7 +39,8 @@ public:
|
||||
Engine& engine,
|
||||
LevelController& level,
|
||||
WorldRenderer& renderer,
|
||||
const Assets& assets
|
||||
const Assets& assets,
|
||||
Player& player
|
||||
);
|
||||
|
||||
void update(float delta, const Camera& camera);
|
||||
|
||||
@ -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<Level> levelPtr
|
||||
chunks(std::make_unique<ChunksController>(
|
||||
*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);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
@ -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<BlocksController> blocks;
|
||||
std::unique_ptr<ChunksController> chunks;
|
||||
std::unique_ptr<PlayerController> player;
|
||||
public:
|
||||
LevelController(Engine* engine, std::unique_ptr<Level> 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();
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user