dzkcc-mflow 0.0.1-beta.9 → 0.0.2

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/README.md CHANGED
@@ -1,11 +1,328 @@
1
1
  # Modular Flow Framework
2
- A modular design and process management framework developed for the cocos engine, suitable for decoupling and dependency injection
3
2
 
4
- ## Install
3
+ ## 1.1 框架概述
4
+
5
+ Cocos模块化流程框架(Modular Flow Framework)是一个为Cocos Creator引擎开发的模块化设计和流程管理框架。该框架旨在提供解耦和依赖注入的能力,帮助开发者构建更加清晰、可维护的游戏项目。
6
+
7
+ ### 1.2 核心特性
8
+
9
+ - **模块化设计**:通过Manager和Model模式实现业务逻辑的模块化管理
10
+ - **依赖注入**:通过装饰器实现自动依赖注入
11
+ - **服务定位器**:统一的服务管理机制
12
+ - **UI管理系统**:完整的UI界面管理方案
13
+ - **事件系统**:强大的事件广播和监听机制
14
+ - **资源加载系统**:统一的资源加载和释放管理
15
+ - **开发工具**:配套的Cocos Creator编辑器插件
16
+
17
+ ### 1.3 安装说明
5
18
 
6
19
  ```bash
7
- # Install dependent modules
8
- npm install
9
- # build
10
- npm run build
20
+ npm i dzkcc-mflow@beta
21
+ ```
22
+
23
+ 安装完成后,重启Cocos Creator引擎。
24
+
25
+ ## 2. 核心概念
26
+
27
+ ### 2.1 Core核心
28
+
29
+ Core是框架的核心,负责管理所有的Manager和Model实例。它继承自`AbstractCore`类,提供了注册和获取Manager/Model的接口。
30
+
31
+ ```typescript
32
+ // 自定义Core需要继承CocosCore
33
+ export class GameCore extends CocosCore { }
34
+ ```
35
+
36
+ 在场景中挂载GameCore组件即可初始化框架。
37
+
38
+ ### 2.2 ServiceLocator服务定位器
39
+
40
+ ServiceLocator用于管理跨领域的基础服务,如EventManager、ResLoader、UIManager等。
41
+
42
+ ```typescript
43
+ // 注册服务
44
+ ServiceLocator.regService('serviceKey', serviceInstance);
45
+
46
+ // 获取服务
47
+ const service = ServiceLocator.getService<ServiceType>('serviceKey');
48
+ ```
49
+
50
+ ### 2.3 Manager管理器
51
+
52
+ Manager负责管理业务领域内的具体实现,通常处理业务逻辑。Manager需要实现`IManager`接口。
53
+
54
+ ```typescript
55
+ export abstract class AbstractManager implements IManager {
56
+ abstract initialize(): void;
57
+ dispose(): void;
58
+ }
59
+ ```
60
+
61
+ ### 2.4 Model模型
62
+
63
+ Model用于数据管理,实现`IModel`接口。
64
+
65
+ ```typescript
66
+ export interface IModel {
67
+ initialize(): void;
68
+ }
69
+ ```
70
+
71
+ ### 2.5 装饰器系统
72
+
73
+ 框架提供了装饰器来简化Manager和Model的注册:
74
+
75
+ ```typescript
76
+ // 注册Manager
77
+ @manager()
78
+ export class GameManager extends AbstractManager {
79
+ // 实现逻辑
80
+ }
81
+
82
+ // 注册Model
83
+ @model()
84
+ export class GameModel implements IModel {
85
+ initialize(): void {
86
+ // 初始化逻辑
87
+ }
88
+ }
89
+ ```
90
+
91
+ ## 3. UI系统
92
+
93
+ ### 3.1 BaseView基础视图
94
+
95
+ 所有UI界面都应该继承`BaseView`类,它提供了以下特性:
96
+
97
+ - 自动事件监听管理(自动注册和注销)
98
+ - 自动资源加载管理(自动释放)
99
+ - 统一的生命周期方法
100
+
101
+ ```typescript
102
+ export abstract class BaseView extends Component implements IView {
103
+ abstract onPause(): void;
104
+ abstract onResume(): void;
105
+ abstract onEnter(args?: any): void;
106
+ onExit(): void;
107
+ }
108
+ ```
109
+
110
+ ### 3.2 UIManager界面管理器
111
+
112
+ UIManager负责管理UI界面的打开、关闭、层级等操作。
113
+
114
+ ```typescript
115
+ // 打开界面
116
+ const view = await mf.gui.open(ViewType, args);
117
+
118
+ // 关闭界面
119
+ mf.gui.close(viewInstance);
120
+
121
+ // 带分组的界面管理
122
+ const view = await mf.gui.openAndPush(ViewType, 'group', args);
123
+ mf.gui.closeAndPop('group');
124
+ ```
125
+
126
+ ## 4. 事件系统
127
+
128
+ ### 4.1 Broadcaster事件广播器
129
+
130
+ 框架提供了基于类型的事件系统,通过Broadcaster实现。
131
+
132
+ ```typescript
133
+ // 监听事件
134
+ mf.event.on('eventKey', (data) => {
135
+ // 处理事件
136
+ });
137
+
138
+ // 广播事件
139
+ mf.event.dispatch('eventKey', data);
140
+
141
+ // 一次性监听
142
+ mf.event.once('eventKey', (data) => {
143
+ // 只会触发一次
144
+ });
145
+ ```
146
+
147
+ ### 4.2 粘性事件
148
+
149
+ 粘性事件可以在没有监听者时暂存,等有监听者时再触发:
150
+
151
+ ```typescript
152
+ // 发送粘性事件
153
+ mf.event.dispatchSticky('stickyEvent', data);
154
+
155
+ // 移除粘性事件
156
+ mf.event.removeStickyBroadcast('stickyEvent');
11
157
  ```
