Minecraft-style clock

This commit is contained in:
Wuzzy 2017-01-09 02:42:05 +01:00
parent 07e824dcae
commit f8f6f57611
15 changed files with 26 additions and 94 deletions

View file

@ -12,9 +12,15 @@ minetest.register_craft({
--Watch tool --Watch tool
watch.registra_item("watch:watch",watch.images_a[3],true) watch.registra_item("watch:watch",watch.images[3],true)
--Faces --Faces
for a=0,11,1 do for a=0,63,1 do
watch.registra_item("watch:watch_a"..tostring(a),watch.images_a[a+1],false) local b = a
if b > 31 then
b = b - 32
else
b = b + 32
end
watch.registra_item("watch:watch_"..tostring(a),watch.images[b+1],false)
end end

View file

@ -7,87 +7,19 @@
watch = {} watch = {}
watch.ultimo_tempo = -1 watch.ultimo_tempo = -1
-- Image of all 12 possible faces, only cover hours, a day is to short to lost time with game minutes... :-P -- Image of all 64 possible faces
watch.images_a = { watch.images = {}
"watch_a0.png", for frame=0,63 do
"watch_a1.png", table.insert(watch.images, "clock_clock.png^[verticalframe:64:"..frame)
"watch_a2.png",
"watch_a3.png",
"watch_a4.png",
"watch_a5.png",
"watch_a6.png",
"watch_a7.png",
"watch_a8.png",
"watch_a9.png",
"watch_a10.png",
"watch_a11.png",
}
watch.images_d={
"watch_d0.png",
"watch_d1.png",
"watch_d2.png",
"watch_d3.png",
"watch_d4.png",
"watch_d5.png",
"watch_d6.png",
"watch_d7.png",
"watch_d8.png",
"watch_d9.png",
"watch_d10.png",
"watch_d11.png",
}
--Catch the sever time and convert to hour, 12000 = 12h = 0.5, 6000 = 6h = 0.25
function watch.pega_hora(tipo)
local tempo_r = "12:00"
local t = minetest.env:get_timeofday()
local tempo = t*24 -- Get the time
local tempo_h = math.floor(tempo) -- Get 24h only, losting minutes
local tempo_m =math.floor((tempo - tempo_h)*60) --Get only minutes
--Hour
local tempo_h_12 = tempo_h
if tempo_h > 12 then -- Converte time to time in 12h format
tempo_h_12 = tempo_h - 12
end
if tipo==2 then -- hh
return(tostring(tempo_h_12))
end
tempo_r = tostring(tempo_h) .. ":"
--Minutes
if tempo_m < 10 then -- Add a zero at left if need.
tempo_r = tempo_r .. "0"
end
tempo_r = tempo_r .. tostring(tempo_m)
return(tempo_r) --HH:MM
end end
--When someone try use the watch. local function round(num)
function watch.usa (itemstack, user, pointed_thing) return math.floor(num + 0.5)
item=itemstack:to_table() end
local meta=DOM_get_item_meta(item)
local w_type="a"
if meta~=nil then function watch.pega_hora()
w_type = meta["w_type"] local t = 64 * minetest.get_timeofday()
end return tostring(round(t))
--DOM_inspeciona_r("Valores no meta:"..dump(meta))
--print("Relógio em modo: "..w_type)
meta["time"] = watch.pega_hora(1)
meta["w_type"] = w_type
DOM_set_item_meta(item, meta)
meta=DOM_get_item_meta(item)
--DOM_inspeciona_r("Valores no meta:"..dump(meta))
minetest.chat_send_player(user:get_player_name(), "[Watch] Time now is:" .. meta["time"])
itemstack:replace(item)
return itemstack
end end
-- Register itens -- Register itens
@ -97,7 +29,6 @@ function watch.registra_item(nome,imagem,aparece_nas_receitas)
g = 0 g = 0
end end
--DOM_inspeciona_r("Registrando item "..nome..","..imagem)
minetest.register_tool(nome, { minetest.register_tool(nome, {
description = "Clock", description = "Clock",
inventory_image = imagem, inventory_image = imagem,
@ -105,34 +36,29 @@ function watch.registra_item(nome,imagem,aparece_nas_receitas)
metadata = {w_type="d"}, metadata = {w_type="d"},
wield_image = "", wield_image = "",
stack_max = 1, stack_max = 1,
on_use = watch.usa,
}) })
end end
minetest.register_globalstep(function(dtime) minetest.register_globalstep(function(dtime)
local t="a" -- d to digital, a to analogic
local now = watch.pega_hora(2) local now = watch.pega_hora()
--DOM_inspeciona_r("Hora:"..now)
if now == "12" then now = "0" end
if watch.ultimo_tempo == now then if watch.ultimo_tempo == now then
return -- return
end end
watch.ultimo_tempo = now watch.ultimo_tempo = now
local players = minetest.get_connected_players() local players = minetest.get_connected_players()
for i,player in ipairs(players) do for i,player in ipairs(players) do
if string.sub(player:get_wielded_item():get_name(), 0, 11) == "watch:watch" then if string.sub(player:get_wielded_item():get_name(), 0, 63) == "watch:watch" then
player:set_wielded_item("watch:watch_"..t..now) player:set_wielded_item("watch:watch_"..now)
end end
for i,stack in ipairs(player:get_inventory():get_list("main")) do for i,stack in ipairs(player:get_inventory():get_list("main")) do
if i<9 and string.sub(stack:get_name(), 0, 11) == "watch:watch" then if i<10 and string.sub(stack:get_name(), 0, 11) == "watch:watch" then
player:get_inventory():remove_item("main", stack:get_name()) player:get_inventory():remove_item("main", stack:get_name())
player:get_inventory():add_item("main", "watch:watch_"..t..now) player:get_inventory():add_item("main", "watch:watch_"..now)
end end
end end
end end

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 298 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 308 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 303 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 303 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 302 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 300 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 300 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 301 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 291 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 301 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 301 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 302 B