refactor: add GUI instance reference to UI nodes

This commit is contained in:
MihailRis
2025-04-02 14:55:53 +03:00
parent 74a94f869c
commit 4c48afbb90
70 changed files with 1133 additions and 832 deletions
+58 -60
View File
@@ -4,9 +4,10 @@
#include <algorithm>
#include <cmath>
#include "BlocksController.hpp"
#include "content/Content.hpp"
#include "core_defs.hpp"
#include "settings.hpp"
#include "engine/Engine.hpp"
#include "items/Inventory.hpp"
#include "items/ItemDef.hpp"
#include "items/ItemStack.hpp"
@@ -16,17 +17,15 @@
#include "objects/Players.hpp"
#include "physics/Hitbox.hpp"
#include "physics/PhysicsSolver.hpp"
#include "scripting/scripting.hpp"
#include "settings.hpp"
#include "voxels/Block.hpp"
#include "voxels/Chunks.hpp"
#include "voxels/voxel.hpp"
#include "window/Camera.hpp"
#include "window/Events.hpp"
#include "window/Window.hpp"
#include "window/input.hpp"
#include "world/Level.hpp"
#include "BlocksController.hpp"
#include "scripting/scripting.hpp"
const float INTERACTION_RELOAD = 0.160f;
const float STEPS_SPEED = 2.2f;
@@ -40,9 +39,7 @@ const float RUN_ZOOM = 1.1f;
const float C_ZOOM = 0.1f;
const float CROUCH_SHIFT_Y = -0.2f;
CameraControl::CameraControl(
Player& player, const CameraSettings& settings
)
CameraControl::CameraControl(Player& player, const CameraSettings& settings)
: player(player),
camera(player.fpCamera),
settings(settings),
@@ -70,7 +67,9 @@ void CameraControl::updateMouse(PlayerInput& input) {
(input.zoom ? settings.sensitivity.get() / 4.f
: settings.sensitivity.get());
auto d = glm::degrees(Events::delta / (float)Window::height * sensitivity);
auto d = glm::degrees(
input.delta / static_cast<float>(Window::height) * sensitivity
);
rotation.x -= d.x;
rotation.y -= d.y;
@@ -147,15 +146,16 @@ void CameraControl::updateFovEffects(
// more extensible but uglier
void CameraControl::switchCamera() {
const std::vector<std::shared_ptr<Camera>> playerCameras {
camera, player.tpCamera, player.spCamera
};
camera, player.tpCamera, player.spCamera};
auto index = std::distance(
playerCameras.begin(),
std::find_if(
playerCameras.begin(),
playerCameras.end(),
[this](auto& ptr) { return ptr.get() == player.currentCamera.get(); }
[this](auto& ptr) {
return ptr.get() == player.currentCamera.get();
}
)
);
if (static_cast<size_t>(index) != playerCameras.size()) {
@@ -205,8 +205,8 @@ void CameraControl::update(
tpCamera->front = camera->front;
tpCamera->right = camera->right;
}
if (player.currentCamera == spCamera ||
player.currentCamera == tpCamera || player.currentCamera == camera) {
if (player.currentCamera == spCamera || player.currentCamera == tpCamera ||
player.currentCamera == camera) {
player.currentCamera->setFov(glm::radians(settings.fov.get()));
}
}
@@ -239,10 +239,7 @@ void PlayerController::onFootstep(const Hitbox& hitbox) {
continue;
}
blocksController.onBlockInteraction(
&player,
glm::ivec3(x, y, z),
def,
BlockInteraction::step
&player, glm::ivec3(x, y, z), def, BlockInteraction::step
);
return;
}
@@ -265,9 +262,9 @@ void PlayerController::updateFootsteps(float delta) {
}
}
void PlayerController::update(float delta, bool input) {
if (input) {
updateKeyboard();
void PlayerController::update(float delta, const Input* inputEvents) {
if (inputEvents) {
updateKeyboard(*inputEvents);
player.updateSelectedEntity();
} else {
resetKeyboard();
@@ -275,7 +272,7 @@ void PlayerController::update(float delta, bool input) {
updatePlayer(delta);
}
void PlayerController::postUpdate(float delta, bool input, bool pause) {
void PlayerController::postUpdate(float delta, const Input* input, bool pause) {
if (!pause) {
updateFootsteps(delta);
}
@@ -287,23 +284,25 @@ void PlayerController::postUpdate(float delta, bool input, bool pause) {
player.postUpdate();
camControl.update(this->input, pause ? 0.0f : delta, *player.chunks);
if (input) {
updateInteraction(delta);
updateInteraction(*input, delta);
} else {
player.selection = {};
}
}
void PlayerController::updateKeyboard() {
input.moveForward = Events::active(BIND_MOVE_FORWARD);
input.moveBack = Events::active(BIND_MOVE_BACK);
input.moveLeft = Events::active(BIND_MOVE_LEFT);
input.moveRight = Events::active(BIND_MOVE_RIGHT);
input.sprint = Events::active(BIND_MOVE_SPRINT);
input.shift = Events::active(BIND_MOVE_CROUCH);
input.cheat = Events::active(BIND_MOVE_CHEAT);
input.jump = Events::active(BIND_MOVE_JUMP);
input.zoom = Events::active(BIND_CAM_ZOOM);
input.cameraMode = Events::jactive(BIND_CAM_MODE);
void PlayerController::updateKeyboard(const Input& inputEvents) {
const auto& bindings = inputEvents.getBindings();
input.moveForward = bindings.active(BIND_MOVE_FORWARD);
input.moveBack = bindings.active(BIND_MOVE_BACK);
input.moveLeft = bindings.active(BIND_MOVE_LEFT);
input.moveRight = bindings.active(BIND_MOVE_RIGHT);
input.sprint = bindings.active(BIND_MOVE_SPRINT);
input.shift = bindings.active(BIND_MOVE_CROUCH);
input.cheat = bindings.active(BIND_MOVE_CHEAT);
input.jump = bindings.active(BIND_MOVE_JUMP);
input.zoom = bindings.active(BIND_CAM_ZOOM);
input.cameraMode = bindings.jactive(BIND_CAM_MODE);
input.delta = inputEvents.getCursor().delta;
}
void PlayerController::resetKeyboard() {
@@ -316,6 +315,7 @@ void PlayerController::resetKeyboard() {
input.shift = false;
input.cheat = false;
input.jump = false;
input.delta = {};
}
void PlayerController::updatePlayer(float delta) {
@@ -328,18 +328,12 @@ static int determine_rotation(
if (def && def->rotatable) {
const std::string& name = def->rotations.name;
if (name == "pipe") {
if (norm.x < 0.0f)
return BLOCK_DIR_WEST;
if (norm.x > 0.0f)
return BLOCK_DIR_EAST;
if (norm.y > 0.0f)
return BLOCK_DIR_UP;
if (norm.y < 0.0f)
return BLOCK_DIR_DOWN;
if (norm.z > 0.0f)
return BLOCK_DIR_NORTH;
if (norm.z < 0.0f)
return BLOCK_DIR_SOUTH;
if (norm.x < 0.0f) return BLOCK_DIR_WEST;
if (norm.x > 0.0f) return BLOCK_DIR_EAST;
if (norm.y > 0.0f) return BLOCK_DIR_UP;
if (norm.y < 0.0f) return BLOCK_DIR_DOWN;
if (norm.z > 0.0f) return BLOCK_DIR_NORTH;
if (norm.z < 0.0f) return BLOCK_DIR_SOUTH;
} else if (name == "pane") {
if (abs(camDir.x) > abs(camDir.z)) {
if (camDir.x > 0.0f) return BLOCK_DIR_EAST;
@@ -417,7 +411,9 @@ voxel* PlayerController::updateSelection(float maxDistance) {
return vox;
}
void PlayerController::processRightClick(const Block& def, const Block& target) {
void PlayerController::processRightClick(
const Block& def, const Block& target
) {
const auto& selection = player.selection;
auto& chunks = *player.chunks;
auto camera = player.fpCamera.get();
@@ -427,8 +423,8 @@ void PlayerController::processRightClick(const Block& def, const Block& target)
if (!input.shift && target.rt.funcsset.oninteract) {
if (scripting::on_block_interact(
&player, target, selection.actualPosition
)) {
&player, target, selection.actualPosition
)) {
return;
}
}
@@ -443,7 +439,8 @@ void PlayerController::processRightClick(const Block& def, const Block& target)
if (def.obstacle) {
const auto& hitboxes = def.rt.hitboxes[state.rotation];
for (const AABB& blockAABB : hitboxes) {
if (level.entities->hasBlockingInside(blockAABB.translated(coord))) {
if (level.entities->hasBlockingInside(blockAABB.translated(coord)
)) {
return;
}
}
@@ -466,7 +463,7 @@ void PlayerController::processRightClick(const Block& def, const Block& target)
if (chosenBlock != vox->id && chosenBlock) {
if (!player.isInfiniteItems()) {
auto& slot = player.getInventory()->getSlot(player.getChosenSlot());
slot.setCount(slot.getCount()-1);
slot.setCount(slot.getCount() - 1);
}
blocksController.placeBlock(
&player, def, state, coord.x, coord.y, coord.z
@@ -490,21 +487,22 @@ void PlayerController::updateEntityInteraction(
}
}
void PlayerController::updateInteraction(float delta) {
void PlayerController::updateInteraction(const Input& inputEvents, float delta) {
auto indices = level.content.getIndices();
const auto& selection = player.selection;
if (interactionTimer > 0.0f) {
interactionTimer -= delta;
}
bool xkey = Events::active(BIND_PLAYER_FAST_INTERACTOIN);
const auto& bindings = inputEvents.getBindings();
bool xkey = bindings.active(BIND_PLAYER_FAST_INTERACTOIN);
float maxDistance = xkey ? 200.0f : 10.0f;
bool longInteraction = interactionTimer <= 0 || xkey;
bool lclick = Events::jactive(BIND_PLAYER_DESTROY) ||
(longInteraction && Events::active(BIND_PLAYER_DESTROY));
bool lattack = Events::jactive(BIND_PLAYER_ATTACK);
bool rclick = Events::jactive(BIND_PLAYER_BUILD) ||
(longInteraction && Events::active(BIND_PLAYER_BUILD));
bool lclick = bindings.jactive(BIND_PLAYER_DESTROY) ||
(longInteraction && bindings.active(BIND_PLAYER_DESTROY));
bool lattack = bindings.jactive(BIND_PLAYER_ATTACK);
bool rclick = bindings.jactive(BIND_PLAYER_BUILD) ||
(longInteraction && bindings.active(BIND_PLAYER_BUILD));
if (lclick || rclick) {
interactionTimer = INTERACTION_RELOAD;
}
@@ -527,8 +525,8 @@ void PlayerController::updateInteraction(float delta) {
auto iend = selection.position;
if (lclick && !input.shift && item.rt.funcsset.on_block_break_by) {
if (scripting::on_item_break_block(
&player, item, iend.x, iend.y, iend.z
)) {
&player, item, iend.x, iend.y, iend.z
)) {
return;
}
}