@cjhd/cj-ecs 1.0.0 → 1.0.1

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.
Files changed (74) hide show
  1. package/assets/common/component/MoveComponent.d.ts +141 -0
  2. package/assets/common/component/NodeComponent.d.ts +98 -0
  3. package/assets/common/system/MoveSystem.d.ts +17 -0
  4. package/assets/ecs/EcsComponent.d.ts +89 -0
  5. package/assets/ecs/{EcsDirty.ts → EcsDirty.d.ts} +340 -459
  6. package/assets/ecs/EcsEntity.d.ts +148 -0
  7. package/assets/ecs/EcsSingleton.d.ts +3 -0
  8. package/assets/ecs/EcsSystem.d.ts +96 -0
  9. package/assets/ecs.d.ts +126 -0
  10. package/assets/lib/EcsCache.d.ts +24 -0
  11. package/assets/lib/EcsFilter.d.ts +57 -0
  12. package/assets/lib/EcsManager.d.ts +251 -0
  13. package/assets/lib/{EcsObject.ts → EcsObject.d.ts} +360 -422
  14. package/assets/lib/EcsTimer.d.ts +119 -0
  15. package/assets/lib/EcsTween.d.ts +168 -0
  16. package/assets/lib/EcsUtils.d.ts +82 -0
  17. package/dist/cocos/assets/common/component/MoveComponent.js +4 -0
  18. package/dist/cocos/assets/common/component/NodeComponent.js +4 -0
  19. package/dist/cocos/assets/common/system/MoveSystem.js +2 -0
  20. package/dist/cocos/assets/ecs/EcsComponent.js +4 -0
  21. package/dist/cocos/assets/ecs/EcsDirty.js +4 -0
  22. package/dist/cocos/assets/ecs/EcsEntity.js +4 -0
  23. package/dist/cocos/assets/ecs/EcsSingleton.js +4 -0
  24. package/dist/cocos/assets/ecs/EcsSystem.js +2 -0
  25. package/dist/cocos/assets/ecs.js +2 -0
  26. package/dist/cocos/assets/lib/EcsCache.js +2 -0
  27. package/dist/cocos/assets/lib/EcsFilter.js +2 -0
  28. package/dist/cocos/assets/lib/EcsManager.js +4 -0
  29. package/dist/cocos/assets/lib/EcsObject.js +4 -0
  30. package/dist/cocos/assets/lib/EcsTimer.js +4 -0
  31. package/dist/cocos/assets/lib/EcsTween.js +4 -0
  32. package/dist/cocos/assets/lib/EcsUtils.js +2 -0
  33. package/dist/cocos/index.js +4 -0
  34. package/{index.ts → index.d.ts} +14 -33
  35. package/package.json +27 -7
  36. package/assets/common/component/MoveComponent.ts +0 -292
  37. package/assets/common/component/MoveComponent.ts.meta +0 -9
  38. package/assets/common/component/NodeComponent.ts +0 -315
  39. package/assets/common/component/NodeComponent.ts.meta +0 -9
  40. package/assets/common/component.meta +0 -12
  41. package/assets/common/system/MoveSystem.ts +0 -108
  42. package/assets/common/system/MoveSystem.ts.meta +0 -9
  43. package/assets/common/system.meta +0 -12
  44. package/assets/common.meta +0 -12
  45. package/assets/ecs/EcsComponent.ts +0 -244
  46. package/assets/ecs/EcsComponent.ts.meta +0 -9
  47. package/assets/ecs/EcsDirty.ts.meta +0 -9
  48. package/assets/ecs/EcsEntity.ts +0 -430
  49. package/assets/ecs/EcsEntity.ts.meta +0 -9
  50. package/assets/ecs/EcsSingleton.ts +0 -6
  51. package/assets/ecs/EcsSingleton.ts.meta +0 -9
  52. package/assets/ecs/EcsSystem.ts +0 -191
  53. package/assets/ecs/EcsSystem.ts.meta +0 -9
  54. package/assets/ecs.meta +0 -12
  55. package/assets/ecs.ts +0 -339
  56. package/assets/ecs.ts.meta +0 -9
  57. package/assets/lib/EcsCache.ts +0 -43
  58. package/assets/lib/EcsCache.ts.meta +0 -9
  59. package/assets/lib/EcsFilter.ts +0 -210
  60. package/assets/lib/EcsFilter.ts.meta +0 -9
  61. package/assets/lib/EcsManager.ts +0 -502
  62. package/assets/lib/EcsManager.ts.meta +0 -9
  63. package/assets/lib/EcsObject.ts.meta +0 -9
  64. package/assets/lib/EcsTimer.ts +0 -239
  65. package/assets/lib/EcsTimer.ts.meta +0 -9
  66. package/assets/lib/EcsTween.ts +0 -486
  67. package/assets/lib/EcsTween.ts.meta +0 -9
  68. package/assets/lib/EcsUtils.ts +0 -352
  69. package/assets/lib/EcsUtils.ts.meta +0 -9
  70. package/assets/lib.meta +0 -12
  71. package/assets.meta +0 -9
  72. package/index.ts.meta +0 -9
  73. package/package.json.meta +0 -11
  74. /package/{.cj-ecs.md → README.md} +0 -0
