diff --git a/res/content/base/scripts/components/drop.lua b/res/content/base/scripts/components/drop.lua index 12e16265..7aa8babb 100644 --- a/res/content/base/scripts/components/drop.lua +++ b/res/content/base/scripts/components/drop.lua @@ -6,7 +6,8 @@ inair = true target = -1 timer = 0.3 -local dropitem = ARGS +local def_index = entity:def_index() +dropitem = ARGS if dropitem then timer = dropitem.pickup_delay or timer end @@ -38,7 +39,7 @@ end function on_grounded(force) local matrix = mat4.idt() - mat4.rotate(matrix, {0, 1, 0}, math.random()*360, matrix) + mat4.rotate(matrix, {0, 1, 0}, math.random() * 360, matrix) mat4.rotate(matrix, {1, 0, 0}, 90, matrix) mat4.scale(matrix, scale, matrix) rig:set_matrix(0, matrix) @@ -50,14 +51,34 @@ function on_fall() end function on_sensor_enter(index, oid) - local playerid = hud.get_player() - local playerentity = player.get_entity(playerid) - if timer < 0.0 and oid == playerentity and index == 0 then - entity:despawn() - inventory.add(player.get_inventory(playerid), dropitem.id, dropitem.count) - audio.play_sound_2d("events/pickup", 0.5, 0.8+math.random()*0.4, "regular") + local other = entities.get(oid) + if not other then + return end - if index == 1 and oid == playerentity then + local pid = other:get_player() + if pid == -1 then + -- other is base:drop too + if index == 0 and other:def_index() == def_index then + local odrop = other:get_component("base:drop") + if odrop.dropitem.id == dropitem.id then + -- // TODO: replace combination logic with item.* function + local stack = item.stack_size(dropitem.id) + local sum = dropitem.count + odrop.dropitem.count + if sum <= stack then + dropitem.count = sum + other:despawn() + end + end + end + return + end + + if timer < 0.0 and index == 0 then + entity:despawn() + inventory.add(player.get_inventory(pid), dropitem.id, dropitem.count) + audio.play_sound_2d("events/pickup", 0.5, 0.8 + math.random() * 0.4, "regular") + end + if index == 1 then target = oid end end @@ -84,15 +105,18 @@ end function on_update(tps) timer = timer - 1.0/tps - if target ~= -1 then - if timer > 0.0 then - return - end - local dir = vec3.sub(entities.get(target).transform:get_pos(), tsf:get_pos()) - vec3.normalize(dir, dir) - vec3.mul(dir, 10.0, dir) - body:set_vel(dir) + + if timer > 0.0 or target == -1 then + return end + local target_entity = entities.get(target) + if not target_entity then + return + end + local dir = vec3.sub(target_entity.transform:get_pos(), tsf:get_pos()) + vec3.normalize(dir, dir) + vec3.mul(dir, 10.0, dir) + body:set_vel(dir) end function on_attacked(attacker, pid) diff --git a/run.sh b/run.sh index 0bf5a60a..612f0693 100755 --- a/run.sh +++ b/run.sh @@ -1,6 +1,66 @@ -mkdir -p build -cd build -cmake -DCMAKE_BUILD_TYPE=Release .. -cmake --build . -j$(nproc) -cd .. -build/VoxelEngine +#!/bin/bash + + + +function delete { + echo "[RUN SCRIPT] Delete build directory" + rm -rf build +} + + +function build { + echo "[RUN SCRIPT] Build project" + mkdir -p build + cd build + cmake -DCMAKE_BUILD_TYPE=Release .. + cmake --build . -j$(nproc) + cd .. +} + + +function rebuild { + delete + build +} + + +run=true +function norun { + echo "[RUN SCRIPT] Build without run" + run= +} + + +function help { + echo "[RUN SCRIPT] Usage: ./run [ARGUMENT]..." + echo "[RUN SCRIPT] Arguments:" + echo "[RUN SCRIPT] -d, --delete Delete build directory" + echo "[RUN SCRIPT] -b, --build Build project" + echo "[RUN SCRIPT] -r, --rebuild Rebuild project" + echo "[RUN SCRIPT] -R, --norun Build without run" + echo "[RUN SCRIPT] -h, --help Print this page" +} + + +while [ -n "$1" ]; do + case "$1" in + -d | --delete) delete ;; + -b | --build) build ;; + -r | --rebuild) rebuild ;; + -R | --norun) norun ;; + -h | --help) help + norun + break ;; + *) echo "[RUN SCRIPT] Unknown argument: $1" + help + norun + break ;; + esac + shift +done + + +if [[ $run ]]; then + echo "[RUN SCRIPT] Run project" + ./build/VoxelEngine +fi \ No newline at end of file diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index b60d8535..3095ed1e 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -592,7 +592,10 @@ void Hud::draw(const DrawContext& ctx){ const uint height = viewport.getHeight(); auto menu = gui.getMenu(); - darkOverlay->setVisible(menu->hasOpenPage()); + bool is_menu_open = menu->hasOpenPage(); + darkOverlay->setVisible(is_menu_open); + menu->setVisible(is_menu_open); + updateElementsPosition(viewport); uicamera->setFov(height); @@ -693,7 +696,6 @@ void Hud::setPause(bool pause) { if (pause && !menu->hasOpenPage()) { menu->setPage("pause"); } - menu->setVisible(pause); } Player* Hud::getPlayer() const { diff --git a/src/graphics/ui/elements/TextBox.cpp b/src/graphics/ui/elements/TextBox.cpp index 12f262f1..1e36c601 100644 --- a/src/graphics/ui/elements/TextBox.cpp +++ b/src/graphics/ui/elements/TextBox.cpp @@ -826,7 +826,7 @@ void TextBox::setCaret(size_t position) { scrolled(-glm::ceil(offset/static_cast(scrollStep)+0.5f)); } uint lcaret = caret - label->getTextLineOffset(line); - int realoffset = font->calcWidth(input, lcaret)-int(textOffset) - padding.x; + int realoffset = font->calcWidth(input, lcaret)-int(textOffset) + 2; if (realoffset-width > 0) { setTextOffset(textOffset + realoffset-width); } else if (realoffset < 0) { diff --git a/src/logic/scripting/lua/libs/libhud.cpp b/src/logic/scripting/lua/libs/libhud.cpp index 1c4245ba..1bdcee12 100644 --- a/src/logic/scripting/lua/libs/libhud.cpp +++ b/src/logic/scripting/lua/libs/libhud.cpp @@ -1,9 +1,9 @@ +#include "libhud.hpp" + #include -#include #include "assets/Assets.hpp" #include "content/Content.hpp" -#include "engine/Engine.hpp" #include "frontend/UiDocument.hpp" #include "frontend/hud.hpp" #include "graphics/ui/elements/InventoryView.hpp" @@ -16,11 +16,7 @@ #include "voxels/voxel.hpp" #include "voxels/blocks_agent.hpp" #include "world/Level.hpp" -#include "api_lua.hpp" -namespace scripting { - extern Hud* hud; -} using namespace scripting; static int l_open_inventory(lua::State*) { @@ -176,22 +172,22 @@ static int l_set_allow_pause(lua::State* L) { } const luaL_Reg hudlib[] = { - {"open_inventory", lua::wrap}, - {"close_inventory", lua::wrap}, - {"open", lua::wrap}, - {"open_block", lua::wrap}, - {"open_permanent", lua::wrap}, - {"show_overlay", lua::wrap}, - {"get_block_inventory", lua::wrap}, - {"close", lua::wrap}, - {"pause", lua::wrap}, - {"resume", lua::wrap}, - {"is_paused", lua::wrap}, - {"is_inventory_open", lua::wrap}, - {"get_player", lua::wrap}, - {"_is_content_access", lua::wrap}, - {"_set_content_access", lua::wrap}, - {"_set_debug_cheats", lua::wrap}, - {"set_allow_pause", lua::wrap}, + {"open_inventory", wrap_hud}, + {"close_inventory", wrap_hud}, + {"open", wrap_hud}, + {"open_block", wrap_hud}, + {"open_permanent", wrap_hud}, + {"show_overlay", wrap_hud}, + {"get_block_inventory", wrap_hud}, + {"close", wrap_hud}, + {"pause", wrap_hud}, + {"resume", wrap_hud}, + {"is_paused", wrap_hud}, + {"is_inventory_open", wrap_hud}, + {"get_player", wrap_hud}, + {"_is_content_access", wrap_hud}, + {"_set_content_access", wrap_hud}, + {"_set_debug_cheats", wrap_hud}, + {"set_allow_pause", wrap_hud}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/libs/libhud.hpp b/src/logic/scripting/lua/libs/libhud.hpp new file mode 100644 index 00000000..74833e4b --- /dev/null +++ b/src/logic/scripting/lua/libs/libhud.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include "api_lua.hpp" + +#include "logic/scripting/scripting_hud.hpp" +#include "graphics/render/WorldRenderer.hpp" +#include "engine/Engine.hpp" + +template +inline int wrap_hud(lua_State* L) { + if (scripting::hud == nullptr) { + return luaL_error( + L, "renderer is not initialized yet, see hud.lua on_hud_open event" + ); + } + return lua::wrap(L); +} diff --git a/src/logic/scripting/lua/libs/libparticles.cpp b/src/logic/scripting/lua/libs/libparticles.cpp index d3bccf76..0c468007 100644 --- a/src/logic/scripting/lua/libs/libparticles.cpp +++ b/src/logic/scripting/lua/libs/libparticles.cpp @@ -1,11 +1,8 @@ -#include "api_lua.hpp" +#include "libhud.hpp" -#include "logic/scripting/scripting_hud.hpp" -#include "graphics/render/WorldRenderer.hpp" #include "graphics/render/ParticlesRenderer.hpp" #include "graphics/render/Emitter.hpp" #include "assets/assets_util.hpp" -#include "engine/Engine.hpp" using namespace scripting; @@ -81,10 +78,10 @@ static int l_is_alive(lua::State* L) { } const luaL_Reg particleslib[] = { - {"emit", lua::wrap}, - {"stop", lua::wrap}, - {"is_alive", lua::wrap}, - {"get_origin", lua::wrap}, - {"set_origin", lua::wrap}, + {"emit", wrap_hud}, + {"stop", wrap_hud}, + {"is_alive", wrap_hud}, + {"get_origin", wrap_hud}, + {"set_origin", wrap_hud}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/libs/libtext3d.cpp b/src/logic/scripting/lua/libs/libtext3d.cpp index f334c85f..4529fc0c 100644 --- a/src/logic/scripting/lua/libs/libtext3d.cpp +++ b/src/logic/scripting/lua/libs/libtext3d.cpp @@ -1,10 +1,7 @@ -#include "api_lua.hpp" +#include "libhud.hpp" -#include "logic/scripting/scripting_hud.hpp" -#include "graphics/render/WorldRenderer.hpp" #include "graphics/render/TextsRenderer.hpp" #include "graphics/render/TextNote.hpp" -#include "engine/Engine.hpp" using namespace scripting; @@ -99,17 +96,17 @@ static int l_set_rotation(lua::State* L) { } const luaL_Reg text3dlib[] = { - {"show", lua::wrap}, - {"hide", lua::wrap}, - {"get_text", lua::wrap}, - {"set_text", lua::wrap}, - {"get_pos", lua::wrap}, - {"set_pos", lua::wrap}, - {"get_axis_x", lua::wrap}, - {"set_axis_x", lua::wrap}, - {"get_axis_y", lua::wrap}, - {"set_axis_y", lua::wrap}, - {"set_rotation", lua::wrap}, - {"update_settings", lua::wrap}, + {"show", wrap_hud}, + {"hide", wrap_hud}, + {"get_text", wrap_hud}, + {"set_text", wrap_hud}, + {"get_pos", wrap_hud}, + {"set_pos", wrap_hud}, + {"get_axis_x", wrap_hud}, + {"set_axis_x", wrap_hud}, + {"get_axis_y", wrap_hud}, + {"set_axis_y", wrap_hud}, + {"set_rotation", wrap_hud}, + {"update_settings", wrap_hud}, {NULL, NULL} }; diff --git a/src/logic/scripting/scripting_hud.cpp b/src/logic/scripting/scripting_hud.cpp index d6f618cb..478b4f9e 100644 --- a/src/logic/scripting/scripting_hud.cpp +++ b/src/logic/scripting/scripting_hud.cpp @@ -65,15 +65,22 @@ void scripting::on_frontend_render() { } void scripting::on_frontend_close() { + auto L = lua::get_main_state(); for (auto& pack : engine->getAllContentPacks()) { lua::emit_event( - lua::get_main_state(), + L, pack.id + ":.hudclose", [&](lua::State* L) { return lua::pushinteger(L, hud->getPlayer()->getId()); } ); } + lua::pushnil(L); + lua::setglobal(L, "hud"); + lua::pushnil(L); + lua::setglobal(L, "gfx"); + + scripting::renderer = nullptr; scripting::hud = nullptr; }