@cc-component/cc-core 1.5.5 → 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.
@@ -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.5",
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
- }