-
Notifications
You must be signed in to change notification settings - Fork 7
/
hud.lua
178 lines (139 loc) · 4.29 KB
/
hud.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
local HUD_POSITION = {x = 0.5, y = 0.2}
local HUD_ALIGNMENT = {x = 1, y = 0}
local hud = {} -- playerName -> {}
-- returns the image (item, node, tool) or ""
local get_image = function(name)
-- minetest.registered_items[name].inventory_image
-- minetest.registered_tools[name].inventory_image
-- minetest.registered_nodes["default:stone"].tiles[1]
-- TODO: look at drawer code
if name == nil then
return ""
end
local node = minetest.registered_nodes[name]
if node ~= nil and node.tiles ~= nil and table.getn(node.tiles) == 1 then
return minetest.inventorycube(node.tiles[1],node.tiles[1],node.tiles[1])
end
local item = minetest.registered_items[name]
if item ~= nil and item.inventory_image ~= nil then
return item.inventory_image
end
local tool = minetest.registered_tools[name]
if tool ~= nil and tool.inventory_image ~= nil then
return tool.inventory_image
end
-- none found
return ""
end
minetest.register_on_joinplayer(function(player)
local playername = player:get_player_name()
local data = {}
data.title = player:hud_add({
hud_elem_type = "text",
position = HUD_POSITION,
offset = {x = 0, y = 0},
text = "",
alignment = HUD_ALIGNMENT,
scale = {x = 100, y = 100},
number = 0xFFFFFF
})
data.mission = player:hud_add({
hud_elem_type = "text",
position = HUD_POSITION,
offset = {x = 0, y = 35},
text = "",
alignment = HUD_ALIGNMENT,
scale = {x = 100, y = 100},
number = 0x00FF00
})
data.time = player:hud_add({
hud_elem_type = "text",
position = HUD_POSITION,
offset = {x = 0, y = 70},
text = "",
alignment = HUD_ALIGNMENT,
scale = {x = 100, y = 100},
number = 0x00FF00
})
hud[playername] = data
end)
minetest.register_on_leaveplayer(function(player)
local playername = player:get_player_name()
hud[playername] = nil
end)
local format_time = function(seconds)
local minutes = math.floor(seconds / 60)
local secs = seconds - (minutes * 60)
if secs < 10 then
return minutes .. ":0" .. secs
else
return minutes .. ":" ..secs
end
end
missions.hud_remove_mission = function(player, mission)
-- remove waypoints from elapsed mission
if mission.hud ~= nil and mission.hud.source ~= nil then
player:hud_remove(mission.hud.source)
end
if mission.hud ~= nil and mission.hud.target ~= nil then
player:hud_remove(mission.hud.target)
end
end
missions.hud_update = function(player, playermissions)
local playername = player:get_player_name()
local now = os.time(os.date("!*t"))
local data = hud[playername]
local topMission = nil
if data ~= nil and playermissions ~= nil then
for i,mission in pairs(playermissions) do
if mission.hud == nil then
if mission.type == "transport" then
-- add waypoint markers if new mission
mission.hud = {}
mission.hud.target = player:hud_add({
hud_elem_type = "waypoint",
name = mission.target.title .. "(Destination)",
text = "m",
number = 0x0000FF,
world_pos = {x=mission.target.x, y=mission.target.y, z=mission.target.z}
})
end
end
-- top mission check
if topMission == nil then
topMission = mission
else
local remainingTime = mission.time - (now - mission.start)
local topRemainingTime = topMission.time - (now - topMission.start)
if remainingTime < topRemainingTime then
topMission = mission
end
end
end
end
if topMission ~= nil then
-- show the first mission to time out
local remainingTime = topMission.time - (now - topMission.start)
player:hud_change(data.title, "text", "Missions: (1/" .. table.getn(playermissions) .. ")")
player:hud_change(data.mission, "text", topMission.name .. " (" .. topMission.type .. ")")
player:hud_change(data.time, "text", "" .. format_time(remainingTime))
if remainingTime > 60 then
player:hud_change(data.time, "number", 0x00FF00)
player:hud_change(data.mission, "number", 0x00FF00)
else
player:hud_change(data.time, "number", 0xFF0000)
player:hud_change(data.mission, "number", 0xFF0000)
end
else
-- no missions running
player:hud_change(data.title, "text", "")
player:hud_change(data.mission, "text", "")
player:hud_change(data.time, "text", "")
local i = 1
while i <= hud_context_count do -- 1..n
player:hud_change(data.context[i], "text", "")
player:hud_change(data.contextcount[i], "text", "")
i = i + 1
end
end
end