@cc-component/cc-core 1.5.4 → 1.5.6

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.
@@ -10,17 +10,18 @@ import { Asset } from 'cc';
10
10
  import { instantiate } from 'cc';
11
11
 
12
12
  /** 特效参数 */
13
- export interface IEffectParams {
13
+ export interface IPoolParams {
14
14
  /**路径 */
15
15
  path: string;
16
16
  /**资源包名 */
17
- bundle: string;
17
+ bundle?: string;
18
18
  /**初始空间坐标 */
19
19
  pos?: Vec3,
20
20
  /**初始世界坐标 */
21
21
  worldPos?: Vec3,
22
- /** 是否播放完成后删除 */
23
- isPlayFinishedRelease?: boolean,
22
+ // /** 是否播放完成后删除 */
23
+ // isPlayFinishedRelease?: boolean,
24
+ parent?: Node
24
25
  }
25
26
 
26
27
  /**
@@ -78,7 +79,7 @@ export class EffectSingleCase {
78
79
  }
79
80
 
80
81
  /** 池中预加载显示对象 */
81
- preload(count: number, params: IEffectParams): Promise<void> {
82
+ preload(count: number, params: IPoolParams): Promise<void> {
82
83
  return new Promise(async (resolve, reject) => {
83
84
  let np = this.effects.get(params.path);
84
85
  if (np == null) {
@@ -103,7 +104,7 @@ export class EffectSingleCase {
103
104
  * @param parent 父节点
104
105
  * @param params 显示参数
105
106
  */
106
- loadAndShow(params?: IEffectParams, parent?: Node): Promise<Node> {
107
+ loadAndShow(params?: IPoolParams, parent?: Node): Promise<Node> {
107
108
  return new Promise(async (resolve, reject) => {
108
109
  var np = this.effects.get(params.path);
109
110
  if (np == undefined) {
@@ -132,7 +133,7 @@ export class EffectSingleCase {
132
133
  * @param parent 父节点
133
134
  * @param params 显示参数
134
135
  */
135
- show(params: IEffectParams, parent?: Node): Node {
136
+ show(params: IPoolParams, parent?: Node): Node {
136
137
  var np = this.effects.get(params.path);
137
138
  if (np == null) {
138
139
  np = new NodePool();
@@ -264,4 +265,40 @@ export class EffectSingleCase {
264
265
  }
265
266
  }
266
267
  }
268
+
269
+ /**获取节点 */
270
+ getNode(params: IPoolParams): Node {
271
+ var np = this.effects.get(params.path);
272
+ const node = np?.get() ?? null
273
+ this.updateNode(node, params)
274
+ return node
275
+ }
276
+
277
+ /** 加载节点对象 */
278
+ load(params: IPoolParams): Promise<Node> {
279
+ return new Promise(async (resolve, reject) => {
280
+ if (!params.bundle) {
281
+ Logger.error('请指定bundleName');
282
+ resolve(null)
283
+ return;
284
+ }
285
+ var np = this.effects.get(params.path);
286
+ if (np == null || np === undefined) {
287
+ np = new NodePool();
288
+ this.effects.set(params.path, np);
289
+ }
290
+ this.res.set(params.path, params.bundle);
291
+ const prefab = await App.LoadAsset(params.bundle, params.path, Prefab);
292
+ const node = instantiate(prefab)
293
+
294
+ this.updateNode(node, params)
295
+ resolve(node);
296
+ });
297
+ }
298
+
299
+ updateNode(node: Node, params: IPoolParams) {
300
+ if (node && node.isValid && params.pos) node.position = params.pos;
301
+ if (node && node.isValid && params.worldPos) node.worldPosition = params.worldPos;
302
+ if (node && node.isValid && params.parent) { params.parent.addChild(node); }
303
+ }
267
304
  }
@@ -0,0 +1,129 @@
1
+ import { Component } from "cc";
2
+ /**状态机 */
3
+ export function GameState<T extends new (...args: any[]) => Component>(Base: T) {
4
+ return class extends Base {
5
+ private states: { action: (value?: string) => Promise<string>; name: string; state: string }[] = [];
6
+ private nextState: ((value?: string | PromiseLike<string>) => void) | null = null;
7
+ StartGame(): Promise<string> { return Promise.resolve("开始游戏"); };
8
+ GameOver(): Promise<string> { return Promise.resolve("游戏结束"); };
9
+ GameWin(): Promise<string> { return Promise.resolve("游戏胜利"); };
10
+ GameFail(): Promise<string> { return Promise.resolve("游戏失败"); };
11
+ GameClose(): Promise<string> { return Promise.resolve("关闭游戏界面"); };
12
+
13
+ protected onLoad(): void {
14
+ this.initStates()
15
+ }
16
+
17
+ /**初始化状态 */
18
+ public initStates() {
19
+ this.states = this.registerStates()
20
+ this.addCommonState();
21
+ }
22
+
23
+ /**注册状态-可重写 */
24
+ registerStates() {
25
+ return [
26
+ { action: this.StartGame, name: "开始游戏", state: "StartGame" },
27
+ { action: this.GameOver, name: "游戏结束", state: "GameOver" },
28
+ { action: this.GameWin, name: "游戏胜利", state: "GameWin" },
29
+ { action: this.GameFail, name: "游戏失败", state: "GameFail" },
30
+ { action: this.GameClose, name: "关闭游戏界面", state: "GameClose" },
31
+ ];
32
+ }
33
+
34
+ /**运行 */
35
+ public async run() {
36
+ this.statesLog("开始")
37
+ let index = 0;
38
+ const self = this;
39
+ while (index < self.states.length) {
40
+ const currentState = self.states[index];
41
+ this.statesLog(currentState.name)
42
+ // 执行当前状态
43
+ const jumpTarget = await currentState.action.call(self);
44
+ if (jumpTarget) {
45
+ const targetIndex = this.states.findIndex(s => s.state === jumpTarget);
46
+ if (targetIndex !== -1) {
47
+ Logger.debug(`[状态机] 跳转到: ${jumpTarget}`);
48
+ index = targetIndex; // 跳转到目标索引
49
+ } else {
50
+ index++; // 正常顺序执行
51
+ }
52
+ } else {
53
+ index++; // 正常顺序执行
54
+ }
55
+ this.nextState = null;
56
+
57
+ }
58
+ this.statesLog("结束")
59
+ }
60
+
61
+ statesLog(name: string) {
62
+ Logger.debug(`状态:${name}`);
63
+ }
64
+
65
+
66
+ /**
67
+ * 在指定方法之后插入新的方法
68
+ * @param afterMethod
69
+ * @param newState
70
+ */
71
+ public insertState(afterMethod: () => Promise<string>, model: { action: () => Promise<string>; name: string; state: string }, isDown: boolean = true) {
72
+ const index = this.states.findIndex((s) => s.action === afterMethod);
73
+ if (index !== -1) {
74
+ if (isDown) {
75
+ this.states.splice(index + 1, 0, model);
76
+ } else {
77
+ this.states.splice(index, 0, model);
78
+ }
79
+ } else {
80
+ console.error(`没有找到:${String(afterMethod)}`);
81
+ }
82
+ }
83
+
84
+ //#region 添加通用状态
85
+ private addCommonState() {
86
+ //插入this.openEndWindow状态后
87
+ //this.insertState(this.openEndWindow, { action: this.activeState, name: "活动", state: "activeState" });
88
+ }
89
+
90
+ activeState() {
91
+ return new Promise<void>((res) => {
92
+ res();
93
+ // setTimeout(() => {
94
+ // res();
95
+ // }, 2000);
96
+ });
97
+ }
98
+
99
+ /**下一个状态
100
+ * @param stateName 指定的状态方法名称
101
+ */
102
+ next(stateName?: string) {
103
+ if (this.nextState) {
104
+ this.nextState(stateName);
105
+ this.nextState = null;
106
+ }
107
+ }
108
+
109
+ // BaseGameState.ts
110
+
111
+ /**
112
+ * 创建一个状态 Promise,并自动绑定 this.nextState
113
+ * 子类只需提供具体的执行逻辑(可选延迟)
114
+ */
115
+ public promise(
116
+ executor: (next: (value?: string | PromiseLike<string>) => void) => void,
117
+ delay: number = 0
118
+ ): Promise<string> {
119
+ return new Promise<string>((res) => {
120
+ this.nextState = res; // 自动绑定
121
+ if (delay > 0) {
122
+ this.scheduleOnce(() => { executor(res); }, delay);
123
+ } else {
124
+ executor(res);
125
+ }
126
+ });
127
+ }
128
+ }
129
+ }
package/index.ts CHANGED
@@ -10,7 +10,7 @@ export * from './assets/core/home/BaseLaunchComponent';
10
10
  export * from './assets/core/home/BaseLoading';
11
11
  export * from './assets/core/home/ProgessWindow';
12
12
  export * from './assets/core/home/BaseModuleConfig';
13
- export * from './assets/core/home/BaseGameState';
13
+ export * from './assets/core/home/GameState';
14
14
  export * from './assets/core/home/EffectSingleCase';
15
15
 
16
16
  //日志
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cc-component/cc-core",
3
- "version": "1.5.4",
3
+ "version": "1.5.6",
4
4
  "engine": ">=3.8.6",
5
5
  "description": "系统组件添加常用扩展方法",
6
6
  "main": "index.ts",
@@ -1,140 +0,0 @@
1
- import { Component } from "cc";
2
-
3
- /**状态机 */
4
- export class BaseGameState extends Component {
5
- private states: { action: (value?: string) => Promise<string>; name: string; state: string }[] = [];
6
- nextState: ((value?: string | PromiseLike<string>) => void) | null = null;
7
- private jumpTarget: string | null = null; // 新增:记录要跳转的目标状态名
8
-
9
- StartGame(): Promise<string> { return Promise.resolve("开始游戏"); };
10
- GameOver(): Promise<string> { return Promise.resolve("游戏结束"); };
11
- GameWin(): Promise<string> { return Promise.resolve("游戏胜利"); };
12
- GameFail(): Promise<string> { return Promise.resolve("游戏失败"); };
13
- GameClose(): Promise<string> { return Promise.resolve("关闭游戏界面"); };
14
-
15
-
16
- protected onLoad(): void {
17
- this.states = this.registerStates()
18
- this.onInit()
19
-
20
- }
21
- /**注册状态-可重写 */
22
- registerStates() {
23
- return [
24
- { action: this.StartGame, name: "开始游戏", state: "StartGame" },
25
- { action: this.GameOver, name: "游戏结束", state: "GameOver" },
26
- { action: this.GameWin, name: "游戏胜利", state: "GameWin" },
27
- { action: this.GameFail, name: "游戏失败", state: "GameFail" },
28
- { action: this.GameClose, name: "关闭游戏界面", state: "GameClose" },
29
- ];
30
- }
31
- onInit() {
32
- this.addCommonState();
33
- }
34
-
35
- /**运行 */
36
- public async run() {
37
- let index = 0;
38
- const self = this;
39
- while (index < self.states.length) {
40
- const currentState = self.states[index];
41
- this.statesLog(currentState.name)
42
- // 执行当前状态
43
- const jumpTarget = await currentState.action.call(self);
44
- if (jumpTarget) {
45
- const targetIndex = this.states.findIndex(s => s.state === jumpTarget);
46
- if (targetIndex !== -1) {
47
- console.log(`[状态机] 跳转到: ${jumpTarget}`);
48
- index = targetIndex; // 跳转到目标索引
49
- } else {
50
- index++; // 正常顺序执行
51
- }
52
- } else {
53
- index++; // 正常顺序执行
54
- }
55
- }
56
- }
57
-
58
- statesLog(name: string) {
59
- console.log(`状态:${name}`);
60
- }
61
-
62
-
63
- /**
64
- * 在指定方法之后插入新的方法
65
- * @param afterMethod
66
- * @param newState
67
- */
68
- public insertState(afterMethod: () => Promise<string>, model: { action: () => Promise<string>; name: string; state: string }, isDown: boolean = true) {
69
- const index = this.states.findIndex((s) => s.action === afterMethod);
70
- if (index !== -1) {
71
- if (isDown) {
72
- this.states.splice(index + 1, 0, model);
73
- } else {
74
- this.states.splice(index, 0, model);
75
- }
76
- } else {
77
- console.error(`没有找到:${String(afterMethod)}`);
78
- }
79
- }
80
-
81
- //#region 添加通用状态
82
- private addCommonState() {
83
- //插入this.openEndWindow状态后
84
- //this.insertState(this.openEndWindow, { action: this.activeState, name: "活动", state: "activeState" });
85
- }
86
-
87
- activeState() {
88
- return new Promise<void>((res) => {
89
- res();
90
- // setTimeout(() => {
91
- // res();
92
- // }, 2000);
93
- });
94
- }
95
-
96
- /**
97
- * 跳转到指定状态(在下一个状态循环时生效)
98
- * @param stateName 目标状态的 state 字段值(如 "GameClose")
99
- */
100
- public jumpToState(stateName: string): void {
101
- const target = this.states.find(s => s.state === stateName);
102
- if (!target) {
103
- console.error(`[状态机] 未找到状态: ${stateName}`);
104
- return;
105
- }
106
- this.jumpTarget = stateName;
107
- // 如果当前有 nextState,立即 resolve 以退出当前状态
108
- if (this.nextState) {
109
- this.nextState();
110
- this.nextState = null;
111
- }
112
- }
113
-
114
- next() {
115
- if (this.nextState) {
116
- this.nextState();
117
- this.nextState = null;
118
- }
119
- }
120
-
121
- // BaseGameState.ts
122
-
123
- /**
124
- * 创建一个状态 Promise,并自动绑定 this.nextState
125
- * 子类只需提供具体的执行逻辑(可选延迟)
126
- */
127
- protected promise(
128
- executor: (next: (value?: string | PromiseLike<string>) => void) => void,
129
- delay: number = 0
130
- ): Promise<string> {
131
- return new Promise<string>((res) => {
132
- this.nextState = res; // 自动绑定
133
- if (delay > 0) {
134
- this.scheduleOnce(() => { executor(res); }, delay);
135
- } else {
136
- executor(res);
137
- }
138
- });
139
- }
140
- }