Store color & fuel level when placing in inventory

master
Jordan Irwin 2021-05-23 04:23:56 -07:00
parent 317573b1f7
commit a98f162ad1
4 changed files with 75 additions and 30 deletions

View File

@ -24,21 +24,27 @@ minetest.register_craftitem("helicopter:heli", {
if minetest.get_node(pointed_thing.above).name ~= "air" then
return
end
local obj = minetest.add_entity(pointed_thing.above, "helicopter:heli")
local ent = obj:get_luaentity()
--local imeta = itemstack:get_meta()
local owner = placer:get_player_name()
ent.owner = owner
--[[
ent.energy = imeta:get_int("energy")
ent.hp = imeta:get_int("hp")]]--
local properties = ent.object:get_properties()
properties.infotext = owner .. " nice helicopter"
ent.object:set_properties(properties)
local imeta = itemstack:get_meta()
local color = imeta:get_string("color")
if color == "" then color = nil end
local fuel = math.floor(imeta:get_float("fuel") * 100) / 100
if not (creative_exists and placer and
local obj = minetest.add_entity(pointed_thing.above, "helicopter:heli")
local ent = obj:get_luaentity()
local owner = placer:get_player_name()
ent.owner = owner
ent.energy = fuel
helicopter.updateIndicator(ent)
if color then
helicopter.paint(ent, color)
end
local properties = ent.object:get_properties()
properties.infotext = owner .. " nice helicopter"
ent.object:set_properties(properties)
if not (helicopter.creative and placer and
creative.is_enabled_for(placer:get_player_name())) then
itemstack:take_item()
end

View File

@ -2,6 +2,24 @@
-- entity
--
-- supported colors
local color_def = {
["#2b2b2b"] = "Black",
["#ffffff"] = "White",
["#9f9f9f"] = "Grey",
["#6d6d6d"] = "Dark Grey",
["#dc1818"] = "Red",
["#07b6bc"] = "Cyan",
["#8c5922"] = "Brown",
["#4ee34c"] = "Green",
["#567a42"] = "Dark Green",
["#ff62c6"] = "Pink",
["#ff8b0e"] = "Orange",
["#a437ff"] = "Violet",
["#ffe400"] = "Yellow",
["#ff0098"] = "Magenta",
}
minetest.register_entity('helicopter:seat_base',{
initial_properties = {
physical = false,
@ -11,17 +29,17 @@ initial_properties = {
mesh = "seat_base.b3d",
textures = {"helicopter_black.png",},
},
on_activate = function(self,std)
self.sdata = minetest.deserialize(std) or {}
if self.sdata.remove then self.object:remove() end
end,
get_staticdata=function(self)
self.sdata.remove=true
return minetest.serialize(self.sdata)
end,
})
minetest.register_entity("helicopter:heli", {
@ -145,7 +163,7 @@ minetest.register_entity("helicopter:heli", {
local is_attached = false
if self.owner then
local player = minetest.get_player_by_name(self.owner)
if player then
local player_attach = player:get_attach()
if player_attach then
@ -170,7 +188,7 @@ minetest.register_entity("helicopter:heli", {
pitch = 1.0,
})
--[[if self.damage > 100 then --if acumulated damage is greater than 100, adieu
helicopter.destroy(self, player)
helicopter.destroy(self, player)
end]]--
end
@ -219,14 +237,14 @@ minetest.register_entity("helicopter:heli", {
if self.owner == nil then
self.owner = name
end
if self.driver_name and self.driver_name ~= name then
-- do not allow other players to remove the object while there is a driver
return
end
local touching_ground, liquid_below = helicopter.check_node_below(self.object)
--XXXXXXXX
local is_attached = false
if puncher:get_attach() == self.pilot_seat_base then is_attached = true end
@ -276,6 +294,19 @@ minetest.register_entity("helicopter:heli", {
if helicopter.pick_up then
local pinv = puncher:get_inventory()
local stack = ItemStack("helicopter:heli")
local imeta = stack:get_meta()
-- store fuel level & color
imeta:set_float("fuel", self.energy)
if self.color and self.color ~= "#0063b0" then -- don't store defult "blue"
imeta:set_string("color", self.color)
end
local color_name = color_def[self.color:lower()]
if color_name then
imeta:set_string("description", color_name .. " Helicopter")
end
if not pinv:room_for_item("main", stack) then
minetest.chat_send_player(puncher:get_player_name(),
"You do not have room in your inventory")
@ -285,7 +316,9 @@ minetest.register_entity("helicopter:heli", {
if self.passenger_seat_base then self.passenger_seat_base:remove() end
self.object:remove()
pinv:add_item("main", stack)
if not helicopter.creative or not pinv:contains_item("main", stack, true) then
pinv:add_item("main", stack)
end
end
else
helicopter.destroy(self, puncher)
@ -293,7 +326,7 @@ minetest.register_entity("helicopter:heli", {
end
end
end,
on_rightclick = function(self, clicker)

View File

@ -12,18 +12,18 @@ initial_properties = {
mesh = "pointer.b3d",
textures = {"clay.png"},
},
on_activate = function(self,std)
self.sdata = minetest.deserialize(std) or {}
if self.sdata.remove then self.object:remove() end
end,
get_staticdata=function(self)
self.sdata.remove=true
return minetest.serialize(self.sdata)
end,
})
function helicopter.get_gauge_angle(value)
@ -42,6 +42,13 @@ function helicopter.contains(table, val)
return false
end
function helicopter.updateIndicator(self)
local energy_indicator_angle = helicopter.get_gauge_angle(self.energy)
self.pointer:set_attach(self.object, '',
{x=0, y=11.26, z=9.37},
{x=0, y=0, z=energy_indicator_angle})
end
function helicopter.loadFuel(self, player_name)
local player = minetest.get_player_by_name(player_name)
local inv = player:get_inventory()
@ -60,10 +67,9 @@ function helicopter.loadFuel(self, player_name)
self.energy = self.energy + fuel
if self.energy > 10 then self.energy = 10 end
local energy_indicator_angle = helicopter.get_gauge_angle(self.energy)
self.pointer:set_attach(self.object,'',{x=0,y=11.26,z=9.37},{x=0,y=0,z=energy_indicator_angle})
helicopter.updateIndicator(self)
end
return true
end

View File

@ -49,7 +49,7 @@ if not minetest.global_exists("matrix3") then
dofile(minetest.get_modpath("helicopter") .. DIR_DELIM .. "matrix.lua")
end
local creative_exists = minetest.global_exists("creative")
helicopter.creative = minetest.global_exists("creative")
function helicopter.check_is_under_water(obj)
local pos_up = obj:get_pos()