refactor: extract method placeBlock and move some code from PlayerController to BlocksController

This commit is contained in:
MihailRis 2024-07-15 15:41:25 +03:00
parent 7c6d620560
commit 4a0bc016ce
5 changed files with 60 additions and 49 deletions

View File

@ -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) {

View File

@ -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);
}

View File

@ -3,6 +3,10 @@
#include "../typedefs.hpp"
#include "../maths/fastmaths.hpp"
#include "../voxels/voxel.hpp"
#include <functional>
#include <glm/glm.hpp>
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<void(
Player*, glm::ivec3, const Block*, BlockInteraction type
)>;
class Clock {
int tickRate;
int tickParts;
@ -38,6 +52,7 @@ class BlocksController {
Clock worldTickClock;
uint padding;
FastRandom random;
std::vector<on_block_interaction> 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_

View File

@ -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);
}

View File

@ -5,7 +5,6 @@
#include <memory>
#include <vector>
#include <functional>
#include <glm/glm.hpp>
class Camera;
@ -46,16 +45,6 @@ public:
void refresh();
};
enum class BlockInteraction {
step,
destruction,
placing
};
using on_block_interaction = std::function<void(
Player*, glm::ivec3, const Block*, BlockInteraction type
)>;
class PlayerController {
Level* level;
std::shared_ptr<Player> player;
@ -63,17 +52,10 @@ class PlayerController {
CameraControl camControl;
BlocksController* blocksController;
std::vector<on_block_interaction> 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_ */