feat: support items data in base pack
This commit is contained in:
parent
5384843892
commit
3aa7d6ac28
@ -1,12 +1,13 @@
|
|||||||
local util = {}
|
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
|
if itemid == 0 or not itemid then
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
return entities.spawn("base:drop", ppos, {base__drop={
|
return entities.spawn("base:drop", ppos, {base__drop={
|
||||||
id=itemid,
|
id=itemid,
|
||||||
count=count,
|
count=count,
|
||||||
|
data=data,
|
||||||
pickup_delay=pickup_delay
|
pickup_delay=pickup_delay
|
||||||
}})
|
}})
|
||||||
end
|
end
|
||||||
|
|||||||
@ -14,6 +14,7 @@ end
|
|||||||
if SAVED_DATA.item then
|
if SAVED_DATA.item then
|
||||||
dropitem.id = item.index(SAVED_DATA.item)
|
dropitem.id = item.index(SAVED_DATA.item)
|
||||||
dropitem.count = SAVED_DATA.count
|
dropitem.count = SAVED_DATA.count
|
||||||
|
dropitem.data = SAVED_DATA.data
|
||||||
end
|
end
|
||||||
|
|
||||||
local DROP_SCALE = 0.3
|
local DROP_SCALE = 0.3
|
||||||
@ -25,6 +26,7 @@ local rotation = mat4.rotate({
|
|||||||
function on_save()
|
function on_save()
|
||||||
SAVED_DATA.item = item.name(dropitem.id)
|
SAVED_DATA.item = item.name(dropitem.id)
|
||||||
SAVED_DATA.count = dropitem.count
|
SAVED_DATA.count = dropitem.count
|
||||||
|
SAVED_DATA.data = dropitem.data
|
||||||
end
|
end
|
||||||
|
|
||||||
do -- setup visuals
|
do -- setup visuals
|
||||||
@ -59,11 +61,10 @@ function on_sensor_enter(index, oid)
|
|||||||
if pid == -1 then
|
if pid == -1 then
|
||||||
-- other is base:drop too
|
-- other is base:drop too
|
||||||
if index == 0 and other:def_index() == def_index then
|
if index == 0 and other:def_index() == def_index then
|
||||||
local odrop = other:get_component("base:drop")
|
local odrop = other:get_component("base:drop").dropitem
|
||||||
if odrop.dropitem.id == dropitem.id then
|
if odrop.id == dropitem.id and not odrop.data then
|
||||||
-- // TODO: replace combination logic with item.* function
|
|
||||||
local stack = item.stack_size(dropitem.id)
|
local stack = item.stack_size(dropitem.id)
|
||||||
local sum = dropitem.count + odrop.dropitem.count
|
local sum = dropitem.count + odrop.count
|
||||||
if sum <= stack then
|
if sum <= stack then
|
||||||
dropitem.count = sum
|
dropitem.count = sum
|
||||||
other:despawn()
|
other:despawn()
|
||||||
@ -75,7 +76,7 @@ function on_sensor_enter(index, oid)
|
|||||||
|
|
||||||
if timer < 0.0 and index == 0 then
|
if timer < 0.0 and index == 0 then
|
||||||
entity:despawn()
|
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")
|
audio.play_sound_2d("events/pickup", 0.5, 0.8 + math.random() * 0.4, "regular")
|
||||||
end
|
end
|
||||||
if index == 1 then
|
if index == 1 then
|
||||||
|
|||||||
@ -14,12 +14,13 @@ function on_hud_open()
|
|||||||
if itemid == 0 then
|
if itemid == 0 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
local data = inventory.get_all_data(invid, slot)
|
||||||
inventory.set(invid, slot, itemid, itemcount-1)
|
inventory.set(invid, slot, itemid, itemcount-1)
|
||||||
|
|
||||||
local pvel = {player.get_vel(pid)}
|
local pvel = {player.get_vel(pid)}
|
||||||
local ppos = vec3.add({player.get_pos(pid)}, {0, 0.7, 0})
|
local ppos = vec3.add({player.get_pos(pid)}, {0, 0.7, 0})
|
||||||
local throw_force = vec3.mul(player.get_dir(pid), DROP_FORCE)
|
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))
|
local velocity = vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL))
|
||||||
drop.rigidbody:set_vel(velocity)
|
drop.rigidbody:set_vel(velocity)
|
||||||
end)
|
end)
|
||||||
|
|||||||
@ -57,6 +57,9 @@ void ItemStack::setField(std::string_view name, dv::value value) {
|
|||||||
}
|
}
|
||||||
if (value == nullptr) {
|
if (value == nullptr) {
|
||||||
fields.erase(std::string(name));
|
fields.erase(std::string(name));
|
||||||
|
if (fields.empty()) {
|
||||||
|
fields = nullptr;
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
fields[std::string(name)] = std::move(value);
|
fields[std::string(name)] = std::move(value);
|
||||||
|
|||||||
@ -54,4 +54,8 @@ public:
|
|||||||
const dv::value& getFields() const {
|
const dv::value& getFields() const {
|
||||||
return fields;
|
return fields;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool hasFields() const {
|
||||||
|
return fields != nullptr;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -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) {
|
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);
|
return lua::pushboolean(L, stack.getField(key) != nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user