mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-13 23:11:49 +00:00
switch to inventory drop area
This commit is contained in:
parent
2a4b8b0297
commit
7c97966377
5 changed files with 75 additions and 41 deletions
|
|
@ -1,5 +1,44 @@
|
||||||
local util = {}
|
local util = {}
|
||||||
|
|
||||||
|
local DROP_FORCE = 8
|
||||||
|
local DROP_INIT_VEL = {0, 3, 0}
|
||||||
|
|
||||||
|
---@param mode integer 0 - whole stack, 1 - single item, 2 - dupe whole stack
|
||||||
|
function util.drop_from_slot(invid, slot, mode)
|
||||||
|
if invid == 0 then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local pid = hud.get_player()
|
||||||
|
if mode == 2 and !player.is_infinite_items(pid) then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local itemid, itemcount = inventory.get(invid, slot)
|
||||||
|
if itemid == 0 then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local drop_itemcount = 1
|
||||||
|
if mode == 0 then
|
||||||
|
drop_itemcount = itemcount
|
||||||
|
end
|
||||||
|
if mode == 2 then
|
||||||
|
drop_itemcount = item.stack_size(itemid)
|
||||||
|
else
|
||||||
|
inventory.set(invid, slot, itemid, itemcount - drop_itemcount)
|
||||||
|
end
|
||||||
|
|
||||||
|
local data = inventory.get_all_data(invid, slot)
|
||||||
|
local pvel = { player.get_vel(pid) }
|
||||||
|
local ppos = vec3.add({ player.get_pos(pid) }, { 0, 0.7, 0 })
|
||||||
|
local throw_force = vec3.mul(player.get_dir(pid), DROP_FORCE)
|
||||||
|
local drop = util.drop(ppos, itemid, drop_itemcount, data, 1.5)
|
||||||
|
if not drop then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local velocity = vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL))
|
||||||
|
drop.rigidbody:set_vel(velocity)
|
||||||
|
end
|
||||||
|
|
||||||
function util.drop(ppos, itemid, count, data, pickup_delay)
|
function util.drop(ppos, itemid, count, data, pickup_delay)
|
||||||
if itemid == 0 or not itemid then
|
if itemid == 0 or not itemid then
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -1,46 +1,10 @@
|
||||||
local base_util = require "util"
|
local base_util = require "util"
|
||||||
|
|
||||||
local DROP_FORCE = 8
|
|
||||||
local DROP_INIT_VEL = {0, 3, 0}
|
|
||||||
|
|
||||||
---@param mode integer 0 - whole stack, 1 - single item, 2 - dupe whole stack
|
|
||||||
function drop_from_slot(invid, slot, mode)
|
|
||||||
local pid = hud.get_player()
|
|
||||||
if mode == 2 and !player.is_infinite_items(pid) then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local itemid, itemcount = inventory.get(invid, slot)
|
|
||||||
if itemid == 0 then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local drop_itemcount = 1
|
|
||||||
if mode == 0 then
|
|
||||||
drop_itemcount = itemcount
|
|
||||||
end
|
|
||||||
if mode == 2 then
|
|
||||||
drop_itemcount = item.stack_size(itemid)
|
|
||||||
else
|
|
||||||
inventory.set(invid, slot, itemid, itemcount - drop_itemcount)
|
|
||||||
end
|
|
||||||
|
|
||||||
local data = inventory.get_all_data(invid, slot)
|
|
||||||
local pvel = { player.get_vel(pid) }
|
|
||||||
local ppos = vec3.add({ player.get_pos(pid) }, { 0, 0.7, 0 })
|
|
||||||
local throw_force = vec3.mul(player.get_dir(pid), DROP_FORCE)
|
|
||||||
local drop = base_util.drop(ppos, itemid, drop_itemcount, data, 1.5)
|
|
||||||
if not drop then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local velocity = vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL))
|
|
||||||
drop.rigidbody:set_vel(velocity)
|
|
||||||
end
|
|
||||||
|
|
||||||
function on_inventory_clicked_outside(exc_invid, exc_slot, mode)
|
|
||||||
drop_from_slot(exc_invid, exc_slot, mode)
|
|
||||||
end
|
|
||||||
|
|
||||||
function on_hud_open()
|
function on_hud_open()
|
||||||
|
events.on("core:drop_outside_inventory", function(mode)
|
||||||
|
local invid = hud.get_exchange_inventory()
|
||||||
|
base_util.drop_from_slot(invid, 0, mode)
|
||||||
|
end)
|
||||||
input.add_callback("player.drop", function()
|
input.add_callback("player.drop", function()
|
||||||
if hud.is_paused() or hud.is_inventory_open() then
|
if hud.is_paused() or hud.is_inventory_open() then
|
||||||
return
|
return
|
||||||
|
|
@ -51,7 +15,7 @@ function on_hud_open()
|
||||||
if input.is_pressed("key:left-ctrl") or input.is_pressed("key:right-ctrl") then
|
if input.is_pressed("key:left-ctrl") or input.is_pressed("key:right-ctrl") then
|
||||||
mode = 0
|
mode = 0
|
||||||
end
|
end
|
||||||
drop_from_slot(invid, slot, mode)
|
base_util.drop_from_slot(invid, slot, mode)
|
||||||
end)
|
end)
|
||||||
rules.create("do-loot-non-player", true)
|
rules.create("do-loot-non-player", true)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
#include "hud.hpp"
|
#include "hud.hpp"
|
||||||
|
|
||||||
#include "ContentGfxCache.hpp"
|
#include "ContentGfxCache.hpp"
|
||||||
|
|
@ -175,6 +177,14 @@ Hud::Hud(Engine& engine, LevelFrontend& frontend, Player& player)
|
||||||
"<container size='4000' color='#00000080' z-index='-1' visible='false'/>"
|
"<container size='4000' color='#00000080' z-index='-1' visible='false'/>"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
inventoryDropArea = guiutil::create(
|
||||||
|
gui,
|
||||||
|
R"(<container size='4000' color='#00000000' z-index='-1' visible='false'
|
||||||
|
onclick='events.emit(\"core:drop_outside_inventory\", 0)'
|
||||||
|
onrightclick='events.emit(\"core:drop_outside_inventory\", 1)'
|
||||||
|
/>)"
|
||||||
|
);
|
||||||
|
|
||||||
uicamera = std::make_unique<Camera>(glm::vec3(), 1);
|
uicamera = std::make_unique<Camera>(glm::vec3(), 1);
|
||||||
uicamera->perspective = false;
|
uicamera->perspective = false;
|
||||||
uicamera->flipped = true;
|
uicamera->flipped = true;
|
||||||
|
|
@ -188,6 +198,7 @@ Hud::Hud(Engine& engine, LevelFrontend& frontend, Player& player)
|
||||||
|
|
||||||
gui.add(debugPanel);
|
gui.add(debugPanel);
|
||||||
gui.add(darkOverlay);
|
gui.add(darkOverlay);
|
||||||
|
gui.add(inventoryDropArea);
|
||||||
gui.add(hotbarView);
|
gui.add(hotbarView);
|
||||||
gui.add(contentAccessPanel);
|
gui.add(contentAccessPanel);
|
||||||
|
|
||||||
|
|
@ -213,6 +224,7 @@ Hud::~Hud() {
|
||||||
}
|
}
|
||||||
gui.remove(hotbarView);
|
gui.remove(hotbarView);
|
||||||
gui.remove(darkOverlay);
|
gui.remove(darkOverlay);
|
||||||
|
gui.remove(inventoryDropArea);
|
||||||
gui.remove(contentAccessPanel);
|
gui.remove(contentAccessPanel);
|
||||||
gui.remove(debugPanel);
|
gui.remove(debugPanel);
|
||||||
}
|
}
|
||||||
|
|
@ -358,6 +370,7 @@ void Hud::update(bool visible) {
|
||||||
contentAccess->setMinSize(glm::vec2(1, windowSize.y));
|
contentAccess->setMinSize(glm::vec2(1, windowSize.y));
|
||||||
hotbarView->setVisible(visible && !(secondUI && !inventoryView));
|
hotbarView->setVisible(visible && !(secondUI && !inventoryView));
|
||||||
darkOverlay->setVisible(isMenuOpen);
|
darkOverlay->setVisible(isMenuOpen);
|
||||||
|
inventoryDropArea->setVisible(inventoryOpen);
|
||||||
menu.setVisible(isMenuOpen);
|
menu.setVisible(isMenuOpen);
|
||||||
|
|
||||||
if (visible) {
|
if (visible) {
|
||||||
|
|
@ -739,6 +752,10 @@ std::shared_ptr<Inventory> Hud::getSecondInventory() {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<Inventory> Hud::getExchangeInventory() {
|
||||||
|
return exchangeSlotInv;
|
||||||
|
}
|
||||||
|
|
||||||
bool Hud::isContentAccess() const {
|
bool Hud::isContentAccess() const {
|
||||||
return showContentPanel;
|
return showContentPanel;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -93,6 +93,8 @@ class Hud : public util::ObjectsKeeper {
|
||||||
std::shared_ptr<gui::UINode> debugPanel;
|
std::shared_ptr<gui::UINode> debugPanel;
|
||||||
/// @brief Overlay used in pause mode
|
/// @brief Overlay used in pause mode
|
||||||
std::shared_ptr<gui::UINode> darkOverlay;
|
std::shared_ptr<gui::UINode> darkOverlay;
|
||||||
|
/// @brief Overlay used in inventory
|
||||||
|
std::shared_ptr<gui::UINode> inventoryDropArea;
|
||||||
/// @brief Inventories interaction agent (grabbed item)
|
/// @brief Inventories interaction agent (grabbed item)
|
||||||
std::shared_ptr<gui::SlotView> exchangeSlot;
|
std::shared_ptr<gui::SlotView> exchangeSlot;
|
||||||
/// @brief Exchange slot inventory (1 slot only)
|
/// @brief Exchange slot inventory (1 slot only)
|
||||||
|
|
@ -207,6 +209,8 @@ public:
|
||||||
|
|
||||||
std::shared_ptr<Inventory> getSecondInventory();
|
std::shared_ptr<Inventory> getSecondInventory();
|
||||||
|
|
||||||
|
std::shared_ptr<Inventory> getExchangeInventory();
|
||||||
|
|
||||||
bool isContentAccess() const;
|
bool isContentAccess() const;
|
||||||
|
|
||||||
void setContentAccess(bool flag);
|
void setContentAccess(bool flag);
|
||||||
|
|
|
||||||
|
|
@ -149,6 +149,15 @@ static int l_get_second_inventory(lua::State* L) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int l_get_exchange_inventory(lua::State* L) {
|
||||||
|
auto inventory = hud->getExchangeInventory();
|
||||||
|
if (inventory == nullptr) {
|
||||||
|
return lua::pushinteger(L, 0);
|
||||||
|
} else {
|
||||||
|
return lua::pushinteger(L, inventory->getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static int l_get_player(lua::State* L) {
|
static int l_get_player(lua::State* L) {
|
||||||
auto player = hud->getPlayer();
|
auto player = hud->getPlayer();
|
||||||
return lua::pushinteger(L, player->getId());
|
return lua::pushinteger(L, player->getId());
|
||||||
|
|
@ -216,6 +225,7 @@ const luaL_Reg hudlib[] = {
|
||||||
{"show_overlay", wrap_hud<l_show_overlay>},
|
{"show_overlay", wrap_hud<l_show_overlay>},
|
||||||
{"get_block_inventory", wrap_hud<l_get_block_inventory>},
|
{"get_block_inventory", wrap_hud<l_get_block_inventory>},
|
||||||
{"get_second_inventory", wrap_hud<l_get_second_inventory>},
|
{"get_second_inventory", wrap_hud<l_get_second_inventory>},
|
||||||
|
{"get_exchange_inventory", wrap_hud<l_get_exchange_inventory>},
|
||||||
{"close", wrap_hud<l_close>},
|
{"close", wrap_hud<l_close>},
|
||||||
{"pause", wrap_hud<l_pause>},
|
{"pause", wrap_hud<l_pause>},
|
||||||
{"resume", wrap_hud<l_resume>},
|
{"resume", wrap_hud<l_resume>},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue