diff options
author | Perttu Ahola <celeron55@gmail.com> | 2012-04-01 16:06:01 +0300 |
---|---|---|
committer | Perttu Ahola <celeron55@gmail.com> | 2012-04-01 16:06:01 +0300 |
commit | f0e7da8a63d858f3b511872cf41cde0eaff6585d (patch) | |
tree | 5586e6807b2fc442db9ee99191caf38b7b77d207 /builtin | |
parent | 5bd32eca0f4c8afa8d933f7d7d1946cabaa8dc43 (diff) | |
download | minetest-f0e7da8a63d858f3b511872cf41cde0eaff6585d.tar.gz minetest-f0e7da8a63d858f3b511872cf41cde0eaff6585d.tar.bz2 minetest-f0e7da8a63d858f3b511872cf41cde0eaff6585d.zip |
Implement dropped items as LuaEntities; leave the old ones as is for compatibility
Diffstat (limited to 'builtin')
-rw-r--r-- | builtin/builtin.lua | 1 | ||||
-rw-r--r-- | builtin/item.lua | 13 | ||||
-rw-r--r-- | builtin/item_entity.lua | 90 |
3 files changed, 102 insertions, 2 deletions
diff --git a/builtin/builtin.lua b/builtin/builtin.lua index bf9fdc1e5..10acd0f52 100644 --- a/builtin/builtin.lua +++ b/builtin/builtin.lua @@ -13,6 +13,7 @@ math.randomseed(os.time()) dofile(minetest.get_modpath("__builtin").."/misc_helpers.lua") dofile(minetest.get_modpath("__builtin").."/item.lua") dofile(minetest.get_modpath("__builtin").."/misc_register.lua") +dofile(minetest.get_modpath("__builtin").."/item_entity.lua") dofile(minetest.get_modpath("__builtin").."/deprecated.lua") dofile(minetest.get_modpath("__builtin").."/misc.lua") dofile(minetest.get_modpath("__builtin").."/privileges.lua") diff --git a/builtin/item.lua b/builtin/item.lua index 3abf30a6d..678a5cdac 100644 --- a/builtin/item.lua +++ b/builtin/item.lua @@ -192,7 +192,17 @@ function minetest.item_place(itemstack, placer, pointed_thing) end function minetest.item_drop(itemstack, dropper, pos) - minetest.env:add_item(pos, itemstack) + if dropper.get_player_name then + local v = dropper:get_look_dir() + local p = {x=pos.x+v.x, y=pos.y+1.5+v.y, z=pos.z+v.z} + local obj = minetest.env:add_item(p, itemstack) + v.x = v.x*2 + v.y = v.y*2 + 1 + v.z = v.z*2 + obj:setvelocity(v) + else + minetest.env:add_item(pos, itemstack) + end return "" end @@ -377,4 +387,3 @@ minetest.noneitemdef_default = { -- This is used for the hand and unknown items on_use = nil, } - diff --git a/builtin/item_entity.lua b/builtin/item_entity.lua new file mode 100644 index 000000000..8468ebedf --- /dev/null +++ b/builtin/item_entity.lua @@ -0,0 +1,90 @@ +-- Minetest: builtin/item_entity.lua + +function minetest.spawn_item(pos, item) + -- Take item in any format + local stack = ItemStack(item) + local obj = minetest.env:add_entity(pos, "__builtin:item") + obj:get_luaentity():set_item(stack:to_string()) + return obj +end + +minetest.register_entity("__builtin:item", { + initial_properties = { + hp_max = 1, + physical = true, + collisionbox = {-0.25,-0.25,-0.25, 0.25,0.25,0.25}, + visual = "sprite", + visual_size = {x=0.5, y=0.5}, + textures = {""}, + spritediv = {x=1, y=1}, + initial_sprite_basepos = {x=0, y=0}, + is_visible = false, + }, + + itemstring = '', + physical_state = true, + + set_item = function(self, itemstring) + self.itemstring = itemstring + local stack = ItemStack(itemstring) + local itemtable = stack:to_table() + local itemname = nil + if itemtable then + itemname = stack:to_table().name + end + local item_texture = nil + if minetest.registered_items[itemname] then + item_texture = minetest.registered_items[itemname].inventory_image + end + item_texture = item_texture or "unknown_item.png" + self.object:set_properties({ + textures = {item_texture}, + is_visible = true, + }) + end, + + get_staticdata = function(self) + return self.itemstring + end, + + on_activate = function(self, staticdata) + self.itemstring = staticdata + self.object:set_armor_groups({immortal=1}) + self.object:setvelocity({x=0, y=2, z=0}) + self.object:setacceleration({x=0, y=-10, z=0}) + self:set_item(self.itemstring) + end, + + on_step = function(self, dtime) + local p = self.object:getpos() + p.y = p.y - 0.3 + local nn = minetest.env:get_node(p).name + if minetest.registered_nodes[nn].walkable then + if self.physical_state then + self.object:setvelocity({x=0,y=0,z=0}) + self.object:setacceleration({x=0, y=0, z=0}) + self.physical_state = false + self.object:set_properties({ + physical = false + }) + end + else + if not self.physical_state then + self.object:setvelocity({x=0,y=0,z=0}) + self.object:setacceleration({x=0, y=-10, z=0}) + self.physical_state = true + self.object:set_properties({ + physical = true + }) + end + end + end, + + on_punch = function(self, hitter) + if self.itemstring ~= '' then + hitter:get_inventory():add_item("main", self.itemstring) + end + self.object:remove() + end, +}) + |