forked from OgelGames/bones
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwaypoints.lua
More file actions
59 lines (54 loc) · 1.73 KB
/
waypoints.lua
File metadata and controls
59 lines (54 loc) · 1.73 KB
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
local S = core.get_translator("bones")
local function add_to_hud(player, waypoint)
waypoint.id = player:hud_add({
[core.features.hud_def_type_field and "type" or "hud_elem_type"] = "waypoint",
name = S("Bones"),
text = "m",
number = 0xFFFFFF,
world_pos = waypoint.pos
})
-- Remove the waypoint after some time
local name = player:get_player_name()
core.after(waypoint.expiry - os.time(), function()
player = core.get_player_by_name(name)
if player then
bones.remove_waypoint(waypoint.pos, player)
end
end)
end
function bones.add_waypoint(pos, player)
local meta = player:get_meta()
local waypoints = core.deserialize(meta:get_string("bone_waypoints")) or {}
local pos_str = core.pos_to_string(pos):gsub("-0", "0")
if not waypoints[pos_str] then
waypoints[pos_str] = {
pos = pos,
expiry = os.time() + bones.waypoint_time,
}
end
add_to_hud(player, waypoints[pos_str])
meta:set_string("bone_waypoints", core.serialize(waypoints))
end
function bones.remove_waypoint(pos, player)
local meta = player:get_meta()
local waypoints = core.deserialize(meta:get_string("bone_waypoints")) or {}
local pos_str = core.pos_to_string(pos):gsub("-0", "0")
if waypoints[pos_str] then
player:hud_remove(waypoints[pos_str].id)
waypoints[pos_str] = nil
meta:set_string("bone_waypoints", core.serialize(waypoints))
end
end
core.register_on_joinplayer(function(player)
local meta = player:get_meta()
local waypoints = core.deserialize(meta:get_string("bone_waypoints")) or {}
local current_time = os.time()
for pos_str, waypoint in pairs(waypoints) do
if current_time < waypoint.expiry then
add_to_hud(player, waypoint)
else
waypoints[pos_str] = nil
end
end
meta:set_string("bone_waypoints", core.serialize(waypoints))
end)