@@ -1,108 +0,0 @@
1
- import { math } from 'cc';
2
- import { EcsSystem } from '../../ecs/EcsSystem';
3
- import { filter } from '../../lib/EcsFilter';
4
- import { IEntity } from '../../lib/EcsObject';
5
- import { IMoveComponent, MoveComponent, MoveTowardType } from '../component/MoveComponent';
6
- import { NodeComponent } from '../component/NodeComponent';
7
-
8
- /**
9
- * 修正为范围值
10
- */
11
- function getAbsoluteValue(input: number, threshold: number) {
12
- if (threshold < 0) {
13
- threshold = -threshold;
14
- }
15
- if (input > 0) {
16
- if (input > threshold) return threshold;
17
- return input;
18
- }
19
- if (input < 0) {
20
- if (input < -threshold) return -threshold;
21
- }
22
- return input;
23
- }
24
-
25
- /**
26
- * 负责驱动move组件
27
- */
28
- export class MoveSystem extends EcsSystem {
29
- private filter = filter.all(MoveComponent, NodeComponent);
30
-
31
- protected execute(dt?: number): void {
32
- this.query(this.filter, MoveComponent).forEach((move) => {
33
- this.handle(move, move.entity, dt);
34
- });
35
- }
36
-
37
- /**
38
- * 处理
39
- * @param move
40
- * @param entity
41
- * @param dt
42
- */
43
- private handle(move: IMoveComponent, entity: IEntity, dt: number) {
44
- // 追踪角度
45
- if (move.target) {
46
- move.toward = MoveComponent.getTowardAngle(entity.getComponent(NodeComponent), move.target);
47
- }
48
-
49
- this.rotate(move, entity, dt);
50
-
51
- // 初速度
52
- const lastSpeed = move.speed;
53
- // 加速度
54
- move.speed += move.options.acceleratedVelocity * dt;
55
- if (move.speed < move.options.minSpeed) {
56
- move.speed = move.options.minSpeed;
57
- }
58
- if (move.speed > move.options.maxSpeed) {
59
- move.speed = move.options.maxSpeed;
60
- }
61
- // 平均速度
62
- const speed = (lastSpeed + move.speed) / 2;
63
- this.run(move, entity, dt, speed);
64
- }
65
-
66
- private run(move: IMoveComponent, entity: IEntity, dt: number, speed: number) {
67
- // 移动距离
68
- const distance = speed * dt;
69
- // 设置节点位移
70
- if (move.angle === MoveTowardType.Right) {
71
- entity.getComponent(NodeComponent).addPosition(distance, 0);
72
- } else if (move.angle === MoveTowardType.Left) {
73
- entity.getComponent(NodeComponent).addPosition(-distance, 0);
74
- } else if (move.angle === MoveTowardType.Up) {
75
- entity.getComponent(NodeComponent).addPosition(0, distance);
76
- } else if (move.angle === MoveTowardType.Down) {
77
- entity.getComponent(NodeComponent).addPosition(0, -distance);
78
- } else {
79
- // 会有细微的误差
80
- const radian = math.toRadian(move.angle);
81
- const x = Math.cos(radian) * distance;
82
- const y = Math.sin(radian) * distance;
83
- entity.getComponent(NodeComponent).addPosition(x, y);
84
- }
85
- // 记录移动
86
- move.distance += distance;
87
- }
88
-
89
- private rotate(move: IMoveComponent, entity: IEntity, dt: number) {
90
- let addAngle = 0;
91
- if (move.toward !== move.angle) {
92
- // 角度差值
93
- const diffAngle = MoveComponent.getAngleFromNeg180to180(move.toward - move.angle);
94
- // 差值限制(实际增加)
95
- addAngle = move.options.angleVelocity === 0 ? diffAngle : getAbsoluteValue(diffAngle, move.options.angleVelocity * dt);
96
- // 实际角度
97
- move.angle += addAngle;
98
-
99
- // 设置节点角度
100
- if (move.options.rotate) {
101
- entity.getComponent(NodeComponent).setAngle(move.angle);
102
- }
103
- }
104
-
105
- return addAngle;
106
- }
107
- }
108
-
@@ -1,9 +0,0 @@
1
- {
2
- "ver": "4.0.24",
3
- "importer": "typescript",
4
- "imported": true,
5
- "uuid": "3c7901a5-7e25-4b92-9fab-8545febc131f",
6
- "files": [],
7
- "subMetas": {},
8
- "userData": {}
9
- }
@@ -1,12 +0,0 @@
1
- {
2
- "ver": "1.2.0",
3
- "importer": "directory",
4
- "imported": true,
5
- "uuid": "92ee71d6-4d80-49c0-ac18-b30302579048",
6
- "files": [],
7
- "subMetas": {},
8
- "userData": {
9
- "compressionType": {},
10
- "isRemoteBundle": {}
11
- }
12
- }
@@ -1,12 +0,0 @@
1
- {
2
- "ver": "1.2.0",
3
- "importer": "directory",
4
- "imported": true,
5
- "uuid": "458425eb-7ee3-40d6-b238-ab3703c68aa0",
6
- "files": [],
7
- "subMetas": {},
8
- "userData": {
9
- "compressionType": {},
10
- "isRemoteBundle": {}
11
- }
12
- }
@@ -1,244 +0,0 @@
1
-
2
- import { ecsclass } from '../lib/EcsManager';
3
- import { EcsBaseComponent, IComponent, IEntity } from '../lib/EcsObject';
4
- import type { EcsDirtyMask, IEcsComponentDirtySink } from './EcsDirty';
5
-
6
- @ecsclass('EcsComponent')
7
- export class EcsComponent<E extends IEntity = IEntity> extends EcsBaseComponent implements IComponent {
8
- declare static readonly Fields: Record<string, any>;
9
-
10
- /**内部函数 */
11
- static updateEnable(comp: EcsComponent) {
12
- comp.updateEnable();
13
- }
14
-
15
- /** 内部函数:ECS/observed runtime 用它把 dirty sink 绑定到组件实例。 */
16
- static bindDirtySink(comp: EcsComponent, sink: IEcsComponentDirtySink | null) {
17
- comp._dirtySink = sink;
18
- }
19
-
20
- /**内部函数 */
21
- static beforeRemove(comp: EcsComponent) {
22
- return comp.innerBeforeRemove();
23
- }
24
-
25
- /**内部函数 */
26
- static onAdd(comp: EcsComponent, entity: IEntity) {
27
- return comp.innerAdd(entity);
28
- }
29
-
30
- /**内部函数 */
31
- static onRemove(comp: EcsComponent) {
32
- return comp.innerRemove();
33
- }
34
-
35
- /**正在初始化中 */
36
- private _initing = false;
37
- /**正在移除中 */
38
- private _removing = false;
39
- /** 当前组件累积的 dirty bit。未安装 observed 时也会维护这个本地状态。 */
40
- private _dirtyMask: EcsDirtyMask = 0;
41
- /** 每次 dirtyMask 从“未包含某 bit”变为“包含某 bit”时递增,方便调试或增量同步判断。 */
42
- private _dirtyVersion = 0;
43
- /** 可选扩展 sink。只有 observed runtime install 后才会非空。 */
44
- private _dirtySink: IEcsComponentDirtySink | null = null;
45
-
46
- /**是否有效 */
47
- private _isValid = false;
48
- public get isValid() {
49
- return this._isValid;
50
- }
51
- private set isValid(value) {
52
- this._isValid = value;
53
- }
54
-
55
- public get isStrictValid() {
56
- return this._isValid && !this._removing;
57
- }
58
-
59
- /**是否生效 */
60
- private _enabled = false;
61
- public get enabled() {
62
- return this._enabled;
63
- }
64
- public set enabled(value) {
65
- if (this._enabled === value) return;
66
- this._enabled = value;
67
- if (this._initing) return;
68
- if (this._removing) return;
69
- this.updateEnable();
70
- }
71
-
72
- /**当前组件所在的实体 */
73
- private _entity: E | null = null;
74
- public get entity() {
75
- return this._entity;
76
- }
77
- private set entity(value) {
78
- this._entity = value;
79
- }
80
-
81
- /**表示该组件是否被启用并且所在的实体也处于启用状态(为false代表不会被查询到) */
82
- public get enabledInHierarchy() {
83
- if (!this._enabled) return false;
84
- if (!this._entity) return false;
85
- if (!this._entity.enabled) return false;
86
- return true;
87
- }
88
-
89
- private __enabled = false;
90
- private updateEnable() {
91
- const enabledInHierarchy = this.enabledInHierarchy;
92
- if (this.__enabled === enabledInHierarchy) return;
93
- this.__enabled = enabledInHierarchy;
94
- if (enabledInHierarchy) {
95
- this.onEnable();
96
- } else {
97
- this.onDisable();
98
- }
99
- const sink = this._dirtySink;
100
- if (sink && sink.onComponentEnabledChanged) {
101
- sink.onComponentEnabledChanged(this, enabledInHierarchy);
102
- }
103
- }
104
-
105
- public get dirty(): boolean {
106
- return this._dirtyMask !== 0;
107
- }
108
-
109
- /**
110
- * 兼容旧式 boolean dirty 用法。
111
- * 设置 true 等价于 markDirty(1),设置 false 会清掉全部 dirty bit。
112
- */
113
- public set dirty(value: boolean) {
114
- if (value) {
115
- this.markDirty(1);
116
- } else {
117
- this.clearDirty(this._dirtyMask);
118
- }
119
- }
120
-
121
- public get dirtyMask(): EcsDirtyMask {
122
- return this._dirtyMask;
123
- }
124
-
125
- public get dirtyVersion(): number {
126
- return this._dirtyVersion;
127
- }
128
-
129
- /** 判断当前组件是否包含指定 dirty bit。 */
130
- public hasDirty(mask: EcsDirtyMask): boolean {
131
- return (this._dirtyMask & mask) !== 0;
132
- }
133
-
134
- /**
135
- * 标记组件 dirty。
136
- *
137
- * 业务组件的写屏障应该调用它,例如 position.setXYRaw 后标记 PositionDirtyBits.Spatial。
138
- * 如果 observed runtime 已安装,会同步入队;未安装时只更新本地 dirtyMask/version。
139
- */
140
- public markDirty(mask: EcsDirtyMask = 1): void {
141
- if (mask === 0) return;
142
-
143
- const oldMask = this._dirtyMask;
144
- const newMask = oldMask | mask;
145
- if (oldMask === newMask) return;
146
-
147
- this._dirtyMask = newMask;
148
- this._dirtyVersion++;
149
-
150
- const sink = this._dirtySink;
151
- if (sink && this.isStrictValid && this.entity) {
152
- sink.onComponentDirty(this, mask, oldMask, newMask);
153
- }
154
- }
155
-
156
- /**
157
- * 清理组件 dirty。
158
- * drain 默认会调用它清理 matched bit;业务也可以在重置组件状态时主动调用。
159
- */
160
- public clearDirty(mask: EcsDirtyMask = this._dirtyMask): void {
161
- if (mask === 0) return;
162
-
163
- const oldMask = this._dirtyMask;
164
- const newMask = oldMask & ~mask;
165
- if (oldMask === newMask) return;
166
-
167
- this._dirtyMask = newMask;
168
-
169
- const sink = this._dirtySink;
170
- if (sink && this.isStrictValid && this.entity && sink.onComponentClearDirty) {
171
- sink.onComponentClearDirty(this, mask, oldMask, newMask);
172
- }
173
- }
174
-
175
- /**
176
- * 静默清理本地 dirtyMask,不通知 sink。
177
- * 只用于组件移除/回收这类生命周期收尾,普通业务不要用它代替 clearDirty。
178
- */
179
- public clearDirtySilent(): void {
180
- this._dirtyMask = 0;
181
- }
182
-
183
- /**
184
- * 组件被添加(内部调用)
185
- */
186
- private innerAdd(entity: E) {
187
- this._initing = true;
188
- this._removing = false;
189
- this._isValid = true;
190
- this._enabled = true;
191
- this.__enabled = false;
192
- this._entity = entity;
193
- this.onAdd();
194
- this._initing = false;
195
- this.__enabled = this.enabledInHierarchy;
196
- this.__enabled && this.onEnable();
197
- }
198
-
199
- /**
200
- * 组件被移除前(内部调用)
201
- */
202
- private innerBeforeRemove() {
203
- this._removing = true;
204
- }
205
-
206
- /**
207
- * 组件被移除(内部调用)
208
- */
209
- private innerRemove() {
210
- const sink = this._dirtySink;
211
- if (sink && sink.onComponentRemoved) {
212
- sink.onComponentRemoved(this);
213
- }
214
- this.__enabled && this.onDisable();
215
- this.onRemove();
216
- this._removing = false;
217
- this._isValid = false;
218
- this._enabled = false;
219
- this.__enabled = false;
220
- this.clearDirtySilent();
221
- this._dirtySink = null;
222
- this._entity = null;
223
- }
224
-
225
- protected onAdd() { }
226
- protected onEnable() { }
227
- protected onDisable() { }
228
- protected onRemove() { }
229
-
230
- public destroy(): boolean {
231
- if (!this.entity) return false;
232
- return this.entity.remove(this);
233
- }
234
-
235
- protected log(...args: any[]) {
236
- console.log(`[${this.ecsName}] [log]`, ...args);
237
- }
238
- protected warn(...args: any[]) {
239
- console.warn(`[${this.ecsName}] [warn]`, ...args);
240
- }
241
- protected error(...args: any[]) {
242
- console.error(`[${this.ecsName}] [error]`, ...args);
243
- }
244
- }
@@ -1,9 +0,0 @@
1
- {
2
- "ver": "4.0.24",
3
- "importer": "typescript",
4
- "imported": true,
5
- "uuid": "e3cef916-2f45-4f63-aadf-45ce183a40c8",
6
- "files": [],
7
- "subMetas": {},
8
- "userData": {}
9
- }
@@ -1,9 +0,0 @@
1
- {
2
- "ver": "4.0.24",
3
- "importer": "typescript",
4
- "imported": true,
5
- "uuid": "642c20f8-46f8-4b42-8d6e-3d5a5b85faf9",
6
- "files": [],
7
- "subMetas": {},
8
- "userData": {}
9
- }