Skip to content

Commit

Permalink
move entity stuff into base mod
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Oct 18, 2023
1 parent 3fbe9b2 commit e433713
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
28 changes: 26 additions & 2 deletions mods/super_sam/itempickup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,31 @@ local item_callbacks = {}

local global_callbacks = {}

minetest.register_entity(":super_sam:item", {
initial_properties = {},
static_save = false,
on_activate = function(self, staticdata)
self.object:set_armor_groups({punch_operable = 1})
local data = minetest.deserialize(staticdata)
self.data = data
self.object:set_properties(data.properties)
end
})

function super_sam.add_item_entity(pos, data)
minetest.add_entity(pos, "super_sam:item", minetest.serialize(data))
end

function super_sam.remove_item_entities(pos1, pos2)
local objects = minetest.get_objects_in_area(pos1, pos2)
for _, object in ipairs(objects) do
local entity = object:get_luaentity()
if entity and entity.name == "super_sam:item" then
object:remove()
end
end
end

local function check_player_for_pickups(player)
local pos = vector.add(player:get_pos(), {x=0, y=0.5, z=0})
local objects = minetest.get_objects_inside_radius(pos, 1.5)
Expand All @@ -13,7 +38,6 @@ local function check_player_for_pickups(player)
local entity = obj:get_luaentity()

if not is_player and entity.name == "super_sam:item" and entity.data then
assert(entity.data.spawner_pos, "entity issue: no 'spawner_pos'")
assert(entity.data.properties, "entity issue: no 'properties'")

local itemname = entity.data.properties.wield_item
Expand All @@ -32,7 +56,7 @@ local function check_player_for_pickups(player)
end

for _, callback in ipairs(global_callbacks) do
callback(player, entity.data.spawner_pos)
callback(player, entity.data)
end
end
end
Expand Down
31 changes: 8 additions & 23 deletions mods/super_sam_game_elements/itemspawner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ local function add_item(pos)
return
end

-- print("Adding item: " .. item_name .. " at pos " .. minetest.pos_to_string(pos))

local item_pos = vector.add(pos, {x=x_offset, y=y_offset, z=z_offset})
minetest.add_entity(item_pos, "super_sam:item", minetest.serialize({
super_sam.add_item_entity(item_pos, {
spawner_pos = pos,
properties = {
visual = "wielditem",
Expand All @@ -44,7 +42,7 @@ local function add_item(pos)
automatic_rotate = 1,
pointable = false
}
}))
})
end

local function remove_item(pos)
Expand All @@ -56,13 +54,7 @@ local function remove_item(pos)
local pos1 = vector.add(item_pos, 0.5)
local pos2 = vector.subtract(item_pos, 0.5)

local objects = minetest.get_objects_in_area(pos1, pos2)
for _, object in ipairs(objects) do
local entity = object:get_luaentity()
if entity and entity.name == "super_sam:item" then
object:remove()
end
end
super_sam.remove_item_entities(pos1, pos2)
end

local function refresh_item(pos)
Expand Down Expand Up @@ -119,16 +111,7 @@ super_sam.register_hidden_node(":super_sam:item_spawner", {

minetest.register_alias("super_sam:item_spawner_hidden", "super_sam:item_spawner")

minetest.register_entity(":super_sam:item", {
initial_properties = {},
static_save = false,
on_activate = function(self, staticdata)
self.object:set_armor_groups({punch_operable = 1})
local data = minetest.deserialize(staticdata)
self.data = data
self.object:set_properties(data.properties)
end
})


minetest.register_lbm({
label = "Item spawner trigger",
Expand All @@ -141,8 +124,10 @@ minetest.register_lbm({
-- time-based item renewal
local collected_spawners = {}

super_sam.register_on_pickup(function(_, spawner_pos)
collected_spawners[minetest.pos_to_string(spawner_pos)] = os.time()
super_sam.register_on_pickup(function(_, data)
if data.spawner_pos then
collected_spawners[minetest.pos_to_string(data.spawner_pos)] = os.time()
end
end)

local function refresh_items()
Expand Down

0 comments on commit e433713

Please sign in to comment.