yl_dice/init.lua
2024-02-13 08:59:21 +03:00

146 lines
3.6 KiB
Lua

local COOLDOWN = 1.8
local PARTICLE_TIME = COOLDOWN - 0.2
local function roll_dice(pos, node)
if not node then
node = minetest.get_node(pos)
end
local timer = minetest.get_node_timer(pos)
if timer:is_started() then
return
end
timer:start(COOLDOWN)
local color = math.floor(node.param2 / 32)
local facedir = math.random(0, 23)
node.param2 = color * 32 + facedir
minetest.swap_node(pos, node)
local spawner_def = {
size = 0.6,
amount = 42,
time = 0.7,
glow = 10,
pos = {
min = vector.add(pos, vector.new(-0.5, -0.5, -0.5)),
max = vector.add(pos, vector.new(0.5, 0.5, 0.5)),
},
vel = {
min = { x = 2, y = 3, z = 2 },
max = { x = -2, y = 1, z = -2 },
},
acc = {
min = { x = 0, y = 10, z = 0 },
max = { x = 0, y = 10, z = 0 },
},
exptime = { min = 0.2, max = 0.5, },
vertical = false,
texture = "yl_dice_6sides.png",
animation = {
type = "sheet_2d",
frames_w = 2,
frames_h = 3,
frame_length = 0.1,
}
}
-- can't be bothered to figure out math for this
local V = {
1, 1, 1, 1,
2, 3, 4, 5,
4, 5, 2, 3,
5, 2, 3, 4,
3, 4, 5, 2,
6, 6, 6, 6,
}
minetest.add_particlespawner(spawner_def)
local val = V[facedir + 1] or 0
local particle_def = {
expirationtime = PARTICLE_TIME,
pos = vector.add(pos, vector.new(0.0, 0.8, 0.0)),
velocity = { x = 0, y = 0.1, z = 0 },
glow = 14,
texture = ("yl_dice_%d.png"):format(val),
size = 4,
}
minetest.add_particle(particle_def)
local spec = "default_dig_cracky" --"yl_dice_roll"
local parameters = {
pos = pos,
max_hear_distance = 32, -- default
pitch = 1.0 + 0.1*math.random(),
}
minetest.sound_play(spec, parameters, true)
end
minetest.register_node("yl_dice:dice", {
description = "Dice",
drawtype = "mesh",
paramtype = "light",
paramtype2 = "colorfacedir",
mesh = "yl_dice6.obj",
tiles = {
"yl_dice_6sides.png",
},
drop = {
items = {
{ items = { "yl_dice:dice" }, inherit_color = true },
}
},
inventory_image = "yl_dice_3.png",
wield_image = "yl_dice_3.png",
groups = { dig_immediate = 2 },
palette = "yl_dice_6_palette.png",
on_construct = function(pos)
roll_dice(pos)
end,
on_rightclick = function(pos, node, puncher, itemstack, pointed_thing)
roll_dice(pos, node)
end,
})
local BASE_BLOCK
if minetest.get_modpath("yl_nether") then
-- FIXME check if this item is actually registered
BASE_BLOCK = "yl_nether:ivory_block"
end
if BASE_BLOCK then
minetest.register_craft({
type = "shaped",
output = minetest.itemstring_with_palette("yl_dice:dice", 0),
recipe = {
{ BASE_BLOCK, "dye:black", "" },
{ "", "", "" },
{ "", "", "" },
}
})
local colors = {
"red",
"blue",
"violet",
"orange",
"yellow",
"green",
"cyan",
}
for i, color in ipairs(colors) do
minetest.register_craft({
type = "shaped",
output = minetest.itemstring_with_palette("yl_dice:dice", 32 * i),
recipe = {
{ "yl_dice:dice", "dye:" .. color, "" },
{ "", "", "" },
{ "", "", "" },
}
})
end
end