diff options
author | GreenXenith <bl00ntdmonkey@yahoo.com> | 2018-09-10 20:45:37 -0700 |
---|---|---|
committer | GreenXenith <bl00ntdmonkey@yahoo.com> | 2018-09-10 20:45:37 -0700 |
commit | 16ca66d435487cdab2997b06bcddef27ea30b207 (patch) | |
tree | 1d2c07828c347b86a4ac4197287a546c98c19e76 | |
parent | 891cfac81d8742a00fa6f5c6c9da1030fee49b0d (diff) |
Taste the 4.2 billion-colored rainbow. Also fix 3d_armor skin bug. Tweak code styling.
-rw-r--r-- | depends.txt | 3 | ||||
-rw-r--r-- | init.lua | 313 | ||||
-rw-r--r-- | textures/bike_inventory.png | bin | 11908 -> 12019 bytes | |||
-rw-r--r-- | textures/bike_painter.png | bin | 0 -> 1891 bytes | |||
-rw-r--r-- | textures/metal_base.png | bin | 0 -> 1565 bytes | |||
-rw-r--r-- | textures/metal_blue.png | bin | 445 -> 0 bytes |
6 files changed, 260 insertions, 56 deletions
diff --git a/depends.txt b/depends.txt index 331d858..8401bd9 100644 --- a/depends.txt +++ b/depends.txt @@ -1 +1,2 @@ -default
\ No newline at end of file +default +dye
\ No newline at end of file @@ -35,7 +35,21 @@ local function get_player_skin(player) elseif skin_mod == "wardrobe" and wardrobe.playerSkins and wardrobe.playerSkins[name] then return wardrobe.playerSkins[name]..armor_tex end - return player:get_properties().textures[1]..armor_tex + local skin = player:get_properties().textures[1] + -- If we just have 3d_armor enabled make sure we get the player skin properly + if minetest.global_exists("armor") then + skin = armor.default_skin..".png" + end + return skin..armor_tex +end + +-- Bike metal texture handling +local function is_hex(color) + return color:match("#%x%x%x%x%x%x") +end + +local function colormetal(color, alpha) + return "metal_base.png^[colorize:"..(color)..":"..tostring(alpha) end -- Keep track of attached players (for leaveplayer) @@ -87,24 +101,26 @@ minetest.register_node("bike:hand", { --[[ Bike ]]-- --- Default textures (overidden when mounted) -local default_tex = { - "metal_grey.png", - "gear.png", - "metal_blue.png", - "leather.png", - "chain.png", - "metal_grey.png", - "leather.png", - "metal_black.png", - "metal_black.png", - "blank.png", - "tread.png", - "gear.png", - "spokes.png", - "tread.png", - "spokes.png", -} +-- Default textures (overidden when mounted or punched with dye) +local function default_tex(metaltex, alpha) + return { + "metal_grey.png", + "gear.png", + colormetal(metaltex, alpha), + "leather.png", + "chain.png", + "metal_grey.png", + "leather.png", + "metal_black.png", + "metal_black.png", + "blank.png", + "tread.png", + "gear.png", + "spokes.png", + "tread.png", + "spokes.png", + } +end -- Entity local bike = { @@ -115,16 +131,18 @@ local bike = { collide_with_objects = false, visual = "mesh", mesh = "bike.b3d", - textures = default_tex, + textures = default_tex("#FFFFFF", 150), stepheight = 0.6, driver = nil, + color = "#FFFFFF", + alpha = 150, old_driver = {}, v = 0, -- Current velocity last_v = 0, -- Last velocity - max_v = 6.9, -- Max velocity + max_v = 6.9, -- Max velocity fast_v = 0, -- Fast adder f_speed = 30, -- Frame speed - last_y = nil, -- Last height + last_y = 0, -- Last height up = false, -- Are we going up? timer = 0, removed = false @@ -134,7 +152,7 @@ local bike = { local function dismount_player(bike, exit) bike.object:set_velocity({x = 0, y = 0, z = 0}) -- Make the bike empty again - bike.object:set_properties({textures = default_tex}) + bike.object:set_properties({textures = default_tex(bike.color, bike.alpha)}) bike.v = 0 if bike.driver then @@ -167,7 +185,7 @@ function bike.on_rightclick(self, clicker) textures = { "metal_grey.png", "gear.png", - "metal_blue.png", + colormetal(self.color, self.alpha), "leather.png", "chain.png", "metal_grey.png", @@ -214,28 +232,71 @@ end function bike.on_activate(self, staticdata, dtime_s) self.object:set_acceleration({x = 0, y = -9.8, z = 0}) self.object:set_armor_groups({immortal = 1}) - if staticdata then - self.v = tonumber(staticdata) + if staticdata ~= "" then + local data = minetest.deserialize(staticdata) + self.v = data.v + self.color = data.color + self.alpha = data.alpha end + self.object:set_properties({textures=default_tex(self.color, self.alpha)}) self.last_v = self.v - -- We aren't going up yet - self.last_y = 0 end +-- Save velocity and color data for reload function bike.get_staticdata(self) - return tostring(self.v) + local data = {v=self.v,color=self.color,alpha=self.alpha} + return minetest.serialize(data) end +-- Pick up/color function bike.on_punch(self, puncher) + local itemstack = puncher:get_wielded_item() + -- Bike painting + if itemstack:get_name() == "bike:painter" then + -- No painting while someone is riding :P + if self.driver then + return + end + -- Get color data + local meta = itemstack:get_meta() + self.color = meta:get_string("paint_color") + self.alpha = meta:get_string("alpha") + self.object:set_properties({ + textures = { + "metal_grey.png", + "gear.png", + colormetal(self.color, self.alpha), + "leather.png", + "chain.png", + "metal_grey.png", + "leather.png", + "metal_black.png", + "metal_black.png", + "blank.png", + "tread.png", + "gear.png", + "spokes.png", + "tread.png", + "spokes.png", + }, + }) + return + end if not puncher or not puncher:is_player() or self.removed then return end + -- Make sure no one is riding if not self.driver then local inv = puncher:get_inventory() -- We can only carry one bike if not inv:contains_item("main", "bike:bike") then - local leftover = inv:add_item("main", "bike:bike") - -- if no room in inventory add the bike to the world + local stack = ItemStack({name="bike:bike", count=1, wear=0}) + local meta = stack:get_meta() + -- Set the stack to the bike color + meta:set_string("color", self.color) + meta:set_string("alpha", self.alpha) + local leftover = inv:add_item("main", stack) + -- If no room in inventory add the bike to the world if not leftover:is_empty() then minetest.add_item(self.object:get_pos(), leftover) end @@ -248,7 +309,7 @@ function bike.on_punch(self, puncher) return end local leftover = inv:add_item("main", "default:steel_ingot 6") - -- if no room in inventory add the iron to the world + -- If no room in inventory add the iron to the world if not leftover:is_empty() then minetest.add_item(self.object:get_pos(), leftover) end @@ -408,7 +469,7 @@ function bike.on_step(self, dtime) end end - -- Wheely will change this + -- Wheely will change turning speed local turn_speed = 1 -- Are we doing a wheely? @@ -484,12 +545,13 @@ end) -- Register the entity minetest.register_entity("bike:bike", bike) --- Craftitem +-- Bike craftitem minetest.register_craftitem("bike:bike", { - description = "bike", + description = "Bike", inventory_image = "bike_inventory.png", wield_scale = {x = 3, y = 3, z = 2}, groups = {flammable = 2}, + stack_max = 1, on_place = function(itemstack, placer, pointed_thing) local under = pointed_thing.under local node = minetest.get_node(under) @@ -505,7 +567,20 @@ minetest.register_craftitem("bike:bike", { return itemstack end - bike = minetest.add_entity(pointed_thing.above, "bike:bike") + -- Place bike with saved color + local meta = itemstack:get_meta() + local color = meta:get_string("color") + local alpha = tonumber(meta:get_string("alpha")) + + -- If it's a new bike, give it default colors + if alpha == nil then + color, alpha = "#FFFFFF", 150 + end + + -- Use the saved color data and place the bike + bike = minetest.add_entity(pointed_thing.above, "bike:bike", minetest.serialize({v=0,color=color,alpha=alpha})) + + -- Point it the right direction if bike then if placer then bike:set_yaw(placer:get_look_horizontal()) @@ -520,7 +595,119 @@ minetest.register_craftitem("bike:bike", { end, }) --- Crafting things +--[[ Painter ]]-- + +-- Helpers +local function rgb_to_hex(r, g, b) + return string.format("#%02X%02X%02X", r, g, b) +end + +local function hex_to_rgb(hex) + hex = hex:gsub("#","") + local rgb = { + r = tonumber("0x"..hex:sub(1,2)), + g = tonumber("0x"..hex:sub(3,4)), + b = tonumber("0x"..hex:sub(5,6)), + } + return rgb +end + +-- Need to convert between 1000 units and 256 +local function from_slider_rgb(value) + value = tonumber(value) + return math.floor((255/1000*value)+0.5) +end + +-- ...and back +local function to_slider_rgb(value) + return 1000/255*value +end + +-- Painter formspec +local function show_painter_form(itemstack, player) + local meta = itemstack:get_meta() + local color = meta:get_string("paint_color") + local alpha = tonumber(meta:get_string("alpha")) + if alpha == nil then + color, alpha = "#FFFFFF", 128 + end + local rgba = hex_to_rgb(color) + rgba.a = alpha + minetest.show_formspec(player:get_player_name(), "bike:painter", + -- Init formspec + "size[6,6;true]".. + "position[0.5, 0.45]".. + -- Hex/Alpha fields + "button[1.6,5.5;2,1;set;Set paint color]".. + "field[0.9,5;2,0.8;hex;Hex Color;"..color.."]".. + "field[2.9,5;2,0.8;alpha;Alpha (0-255);"..tostring(alpha).."]".. + -- RGBA sliders + "scrollbar[0,2;5,0.3;horizontal;r;"..tostring(to_slider_rgb(rgba.r)).."]".. + "label[5.1,1.9;R: "..tostring(rgba.r).."]".. + "scrollbar[0,2.6;5,0.3;horizontal;g;"..tostring(to_slider_rgb(rgba.g)).."]".. + "label[5.1,2.5;G: "..tostring(rgba.g).."]".. + "scrollbar[0,3.2;5,0.3;horizontal;b;"..tostring(to_slider_rgb(rgba.b)).."]".. + "label[5.1,3.1;B: "..tostring(rgba.b).."]".. + "scrollbar[0,3.8;5,0.3;horizontal;a;"..tostring(to_slider_rgb(rgba.a)).."]".. + "label[5.1,3.7;A: "..tostring(rgba.a).."]".. + -- Preview + "label[1,0;Preview:]".. + "image[2,0;2,2;metal_base.png^[colorize:"..color..":"..tostring(rgba.a).."]" + ) +end + +minetest.register_on_player_receive_fields(function(player, formname, fields) + if formname == "bike:painter" then + local itemstack = player:get_wielded_item() + if fields.set then + if itemstack:get_name() == "bike:painter" then + local meta = itemstack:get_meta() + local hex = fields.hex + local alpha = tonumber(fields.alpha) + if is_hex(hex) == nil then + hex = "#FFFFFF" + end + if alpha < 0 or alpha > 255 then + alpha = 128 + end + -- Save color data to painter (rgba sliders will adjust to hex/alpha too!) + meta:set_string("paint_color", hex) + meta:set_string("alpha", tostring(alpha)) + meta:set_string("description", "Bike Painter ("..hex:upper()..", A: "..tostring(alpha)..")") + player:set_wielded_item(itemstack) + show_painter_form(itemstack, player) + return + end + end + if fields.r or fields.g or fields.b or fields.a then + if itemstack:get_name() == "bike:painter" then + -- Save on slider adjustment (hex/alpha will adjust to match the rgba!) + local meta = itemstack:get_meta() + local function sval(value) + return from_slider_rgb(value:gsub(".*:", "")) + end + meta:set_string("paint_color", rgb_to_hex(sval(fields.r),sval(fields.g),sval(fields.b))) + meta:set_string("alpha", sval(fields.a)) + -- Keep track of what this painter is painting + meta:set_string("description", "Bike Painter ("..meta:get_string("paint_color"):upper()..", A: "..meta:get_string("alpha")..")") + player:set_wielded_item(itemstack) + show_painter_form(itemstack, player) + end + end + end +end) + +-- Make the actual thingy +minetest.register_tool("bike:painter", { + description = "Bike Painter", + inventory_image = "bike_painter.png", + wield_scale = {x = 2, y = 2, z = 1}, + on_place = show_painter_form, + on_secondary_use = show_painter_form, +}) + +--[[ Crafts ]]-- + minetest.register_craftitem("bike:wheel", { description = "Bike Wheel", inventory_image = "bike_wheel.png", @@ -531,39 +718,55 @@ minetest.register_craftitem("bike:handles", { inventory_image = "bike_handles.png", }) +-- To rubber, or not to rubber. That is the question. +local rubber + if minetest.get_modpath("technic") ~= nil then - minetest.register_craft({ - output = "bike:wheel 2", - recipe = { - {"", "technic:rubber", ""}, - {"technic:rubber", "default:steel_ingot", "technic:rubber"}, - {"", "technic:rubber", ""}, - }, - }) + rubber = "technic:rubber" else - minetest.register_craft({ - output = "bike:wheel 2", - recipe = { - {"", "group:wood", ""}, - {"group:wood", "default:steel_ingot", "group:wood"}, - {"", "group:wood", ""}, - }, - }) + rubber = "group:wood" end minetest.register_craft({ + output = "bike:wheel 2", + recipe = { + {"", rubber, ""}, + {rubber, "default:steel_ingot", rubber}, + {"", rubber, ""}, + }, +}) + +minetest.register_craft({ output = "bike:handles", recipe = { {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"}, - {"group:wood", "", "group:wood"}, + {rubber, "", rubber}, }, }) minetest.register_craft({ output = "bike:bike", recipe = { - {"bike:handles", "", "group:wood"}, + {"bike:handles", "", rubber}, {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"}, {"bike:wheel", "", "bike:wheel"}, }, }) + +-- Because not everyone likes vessels +local container + +if minetest.get_modpath("vessels") ~= nil then + container = "vessels:glass_bottle" +else + container = "default:glass" +end + +minetest.register_craft({ + output = "bike:painter", + recipe = { + {"", container, ""}, + {"default:steel_ingot", "dye:red", "dye:green"}, + {"", rubber, "dye:blue"}, + }, +}) diff --git a/textures/bike_inventory.png b/textures/bike_inventory.png Binary files differindex b18a81e..ff7acfa 100644 --- a/textures/bike_inventory.png +++ b/textures/bike_inventory.png diff --git a/textures/bike_painter.png b/textures/bike_painter.png Binary files differnew file mode 100644 index 0000000..f9653df --- /dev/null +++ b/textures/bike_painter.png diff --git a/textures/metal_base.png b/textures/metal_base.png Binary files differnew file mode 100644 index 0000000..c41306c --- /dev/null +++ b/textures/metal_base.png diff --git a/textures/metal_blue.png b/textures/metal_blue.png Binary files differdeleted file mode 100644 index 2542c11..0000000 --- a/textures/metal_blue.png +++ /dev/null |