mirror of
https://github.com/tacigar/maidroid.git
synced 2025-01-10 08:10:17 +01:00
Merge pull request #53 from tacigar/create_holding_item_system
Create holding item system
This commit is contained in:
commit
55486406f0
@ -30,6 +30,14 @@ function maidroid.is_core(item_name)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- maidroid.is_maidroid reports whether a name is maidroid's name.
|
||||||
|
function maidroid.is_maidroid(name)
|
||||||
|
if maidroid.registered_maidroids[name] then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
---------------------------------------------------------------------
|
---------------------------------------------------------------------
|
||||||
|
|
||||||
-- maidroid.maidroid represents a table that contains common methods
|
-- maidroid.maidroid represents a table that contains common methods
|
||||||
@ -117,15 +125,12 @@ end
|
|||||||
-- maidroid.maidroid.set_yaw_by_direction sets the maidroid's yaw
|
-- maidroid.maidroid.set_yaw_by_direction sets the maidroid's yaw
|
||||||
-- by a direction vector.
|
-- by a direction vector.
|
||||||
function maidroid.maidroid.set_yaw_by_direction(self, direction)
|
function maidroid.maidroid.set_yaw_by_direction(self, direction)
|
||||||
self.object:setyaw(math.atan2(direction.z, direction.x) + math.pi / 2)
|
self.object:setyaw(math.atan2(direction.z, direction.x) - math.pi / 2)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- maidroid.maidroid.get_wield_item_info returns the maidroid's wield item's stack.
|
-- maidroid.maidroid.get_wield_item_info returns the maidroid's wield item's stack.
|
||||||
function maidroid.maidroid.get_wield_item_stack(self)
|
function maidroid.maidroid.get_wield_item_stack(self)
|
||||||
local inv = self:get_inventory()
|
local inv = self:get_inventory()
|
||||||
if inv:is_empty("wield_item") then
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
return inv:get_stack("wield_item", 1)
|
return inv:get_stack("wield_item", 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -196,6 +201,59 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
--------------------------------------------------------------------
|
||||||
|
|
||||||
|
-- register empty item entity definition.
|
||||||
|
-- this entity may be hold by maidroid's hands.
|
||||||
|
;(function ()
|
||||||
|
local function on_activate(self, staticdata)
|
||||||
|
-- attach to the nearest maidroid.
|
||||||
|
local all_objects = minetest.get_objects_inside_radius(self.object:getpos(), 0.1)
|
||||||
|
for _, obj in ipairs(all_objects) do
|
||||||
|
local luaentity = obj:get_luaentity()
|
||||||
|
|
||||||
|
if maidroid.is_maidroid(luaentity.name) then
|
||||||
|
self.object:set_attach(obj, "Arm_R", {x = 0.075, y = 0.60, z = -0.20}, {x = 0, y = 90, z = 0})
|
||||||
|
self.object:set_properties{textures="air"}
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function on_step(self, staticdata)
|
||||||
|
local all_objects = minetest.get_objects_inside_radius(self.object:getpos(), 0.1)
|
||||||
|
for _, obj in ipairs(all_objects) do
|
||||||
|
local luaentity = obj:get_luaentity()
|
||||||
|
|
||||||
|
if maidroid.is_maidroid(luaentity.name) then
|
||||||
|
local stack = luaentity:get_wield_item_stack()
|
||||||
|
if stack:get_name() ~= itemname then
|
||||||
|
if stack:is_empty() then
|
||||||
|
self.itemname = "air"
|
||||||
|
self.object:set_properties{textures=self.itemname}
|
||||||
|
else
|
||||||
|
self.itemname = stack:get_name()
|
||||||
|
self.object:set_properties{textures=self.itemname}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
minetest.register_entity("maidroid:dummy_item", {
|
||||||
|
hp_max = 1,
|
||||||
|
visual = "wielditem",
|
||||||
|
visual_size = {x = 0.025, y = 0.025},
|
||||||
|
collisionbox = {0, 0, 0, 0, 0, 0},
|
||||||
|
physical = false,
|
||||||
|
textures = {"air"},
|
||||||
|
on_activate = on_activate,
|
||||||
|
on_step = on_step,
|
||||||
|
itemname = "air",
|
||||||
|
})
|
||||||
|
end) ()
|
||||||
|
|
||||||
---------------------------------------------------------------------
|
---------------------------------------------------------------------
|
||||||
|
|
||||||
-- maidroid.register_core registers a definition of a new core.
|
-- maidroid.register_core registers a definition of a new core.
|
||||||
@ -211,6 +269,8 @@ end
|
|||||||
|
|
||||||
-- maidroid.register_maidroid registers a definition of a new maidroid.
|
-- maidroid.register_maidroid registers a definition of a new maidroid.
|
||||||
function maidroid.register_maidroid(product_name, def)
|
function maidroid.register_maidroid(product_name, def)
|
||||||
|
maidroid.registered_maidroids[product_name] = def
|
||||||
|
|
||||||
-- initialize manufacturing number of a new maidroid.
|
-- initialize manufacturing number of a new maidroid.
|
||||||
if maidroid.manufacturing_data[product_name] == nil then
|
if maidroid.manufacturing_data[product_name] == nil then
|
||||||
maidroid.manufacturing_data[product_name] = 0
|
maidroid.manufacturing_data[product_name] = 0
|
||||||
@ -310,6 +370,9 @@ function maidroid.register_maidroid(product_name, def)
|
|||||||
maidroid.manufacturing_data[product_name] = maidroid.manufacturing_data[product_name] + 1
|
maidroid.manufacturing_data[product_name] = maidroid.manufacturing_data[product_name] + 1
|
||||||
create_inventory(self)
|
create_inventory(self)
|
||||||
self.nametag = self.inventory_name
|
self.nametag = self.inventory_name
|
||||||
|
|
||||||
|
-- attach dummy item to new maidroid.
|
||||||
|
minetest.add_entity(self.object:getpos(), "maidroid:dummy_item")
|
||||||
else
|
else
|
||||||
-- if static data is not empty string, this object has beed already created.
|
-- if static data is not empty string, this object has beed already created.
|
||||||
local data = minetest.deserialize(staticdata)
|
local data = minetest.deserialize(staticdata)
|
||||||
@ -377,6 +440,7 @@ function maidroid.register_maidroid(product_name, def)
|
|||||||
|
|
||||||
-- on_step is a callback function that is called every delta times.
|
-- on_step is a callback function that is called every delta times.
|
||||||
local function on_step(self, dtime)
|
local function on_step(self, dtime)
|
||||||
|
-- do core method.
|
||||||
if (not self.pause) and self.core_name ~= "" then
|
if (not self.pause) and self.core_name ~= "" then
|
||||||
local core = maidroid.registered_cores[self.core_name]
|
local core = maidroid.registered_cores[self.core_name]
|
||||||
core.on_step(self, dtime)
|
core.on_step(self, dtime)
|
||||||
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user