-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapi.lua
More file actions
147 lines (119 loc) · 3.54 KB
/
Copy pathapi.lua
File metadata and controls
147 lines (119 loc) · 3.54 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
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
local S = emote.S
local facedir_to_look_horizontal = emote.util.facedir_to_look_horizontal
local vector_rotate_xz = emote.util.vector_rotate_xz
emote.emotes = {}
emote.attached_to_node = {}
emote.emoting = {}
-- API functions
function emote.register_emote(name, def)
emote.emotes[name] = def
core.register_chatcommand(name, {
description = S("Makes your character perform the @1 emote", name),
func = function(playername)
local player = core.get_player_by_name(playername)
if emote.start(player, name) then
if not emote.settings.announce_in_chat then
return true, S("You @1", name)
end
else
if not emote.settings.announce_in_chat then
return false, S("You failed to @1", name)
end
end
end,
})
end
function emote.start(player, emote_name)
if not core.is_player(player) then
emote.emoting[player] = nil
return
end
local emote_def = emote.emotes[emote_name]
if not emote_def then
return false
end
local player_name = player:get_player_name()
player_api.set_animation(player, emote_def.anim_name, emote_def.speed)
if emote_name == "stand" then
emote.emoting[player] = nil
player_api.player_attached[player_name] = nil
else
emote.emoting[player] = true
player_api.player_attached[player_name] = true
end
if emote_def.eye_offset then
player:set_eye_offset(emote_def.eye_offset, emote_def.eye_offset)
else
player:set_eye_offset()
end
if emote.settings.announce_in_chat then
core.chat_send_all(("* %s %s"):format(player_name, emote_def.description))
end
if emote_def.stop_after then
core.after(emote_def.stop_after, emote.stop, player)
end
return true
end
function emote.stop(player)
emote.start(player, "stand")
end
function emote.list()
local r = {}
for emote_name, _ in pairs(emote.emotes) do
table.insert(r, emote_name)
end
return r
end
function emote.attach_to_node(player, pos, locked)
local node = core.get_node(pos)
if node.name == "ignore" then
return false
end
if emote.attached_to_node[player] then
return
end
local def = core.registered_nodes[node.name].emote or {}
local emotedef = {
eye_offset = def.eye_offset or { x = 0, y = 1 / 2, z = 0 },
player_offset = def.player_offset or { x = 0, y = 0, z = 0 },
look_horizontal_offset = def.look_horizontal_offset or 0,
emotestring = def.emotestring or "sit",
}
local look_horizontal = facedir_to_look_horizontal(node.param2)
local offset = vector_rotate_xz(emotedef.player_offset, look_horizontal)
local rotation = look_horizontal + emotedef.look_horizontal_offset
local new_pos = vector.add(pos, offset)
emote.start(player, emotedef.emotestring)
if locked then
local object = core.add_entity(new_pos, "emote:attacher")
if object then
object:get_luaentity():init(player)
object:set_yaw(rotation)
player:set_attach(object, "", emotedef.eye_offset, core.facedir_to_dir(node.param2))
emote.attached_to_node[player] = object
end
else
player:set_pos(new_pos)
player:set_eye_offset(emotedef.eye_offset, { x = 0, y = 0, z = 0 })
end
player:set_look_horizontal(rotation)
end
function emote.attach_to_entity(player, emotestring, obj)
-- not implemented yet.
end
function emote.detach(player)
if emote.attached_to_node[player] then
emote.attached_to_node[player]:detach()
end
-- check if attached?
player:set_eye_offset(vector.new(), vector.new())
emote.stop(player)
end
core.register_globalstep(function()
for player in pairs(emote.emoting) do
local ctrl = player:get_player_control()
if ctrl and (ctrl.jump or ctrl.up or ctrl.down or ctrl.left or ctrl.right) then
emote.stop(player)
end
end
end)