refactor: extract method placeBlock and move some code from PlayerController to BlocksController
This commit is contained in:
parent
7c6d620560
commit
4a0bc016ce
@ -22,7 +22,7 @@ LevelFrontend::LevelFrontend(LevelController* controller, Assets* assets)
|
|||||||
BlocksPreview::build(contentCache.get(), assets, level->content),
|
BlocksPreview::build(contentCache.get(), assets, level->content),
|
||||||
"block-previews"
|
"block-previews"
|
||||||
);
|
);
|
||||||
controller->getPlayerController()->listenBlockInteraction(
|
controller->getBlocksController()->listenBlockInteraction(
|
||||||
[=](Player*, glm::ivec3 pos, const Block* def, BlockInteraction type) {
|
[=](Player*, glm::ivec3 pos, const Block* def, BlockInteraction type) {
|
||||||
auto material = level->content->findBlockMaterial(def->material);
|
auto material = level->content->findBlockMaterial(def->material);
|
||||||
if (material == nullptr) {
|
if (material == nullptr) {
|
||||||
|
|||||||
@ -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) {
|
void BlocksController::breakBlock(Player* player, const Block* def, int x, int y, int z) {
|
||||||
chunks->set(x,y,z, 0, {});
|
onBlockInteraction(
|
||||||
lighting->onBlockSet(x,y,z, 0);
|
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);
|
scripting::on_block_broken(player, def, x, y, z);
|
||||||
updateSides(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) {
|
void BlocksController::updateBlock(int x, int y, int z) {
|
||||||
voxel* vox = chunks->get(x, y, z);
|
voxel* vox = chunks->get(x, y, z);
|
||||||
if (vox == nullptr)
|
if (vox == nullptr)
|
||||||
@ -201,3 +214,18 @@ void BlocksController::unbindInventory(int x, int y, int z) {
|
|||||||
int lz = z - chunk->z * CHUNK_D;
|
int lz = z - chunk->z * CHUNK_D;
|
||||||
chunk->removeBlockInventory(lx, y, lz);
|
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);
|
||||||
|
}
|
||||||
|
|||||||
@ -3,6 +3,10 @@
|
|||||||
|
|
||||||
#include "../typedefs.hpp"
|
#include "../typedefs.hpp"
|
||||||
#include "../maths/fastmaths.hpp"
|
#include "../maths/fastmaths.hpp"
|
||||||
|
#include "../voxels/voxel.hpp"
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
class Player;
|
class Player;
|
||||||
class Block;
|
class Block;
|
||||||
@ -10,6 +14,16 @@ class Level;
|
|||||||
class Chunks;
|
class Chunks;
|
||||||
class Lighting;
|
class Lighting;
|
||||||
|
|
||||||
|
enum class BlockInteraction {
|
||||||
|
step,
|
||||||
|
destruction,
|
||||||
|
placing
|
||||||
|
};
|
||||||
|
|
||||||
|
using on_block_interaction = std::function<void(
|
||||||
|
Player*, glm::ivec3, const Block*, BlockInteraction type
|
||||||
|
)>;
|
||||||
|
|
||||||
class Clock {
|
class Clock {
|
||||||
int tickRate;
|
int tickRate;
|
||||||
int tickParts;
|
int tickParts;
|
||||||
@ -38,6 +52,7 @@ class BlocksController {
|
|||||||
Clock worldTickClock;
|
Clock worldTickClock;
|
||||||
uint padding;
|
uint padding;
|
||||||
FastRandom random;
|
FastRandom random;
|
||||||
|
std::vector<on_block_interaction> blockInteractionCallbacks;
|
||||||
public:
|
public:
|
||||||
BlocksController(Level* level, uint padding);
|
BlocksController(Level* level, uint padding);
|
||||||
|
|
||||||
@ -45,6 +60,7 @@ public:
|
|||||||
void updateBlock(int x, int y, int z);
|
void updateBlock(int x, int y, int z);
|
||||||
|
|
||||||
void breakBlock(Player* player, const Block* def, 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 update(float delta);
|
||||||
void randomTick(int tickid, int parts);
|
void randomTick(int tickid, int parts);
|
||||||
@ -52,6 +68,15 @@ public:
|
|||||||
int64_t createBlockInventory(int x, int y, int z);
|
int64_t createBlockInventory(int x, int y, int z);
|
||||||
void bindInventory(int64_t invid, 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 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_
|
#endif // LOGIC_BLOCKS_CONTROLLER_HPP_
|
||||||
|
|||||||
@ -196,15 +196,6 @@ PlayerController::PlayerController(
|
|||||||
blocksController(blocksController)
|
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) {
|
void PlayerController::onFootstep(const Hitbox& hitbox) {
|
||||||
auto pos = hitbox.position;
|
auto pos = hitbox.position;
|
||||||
@ -220,7 +211,8 @@ void PlayerController::onFootstep(const Hitbox& hitbox) {
|
|||||||
auto def = level->content->getIndices()->blocks.get(vox->id);
|
auto def = level->content->getIndices()->blocks.get(vox->id);
|
||||||
if (!def->obstacle)
|
if (!def->obstacle)
|
||||||
continue;
|
continue;
|
||||||
onBlockInteraction(
|
blocksController->onBlockInteraction(
|
||||||
|
player.get(),
|
||||||
glm::ivec3(x, y, z), def,
|
glm::ivec3(x, y, z), def,
|
||||||
BlockInteraction::step
|
BlockInteraction::step
|
||||||
);
|
);
|
||||||
@ -402,7 +394,6 @@ void PlayerController::processRightClick(Block* def, Block* target) {
|
|||||||
const auto& selection = player->selection;
|
const auto& selection = player->selection;
|
||||||
auto chunks = level->chunks.get();
|
auto chunks = level->chunks.get();
|
||||||
auto camera = player->camera.get();
|
auto camera = player->camera.get();
|
||||||
auto lighting = level->lighting.get();
|
|
||||||
|
|
||||||
blockstate state {};
|
blockstate state {};
|
||||||
state.rotation = determine_rotation(def, selection.normal, camera->dir);
|
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) {
|
if (chosenBlock != vox->id && chosenBlock) {
|
||||||
onBlockInteraction(coord, def, BlockInteraction::placing);
|
blocksController->placeBlock(
|
||||||
chunks->set(coord.x, coord.y, coord.z, chosenBlock, state);
|
player.get(), def, state, coord.x, coord.y, coord.z);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -480,10 +466,6 @@ void PlayerController::updateInteraction() {
|
|||||||
}
|
}
|
||||||
auto target = indices->blocks.get(vox->id);
|
auto target = indices->blocks.get(vox->id);
|
||||||
if (lclick && target->breakable){
|
if (lclick && target->breakable){
|
||||||
onBlockInteraction(
|
|
||||||
iend, target,
|
|
||||||
BlockInteraction::destruction
|
|
||||||
);
|
|
||||||
blocksController->breakBlock(player.get(), target, iend.x, iend.y, iend.z);
|
blocksController->breakBlock(player.get(), target, iend.x, iend.y, iend.z);
|
||||||
}
|
}
|
||||||
if (rclick && !input.shift) {
|
if (rclick && !input.shift) {
|
||||||
@ -512,7 +494,3 @@ void PlayerController::updateInteraction() {
|
|||||||
Player* PlayerController::getPlayer() {
|
Player* PlayerController::getPlayer() {
|
||||||
return player.get();
|
return player.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerController::listenBlockInteraction(const on_block_interaction& callback) {
|
|
||||||
blockInteractionCallbacks.push_back(callback);
|
|
||||||
}
|
|
||||||
|
|||||||
@ -5,7 +5,6 @@
|
|||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <functional>
|
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
class Camera;
|
class Camera;
|
||||||
@ -46,16 +45,6 @@ public:
|
|||||||
void refresh();
|
void refresh();
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class BlockInteraction {
|
|
||||||
step,
|
|
||||||
destruction,
|
|
||||||
placing
|
|
||||||
};
|
|
||||||
|
|
||||||
using on_block_interaction = std::function<void(
|
|
||||||
Player*, glm::ivec3, const Block*, BlockInteraction type
|
|
||||||
)>;
|
|
||||||
|
|
||||||
class PlayerController {
|
class PlayerController {
|
||||||
Level* level;
|
Level* level;
|
||||||
std::shared_ptr<Player> player;
|
std::shared_ptr<Player> player;
|
||||||
@ -63,17 +52,10 @@ class PlayerController {
|
|||||||
CameraControl camControl;
|
CameraControl camControl;
|
||||||
BlocksController* blocksController;
|
BlocksController* blocksController;
|
||||||
|
|
||||||
std::vector<on_block_interaction> blockInteractionCallbacks;
|
|
||||||
|
|
||||||
void updateKeyboard();
|
void updateKeyboard();
|
||||||
void resetKeyboard();
|
void resetKeyboard();
|
||||||
void updatePlayer(float delta);
|
void updatePlayer(float delta);
|
||||||
void updateInteraction();
|
void updateInteraction();
|
||||||
void onBlockInteraction(
|
|
||||||
glm::ivec3 pos,
|
|
||||||
const Block* def,
|
|
||||||
BlockInteraction type
|
|
||||||
);
|
|
||||||
|
|
||||||
float stepsTimer = 0.0f;
|
float stepsTimer = 0.0f;
|
||||||
void onFootstep(const Hitbox& hitbox);
|
void onFootstep(const Hitbox& hitbox);
|
||||||
@ -90,8 +72,6 @@ public:
|
|||||||
void update(float delta, bool input, bool pause);
|
void update(float delta, bool input, bool pause);
|
||||||
void postUpdate(float delta, bool input, bool pause);
|
void postUpdate(float delta, bool input, bool pause);
|
||||||
Player* getPlayer();
|
Player* getPlayer();
|
||||||
|
|
||||||
void listenBlockInteraction(const on_block_interaction& callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* PLAYER_CONTROL_HPP_ */
|
#endif /* PLAYER_CONTROL_HPP_ */
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user