Lua注释

通过如下方式编写注释来实现不同事件的智能提示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
---@class Player
---@field event fun(self: Player, name: '事件1', callback: fun(u: cls_unit, data: event_data_1))
---@field event fun(self: Player, name: '事件2', callback: fun(u: cls_unit, data: event_data_2))
---@field event fun(self: Player, name: '事件3', callback: fun(u: cls_unit, data: event_data_3))
---@field event fun(self: Player, name: '事件4', callback: fun(u: cls_unit, data: event_data_4))
---@field event fun(self: Player, name: '事件5', callback: fun(u: cls_unit, data: event_data_5))
local Player = {}

---@class event_data_1
---@class event_data_2
---@class event_data_3
---@class event_data_4
---@class event_data_5

---@type Player
local pp = {}

pp:event('事件4', function (u, data)

end)

通过如下方式编写注释来实现不同属性的智能提示。

1
2
3
4
5
6
7
8
9
plr:inc_attr('@攻击力', 100)
plr:inc_attr('@护甲', 100)
plr:inc_attr('@法术强度', 100)

---@class Player
---@field inc_attr fun(self: Player, name: '@攻击力', value: integer): integer
---@field inc_attr fun(self: Player, name: '@护甲', value: integer): integer
---@field inc_attr fun(self: Player, name: '@法术强度', value: integer): integer
local Player = {}

更实际的例子。

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

---@class event_util
---@field register fun(event_name: '单位-冷却普攻', which_unit: cls_unit, label: string, cooldown: number, callback: fun(u2: cls_unit))
local event_util = {}

local event_handlers = {

---@param which_unit cls_unit
---@param label string
---@param cooldown number
---@param callback fun(u2: cls_unit)
['单位-冷却普攻'] = function(which_unit, label, cooldown, callback)
local o = {}

msg = which_unit:on(enums.umsg.unit_damage_calc, function(_, u2, event_data)
---@cast event_data damage_event_data

if is_first_normal_attack(event_data) then
which_unit:do_cool_action(label, cooldown, function()
callback(u2)
end)
end
end)

local function stop()
if msg then
which_unit:off(enums.umsg.unit_damage_calc, msg)
msg = nil
end
end

o.msg = msg
o.stop = stop

return o
end,

}

function event_util.register(event_name, ...)
event_handlers(event_name, ...)
end

function event_util.test()
event_util.register('单位-冷却普攻', 'Yxsk127', 10, function(u2)
on_trigger(u2)
end)
end