]> git.lizzy.rs Git - minetest.git/blob - builtin/game/statbars.lua
Translated using Weblate (Czech)
[minetest.git] / builtin / game / statbars.lua
1
2 local health_bar_definition =
3 {
4         hud_elem_type = "statbar",
5         position = { x=0.5, y=1 },
6         text = "heart.png",
7         number = 20,
8         direction = 0,
9         size = { x=24, y=24 },
10         offset = { x=(-10*24)-25, y=-(48+24+16)},
11 }
12
13 local breath_bar_definition =
14 {
15         hud_elem_type = "statbar",
16         position = { x=0.5, y=1 },
17         text = "bubble.png",
18         number = 20,
19         direction = 0,
20         size = { x=24, y=24 },
21         offset = {x=25,y=-(48+24+16)},
22 }
23
24 local hud_ids = {}
25
26 local function initialize_builtin_statbars(player)
27
28         if not player:is_player() then
29                 return
30         end
31
32         local name = player:get_player_name()
33
34         if name == "" then
35                 return
36         end
37
38         if (hud_ids[name] == nil) then
39                 hud_ids[name] = {}
40                 -- flags are not transmitted to client on connect, we need to make sure
41                 -- our current flags are transmitted by sending them actively
42                 player:hud_set_flags(player:hud_get_flags())
43         end
44
45         if player:hud_get_flags().healthbar and
46                         core.is_yes(core.setting_get("enable_damage")) then
47                 if hud_ids[name].id_healthbar == nil then
48                         health_bar_definition.number = player:get_hp()
49                         hud_ids[name].id_healthbar  = player:hud_add(health_bar_definition)
50                 end
51         else
52                 if hud_ids[name].id_healthbar ~= nil then
53                         player:hud_remove(hud_ids[name].id_healthbar)
54                         hud_ids[name].id_healthbar = nil
55                 end
56         end
57
58         if (player:get_breath() < 11) then
59                 if player:hud_get_flags().breathbar and
60                         core.is_yes(core.setting_get("enable_damage")) then
61                         if hud_ids[name].id_breathbar == nil then
62                                 hud_ids[name].id_breathbar = player:hud_add(breath_bar_definition)
63                         end
64                 else
65                         if hud_ids[name].id_breathbar ~= nil then
66                                 player:hud_remove(hud_ids[name].id_breathbar)
67                                 hud_ids[name].id_breathbar = nil
68                         end
69                 end
70         elseif hud_ids[name].id_breathbar ~= nil then
71                 player:hud_remove(hud_ids[name].id_breathbar)
72                 hud_ids[name].id_breathbar = nil
73         end
74 end
75
76 local function cleanup_builtin_statbars(player)
77
78         if not player:is_player() then
79                 return
80         end
81
82         local name = player:get_player_name()
83
84         if name == "" then
85                 return
86         end
87
88         hud_ids[name] = nil
89 end
90
91 local function player_event_handler(player,eventname)
92         assert(player:is_player())
93
94         local name = player:get_player_name()
95
96         if name == "" then
97                 return
98         end
99
100         if eventname == "health_changed" then
101                 initialize_builtin_statbars(player)
102
103                 if hud_ids[name].id_healthbar ~= nil then
104                         player:hud_change(hud_ids[name].id_healthbar,"number",player:get_hp())
105                         return true
106                 end
107         end
108
109         if eventname == "breath_changed" then
110                 initialize_builtin_statbars(player)
111
112                 if hud_ids[name].id_breathbar ~= nil then
113                         player:hud_change(hud_ids[name].id_breathbar,"number",player:get_breath()*2)
114                         return true
115                 end
116         end
117
118         if eventname == "hud_changed" then
119                 initialize_builtin_statbars(player)
120                 return true
121         end
122
123         return false
124 end
125
126 function core.hud_replace_builtin(name, definition)
127
128         if definition == nil or
129                 type(definition) ~= "table" or
130                 definition.hud_elem_type ~= "statbar" then
131                 return false
132         end
133
134         if name == "health" then
135                 health_bar_definition = definition
136
137                 for name,ids in pairs(hud_ids) do
138                         local player = core.get_player_by_name(name)
139                         if  player and hud_ids[name].id_healthbar then
140                                 player:hud_remove(hud_ids[name].id_healthbar)
141                                 initialize_builtin_statbars(player)
142                         end
143                 end
144                 return true
145         end
146
147         if name == "breath" then
148                 breath_bar_definition = definition
149
150                 for name,ids in pairs(hud_ids) do
151                         local player = core.get_player_by_name(name)
152                         if  player and hud_ids[name].id_breathbar then
153                                 player:hud_remove(hud_ids[name].id_breathbar)
154                                 initialize_builtin_statbars(player)
155                         end
156                 end
157                 return true
158         end
159
160         return false
161 end
162
163 core.register_on_joinplayer(initialize_builtin_statbars)
164 core.register_on_leaveplayer(cleanup_builtin_statbars)
165 core.register_playerevent(player_event_handler)