From b3ac1bd8f4f2838c3a303f17a7ee0a3bffb32521 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 26 Nov 2024 11:01:29 +0300 Subject: [PATCH] update base:drop component --- res/content/base/modules/util.lua | 4 ++-- res/content/base/scripts/components/drop.lua | 15 +++++++++------ res/content/base/scripts/hud.lua | 2 +- res/scripts/post_content.lua | 12 ++++++++++++ src/logic/scripting/scripting.cpp | 1 + 5 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 res/scripts/post_content.lua diff --git a/res/content/base/modules/util.lua b/res/content/base/modules/util.lua index 6982780b..a4519b53 100644 --- a/res/content/base/modules/util.lua +++ b/res/content/base/modules/util.lua @@ -1,10 +1,10 @@ local base_entities = {} -function base_entities.drop(ppos, itemid, count, ready) +function base_entities.drop(ppos, itemid, count, pickup_delay) return entities.spawn("base:drop", ppos, {base__drop={ id=itemid, count=count, - ready=ready + pickup_delay=pickup_delay }}) end diff --git a/res/content/base/scripts/components/drop.lua b/res/content/base/scripts/components/drop.lua index 9de9bdf7..12e16265 100644 --- a/res/content/base/scripts/components/drop.lua +++ b/res/content/base/scripts/components/drop.lua @@ -4,11 +4,11 @@ local rig = entity.skeleton inair = true target = -1 -ready = false +timer = 0.3 local dropitem = ARGS if dropitem then - ready = dropitem.ready + timer = dropitem.pickup_delay or timer end if SAVED_DATA.item then dropitem.id = item.index(SAVED_DATA.item) @@ -43,7 +43,6 @@ function on_grounded(force) mat4.scale(matrix, scale, matrix) rig:set_matrix(0, matrix) inair = false - ready = true end function on_fall() @@ -53,12 +52,12 @@ end function on_sensor_enter(index, oid) local playerid = hud.get_player() local playerentity = player.get_entity(playerid) - if ready and oid == playerentity and index == 0 then + 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") end - if index == 1 and ready and oid == playerentity then + if index == 1 and oid == playerentity then target = oid end end @@ -83,8 +82,12 @@ function on_render() end end -function on_update() +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) diff --git a/res/content/base/scripts/hud.lua b/res/content/base/scripts/hud.lua index de600599..31125631 100644 --- a/res/content/base/scripts/hud.lua +++ b/res/content/base/scripts/hud.lua @@ -19,7 +19,7 @@ function on_hud_open() 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) + local drop = base_util.drop(ppos, itemid, 1, 1.5) local velocity = vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL)) drop.rigidbody:set_vel(velocity) end) diff --git a/res/scripts/post_content.lua b/res/scripts/post_content.lua new file mode 100644 index 00000000..3d1ffa33 --- /dev/null +++ b/res/scripts/post_content.lua @@ -0,0 +1,12 @@ +function make_read_only(t) + setmetatable(t, { + __newindex = function() + error("table is read-only") + end + }) +end + +make_read_only(block.properties) +for k,v in pairs(block.properties) do + make_read_only(v) +end diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index 543b2732..8fd9b231 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -195,6 +195,7 @@ void scripting::on_content_load(Content* content) { lua::setfield(L, "properties"); lua::pop(L); } + load_script(fs::path("post_content.lua"), true); load_script(fs::path("stdcmd.lua"), true); }