feat: support items data in base pack

This commit is contained in:
MihailRis 2025-02-18 01:03:34 +03:00
parent 5384843892
commit 3aa7d6ac28
6 changed files with 21 additions and 8 deletions

View File

@ -1,12 +1,13 @@
local util = {}
function util.drop(ppos, itemid, count, pickup_delay)
function util.drop(ppos, itemid, count, data, pickup_delay)
if itemid == 0 or not itemid then
return nil
end
return entities.spawn("base:drop", ppos, {base__drop={
id=itemid,
count=count,
data=data,
pickup_delay=pickup_delay
}})
end

View File

@ -14,6 +14,7 @@ end
if SAVED_DATA.item then
dropitem.id = item.index(SAVED_DATA.item)
dropitem.count = SAVED_DATA.count
dropitem.data = SAVED_DATA.data
end
local DROP_SCALE = 0.3
@ -25,6 +26,7 @@ local rotation = mat4.rotate({
function on_save()
SAVED_DATA.item = item.name(dropitem.id)
SAVED_DATA.count = dropitem.count
SAVED_DATA.data = dropitem.data
end
do -- setup visuals
@ -59,11 +61,10 @@ function on_sensor_enter(index, oid)
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 odrop = other:get_component("base:drop").dropitem
if odrop.id == dropitem.id and not odrop.data then
local stack = item.stack_size(dropitem.id)
local sum = dropitem.count + odrop.dropitem.count
local sum = dropitem.count + odrop.count
if sum <= stack then
dropitem.count = sum
other:despawn()
@ -75,7 +76,7 @@ function on_sensor_enter(index, oid)
if timer < 0.0 and index == 0 then
entity:despawn()
inventory.add(player.get_inventory(pid), dropitem.id, dropitem.count)
inventory.add(player.get_inventory(pid), dropitem.id, dropitem.count, dropitem.data)
audio.play_sound_2d("events/pickup", 0.5, 0.8 + math.random() * 0.4, "regular")
end
if index == 1 then

View File

@ -14,12 +14,13 @@ function on_hud_open()
if itemid == 0 then
return
end
local data = inventory.get_all_data(invid, slot)
inventory.set(invid, slot, itemid, itemcount-1)
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, 1, 1.5)
local drop = base_util.drop(ppos, itemid, 1, data, 1.5)
local velocity = vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL))
drop.rigidbody:set_vel(velocity)
end)

View File

@ -57,6 +57,9 @@ void ItemStack::setField(std::string_view name, dv::value value) {
}
if (value == nullptr) {
fields.erase(std::string(name));
if (fields.empty()) {
fields = nullptr;
}
return;
}
fields[std::string(name)] = std::move(value);

View File

@ -54,4 +54,8 @@ public:
const dv::value& getFields() const {
return fields;
}
bool hasFields() const {
return fields != nullptr;
}
};

View File

@ -208,7 +208,10 @@ static int l_get_all_data(lua::State* L, ItemStack& stack) {
}
static int l_has_data(lua::State* L, ItemStack& stack) {
auto key = lua::require_string(L, 3);
auto key = lua::tostring(L, 3);
if (key == nullptr) {
return lua::pushboolean(L, stack.hasFields());
}
return lua::pushboolean(L, stack.getField(key) != nullptr);
}