From 4a0bc016ce961eaad57a59c4603ba55f0789e185 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 15 Jul 2024 15:41:25 +0300 Subject: [PATCH] refactor: extract method placeBlock and move some code from PlayerController to BlocksController --- src/frontend/LevelFrontend.cpp | 2 +- src/logic/BlocksController.cpp | 32 ++++++++++++++++++++++++++++++-- src/logic/BlocksController.hpp | 25 +++++++++++++++++++++++++ src/logic/PlayerController.cpp | 30 ++++-------------------------- src/logic/PlayerController.hpp | 20 -------------------- 5 files changed, 60 insertions(+), 49 deletions(-) diff --git a/src/frontend/LevelFrontend.cpp b/src/frontend/LevelFrontend.cpp index 3dffae5c..13157da7 100644 --- a/src/frontend/LevelFrontend.cpp +++ b/src/frontend/LevelFrontend.cpp @@ -22,7 +22,7 @@ LevelFrontend::LevelFrontend(LevelController* controller, Assets* assets) BlocksPreview::build(contentCache.get(), assets, level->content), "block-previews" ); - controller->getPlayerController()->listenBlockInteraction( + controller->getBlocksController()->listenBlockInteraction( [=](Player*, glm::ivec3 pos, const Block* def, BlockInteraction type) { auto material = level->content->findBlockMaterial(def->material); if (material == nullptr) { diff --git a/src/logic/BlocksController.cpp b/src/logic/BlocksController.cpp index 218100c1..5a57d729 100644 --- a/src/logic/BlocksController.cpp +++ b/src/logic/BlocksController.cpp @@ -71,12 +71,25 @@ void BlocksController::updateSides(int x, int y, int z) { } void BlocksController::breakBlock(Player* player, const Block* def, int x, int y, int z) { - chunks->set(x,y,z, 0, {}); - lighting->onBlockSet(x,y,z, 0); + onBlockInteraction( + player, glm::ivec3(x, y, z), def, BlockInteraction::destruction); + chunks->set(x, y, z, 0, {}); + lighting->onBlockSet(x, y, z, 0); scripting::on_block_broken(player, def, x, y, z); updateSides(x, y, z); } +void BlocksController::placeBlock(Player* player, const Block* def, blockstate state, int x, int y, int z) { + onBlockInteraction( + player, glm::ivec3(x, y, z), def, BlockInteraction::placing); + chunks->set(x, y, z, def->rt.id, state); + lighting->onBlockSet(x, y, z, def->rt.id); + if (def->rt.funcsset.onplaced) { + scripting::on_block_placed(player, def, x, y, z); + } + updateSides(x, y, z); +} + void BlocksController::updateBlock(int x, int y, int z) { voxel* vox = chunks->get(x, y, z); if (vox == nullptr) @@ -201,3 +214,18 @@ void BlocksController::unbindInventory(int x, int y, int z) { int lz = z - chunk->z * CHUNK_D; chunk->removeBlockInventory(lx, y, lz); } + +void BlocksController::onBlockInteraction( + Player* player, + glm::ivec3 pos, + const Block* def, + BlockInteraction type +) { + for (const auto& callback : blockInteractionCallbacks) { + callback(player, pos, def, type); + } +} + +void BlocksController::listenBlockInteraction(const on_block_interaction& callback) { + blockInteractionCallbacks.push_back(callback); +} diff --git a/src/logic/BlocksController.hpp b/src/logic/BlocksController.hpp index 9beb09bf..4c989bc4 100644 --- a/src/logic/BlocksController.hpp +++ b/src/logic/BlocksController.hpp @@ -3,6 +3,10 @@ #include "../typedefs.hpp" #include "../maths/fastmaths.hpp" +#include "../voxels/voxel.hpp" + +#include +#include class Player; class Block; @@ -10,6 +14,16 @@ class Level; class Chunks; class Lighting; +enum class BlockInteraction { + step, + destruction, + placing +}; + +using on_block_interaction = std::function; + class Clock { int tickRate; int tickParts; @@ -38,6 +52,7 @@ class BlocksController { Clock worldTickClock; uint padding; FastRandom random; + std::vector blockInteractionCallbacks; public: BlocksController(Level* level, uint padding); @@ -45,6 +60,7 @@ public: void updateBlock(int x, int y, int z); void breakBlock(Player* player, const Block* def, int x, int y, int z); + void placeBlock(Player* player, const Block* def, blockstate state, int x, int y, int z); void update(float delta); void randomTick(int tickid, int parts); @@ -52,6 +68,15 @@ public: int64_t createBlockInventory(int x, int y, int z); void bindInventory(int64_t invid, int x, int y, int z); void unbindInventory(int x, int y, int z); + + void onBlockInteraction( + Player* player, + glm::ivec3 pos, + const Block* def, + BlockInteraction type + ); + + void listenBlockInteraction(const on_block_interaction& callback); }; #endif // LOGIC_BLOCKS_CONTROLLER_HPP_ diff --git a/src/logic/PlayerController.cpp b/src/logic/PlayerController.cpp index aeca1f17..13c53f08 100644 --- a/src/logic/PlayerController.cpp +++ b/src/logic/PlayerController.cpp @@ -196,15 +196,6 @@ PlayerController::PlayerController( blocksController(blocksController) {} -void PlayerController::onBlockInteraction( - glm::ivec3 pos, - const Block* def, - BlockInteraction type -) { - for (const auto& callback : blockInteractionCallbacks) { - callback(player.get(), pos, def, type); - } -} void PlayerController::onFootstep(const Hitbox& hitbox) { auto pos = hitbox.position; @@ -220,7 +211,8 @@ void PlayerController::onFootstep(const Hitbox& hitbox) { auto def = level->content->getIndices()->blocks.get(vox->id); if (!def->obstacle) continue; - onBlockInteraction( + blocksController->onBlockInteraction( + player.get(), glm::ivec3(x, y, z), def, BlockInteraction::step ); @@ -402,7 +394,6 @@ void PlayerController::processRightClick(Block* def, Block* target) { const auto& selection = player->selection; auto chunks = level->chunks.get(); auto camera = player->camera.get(); - auto lighting = level->lighting.get(); blockstate state {}; state.rotation = determine_rotation(def, selection.normal, camera->dir); @@ -440,13 +431,8 @@ void PlayerController::processRightClick(Block* def, Block* target) { } } if (chosenBlock != vox->id && chosenBlock) { - onBlockInteraction(coord, def, BlockInteraction::placing); - chunks->set(coord.x, coord.y, coord.z, chosenBlock, state); - lighting->onBlockSet(coord.x, coord.y, coord.z, chosenBlock); - if (def->rt.funcsset.onplaced) { - scripting::on_block_placed(player.get(), def, coord.x, coord.y, coord.z); - } - blocksController->updateSides(coord.x, coord.y, coord.z); + blocksController->placeBlock( + player.get(), def, state, coord.x, coord.y, coord.z); } } @@ -480,10 +466,6 @@ void PlayerController::updateInteraction() { } auto target = indices->blocks.get(vox->id); if (lclick && target->breakable){ - onBlockInteraction( - iend, target, - BlockInteraction::destruction - ); blocksController->breakBlock(player.get(), target, iend.x, iend.y, iend.z); } if (rclick && !input.shift) { @@ -512,7 +494,3 @@ void PlayerController::updateInteraction() { Player* PlayerController::getPlayer() { return player.get(); } - -void PlayerController::listenBlockInteraction(const on_block_interaction& callback) { - blockInteractionCallbacks.push_back(callback); -} diff --git a/src/logic/PlayerController.hpp b/src/logic/PlayerController.hpp index 140a001f..69938fd4 100644 --- a/src/logic/PlayerController.hpp +++ b/src/logic/PlayerController.hpp @@ -5,7 +5,6 @@ #include #include -#include #include class Camera; @@ -46,16 +45,6 @@ public: void refresh(); }; -enum class BlockInteraction { - step, - destruction, - placing -}; - -using on_block_interaction = std::function; - class PlayerController { Level* level; std::shared_ptr player; @@ -63,17 +52,10 @@ class PlayerController { CameraControl camControl; BlocksController* blocksController; - std::vector blockInteractionCallbacks; - void updateKeyboard(); void resetKeyboard(); void updatePlayer(float delta); void updateInteraction(); - void onBlockInteraction( - glm::ivec3 pos, - const Block* def, - BlockInteraction type - ); float stepsTimer = 0.0f; void onFootstep(const Hitbox& hitbox); @@ -90,8 +72,6 @@ public: void update(float delta, bool input, bool pause); void postUpdate(float delta, bool input, bool pause); Player* getPlayer(); - - void listenBlockInteraction(const on_block_interaction& callback); }; #endif /* PLAYER_CONTROL_HPP_ */