Skip to content

Commit

Permalink
item physics
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Oct 18, 2023
1 parent e433713 commit 2cb1bd0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 16 deletions.
16 changes: 16 additions & 0 deletions mods/super_sam/itempickup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ minetest.register_entity(":super_sam:item", {
local data = minetest.deserialize(staticdata)
self.data = data
self.object:set_properties(data.properties)
if data.velocity then
self.object:set_velocity(data.velocity)
end
if data.acceleration then
self.object:set_acceleration(data.acceleration)
end
end,
on_step = function(self, _, moveresult)
if not self.data.enable_physics then
return
end

if moveresult.touching_ground and not moveresult.standing_on_object then
self.object:set_velocity({ x=0, y=0, z=0 })
self.data.enable_physics = false
end
end
})

Expand Down
71 changes: 55 additions & 16 deletions mods/super_sam_game_elements/box.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ local function show_formspec(pos, playername)
local formspec = [[
size[8,7;]
field[0.2,0.5;2,1;maxvel;Max Speed;]] .. meta:get_string("maxvel") .. [[]
field[2.2,0.5;2,1;minvel;Min Speed;]] .. meta:get_string("minvel") .. [[]
field[0.2,0.5;2,1;maxvel;Max Velocity;]] .. meta:get_string("maxvel") .. [[]
field[2.2,0.5;2,1;minvel;Min Velocity;]] .. meta:get_string("minvel") .. [[]
field[4.2,0.5;2,1;regenerate;Regenerate;]] .. meta:get_string("regenerate") .. [[]
button_exit[6,0.2;2,1;save;Save]
list[context;main;0,1.2;8,1;]
list[nodemeta:]] .. pos.x .. "," .. pos.y .. "," .. pos.z .. [[;main;0,1.2;8,1;]
list[current_player;main;0,2.5;8,4;]
listring[]
Expand Down Expand Up @@ -70,20 +70,59 @@ minetest.register_node(":super_sam:box", {
on_rightclick = function(pos, _, player)
show_formspec(pos, player:get_player_name())
end,
on_timer = function(pos)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local stack = inv:get_stack("main", 1)
local item_name = stack:get_name()

if not item_name or item_name == "" then
return
end
on_punch = function(pos)
local meta = minetest.get_meta(pos)

-- TODO
local max_vel = minetest.string_to_pos(meta:get_string("maxvel"))
local min_vel = minetest.string_to_pos(meta:get_string("minvel"))

local interval = tonumber(meta:get_string("regenerate")) or 5
local inv = meta:get_inventory()
for i=1,8 do
local stack = inv:get_stack("main", i)
local item_name = stack:get_name()

if item_name and item_name ~= "" then

local velocity = {
x = (math.random() * (max_vel.x - min_vel.x)) + min_vel.x,
y = (math.random() * (max_vel.y - min_vel.y)) + min_vel.y,
z = (math.random() * (max_vel.z - min_vel.z)) + min_vel.z,
}

super_sam.add_item_entity(pos, {
enable_physics = true,
velocity = velocity,
acceleration = { x=0, y=-10, z=0 },
properties = {
visual = "wielditem",
wield_item = item_name,
visual_size = { x=0.5, y=0.5 },
automatic_rotate = 1,
pointable = false,
physical = true
}
})
end
end

-- swap to hidden node and start regeneration timer
minetest.swap_node(pos, { name = "super_sam:box_hidden" })
local regenerate = tonumber(meta:get_string("regenerate")) or 5
local timer = minetest.get_node_timer(pos)
timer:start(interval)
end
timer:start(regenerate)
end
})

minetest.register_node(":super_sam:box_hidden", {
description = "Box (hidden)",
tiles = {},
drawtype = "airlike",
paramtype = "light",
sunlight_propagates = true,
pointable = false,
walkable = false,
diggable = false,
on_timer = function(pos)
minetest.swap_node(pos, { name = "super_sam:box" })
end
})

0 comments on commit 2cb1bd0

Please sign in to comment.