mm_os 3.3.1 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -201
- package/README.md +491 -99
- package/README_EN.md +498 -0
- package/adapter/adapter.js +431 -0
- package/adapter/custom_persistence.js +660 -0
- package/adapter/mqtt.js +273 -0
- package/adapter/socket.js +113 -0
- package/adapter/web.js +67 -0
- package/adapter/websocket.js +146 -0
- package/com/api/com.json +5 -0
- package/{core/com → com}/api/config.tpl.json +8 -8
- package/com/api/drive.js +708 -0
- package/com/api/index.js +198 -0
- package/com/api/oauth.js +200 -0
- package/com/api/script.tpl.js +32 -0
- package/com/cmd/README.md +11 -0
- package/com/cmd/com.json +5 -0
- package/com/cmd/config.tpl.json +122 -0
- package/com/cmd/drive.js +1548 -0
- package/com/cmd/index.js +1066 -0
- package/com/cmd/msg.json +48 -0
- package/com/cmd/nlp.js +525 -0
- package/com/cmd/script.tpl.js +32 -0
- package/com/db/com.json +5 -0
- package/com/db/drive.js +1999 -0
- package/com/db/index.js +242 -0
- package/{core/com → com}/event/README.md +4 -4
- package/com/event/com.json +5 -0
- package/{core/com → com}/event/config.tpl.json +18 -18
- package/com/event/drive.js +59 -0
- package/com/event/index.js +409 -0
- package/com/event/script.tpl.js +23 -0
- package/com/mqtt/com.json +5 -0
- package/{core/com → com}/mqtt/config.tpl.json +3 -5
- package/com/mqtt/drive.js +676 -0
- package/com/mqtt/index.js +822 -0
- package/com/mqtt/mm_mqtt.js +425 -0
- package/com/mqtt/script.tpl.js +723 -0
- package/com/nav/com.json +5 -0
- package/com/nav/config.tpl.json +84 -0
- package/com/nav/drive.js +702 -0
- package/com/nav/index.js +231 -0
- package/{core/com → com}/nav/tpl/admin_pc/page_config.vue +280 -280
- package/{core/com → com}/nav/tpl/admin_pc/page_config_form.vue +194 -194
- package/com/nav/tpl/admin_pc/page_form.vue +180 -0
- package/com/nav/tpl/admin_pc/page_view.vue +124 -0
- package/com/nav/tpl/dev_pc/page_default.vue +247 -0
- package/com/nav/tpl/dev_pc/page_type.vue +313 -0
- package/com/nav/tpl/home_pc/page_default.vue +234 -0
- package/com/nav/tpl/home_pc/page_form.vue +137 -0
- package/com/nav/tpl/home_pc/page_list.vue +234 -0
- package/com/nav/tpl/home_pc/page_nav.vue +221 -0
- package/com/nav/tpl/home_pc/page_type.vue +234 -0
- package/com/nav/tpl/home_pc/page_view.vue +125 -0
- package/com/nav/tpl/home_phone/page_channel.vue +234 -0
- package/com/nav/tpl/home_phone/page_default.vue +234 -0
- package/com/nav/tpl/home_phone/page_form.vue +137 -0
- package/com/nav/tpl/home_phone/page_nav.vue +237 -0
- package/com/nav/tpl/home_phone/page_type.vue +234 -0
- package/com/nav/tpl/home_phone/page_view.vue +125 -0
- package/com/nav/viewmodel.js +446 -0
- package/com/param/com.json +5 -0
- package/{core/com → com}/param/config.tpl.json +7 -1
- package/com/param/drive.js +502 -0
- package/com/param/index.js +155 -0
- package/com/param/script.tpl.js +12 -0
- package/com/pendant/com.json +5 -0
- package/{core/com/component → com/pendant}/config.tpl.json +15 -13
- package/com/pendant/drive.js +204 -0
- package/com/pendant/index.js +441 -0
- package/com/pendant/pendant.html +16 -0
- package/com/pendant/script.tpl.js +18 -0
- package/com/socket/com.json +5 -0
- package/com/socket/config.tpl.json +12 -0
- package/com/socket/drive.js +651 -0
- package/com/socket/index.js +351 -0
- package/com/socket/script.tpl.js +41 -0
- package/com/sql/com.json +5 -0
- package/{core/com → com}/sql/config.tpl.json +13 -9
- package/com/sql/drive.js +1259 -0
- package/com/sql/index.js +150 -0
- package/com/sql/script.tpl.js +47 -0
- package/com/static/com.json +5 -0
- package/{core/com → com}/static/config.tpl.json +10 -6
- package/com/static/drive.js +194 -0
- package/com/static/index.js +226 -0
- package/com/static/script.tpl.js +28 -0
- package/com/task/com.json +5 -0
- package/{core/com → com}/task/config.tpl.json +4 -6
- package/com/task/drive.js +405 -0
- package/com/task/index.js +148 -0
- package/com/task/script.tpl.js +37 -0
- package/com/template/com.json +5 -0
- package/com/template/config.tpl.json +16 -0
- package/com/template/drive.js +80 -0
- package/com/template/index.js +141 -0
- package/com.js +156 -0
- package/common/README.md +2 -0
- package/common/handler/msg/handler.json +22 -0
- package/common/handler/msg/index.js +23 -0
- package/common/handler/player/handler.json +22 -0
- package/common/handler/player/index.js +287 -0
- package/common/handler/user/handler.json +22 -0
- package/common/handler/user/index.js +23 -0
- package/common/middleware/web_after/index.js +29 -0
- package/common/middleware/web_after/middleware.json +9 -0
- package/common/middleware/web_base/index.js +113 -0
- package/common/middleware/web_base/middleware.json +19 -0
- package/common/middleware/web_before/index.js +33 -0
- package/common/middleware/web_before/middleware.json +9 -0
- package/common/middleware/web_cors/index.js +87 -0
- package/common/middleware/web_cors/middleware.json +24 -0
- package/common/middleware/web_error/index.js +119 -0
- package/common/middleware/web_error/middleware.json +18 -0
- package/common/middleware/web_ip/index.js +15 -0
- package/common/middleware/web_ip/middleware.json +14 -0
- package/common/middleware/web_logger/index.js +156 -0
- package/common/middleware/web_logger/middleware.json +14 -0
- package/common/middleware/web_main/index.js +24 -0
- package/common/middleware/web_main/middleware.json +9 -0
- package/common/middleware/web_static/index.js +73 -0
- package/common/middleware/web_static/middleware.json +54 -0
- package/common/middleware/web_waf/index.js +385 -0
- package/common/middleware/web_waf/middleware.json +13 -0
- package/common/model/msg/index.js +88 -0
- package/common/model/msg/model.json +401 -0
- package/common/model/player/index.js +63 -0
- package/common/model/player/model.json +185 -0
- package/common/model/user/index.js +11 -0
- package/common/model/user/model.json +219 -0
- package/core/app/config.tpl.json +67 -0
- package/core/app/index.js +632 -0
- package/core/app/script.tpl.js +52 -0
- package/core/channel/index.js +899 -0
- package/core/channel/matcher.js +585 -0
- package/core/com/config.tpl.json +16 -0
- package/core/com/index.js +74 -0
- package/core/com/script.tpl.js +5 -0
- package/core/component/component.js +42 -0
- package/core/component/config.tpl.json +63 -0
- package/core/component/index.js +273 -0
- package/core/component/script.tpl.js +19 -0
- package/core/controller/config.tpl.json +14 -0
- package/core/controller/index.js +373 -0
- package/core/controller/script.tpl.js +27 -0
- package/core/factory/config.tpl.json +14 -0
- package/core/factory/entity.js +275 -0
- package/core/factory/index.js +241 -0
- package/core/factory/script.tpl.js +16 -0
- package/core/game/bat/index.js +137 -0
- package/core/game/bat/world.js +622 -0
- package/core/game/config.tpl.json +16 -0
- package/core/game/entity_admin.js +230 -0
- package/core/game/index.js +186 -0
- package/core/handler/config.tpl.json +22 -0
- package/core/handler/index.js +181 -0
- package/core/handler/script.tpl.js +23 -0
- package/core/logic/config.tpl.json +14 -0
- package/core/logic/index.js +59 -0
- package/core/logic/script.tpl.js +19 -0
- package/core/middleware/config.tpl.json +16 -0
- package/core/middleware/index.js +125 -0
- package/core/middleware/script.tpl.js +37 -0
- package/core/mod/config.tpl.json +22 -0
- package/core/mod/index.js +130 -0
- package/core/mod/script.tpl.js +34 -0
- package/core/model/config.tpl.json +219 -0
- package/core/model/index.js +272 -0
- package/core/model/model.js +27 -0
- package/core/model/script.tpl.js +20 -0
- package/core/notifier/config.tpl.json +14 -0
- package/core/notifier/index.js +77 -0
- package/core/notifier/script.tpl.js +20 -0
- package/core/plugin/config.tpl.json +24 -0
- package/core/plugin/index.js +232 -0
- package/core/plugin/script.tpl.js +51 -0
- package/core/pusher/config.tpl.json +14 -0
- package/core/pusher/index.js +161 -0
- package/core/pusher/script.tpl.js +20 -0
- package/core/room/bat/index.js +170 -0
- package/core/room/bat/room.js +524 -0
- package/core/room/config.tpl.json +20 -0
- package/core/room/index.js +249 -0
- package/core/room/room.js +61 -0
- package/core/scene/config.tpl.json +14 -0
- package/core/scene/index.js +466 -0
- package/core/scene/loop.js +1255 -0
- package/core/scene/map.js +28 -0
- package/core/scene/script.tpl.js +22 -0
- package/core/sender/config.tpl.json +14 -0
- package/core/sender/index.js +79 -0
- package/core/sender/script.tpl.js +20 -0
- package/core/service/config.tpl.json +14 -0
- package/core/service/index.js +100 -0
- package/core/service/script.tpl.js +25 -0
- package/core/store/config.tpl.json +26 -0
- package/core/store/index.js +1755 -0
- package/core/store/script.tpl.js +22 -0
- package/core/store/sql.js +1464 -0
- package/core/system/config.tpl.json +18 -0
- package/core/system/index.js +312 -0
- package/core/system/script.tpl.js +77 -0
- package/core/view/config.tpl.json +14 -0
- package/core/view/index.js +91 -0
- package/core/view/script.tpl.js +20 -0
- package/core/zone/bat/index.js +725 -0
- package/core/zone/config.tpl.json +54 -0
- package/core/zone/index.js +614 -0
- package/core/zone/script.tpl.js +10 -0
- package/core/zone/zone_bat.js +136 -0
- package/core//345/237/272/347/261/273/346/250/241/345/235/227/346/270/205/345/215/225.md +24 -0
- package/index.js +17 -333
- package/os.js +57 -0
- package/package.json +58 -58
- package/server.js +598 -0
- package/README.en.md +0 -36
- package/conf.json +0 -3
- package/core/base/mqtt/index.js +0 -1110
- package/core/base/mqtt/lib.js +0 -40
- package/core/base/web/index.js +0 -245
- package/core/com/api/com.json +0 -4
- package/core/com/api/drive.js +0 -668
- package/core/com/api/index.js +0 -108
- package/core/com/api/oauth.js +0 -158
- package/core/com/api/script.js +0 -32
- package/core/com/app/README.md +0 -3
- package/core/com/app/com.json +0 -4
- package/core/com/app/config.tpl.json +0 -16
- package/core/com/app/drive.js +0 -309
- package/core/com/app/index.js +0 -211
- package/core/com/app/script.js +0 -155
- package/core/com/cmd/com.json +0 -4
- package/core/com/cmd/config.tpl.json +0 -66
- package/core/com/cmd/drive.js +0 -513
- package/core/com/cmd/index.js +0 -354
- package/core/com/cmd/old/5w2h.js +0 -54
- package/core/com/cmd/old/drive.js +0 -423
- package/core/com/cmd/script.js +0 -11
- package/core/com/component/README.md +0 -3
- package/core/com/component/com.json +0 -4
- package/core/com/component/component.html +0 -16
- package/core/com/component/drive.js +0 -197
- package/core/com/component/index.js +0 -312
- package/core/com/component/script.js +0 -18
- package/core/com/db/com.json +0 -4
- package/core/com/db/drive.js +0 -1160
- package/core/com/db/index.js +0 -176
- package/core/com/event/com.json +0 -4
- package/core/com/event/drive.js +0 -133
- package/core/com/event/index.js +0 -345
- package/core/com/event/script.js +0 -26
- package/core/com/eventer/com.js +0 -477
- package/core/com/eventer/com.json +0 -4
- package/core/com/middleware/com.js +0 -154
- package/core/com/middleware/com.json +0 -4
- package/core/com/middleware/config.tpl.json +0 -8
- package/core/com/middleware/script.js +0 -9
- package/core/com/mqtt/com.json +0 -4
- package/core/com/mqtt/drive.js +0 -600
- package/core/com/mqtt/index.js +0 -572
- package/core/com/mqtt/mm_mqtt.js +0 -330
- package/core/com/mqtt/script.js +0 -604
- package/core/com/msg/com.js +0 -296
- package/core/com/msg/com.json +0 -4
- package/core/com/nav/com.json +0 -4
- package/core/com/nav/config.tpl.json +0 -75
- package/core/com/nav/drive.js +0 -549
- package/core/com/nav/index.js +0 -182
- package/core/com/nav/tpl/admin_pc/page_form.vue +0 -180
- package/core/com/nav/tpl/admin_pc/page_view.vue +0 -124
- package/core/com/nav/tpl/dev_pc/page_default.vue +0 -247
- package/core/com/nav/tpl/dev_pc/page_type.vue +0 -313
- package/core/com/nav/tpl/home_pc/page_default.vue +0 -234
- package/core/com/nav/tpl/home_pc/page_form.vue +0 -137
- package/core/com/nav/tpl/home_pc/page_list.vue +0 -234
- package/core/com/nav/tpl/home_pc/page_nav.vue +0 -221
- package/core/com/nav/tpl/home_pc/page_type.vue +0 -234
- package/core/com/nav/tpl/home_pc/page_view.vue +0 -125
- package/core/com/nav/tpl/home_phone/page_channel.vue +0 -234
- package/core/com/nav/tpl/home_phone/page_default.vue +0 -234
- package/core/com/nav/tpl/home_phone/page_form.vue +0 -137
- package/core/com/nav/tpl/home_phone/page_nav.vue +0 -237
- package/core/com/nav/tpl/home_phone/page_type.vue +0 -234
- package/core/com/nav/tpl/home_phone/page_view.vue +0 -125
- package/core/com/nav/viewmodel.js +0 -296
- package/core/com/param/drive.js +0 -366
- package/core/com/param/index.js +0 -80
- package/core/com/param/script.js +0 -12
- package/core/com/param/test.js +0 -98
- package/core/com/plugin/README.md +0 -3
- package/core/com/plugin/com.json +0 -4
- package/core/com/plugin/config.tpl.json +0 -26
- package/core/com/plugin/drive.js +0 -536
- package/core/com/plugin/index.js +0 -259
- package/core/com/plugin/script.js +0 -213
- package/core/com/rpc/com.json +0 -4
- package/core/com/rpc/drive.js +0 -160
- package/core/com/rpc/index.js +0 -87
- package/core/com/rpc/rpc.js +0 -118
- package/core/com/socket/com.json +0 -4
- package/core/com/socket/config.tpl.json +0 -14
- package/core/com/socket/drive.js +0 -403
- package/core/com/socket/index.js +0 -62
- package/core/com/socket/script.js +0 -42
- package/core/com/sql/drive.js +0 -1087
- package/core/com/sql/index.js +0 -83
- package/core/com/sql/script.js +0 -48
- package/core/com/static/com.json +0 -4
- package/core/com/static/drive.js +0 -220
- package/core/com/static/index.js +0 -149
- package/core/com/static/script.js +0 -28
- package/core/com/task/com.json +0 -4
- package/core/com/task/drive.js +0 -403
- package/core/com/task/index.js +0 -110
- package/core/com/task/script.js +0 -37
- package/core/com/timer/com.js +0 -217
- package/core/com/timer/com.json +0 -4
- package/core/com/tpl/com.js +0 -19
- package/core/com/tpl/com.json +0 -4
- package/lib/actions.js +0 -50
- package/lib/base.js +0 -361
- package/lib/com.js +0 -29
- package/lib/ref.js +0 -121
- package/middleware/cors/index.js +0 -119
- package/middleware/cors/middleware.json +0 -20
- package/middleware/csrf/index.js +0 -202
- package/middleware/csrf/middleware.json +0 -24
- package/middleware/ip_firewall/index.js +0 -476
- package/middleware/ip_firewall/middleware.json +0 -109
- package/middleware/mqtt_base/index.js +0 -10
- package/middleware/mqtt_base/middleware.json +0 -11
- package/middleware/security_audit/index.js +0 -543
- package/middleware/security_audit/middleware.json +0 -48
- package/middleware/waf/index.js +0 -343
- package/middleware/waf/middleware.json +0 -10
- package/middleware/waf_ddos/index.js +0 -520
- package/middleware/waf_ddos/middleware.json +0 -38
- package/middleware/waf_xss/index.js +0 -269
- package/middleware/waf_xss/middleware.json +0 -18
- package/middleware/web_after/index.js +0 -33
- package/middleware/web_after/middleware.json +0 -10
- package/middleware/web_base/index.js +0 -90
- package/middleware/web_base/middleware.json +0 -10
- package/middleware/web_before/index.js +0 -27
- package/middleware/web_before/middleware.json +0 -10
- package/middleware/web_check/index.js +0 -28
- package/middleware/web_check/middleware.json +0 -10
- package/middleware/web_main/index.js +0 -28
- package/middleware/web_main/middleware.json +0 -10
- package/middleware/web_proxy/index.js +0 -37
- package/middleware/web_proxy/middleware.json +0 -10
- package/middleware/web_render/index.js +0 -87
- package/middleware/web_render/middleware.json +0 -10
- package/middleware/web_socket/index.js +0 -34
- package/middleware/web_socket/middleware.json +0 -10
- package/middleware/web_static/index.js +0 -115
- package/middleware/web_static/middleware.json +0 -10
- /package/{core/com → com}/api/README.md +0 -0
- /package/{core/com → com}/db/README.md +0 -0
- /package/{core/com → com}/mqtt/README.md +0 -0
- /package/{core/com → com}/nav/README.md +0 -0
- /package/{core/com → com}/nav/tpl/admin_pc/page_channel.vue +0 -0
- /package/{core/com → com}/nav/tpl/admin_pc/page_default.vue +0 -0
- /package/{core/com → com}/nav/tpl/admin_pc/page_lang.vue +0 -0
- /package/{core/com → com}/nav/tpl/admin_pc/page_nav.vue +0 -0
- /package/{core/com → com}/nav/tpl/admin_pc/page_table.vue +0 -0
- /package/{core/com → com}/nav/tpl/admin_pc/page_type.vue +0 -0
- /package/{core/com → com}/nav/tpl/dev_pc/page_channel.vue +0 -0
- /package/{core/com → com}/nav/tpl/dev_pc/page_config.vue +0 -0
- /package/{core/com → com}/nav/tpl/dev_pc/page_form.vue +0 -0
- /package/{core/com → com}/nav/tpl/dev_pc/page_nav.vue +0 -0
- /package/{core/com → com}/nav/tpl/dev_pc/page_table.vue +0 -0
- /package/{core/com → com}/nav/tpl/home_pc/page_channel.vue +0 -0
- /package/{core/com → com}/nav/tpl/home_phone/page_list.vue +0 -0
- /package/{core/com → com}/param/README.md +0 -0
- /package/{core/com/cmd → com/pendant}/README.md +0 -0
- /package/{core/com → com}/socket/README.md +0 -0
- /package/{core/com → com}/sql/README.md +0 -0
- /package/{core/com → com}/static/README.md +0 -0
- /package/{core/com → com}/task/README.md +0 -0
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
const {
|
|
2
|
+
Drive
|
|
3
|
+
} = require('mm_machine');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 场景基类
|
|
7
|
+
* 负责管理游戏场景中的实体和图层,处理场景生命周期
|
|
8
|
+
* 场景是Entity实例的容器,管理实体的生命周期
|
|
9
|
+
*/
|
|
10
|
+
class Scene extends Drive {
|
|
11
|
+
static config = {
|
|
12
|
+
// 场景名称
|
|
13
|
+
name: 'default',
|
|
14
|
+
// 场景描述
|
|
15
|
+
desc: '',
|
|
16
|
+
// 场景优先级
|
|
17
|
+
priority: 0,
|
|
18
|
+
// 是否激活场景
|
|
19
|
+
active: true
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* 构造函数
|
|
23
|
+
* @param {object} config 配置参数
|
|
24
|
+
*/
|
|
25
|
+
constructor(config, parent) {
|
|
26
|
+
super({ ...Scene.config, ...config || {} }, parent);
|
|
27
|
+
// 系统实例集合,主要调用场景系统SceneSystem
|
|
28
|
+
this._system = {};
|
|
29
|
+
// 图层映射集合
|
|
30
|
+
this._layers = {};
|
|
31
|
+
// 地图实例集合
|
|
32
|
+
this.map = {};
|
|
33
|
+
// /**
|
|
34
|
+
// * 地图集合
|
|
35
|
+
// * @type {object} 地图对象
|
|
36
|
+
// */
|
|
37
|
+
// this.maps = new Map();
|
|
38
|
+
// // 实体集合
|
|
39
|
+
// this._entities = new Map();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 预设
|
|
45
|
+
*/
|
|
46
|
+
Scene.prototype._preset = function () {
|
|
47
|
+
// 是否激活场景
|
|
48
|
+
this.active = this.config.active;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 获取模板目录
|
|
53
|
+
* @returns {string} 模板目录
|
|
54
|
+
*/
|
|
55
|
+
Scene.prototype.getTplDir = function () {
|
|
56
|
+
return __dirname;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 初始化核心
|
|
61
|
+
* @override
|
|
62
|
+
* @param {object} system 系统对象
|
|
63
|
+
* @param {object} eventer 事件对象
|
|
64
|
+
* @param {object} logger 日志对象
|
|
65
|
+
* @returns {Promise<void>}
|
|
66
|
+
*/
|
|
67
|
+
Scene.prototype._initCore = async function (system, eventer, logger) {
|
|
68
|
+
// 初始化依赖项
|
|
69
|
+
if (logger) {
|
|
70
|
+
this.setLogger(logger);
|
|
71
|
+
}
|
|
72
|
+
if (eventer) {
|
|
73
|
+
this.getEventer = function () {
|
|
74
|
+
return eventer;
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
if (system) {
|
|
78
|
+
this._system = system;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// 初始化场景
|
|
82
|
+
await this._loadSources();
|
|
83
|
+
// 初始化地图
|
|
84
|
+
await this._initMap();
|
|
85
|
+
// 初始化实体
|
|
86
|
+
await this._initEntities();
|
|
87
|
+
|
|
88
|
+
// 触发场景加载事件
|
|
89
|
+
this.emitEvent('scene_load', {
|
|
90
|
+
scene_id: this.scene_id,
|
|
91
|
+
scene_name: this.config.name,
|
|
92
|
+
timestamp: Date.now()
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 初始化地图
|
|
98
|
+
* @private
|
|
99
|
+
*/
|
|
100
|
+
Scene.prototype._initMap = async function () {
|
|
101
|
+
// 子类应重写此方法初始化地图
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* 加载场景资源
|
|
106
|
+
* @private
|
|
107
|
+
* @returns {Promise<void>}
|
|
108
|
+
*/
|
|
109
|
+
Scene.prototype._loadSources = async function () {
|
|
110
|
+
// 子类应重写此方法加载场景所需资源
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* 添加实体到场景
|
|
115
|
+
* @param {object} entity 实体对象
|
|
116
|
+
* @returns {boolean} 添加是否成功
|
|
117
|
+
*/
|
|
118
|
+
Scene.prototype.addEntity = function (entity) {
|
|
119
|
+
// 参数校验
|
|
120
|
+
if (!entity || typeof entity !== 'object') {
|
|
121
|
+
this.log('error', '实体必须是非空对象');
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// 检查实体是否已存在
|
|
126
|
+
if (this._entities.has(entity.entity_id)) {
|
|
127
|
+
return true; // 已存在,返回成功
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// 添加实体
|
|
131
|
+
this._entities.set(entity.entity_id, entity);
|
|
132
|
+
|
|
133
|
+
// 设置实体的场景引用
|
|
134
|
+
if (entity.setScene && typeof entity.setScene === 'function') {
|
|
135
|
+
entity.setScene(this);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
this.log('info', `实体 [${entity.entity_id}] 添加到场景`);
|
|
139
|
+
|
|
140
|
+
return true;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* 从场景移除实体
|
|
145
|
+
* @param {object|string} entity 实体对象或实体ID
|
|
146
|
+
* @returns {boolean} 移除是否成功
|
|
147
|
+
*/
|
|
148
|
+
Scene.prototype.delEntity = function (entity) {
|
|
149
|
+
// 参数校验
|
|
150
|
+
if (!entity) {
|
|
151
|
+
this.log('error', '实体参数不能为空');
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// 如果是ID,直接从Map中移除
|
|
156
|
+
if (typeof entity === 'string') {
|
|
157
|
+
if (!this._entities.has(entity)) {
|
|
158
|
+
return false; // 实体不存在
|
|
159
|
+
}
|
|
160
|
+
this._entities.delete(entity);
|
|
161
|
+
this.log('info', `实体 [${entity}] 从场景移除`);
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// 如果是对象,使用实体ID移除
|
|
166
|
+
if (!entity.entity_id || !this._entities.has(entity.entity_id)) {
|
|
167
|
+
return false; // 实体不存在于场景中
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
this._entities.delete(entity.entity_id);
|
|
171
|
+
|
|
172
|
+
this.log('info', `实体 [${entity.entity_id}] 从场景移除`);
|
|
173
|
+
|
|
174
|
+
return true;
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* 根据ID或属性查找实体
|
|
179
|
+
* @param {string | number | object} query 查询条件
|
|
180
|
+
* @returns {object|null} 找到的实体或null
|
|
181
|
+
*/
|
|
182
|
+
Scene.prototype.findEntity = function (query) {
|
|
183
|
+
if (!query) {
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (typeof query === 'string' || typeof query === 'number') {
|
|
188
|
+
return this._findById(query);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (query.prototype && typeof query === 'function') {
|
|
192
|
+
return this._findByType(query);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (typeof query === 'object') {
|
|
196
|
+
return this._findByProps(query);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return null;
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* 根据ID查找实体
|
|
204
|
+
* @param {string | number} id 实体ID
|
|
205
|
+
* @returns {object|null} 找到的实体或null
|
|
206
|
+
*/
|
|
207
|
+
Scene.prototype._findById = function (id) {
|
|
208
|
+
return this._entities.get(id) || null;
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* 根据构造函数类型查找实体
|
|
213
|
+
* @param {Function} type 构造函数
|
|
214
|
+
* @returns {object|null} 找到的实体或null
|
|
215
|
+
*/
|
|
216
|
+
Scene.prototype._findByType = function (type) {
|
|
217
|
+
for (let entity of this._entities.values()) {
|
|
218
|
+
if (entity instanceof type) {
|
|
219
|
+
return entity;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return null;
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* 根据属性对象查找实体
|
|
227
|
+
* @param {object} props 属性对象
|
|
228
|
+
* @returns {object|null} 找到的实体或null
|
|
229
|
+
*/
|
|
230
|
+
Scene.prototype._findByProps = function (props) {
|
|
231
|
+
for (let entity of this._entities.values()) {
|
|
232
|
+
if (this._matchProps(entity, props)) {
|
|
233
|
+
return entity;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return null;
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* 检查实体属性是否匹配
|
|
241
|
+
* @param {object} entity 实体
|
|
242
|
+
* @param {object} props 属性对象
|
|
243
|
+
* @returns {boolean} 是否匹配
|
|
244
|
+
*/
|
|
245
|
+
Scene.prototype._matchProps = function (entity, props) {
|
|
246
|
+
for (let key in props) {
|
|
247
|
+
if (entity[key] !== props[key]) {
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return true;
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* 查找符合条件的所有实体
|
|
256
|
+
* @param {object} query 查询条件
|
|
257
|
+
* @returns {Array} 符合条件的实体数组
|
|
258
|
+
*/
|
|
259
|
+
Scene.prototype.findEntities = function (query = {}) {
|
|
260
|
+
if (!query || typeof query !== 'object') {
|
|
261
|
+
return [...this._entities.values()];
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
let result = [];
|
|
265
|
+
for (let entity of this._entities.values()) {
|
|
266
|
+
let match = true;
|
|
267
|
+
for (let key in query) {
|
|
268
|
+
if (entity[key] !== query[key]) {
|
|
269
|
+
match = false;
|
|
270
|
+
break;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
if (match) {
|
|
274
|
+
result.push(entity);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return result;
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* 添加图层
|
|
282
|
+
* @param {string} name 图层名称
|
|
283
|
+
* @param {object} layer 图层对象
|
|
284
|
+
* @returns {boolean} 添加是否成功
|
|
285
|
+
*/
|
|
286
|
+
Scene.prototype.addLayer = function (name, layer) {
|
|
287
|
+
// 参数校验
|
|
288
|
+
if (!name || typeof name !== 'string') {
|
|
289
|
+
this.log('error', '图层名称必须是非空字符串');
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (!layer || typeof layer !== 'object') {
|
|
294
|
+
this.log('error', '图层对象必须是非空对象');
|
|
295
|
+
return false;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// 添加图层
|
|
299
|
+
this._layers[name] = layer;
|
|
300
|
+
|
|
301
|
+
return true;
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* 获取图层
|
|
306
|
+
* @param {string} name 图层名称
|
|
307
|
+
* @returns {object | null} 图层对象或null
|
|
308
|
+
*/
|
|
309
|
+
Scene.prototype.getLayer = function (name) {
|
|
310
|
+
if (!name || typeof name !== 'string') {
|
|
311
|
+
this.log('error', '图层名称必须是非空字符串');
|
|
312
|
+
return null;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
return this._layers[name] || null;
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* 更新场景
|
|
320
|
+
* @param {number} delta 时间间隔(毫秒)
|
|
321
|
+
*/
|
|
322
|
+
Scene.prototype.set = function (delta) {
|
|
323
|
+
// 如果场景不活跃,不更新
|
|
324
|
+
if (!this.active) {
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// 更新所有实体
|
|
329
|
+
for (let entity of this._entities.values()) {
|
|
330
|
+
try {
|
|
331
|
+
if (entity.update && typeof entity.update === 'function') {
|
|
332
|
+
entity.update(delta);
|
|
333
|
+
}
|
|
334
|
+
} catch (error) {
|
|
335
|
+
this.log('error', '更新实体时出错:', error);
|
|
336
|
+
// 可以选择移除出错的实体
|
|
337
|
+
// this.delEntity(entity);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* 渲染场景
|
|
344
|
+
*/
|
|
345
|
+
Scene.prototype.render = function () {
|
|
346
|
+
// 如果场景不活跃,不渲染
|
|
347
|
+
if (!this.active) {
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// 渲染所有图层
|
|
352
|
+
Object.values(this._layers).forEach(layer => {
|
|
353
|
+
try {
|
|
354
|
+
if (layer.render && typeof layer.render === 'function') {
|
|
355
|
+
layer.render();
|
|
356
|
+
}
|
|
357
|
+
} catch (error) {
|
|
358
|
+
this.log('error', '渲染图层时出错:', error);
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* 清理场景资源
|
|
365
|
+
* @override
|
|
366
|
+
*/
|
|
367
|
+
Scene.prototype._destroy = function () {
|
|
368
|
+
try {
|
|
369
|
+
// 触发场景卸载事件
|
|
370
|
+
this.emitEvent('scene_unload', {
|
|
371
|
+
scene_id: this.scene_id,
|
|
372
|
+
scene_name: this.config.name,
|
|
373
|
+
timestamp: Date.now()
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
// 清理所有实体
|
|
377
|
+
for (let entity of this.map._entities.values()) {
|
|
378
|
+
try {
|
|
379
|
+
if (entity.destroy && typeof entity.destroy === 'function') {
|
|
380
|
+
entity.destroy();
|
|
381
|
+
}
|
|
382
|
+
} catch (error) {
|
|
383
|
+
this.log('error', '销毁实体时出错:', error);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// 清空引用
|
|
388
|
+
this.map._entities = null;
|
|
389
|
+
} catch (error) {
|
|
390
|
+
this.log('error', '场景销毁失败:', error);
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* 实体进入场景
|
|
396
|
+
* @param {object} entity 实体对象
|
|
397
|
+
* @returns {boolean} 是否成功进入
|
|
398
|
+
*/
|
|
399
|
+
Scene.prototype.enter = async function (entity) {
|
|
400
|
+
// 参数校验
|
|
401
|
+
if (!entity || typeof entity !== 'object') {
|
|
402
|
+
throw new TypeError('实体对象必须是非空对象');
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// 添加实体到场景
|
|
406
|
+
this._entities.set(entity.entity_id, entity);
|
|
407
|
+
|
|
408
|
+
// 触发实体进入场景事件(如果事件总线存在)
|
|
409
|
+
this.emitEvent('entity_enter_scene', {
|
|
410
|
+
scene_id: this.scene_id,
|
|
411
|
+
scene_name: this.config.name,
|
|
412
|
+
entity_id: entity.entity_id,
|
|
413
|
+
timestamp: Date.now()
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
return true;
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* 实体离开场景
|
|
421
|
+
* @param {object} entity 实体对象
|
|
422
|
+
* @returns {boolean} 是否成功离开
|
|
423
|
+
*/
|
|
424
|
+
Scene.prototype.leave = async function (entity) {
|
|
425
|
+
// 参数校验
|
|
426
|
+
if (!entity || typeof entity !== 'object') {
|
|
427
|
+
throw new TypeError('实体对象必须是非空对象');
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// 从场景移除实体
|
|
431
|
+
if (!entity.entity_id || !this._entities.has(entity.entity_id)) {
|
|
432
|
+
throw new Error('实体不在场景中');
|
|
433
|
+
}
|
|
434
|
+
this._entities.delete(entity.entity_id);
|
|
435
|
+
|
|
436
|
+
// 触发实体离开场景事件(如果事件总线存在)
|
|
437
|
+
this.emitEvent('entity_leave_scene', {
|
|
438
|
+
scene_id: this.scene_id,
|
|
439
|
+
scene_name: this.config.name,
|
|
440
|
+
entity_id: entity.entity_id,
|
|
441
|
+
timestamp: Date.now()
|
|
442
|
+
});
|
|
443
|
+
return true;
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* 更新场景
|
|
448
|
+
* @param {number} delta 时间间隔(毫秒)
|
|
449
|
+
*/
|
|
450
|
+
Scene.prototype.update = function (delta) {
|
|
451
|
+
// 如果场景不活跃,不更新
|
|
452
|
+
if (!this.isRunning()) {
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// 触发场景更新事件
|
|
457
|
+
this.emitEvent('scene_update', {
|
|
458
|
+
scene_id: this.scene_id,
|
|
459
|
+
scene_name: this.config.name,
|
|
460
|
+
delta: delta,
|
|
461
|
+
timestamp: Date.now()
|
|
462
|
+
});
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
// 导出模块
|
|
466
|
+
exports.Scene = Scene;
|