player.get_selected_block

This commit is contained in:
MihailRis 2024-05-31 08:33:37 +03:00
parent 29ca6e969c
commit 74042b5c2a
13 changed files with 32 additions and 18 deletions

View File

@ -2,8 +2,6 @@
#define CODERS_TOML_HPP_ #define CODERS_TOML_HPP_
#include "../data/dynamic.hpp" #include "../data/dynamic.hpp"
#include "commons.hpp"
#include <string> #include <string>
class SettingsHandler; class SettingsHandler;

View File

@ -25,11 +25,11 @@ public:
enum class DependencyLevel { enum class DependencyLevel {
required, // dependency must be installed required, // dependency must be installed
optional, // dependency will be installed if found optional, // dependency will be installed if found
weak, // dependency will not be installed automatically weak, // only affects packs order
}; };
/// @brief Content-pack that should be installed before the dependent /// @brief Content-pack that should be installed earlier the dependent
struct DependencyPack { struct DependencyPack {
DependencyLevel level; DependencyLevel level;
std::string id; std::string id;

View File

@ -44,7 +44,7 @@ void Logger::log(LogLevel level, const std::string& name, std::string message) {
auto ms = duration_cast<milliseconds>(system_clock::now().time_since_epoch()) % 1000; auto ms = duration_cast<milliseconds>(system_clock::now().time_since_epoch()) % 1000;
ss << " " << std::put_time(std::localtime(&tm), "%Y/%m/%d %T"); ss << " " << std::put_time(std::localtime(&tm), "%Y/%m/%d %T");
ss << '.' << std::setfill('0') << std::setw(3) << ms.count(); ss << '.' << std::setfill('0') << std::setw(3) << ms.count();
ss << utcOffset << " (" << std::setfill(' ') << std::setw(moduleLen) << name << ") "; ss << utcOffset << " [" << std::setfill(' ') << std::setw(moduleLen) << name << "] ";
ss << message; ss << message;
{ {
std::lock_guard<std::mutex> lock(mutex); std::lock_guard<std::mutex> lock(mutex);

View File

@ -20,7 +20,7 @@ namespace files {
std::ifstream file; std::ifstream file;
size_t filelength; size_t filelength;
public: public:
rafile(std::filesystem::path filename); rafile(fs::path filename);
void seekg(std::streampos pos); void seekg(std::streampos pos);
void read(char* buffer, std::streamsize size); void read(char* buffer, std::streamsize size);

View File

@ -79,8 +79,12 @@ std::shared_ptr<UINode> create_debug_panel(
if (def) { if (def) {
stream << L" (" << util::str2wstr_utf8(def->name) << L")"; stream << L" (" << util::str2wstr_utf8(def->name) << L")";
} }
return L"block: "+std::to_wstring(player->selectedVoxel.id)+ if (player->selectedVoxel.id == BLOCK_VOID) {
L" "+stream.str(); return std::wstring {L"block: none"};
} else {
return L"block: "+std::to_wstring(player->selectedVoxel.id)+
L" "+stream.str();
}
})); }));
panel->add(create_label([=](){ panel->add(create_label([=](){
return L"seed: "+std::to_wstring(level->getWorld()->getSeed()); return L"seed: "+std::to_wstring(level->getWorld()->getSeed());

View File

@ -4,7 +4,6 @@
#include "../../typedefs.hpp" #include "../../typedefs.hpp"
#include "ImageData.hpp" #include "ImageData.hpp"
#include <string>
#include <memory> #include <memory>
class Texture { class Texture {

View File

@ -194,7 +194,7 @@ void WorldRenderer::renderBlockSelection(Camera* camera, Shader* linesShader) {
auto indices = level->content->getIndices(); auto indices = level->content->getIndices();
blockid_t id = PlayerController::selectedBlockId; blockid_t id = PlayerController::selectedBlockId;
auto block = indices->getBlockDef(id); auto block = indices->getBlockDef(id);
const glm::vec3 pos = PlayerController::selectedBlockPosition; const glm::ivec3 pos = player->selectedBlockPosition;
const glm::vec3 point = PlayerController::selectedPointPosition; const glm::vec3 point = PlayerController::selectedPointPosition;
const glm::vec3 norm = PlayerController::selectedBlockNormal; const glm::vec3 norm = PlayerController::selectedBlockNormal;
@ -206,7 +206,7 @@ void WorldRenderer::renderBlockSelection(Camera* camera, Shader* linesShader) {
linesShader->uniformMatrix("u_projview", camera->getProjView()); linesShader->uniformMatrix("u_projview", camera->getProjView());
lineBatch->lineWidth(2.0f); lineBatch->lineWidth(2.0f);
for (auto& hitbox: hitboxes) { for (auto& hitbox: hitboxes) {
const glm::vec3 center = pos + hitbox.center(); const glm::vec3 center = glm::vec3(pos) + hitbox.center();
const glm::vec3 size = hitbox.size(); const glm::vec3 size = hitbox.size();
lineBatch->box(center, size + glm::vec3(0.02), glm::vec4(0.f, 0.f, 0.f, 0.5f)); lineBatch->box(center, size + glm::vec3(0.02), glm::vec4(0.f, 0.f, 0.f, 0.5f));
if (player->debug) { if (player->debug) {

View File

@ -166,7 +166,6 @@ void CameraControl::update(const PlayerInput& input, float delta, Chunks* chunks
} }
} }
glm::vec3 PlayerController::selectedBlockPosition;
glm::vec3 PlayerController::selectedPointPosition; glm::vec3 PlayerController::selectedPointPosition;
glm::ivec3 PlayerController::selectedBlockNormal; glm::ivec3 PlayerController::selectedBlockNormal;
int PlayerController::selectedBlockId = -1; int PlayerController::selectedBlockId = -1;
@ -362,11 +361,11 @@ void PlayerController::updateInteraction(){
maxDistance, maxDistance,
end, norm, iend end, norm, iend
); );
if (vox != nullptr){ if (vox != nullptr) {
player->selectedVoxel = *vox; player->selectedVoxel = *vox;
selectedBlockId = vox->id; selectedBlockId = vox->id;
selectedBlockRotation = vox->rotation(); selectedBlockRotation = vox->rotation();
selectedBlockPosition = iend; player->selectedBlockPosition = iend;
selectedPointPosition = end; selectedPointPosition = end;
selectedBlockNormal = norm; selectedBlockNormal = norm;
int x = iend.x; int x = iend.x;
@ -443,6 +442,7 @@ void PlayerController::updateInteraction(){
} else { } else {
selectedBlockId = -1; selectedBlockId = -1;
selectedBlockRotation = 0; selectedBlockRotation = 0;
player->selectedVoxel.id = BLOCK_VOID;
} }
if (rclick) { if (rclick) {
if (item->rt.funcsset.on_use) { if (item->rt.funcsset.on_use) {

View File

@ -77,7 +77,6 @@ class PlayerController {
void onFootstep(); void onFootstep();
void updateFootsteps(float delta); void updateFootsteps(float delta);
public: public:
static glm::vec3 selectedBlockPosition;
static glm::ivec3 selectedBlockNormal; static glm::ivec3 selectedBlockNormal;
static glm::vec3 selectedPointPosition; static glm::vec3 selectedPointPosition;
static int selectedBlockId; static int selectedBlockId;

View File

@ -132,6 +132,20 @@ static int l_player_set_noclip(lua_State* L) {
return 0; return 0;
} }
static int l_player_get_selected_block(lua_State* L) {
if (auto player = get_player(L, 1)) {
if (player->selectedVoxel.id == BLOCK_VOID) {
return 0;
}
const glm::ivec3 pos = player->selectedBlockPosition;
lua_pushinteger(L, pos.x);
lua_pushinteger(L, pos.y);
lua_pushinteger(L, pos.z);
return 3;
}
return 0;
}
const luaL_Reg playerlib [] = { const luaL_Reg playerlib [] = {
{"get_pos", lua_wrap_errors<l_player_get_pos>}, {"get_pos", lua_wrap_errors<l_player_get_pos>},
{"set_pos", lua_wrap_errors<l_player_set_pos>}, {"set_pos", lua_wrap_errors<l_player_set_pos>},
@ -144,5 +158,6 @@ const luaL_Reg playerlib [] = {
{"set_flight", lua_wrap_errors<l_player_set_flight>}, {"set_flight", lua_wrap_errors<l_player_set_flight>},
{"is_noclip", lua_wrap_errors<l_player_is_noclip>}, {"is_noclip", lua_wrap_errors<l_player_is_noclip>},
{"set_noclip", lua_wrap_errors<l_player_set_noclip>}, {"set_noclip", lua_wrap_errors<l_player_set_noclip>},
{"get_selected_block", lua_wrap_errors<l_player_get_selected_block>},
{NULL, NULL} {NULL, NULL}
}; };

View File

@ -46,6 +46,7 @@ public:
bool debug = false; bool debug = false;
voxel selectedVoxel {0, 0}; voxel selectedVoxel {0, 0};
glm::vec3 cam {}; glm::vec3 cam {};
glm::ivec3 selectedBlockPosition {};
Player(glm::vec3 position, float speed, std::shared_ptr<Inventory> inv); Player(glm::vec3 position, float speed, std::shared_ptr<Inventory> inv);
~Player(); ~Player();

View File

@ -57,7 +57,7 @@ struct GraphicsSettings {
/// 1.0 is linear, 2.0 is quadratic /// 1.0 is linear, 2.0 is quadratic
NumberSetting fogCurve {1.6f, 1.0f, 6.0f}; NumberSetting fogCurve {1.6f, 1.0f, 6.0f};
/// @brief Lighting gamma /// @brief Lighting gamma
NumberSetting gamma {1.0f, 0.5f, 2.0f}; NumberSetting gamma {1.0f, 0.4f, 1.0f};
/// @brief Enable blocks backlight to prevent complete darkness /// @brief Enable blocks backlight to prevent complete darkness
FlagSetting backlight {true}; FlagSetting backlight {true};
/// @brief Enable chunks frustum culling /// @brief Enable chunks frustum culling

View File

@ -12,7 +12,6 @@
#include "../voxels/Chunk.hpp" #include "../voxels/Chunk.hpp"
#include "../voxels/Chunks.hpp" #include "../voxels/Chunks.hpp"
#include "../voxels/ChunksStorage.hpp" #include "../voxels/ChunksStorage.hpp"
#include "../window/Camera.hpp"
#include "../world/WorldGenerators.hpp" #include "../world/WorldGenerators.hpp"
#include <memory> #include <memory>
@ -135,7 +134,6 @@ std::unique_ptr<Level> World::load(
} }
} }
} }
(void)world.release();
return level; return level;
} }