0
0
Fork 0

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.registra_item("watch:watch",watch.images_a[3],true)
watch.registra_item("watch:watch",watch.images[3],true)
--Faces
for a=0,11,1 do
watch.registra_item("watch:watch_a"..tostring(a),watch.images_a[a+1],false)
for a=0,63,1 do
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

View File

@ -7,87 +7,19 @@
watch = {}
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
watch.images_a = {
"watch_a0.png",
"watch_a1.png",
"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
-- Image of all 64 possible faces
watch.images = {}
for frame=0,63 do
table.insert(watch.images, "clock_clock.png^[verticalframe:64:"..frame)
end
--When someone try use the watch.
function watch.usa (itemstack, user, pointed_thing)
item=itemstack:to_table()
local meta=DOM_get_item_meta(item)
local w_type="a"
local function round(num)
return math.floor(num + 0.5)
end
if meta~=nil then
w_type = meta["w_type"]
end
--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
function watch.pega_hora()
local t = 64 * minetest.get_timeofday()
return tostring(round(t))
end
-- Register itens
@ -97,7 +29,6 @@ function watch.registra_item(nome,imagem,aparece_nas_receitas)
g = 0
end
--DOM_inspeciona_r("Registrando item "..nome..","..imagem)
minetest.register_tool(nome, {
description = "Clock",
inventory_image = imagem,
@ -105,34 +36,29 @@ function watch.registra_item(nome,imagem,aparece_nas_receitas)
metadata = {w_type="d"},
wield_image = "",
stack_max = 1,
on_use = watch.usa,
})
end
minetest.register_globalstep(function(dtime)
local t="a" -- d to digital, a to analogic
local now = watch.pega_hora(2)
--DOM_inspeciona_r("Hora:"..now)
if now == "12" then now = "0" end
local now = watch.pega_hora()
if watch.ultimo_tempo == now then
return
-- return
end
watch.ultimo_tempo = now
local players = minetest.get_connected_players()
local players = minetest.get_connected_players()
for i,player in ipairs(players) do
if string.sub(player:get_wielded_item():get_name(), 0, 11) == "watch:watch" then
player:set_wielded_item("watch:watch_"..t..now)
if string.sub(player:get_wielded_item():get_name(), 0, 63) == "watch:watch" then
player:set_wielded_item("watch:watch_"..now)
end
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():add_item("main", "watch:watch_"..t..now)
player:get_inventory():add_item("main", "watch:watch_"..now)
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