mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-05 02:51:49 +00:00
refactor: extract method placeBlock and move some code from PlayerController to BlocksController
This commit is contained in:
parent
7c6d620560
commit
4a0bc016ce
5 changed files with 60 additions and 49 deletions
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue