-
Notifications
You must be signed in to change notification settings - Fork 11
/
entities.lua
93 lines (74 loc) · 1.85 KB
/
entities.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
local get_entity_name = function(rank)
return minetest.get_current_modname() .. ":xp" .. rank.xp
end
xp_redo.create_entity_properties = function(rank)
return {
visual = "cube",
visual_size = {x=0.5,y=0.5},
textures = {
rank.icon,
rank.icon,
rank.icon,
rank.icon,
rank.icon,
rank.icon
},
physical = false,
collide_with_objects = false,
pointable = false,
static_save = false
}
end
xp_redo.register_rank_entity = function(rank)
rank.entityname = get_entity_name(rank)
minetest.register_entity(rank.entityname, {
initial_properties = xp_redo.create_entity_properties(rank),
on_step = function(self)
if self.object.get_attach and not self.object:get_attach() then
self.object:remove()
end
end,
attached = nil
});
end
local player_data = {}
xp_redo.remove_rank_entity = function(player)
local playername = player:get_player_name()
local data = player_data[playername]
if not data then
return
end
local entity = data.entity
if entity then
entity:set_detach()
entity:remove()
end
player_data[playername] = nil
end
minetest.register_on_leaveplayer(function(player)
xp_redo.remove_rank_entity(player)
end)
xp_redo.update_rank_entity = function(player, rank)
if not player or not player:is_player() or not rank then
return
end
local state = player:get_meta():get(xp_redo.HUD_DISPLAY_STATE_NAME)
if state == "off" then return end
local playername = player:get_player_name()
local data = player_data[playername]
-- TODO: check new rank
if not data then
local pos = player:get_pos()
local entity = minetest.add_entity(pos, rank.entityname)
entity:set_attach(player, "", {x=0,y=25,z=0}, {x=0,y=0,z=0})
data = {
entity = entity,
rank = rank
}
player_data[playername] = data
end
if data.rank.xp ~= rank.xp then
-- rank changed, remove entity
xp_redo.remove_rank_entity(player)
end
end