From d1e5ec27dc3b0a32f71d6d72ff2bf93c1b719d17 Mon Sep 17 00:00:00 2001 From: Alexsandro Percy Date: Sun, 1 May 2022 11:19:14 -0300 Subject: [PATCH] added light support --- init.lua | 82 ++++++++++++++++++++++++++++++++++++ textures/airutils_light.png | Bin 0 -> 246 bytes 2 files changed, 82 insertions(+) create mode 100644 textures/airutils_light.png diff --git a/init.lua b/init.lua index c6c24cc..71536e9 100644 --- a/init.lua +++ b/init.lua @@ -317,3 +317,85 @@ function airutils.paint(self, colstr, texture_name) end end +function airutils.get_xz_from_hipotenuse(orig_x, orig_z, yaw, distance) + --cara, o minetest é bizarro, ele considera o eixo no sentido ANTI-HORÁRIO... Então pra equação funcionar, subtrair o angulo de 360 antes + yaw = math.rad(360) - yaw + local z = (math.cos(yaw)*distance) + orig_z + local x = (math.sin(yaw)*distance) + orig_x + return x, z +end + +minetest.register_node("airutils:light", { + drawtype = "glasslike", + tile_images = {"airutils_light.png"}, + inventory_image = minetest.inventorycube("airutils_light.png"), + paramtype = "light", + walkable = false, + is_ground_content = true, + light_propagates = true, + sunlight_propagates = true, + light_source = 14, + selection_box = { + type = "fixed", + fixed = {0, 0, 0, 0, 0, 0}, + }, +}) + +function airutils.remove_light(self) + if self._light_old_pos then + --force the remotion of the last light + minetest.add_node(self._light_old_pos, {name="air"}) + self._light_old_pos = nil + end +end + +function airutils.swap_node(self, pos) + local target_pos = pos + local have_air = false + local node = nil + local count = 0 + while have_air == false and count <= 3 do + node = minetest.get_node(target_pos) + if node.name == "air" then + have_air = true + break + end + count = count + 1 + target_pos.y = target_pos.y + 1 + end + + if have_air then + minetest.set_node(target_pos, {name='airutils:light'}) + airutils.remove_light(self) + self._light_old_pos = target_pos + return true + end + return false +end + +function airutils.put_light(self) + local pos = self.object:get_pos() + pos.y = pos.y + 1 + local yaw = self.object:get_yaw() + local lx, lz = airutils.get_xz_from_hipotenuse(pos.x, pos.z, yaw, 10) + local light_pos = {x=lx, y=pos.y, z=lz} + + local cast = minetest.raycast(pos, light_pos, false, false) + local thing = cast:next() + local was_set = false + while thing do + if thing.type == "node" then + local ipos = thing.intersection_point + if ipos then + was_set = airutils.swap_node(self, ipos) + end + end + thing = cast:next() + end + if was_set == false then + local n = minetest.get_node_or_nil(light_pos) + if n and n.name == 'air' then + airutils.swap_node(self, light_pos) + end + end +end diff --git a/textures/airutils_light.png b/textures/airutils_light.png new file mode 100644 index 0000000000000000000000000000000000000000..334cb078a9a03b0d90342ac9418482494231ae71 GIT binary patch literal 246 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Ea{HEjtmSN`?>!lvI6;>1s;*b z3=G^tAk28_ZrvZCAbW|YuPgf<7GV}`=5HlBUO*wK64!{5;QX|b^2DN4hJeJ(yb?V> z*ARs=V?9$nLj!{^MGJwd;yqm)LoEE0f7H89W@P^V|NMvf9z552_&Tog1i5eV-N7Sk z+Hl0-NQZ6!&m@u547G;I44P~Qt{ghEa1vvNrrzopr0Oz_*WB>pF literal 0 HcmV?d00001