This repository was archived by the owner on Aug 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBehaviorLib.js
More file actions
269 lines (262 loc) · 7.22 KB
/
BehaviorLib.js
File metadata and controls
269 lines (262 loc) · 7.22 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// ==UserScript==
const gameConfig = {
type: "BehaviorLib",
title: "BehaviorLib",
doc: `EasyBox3Lib的附属库,用于控制对象行为,需要安装EasyBox3Lib。EasyBox3Lib的安装见帮助链接`,
help: "https://qndm.github.io/EasyBox3Lib",
file: true,
isClient: false
}
// ==UserScript==
/**
* BehaviorLib 库
* 用于控制实体/非实体行为的库
* 依赖EasyBox3Lib 0.1.6
* @author qndm
* @module BehaviorLib
* @version 0.0.6
* @license MIT
*/
/**
* 调用其他行为的函数
* @async
* @callback CallBehaviorCallback
* @param {string} behaviorId 要调用的行为id
* @param {any} data 传入行动的数据
* @returns {any} 行为返回结果
*/
/**
* 行为函数
* @async
* @callback BehaviorInitCallback
* @param {object} self 行为自身
* @param {any} data 行为数据。若不是`CallBehaviorCallback`中则为`null`
* @param {CallBehaviorCallback} callBehavior 调用其他行为
* @returns {any} 行为返回结果
*/
/**
* 行为组数据
* @typedef BehaviorGroupData
* @property {string} behavior 行为id
* @property {any} data id为`id`的行为的数据
*/
/**
* 行为目标对象行为
* @typedef BehaviorTargetBehavior
* @property {string} id 行为/行为组id
* @property {number} priority 行为优先级
* @property {boolean} disabled 是否禁用行为
*/
/**
* EasyBox3Lib
*/
const EBL = global.EasyBox3Lib,
/**
* 配置文件
*/
CONFIG = require('./EasyBox3Lib.config.js'),
/**
* 建议EasyBox3Lib版本
* @type {number[]}
*/
EBL_VERSION = [0, 1, 6],
/**
* 当前版本
* @type {number[]}
*/
VERSION = [0, 0, 6];
/**
* @type {Map<string, Behavior>}
*/
var behaviorRegistry = new Map();
if (EBL === undefined)
throw "EasyBox3Lib 未安装";
if (global.BehaviorLib)
throw "请勿重复加载 BehaviorLib"
if (EBL.version.toString() !== EBL_VERSION.toString())
EBL.output("warn", 'EasyBox3Lib 版本', EBL.version.join('.'), '建议', EBL_VERSION.join('.'));
/**
* 空值合并
* @param {*} a
* @param {*} b
* @returns {*}
*/
function nullc(a, b) {
if (a === null || a === undefined)
return b;
else return a;
}
// ----- BehaviorLib Start -----
/**
* 定义一种行为
* @param {string} 该行为的id,不可重复
* @param {BehaviorInitCallback} init 行为主函数
* @param {number} defaultPriority 行为默认优先级,越高越先执行
*/
function Behavior(id, init, defaultPriority = 1) {
/**
* 该行为的id,不可重复
* @type {string}
*/
this.id = id;
/**
* 行为主函数
* @type {BehaviorInitCallback}
*/
this.init = init;
/**
* 行为默认优先级,越高越先执行
* @type {number}
*/
this.defaultPriority = defaultPriority;
}
/**
* 行为目标对象
*/
class BehaviorTarget {
/**
* 行为目标自身
* @type {any}
*/
self = undefined;
/**
* 行为目标所具有的行为
* @type {BehaviorTargetBehavior[]}
*/
behaviors = [];
/**
* @type {EasyBox3Lib.onTickEventToken | undefined}
*/
onTickEvent = undefined;
/**
* 行为对象数据
*/
data = {};
constructor(self) {
this.self = self;
this.behaviors = [];
this.onTickEvent = undefined;
this.data = {};
}
async _runBehaviorTick() {
for (const behavior of this.behaviors) {
if (behavior.disabled)
continue;
await behaviorRegistry.get(behavior.id).init(this.self, null, this.callBehavior);
}
}
/**
* 查找行为在行为列表里的位置
* 如果不存在,返回`-1`
* @param {string} behavior 行为id
* @private
* @returns {number} 行为在行为列表里的位置
*/
_getBehaviorIndex(behavior) {
for (const i in this.behaviors) {
if (this.behaviors[i].id === behavior)
return i;
}
return -1;
}
/**
* 添加行为
* @param {string} behavior 行为id
* @param {number} priority 行为优先级
*/
addBehavior(behavior, priority = behavior.priority) {
if (this.hasBehavior(behavior))
EBL.thr("[BEHAVIOR_TARGET] 添加行为失败:行为已存在");
this.behaviors.push({ id: behavior, priority, disabled: false });
this.behaviors.sort(a, b => b.priority - a.priority);
}
/**
* 移除行为
* @param {string} behavior 行为id
*/
removeBehavior(behavior) {
if (!this.hasBehavior(behavior))
EBL.thr('[BEHAVIOR]', behavior, '行为不存在');
this.behaviors.splice(this._getBehaviorIndex(behavior), 1);
}
/**
* 对该对象启用行为
* @param {string} behavior 行为id
*/
enableBehavior(behavior) {
if (!this.hasBehavior(behavior))
EBL.thr('[BEHAVIOR]', behavior, '行为不存在');
this.behaviors[this._getBehaviorIndex(behavior)].disabled = false;
}
/**
* 对该对象禁用行为
* 禁用后,该对象将不会执行该行为
* @param {string} behavior 行为id
*/
disabledBehavior(behavior) {
if (!this.hasBehavior(behavior))
EBL.thr('[BEHAVIOR]', behavior, '行为不存在');
this.behaviors[this._getBehaviorIndex(behavior)].disabled = true;
}
/**
* 检查是否有某种行为
* @param {string} id 行为id
* @returns {boolean} 是否有行为
*/
hasBehavior(id) {
return this.behaviors.some(behavior => behavior.id == id);
}
/**
* 调用其他行为的函数
* @async
* @param {string} behaviorId 要调用的行为id
* @param {any} data 传入行为的数据
* @returns {any} 行为返回结果
*/
async callBehavior(behaviorId, data) {
await behaviorRegistry.get(behaviorId).init(this.self, data, this.callBehavior);
}
/**
* 为该对象的行为创建onTick
* @param {number} tpc 每循环执行次数
* @param {number} performanceImpact 性能影响程度
* @returns {EasyBox3Lib.onTickEventToken} 事件令牌
*/
createOnTickEvent(tpc = nullc(CONFIG.BehaviorLib.defaultTpc, 2), performanceImpact = nullc(CONFIG.BehaviorLib.defaultPerformanceImpact, 1)) {
this.onTickEvent = EBL.ot(async () => {
await this._runBehaviorTick();
}, tpc, performanceImpact);
return this.onTickEvent;
}
}
/**
* 注册一种行为
* @param {Behavior} behavior 要注册的行为
*/
function registerBehavior(behavior) {
if (!(behavior instanceof Behavior)) {
EBL.thr("[BEHAVIOR] 注册失败:未知行为类型");
}
if (behaviorRegistry.has(behavior.id)) {
EBL.thr("[BEHAVIOR] 注册失败:行为 " + behavior.id + " 已注册类型");
}
behaviorRegistry.set(behavior.id, behavior);
}
// ----- BehaviorLib End -----
EBL.regIndex(Behavior, registerBehavior)
const BehaviorLib = {
Behavior,
B: Behavior,
BehaviorTarget,
BT: BehaviorTarget,
registerBehavior,
regB: registerBehavior,
version: VERSION
};
/**
* BehaviorLib的全局对象
* @global
*/
global.BehaviorLib = BehaviorLib;
console.log("BehaviorLib", VERSION.join('.'));
module.exports = BehaviorLib;