158
+
159
+ ## 5. 资源加载系统
160
+
161
+ ### 5.1 ResLoader资源加载器
162
+
163
+ ResLoader提供了统一的资源加载和释放接口:
164
+
165
+ ```typescript
166
+ // 加载预制体
167
+ const prefab = await mf.res.loadPrefab('path/to/prefab');
168
+
169
+ // 加载精灵帧
170
+ const spriteFrame = await mf.res.loadSpriteFrame(spriteComponent, 'path/to/sprite');
171
+
172
+ // 加载Spine动画
173
+ const spineData = await mf.res.loadSpine(spineComponent, 'path/to/spine');
174
+
175
+ // 释放资源
176
+ mf.res.release('path/to/asset');
177
+ ```
178
+
179
+ ## 6. 开发工具
180
+
181
+ 框架配套了Cocos Creator编辑器插件`mflow-tools`,可以:
182
+
183
+ 1. 自动生成UI脚本
184
+ 2. 自动引用Prefab上需要操作的元素
185
+ 3. 自动挂载脚本组件
186
+
187
+ ### 6.1 使用方法
188
+
189
+ 1. 在Prefab中,将需要引用的节点重命名为`#属性名#组件类型`格式,例如:
190
+ - `#titleLabel#Label` 表示引用Label组件
191
+ - `#closeButton#Button` 表示引用Button组件
192
+ - `#contentNode` 表示引用Node节点
193
+
194
+ 2. 在Hierarchy面板中右键点击Prefab节点,选择"导出到脚本"
195
+
196
+ 3. 插件会自动生成基础脚本和业务脚本,并自动设置引用关系
197
+
198
+ ## 7. 完整示例
199
+
200
+ ### 7.1 创建Manager
201
+
202
+ ```typescript
203
+ @manager()
204
+ export class GameManager extends AbstractManager {
205
+ private _score: number = 0;
206
+
207
+ initialize(): void {
208
+ console.log('GameManager initialized');
209
+ }
210
+
211
+ get score(): number {
212
+ return this._score;
213
+ }
214
+
215
+ addScore(value: number): void {
216
+ this._score += value;
217
+ // 广播分数变化事件
218
+ this.getEventManager().dispatch('scoreChanged', this._score);
219
+ }
220
+ }
221
+ ```
222
+
223
+ ### 7.2 创建Model
224
+
225
+ ```typescript
226
+ @model()
227
+ export class GameModel implements IModel {
228
+ private _playerName: string = '';
229
+
230
+ initialize(): void {
231
+ console.log('GameModel initialized');
232
+ }
233
+
234
+ get playerName(): string {
235
+ return this._playerName;
236
+ }
237
+
238
+ set playerName(name: string) {
239
+ this._playerName = name;
240
+ }
241
+ }
242
+ ```
243
+
244
+ ### 7.3 创建UI界面
245
+
246
+ ```typescript
247
+ // BaseHomeView.ts (由工具自动生成)
248
+ import { _decorator, Component, Label, Button } from 'cc';
249
+ import { BaseView } from "dzkcc-mflow/libs";
250
+
251
+ const { ccclass, property, disallowMultiple } = _decorator;
252
+
253
+ @disallowMultiple()
254
+ export abstract class BaseHomeView extends BaseView {
255
+ /** @internal */
256
+ private static readonly __path__: string = "ui/home";
257
+
258
+ @property({ type: Label }) titleLabel: Label = null!;
259
+ @property({ type: Button }) startButton: Button = null!;
260
+ }
261
+
262
+ // HomeView.ts (业务实现)
263
+ import { BaseHomeView } from './BaseHomeView';
264
+ import { _decorator } from 'cc';
265
+
266
+ const { ccclass, property } = _decorator;
267
+
268
+ @ccclass('HomeView')
269
+ export class HomeView extends BaseHomeView {
270
+ onEnter(args?: any): void {
271
+ // 监听按钮点击
272
+ this.startButton.node.on(Button.EventType.CLICK, this.onStartClick, this);
273
+
274
+ // 监听分数变化
275
+ this.event.on('scoreChanged', this.onScoreChanged, this);
276
+ }
277
+
278
+ onExit(): void {
279
+ // BaseView会自动清理事件监听
280
+ }
281
+
282
+ onPause(): void {
283
+ // 界面暂停时的处理
284
+ }
285
+
286
+ onResume(): void {
287
+ // 界面恢复时的处理
288
+ }
289
+
290
+ private onStartClick(): void {
291
+ // 获取GameManager并调用方法
292
+ const gameManager = this.getManager(GameManager);
293
+ gameManager.addScore(10);
294
+ }
295
+
296
+ private onScoreChanged(score: number): void {
297
+ this.titleLabel.string = `分数: ${score}`;
298
+ }
299
+ }
300
+ ```
301
+
302
+ ### 7.4 在场景中使用
303
+
304
+ ```typescript
305
+ // 在游戏启动时
306
+ export class GameApp extends Component {
307
+ start(): void {
308
+ // 打开主界面
309
+ mf.gui.open(HomeView);
310
+ }
311
+ }
312
+ ```
313
+
314
+ ## 8. 最佳实践
315
+
316
+ 1. **模块化设计**:将相关的业务逻辑封装在对应的Manager中
317
+ 2. **数据驱动**:使用Model管理数据状态
318
+ 3. **事件解耦**:通过事件系统实现模块间通信
319
+ 4. **资源管理**:使用BaseView自动管理资源加载和释放
320
+ 5. **依赖注入**:使用装饰器简化依赖管理
321
+ 6. **工具辅助**:使用mflow-tools提高开发效率
322
+
323
+ ## 9. 注意事项
324
+
325
+ 1. 确保在使用框架功能前Core已经初始化
326
+ 2. 注意资源的正确加载和释放,避免内存泄漏
327
+ 3. 合理使用事件系统,避免事件监听过多影响性能
328
+ 4. 使用BaseView的子类时,确保正确实现所有抽象方法
@@ -5,8 +5,10 @@ const DefaultBundle = "resources";
5
5
  class ResLoader {
6
6
  loadAsset(path, type, nameOrUrl = DefaultBundle) {
7
7
  //TODO: bundle.release和assetManager.releaseAsset的区别?
8
+ //TODO: prefab是否需要addRef,prefab被克隆出来的节点被销毁时,对应的prefab如何处理?
8
9
  if (assetManager.assets.has(path)) {
9
10
  const asset = assetManager.assets.get(path);
11
+ asset.addRef();
10
12
  return Promise.resolve(asset);
11
13
  }
12
14
  return new Promise((resolve, reject) => {
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dzkcc-mflow",
3
- "version": "0.0.1-beta.9",
3
+ "version": "0.0.2",
4
4
  "description": "A modular design and process management framework developed for the cocos engine, suitable for decoupling and dependency injection.",
5
5
  "author": "duanzhk",
6
6
  "license": "MIT",
@@ -28,7 +28,7 @@
28
28
  "scripts": {
29
29
  "watch": "tsc -w",
30
30
  "build": "rm -rf dist && rollup -c && node ./scripts/build-tools.js",
31
- "pub-beta": "npm publish --tag beta",
31
+ "pub-beta": "npm version patch && npm publish --tag beta",
32
32
  "postinstall": "node ./scripts/postinstall.js"
33
33
  },
34
34
  "devDependencies": {
@@ -1,6 +1,6 @@
1
1
  import fs from 'fs';
2
2
  import path from 'path';
3
- import unzipper from 'unzipper';
3
+ // import unzipper from 'unzipper';
4
4
  import { execSync } from 'child_process';
5
5
  import { fileURLToPath } from 'url';
6
6
  import { dirname } from 'path';
@@ -9,20 +9,34 @@ import { dirname } from 'path';
9
9
  const __filename = fileURLToPath(import.meta.url);
10
10
  const __dirname = dirname(__filename);
11
11
 
12
- // 1. 创建 extensions 目录
12
+ // 1. 确保 unzipper 已安装
13
+ try {
14
+ execSync('npm install --save-dev unzipper', {
15
+ cwd: path.join( __dirname, '..'),
16
+ stdio: 'inherit'
17
+ });
18
+ } catch (e) {
19
+ console.error('安装 unzipper 失败:', e);
20
+ process.exit(1);
21
+ }
22
+
23
+ // 2. 动态导入 unzipper
24
+ const unzipper = await import('unzipper');
25
+
26
+ // 3. 创建 extensions 目录
13
27
  const extensionsDir = path.join(__dirname, '../../../extensions');
14
28
  if (!fs.existsSync(extensionsDir)) {
15
29
  fs.mkdirSync(extensionsDir);
16
30
  }
17
31
  console.log(`${extensionsDir} created`);
18
- // 2. 解压 mflow-tools.zip
32
+ // 4. 解压 mflow-tools.zip
19
33
  const zipPath = path.join(__dirname, '../dist/mflow-tools.zip');
20
34
  const extractPath = path.join(extensionsDir, 'mflow-tools');
21
35
 
22
36
  fs.createReadStream(zipPath)
23
37
  .pipe(unzipper.Extract({ path: extractPath }))
24
38
  .on('close', () => {
25
- // 3. 在解压目录中执行 npm install
39
+ // 5. 在解压目录中执行 npm install
26
40
  execSync('npm install', {
27
41
  cwd: extractPath,
28
42
  stdio: 'inherit'
package/README.zh-CN.md DELETED
@@ -1,11 +0,0 @@
1
- # Modular Flow Framework
2
- 模块化设计和流程管理,适合解耦和依赖注入的框架
3
-
4
- ## 安装
5
-
6
- ```bash
7
- # 安装依赖模块
8
- npm install
9
- # 构建
10
- npm run build
11
- ```