use only one entity for drawing
parent
7f243cfcf1
commit
ec89ba4c94
185
init.lua
185
init.lua
|
@ -63,21 +63,62 @@ picnode = {
|
||||||
}
|
}
|
||||||
|
|
||||||
-- picture texture entity
|
-- picture texture entity
|
||||||
|
|
||||||
|
paintbox1 = { -0.5,-0.5,0,0.5,0.5,0 }
|
||||||
|
paintbox2 = { 0,-0.5,-0.5,0,0.5,0.5 }
|
||||||
|
|
||||||
picent = {
|
picent = {
|
||||||
collisionbox = { 0,0,0,0,0,0 },
|
collisionbox = { 0, 0, 0, 0, 0, 0 },
|
||||||
visual = "upright_sprite",
|
visual = "upright_sprite",
|
||||||
textures = { "white.png" },
|
textures = { "white.png" },
|
||||||
|
|
||||||
on_activate = function(self, staticdata)
|
on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir)
|
||||||
|
local ppos = puncher:getpos()
|
||||||
|
--get player eye level
|
||||||
|
ppos = { x = ppos.x, y = ppos.y+(1.5 + 1/16), z = ppos.z }
|
||||||
|
|
||||||
|
local pos = self.object:getpos()
|
||||||
|
local l = puncher:get_look_dir()
|
||||||
|
|
||||||
|
local d = dirs[self.fd]
|
||||||
|
local od = dirs[(self.fd + 1) % 4]
|
||||||
|
local normal = { x = d.x, y = 0, z = d.z }
|
||||||
|
local p = intersect(ppos, l, pos, normal)
|
||||||
|
|
||||||
|
local off = -0.5
|
||||||
|
pos = { x = pos.x + off * od.x, y=pos.y + off, z=pos.z + off * od.z }
|
||||||
|
p = sub(p, pos)
|
||||||
|
local x = math.abs(p.x + p.z)
|
||||||
|
local y = p.y
|
||||||
|
|
||||||
|
x = round(x/(1/res))
|
||||||
|
y = round((1-y)/(1/res))
|
||||||
|
|
||||||
|
x = clamp(x)
|
||||||
|
y = clamp(y-1)
|
||||||
|
|
||||||
|
local name = puncher:get_wielded_item():get_name()
|
||||||
|
name = string.split(name, "_")[2]
|
||||||
|
local t = textures[name]
|
||||||
|
if t then
|
||||||
|
self.grid[x][y]=colors[name]
|
||||||
|
self.object:set_properties({textures = { to_imagestring(self.grid) }})
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
|
||||||
|
on_activate = function(self, staticdata)
|
||||||
local pos = self.object:getpos()
|
local pos = self.object:getpos()
|
||||||
local meta = minetest.env:get_meta(pos)
|
local meta = minetest.env:get_meta(pos)
|
||||||
local data = meta:get_string("painting:picturedata")
|
local data = meta:get_string("painting:picturedata")
|
||||||
|
|
||||||
if not data then return end
|
if data == "" then
|
||||||
data = minetest.deserialize(data)
|
self.grid = initgrid()
|
||||||
data = to_imagestring(data)
|
else
|
||||||
self.object:set_properties({textures = { data }})
|
data = minetest.deserialize(data)
|
||||||
end
|
data = to_imagestring(data)
|
||||||
|
self.object:set_properties({textures = { data }})
|
||||||
|
end
|
||||||
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
--paintedcanvas picture inventory item
|
--paintedcanvas picture inventory item
|
||||||
|
@ -145,52 +186,34 @@ canvasnode = {
|
||||||
groups = { snappy = 2, choppy = 2, oddly_breakable_by_hand = 2 },
|
groups = { snappy = 2, choppy = 2, oddly_breakable_by_hand = 2 },
|
||||||
|
|
||||||
drop = "",
|
drop = "",
|
||||||
|
|
||||||
can_dig = function ()
|
|
||||||
return true
|
|
||||||
end,
|
|
||||||
|
|
||||||
on_construct = function(pos)
|
on_construct = function(pos)
|
||||||
local node = minetest.env:get_node(pos)
|
local easel = minetest.env:get_node({ x = pos.x, y = pos.y - 1, z = pos.z})
|
||||||
local fd = node.param2
|
local fd = easel.param2
|
||||||
local dir = dirs[(fd + 1) % 4]
|
local dir = dirs[fd]
|
||||||
local off = -(0.5 - 1/(res*2))
|
pos = {x = pos.x - 0.01 * dir.x, y = pos.y, z = pos.z - 0.01 * dir.z}
|
||||||
local master
|
|
||||||
|
local p = minetest.env:add_entity(pos, "painting:picent"):get_luaentity()
|
||||||
for y = 0, res-1 do
|
p.object:setyaw(math.pi*easel.param2/-2)
|
||||||
for x = 0, res-1 do
|
p.fd = easel.param2
|
||||||
local np = { x = pos.x + off*dir.x + x/res*dir.x,
|
p.grid = initgrid()
|
||||||
y = pos.y - off - y/res,
|
if fd == 0 or fd == 2 then
|
||||||
z = pos.z + off*dir.z + x/res*dir.z }
|
p.object:set_properties({ collisionbox = paintbox1 })
|
||||||
|
else
|
||||||
local p = "painting:pixel_white"
|
p.object:set_properties({ collisionbox = paintbox2 })
|
||||||
p = minetest.env:add_entity(np, p):get_luaentity()
|
|
||||||
if x==0 and y == 0 then
|
|
||||||
p.grid = initgrid()
|
|
||||||
master = p
|
|
||||||
else
|
|
||||||
p.grid = nil
|
|
||||||
end
|
|
||||||
p.parent = master
|
|
||||||
p.object:setyaw(math.pi*fd/-2)
|
|
||||||
p.pos={x=x, y=y}
|
|
||||||
p.name="easel"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
|
||||||
after_dig_node=function(pos, oldnode, oldmetadata, digger)
|
after_dig_node=function(pos, oldnode, oldmetadata, digger)
|
||||||
--get data and remove pixels
|
--get data and remove pixels
|
||||||
local data
|
local data
|
||||||
local objects = minetest.env:get_objects_inside_radius(pos, 1)
|
local objects = minetest.env:get_objects_inside_radius(pos, 0.5)
|
||||||
for _, e in ipairs(objects) do
|
for _, e in ipairs(objects) do
|
||||||
e = e:get_luaentity()
|
e = e:get_luaentity()
|
||||||
if e.name == "easel" then
|
if e.grid then
|
||||||
if e.grid then
|
data = e.grid
|
||||||
data = e.grid
|
|
||||||
end
|
|
||||||
e.object:remove()
|
|
||||||
end
|
end
|
||||||
|
e.object:remove()
|
||||||
end
|
end
|
||||||
|
|
||||||
if data then
|
if data then
|
||||||
|
@ -243,7 +266,7 @@ easel = {
|
||||||
|
|
||||||
meta:set_int("has_canvas", 1)
|
meta:set_int("has_canvas", 1)
|
||||||
local itemstack = ItemStack("painting:canvas")
|
local itemstack = ItemStack("painting:canvas")
|
||||||
player:get_inventory():remove_item("main", itemstack)
|
player:get_inventory():remove_item("main", itemstack)
|
||||||
end,
|
end,
|
||||||
|
|
||||||
can_dig = function(pos,player)
|
can_dig = function(pos,player)
|
||||||
|
@ -257,30 +280,7 @@ easel = {
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
--pixel and brushes
|
--brushes
|
||||||
local c = 1/(res*2)
|
|
||||||
local p = "white.png"
|
|
||||||
|
|
||||||
pixel = {
|
|
||||||
physical = true,
|
|
||||||
collisionbox = { -c, -c, -c, c, c, c },
|
|
||||||
visual = "cube",
|
|
||||||
textures = { p, p, p, p, p, p },
|
|
||||||
visual_size = { x = 1/res, y = 1/res },
|
|
||||||
automatic_rotate = false,
|
|
||||||
|
|
||||||
on_punch = function(self, hitter)
|
|
||||||
local name = hitter:get_wielded_item():get_name()
|
|
||||||
name = string.split(name, "_")[2]
|
|
||||||
|
|
||||||
self.parent.grid[self.pos.x][self.pos.y]=colors[name]
|
|
||||||
|
|
||||||
local p = textures[name]
|
|
||||||
if p then
|
|
||||||
self.object:set_properties({textures = { p, p, p, p, p, p }})
|
|
||||||
end
|
|
||||||
end
|
|
||||||
}
|
|
||||||
|
|
||||||
brush = {
|
brush = {
|
||||||
description = "brush",
|
description = "brush",
|
||||||
|
@ -292,12 +292,7 @@ brush = {
|
||||||
tool_capabilities = {
|
tool_capabilities = {
|
||||||
full_punch_interval = 1.0,
|
full_punch_interval = 1.0,
|
||||||
max_drop_level=0,
|
max_drop_level=0,
|
||||||
groupcaps={
|
groupcaps = {}
|
||||||
-- For example:
|
|
||||||
fleshy={times={[2]=0.80, [3]=0.40}, maxwear=0.05, maxlevel=1},
|
|
||||||
snappy={times={[2]=0.80, [3]=0.40}, maxwear=0.05, maxlevel=1},
|
|
||||||
choppy={times={[3]=0.90}, maxwear=0.05, maxlevel=0}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -316,7 +311,6 @@ revcolors = {}
|
||||||
for color, _ in pairs(textures) do
|
for color, _ in pairs(textures) do
|
||||||
table.insert(revcolors, color)
|
table.insert(revcolors, color)
|
||||||
|
|
||||||
minetest.register_entity("painting:pixel_"..color, pixel)
|
|
||||||
minetest.register_tool("painting:brush_"..color, brush)
|
minetest.register_tool("painting:brush_"..color, brush)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -350,7 +344,38 @@ function to_imagestring(data)
|
||||||
end
|
end
|
||||||
|
|
||||||
dirs = {
|
dirs = {
|
||||||
[0] = {x = 0, z = 1},
|
[0] = { x = 0, z = 1 },
|
||||||
[1] = {x = 1, z = 0},
|
[1] = { x = 1, z = 0 },
|
||||||
[2] = {x = 0, z =-1},
|
[2] = { x = 0, z =-1 },
|
||||||
[3] = {x =-1, z = 0} }
|
[3] = { x =-1, z = 0 } }
|
||||||
|
|
||||||
|
function sub(v, w)
|
||||||
|
return { x = v.x - w.x,
|
||||||
|
y = v.y - w.y,
|
||||||
|
z = v.z - w.z }
|
||||||
|
end
|
||||||
|
|
||||||
|
function dot(v, w)
|
||||||
|
return v.x * w.x + v.y * w.y + v.z * w.z
|
||||||
|
end
|
||||||
|
|
||||||
|
function intersect(pos, dir, origin, normal)
|
||||||
|
local t = -(dot(sub(pos, origin), normal)) / dot(dir, normal)
|
||||||
|
return { x = pos.x + dir.x * t,
|
||||||
|
y = pos.y + dir.y * t,
|
||||||
|
z = pos.z + dir.z * t }
|
||||||
|
end
|
||||||
|
|
||||||
|
function round(num)
|
||||||
|
return math.floor(num+0.5)
|
||||||
|
end
|
||||||
|
|
||||||
|
function clamp(num)
|
||||||
|
if num < 0 then
|
||||||
|
return 0
|
||||||
|
elseif num > 15 then
|
||||||
|
return 15
|
||||||
|
else
|
||||||
|
return num
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in New Issue