-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateTransfer.lua
More file actions
123 lines (107 loc) · 2.47 KB
/
StateTransfer.lua
File metadata and controls
123 lines (107 loc) · 2.47 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
-- 状态转换条件 Hub
-- @author: yangfch3
-- @date: 2021/04/27 11:06
------------------------------------------
-- 条件判断函数
-- 传入一个基准值,返回一个利用运行值基准值进行判断的函数
-- 基准值 - 在创建 Layout 里的 ST 时传入
-- 运行值 - 在 StateMachine 里调用 ST 判断时传入
local function equal(a, def)
assert(a ~= nil)
return function(b)
return a == b
end
end
local function less(a, def)
assert(a ~= nil)
return function(b)
b = b or def or 0
return b < a
end
end
local function eLess(a, def)
assert(a ~= nil)
return function(b)
b = b or def or 0
return b <= a
end
end
local function above(a, def)
assert(a ~= nil)
return function(b)
b = b or def or 0
return b > a
end
end
local function eAbove(a, def)
assert(a ~= nil)
return function(b)
b = b or def or 0
return b >= a
end
end
local function is_true()
return equal(true, false)
end
local function is_false()
return equal(false, true)
end
local function trigger()
return equal(true, false)
end
--- State Transfer 包装方法
local function st_and(condList)
return function(dictTransfer)
for name, cond in ipairs(condList) do
local pass = cond(dictTransfer[name], dictTransfer)
if not pass then
print("cond failed", name)
return false
end
end
return true
end
end
local function st_or(condList)
return function(dictTransfer)
for name, cond in ipairs(condList) do
local pass = cond(dictTransfer[name])
if pass then
return true
end
end
return false
end
end
local function st_not(condList)
return function(dictTransfer)
for name, cond in ipairs(condList) do
local pass = cond(dictTransfer[name])
if pass then
print("cond failed", name)
return false
end
end
return true
end
end
local function st(cond)
local name, func = next(cond)
return function(dictTransfer)
return func(dictTransfer[name])
end
end
return {
equal = equal,
less = less,
eLess = eLess,
above = above,
eAbove = eAbove,
is_true = is_true,
is_false = is_false,
trigger = trigger,
st_and = st_and,
st_or = st_or,
st_not = st_not,
st = st,
}