@cjhd/cj-ecs 1.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.
Files changed (43) hide show
  1. package/.cj-ecs.md +12 -0
  2. package/assets/common/component/MoveComponent.ts +292 -0
  3. package/assets/common/component/MoveComponent.ts.meta +9 -0
  4. package/assets/common/component/NodeComponent.ts +315 -0
  5. package/assets/common/component/NodeComponent.ts.meta +9 -0
  6. package/assets/common/component.meta +12 -0
  7. package/assets/common/system/MoveSystem.ts +108 -0
  8. package/assets/common/system/MoveSystem.ts.meta +9 -0
  9. package/assets/common/system.meta +12 -0
  10. package/assets/common.meta +12 -0
  11. package/assets/ecs/EcsComponent.ts +244 -0
  12. package/assets/ecs/EcsComponent.ts.meta +9 -0
  13. package/assets/ecs/EcsDirty.ts +459 -0
  14. package/assets/ecs/EcsDirty.ts.meta +9 -0
  15. package/assets/ecs/EcsEntity.ts +430 -0
  16. package/assets/ecs/EcsEntity.ts.meta +9 -0
  17. package/assets/ecs/EcsSingleton.ts +6 -0
  18. package/assets/ecs/EcsSingleton.ts.meta +9 -0
  19. package/assets/ecs/EcsSystem.ts +191 -0
  20. package/assets/ecs/EcsSystem.ts.meta +9 -0
  21. package/assets/ecs.meta +12 -0
  22. package/assets/ecs.ts +339 -0
  23. package/assets/ecs.ts.meta +9 -0
  24. package/assets/lib/EcsCache.ts +43 -0
  25. package/assets/lib/EcsCache.ts.meta +9 -0
  26. package/assets/lib/EcsFilter.ts +210 -0
  27. package/assets/lib/EcsFilter.ts.meta +9 -0
  28. package/assets/lib/EcsManager.ts +502 -0
  29. package/assets/lib/EcsManager.ts.meta +9 -0
  30. package/assets/lib/EcsObject.ts +422 -0
  31. package/assets/lib/EcsObject.ts.meta +9 -0
  32. package/assets/lib/EcsTimer.ts +239 -0
  33. package/assets/lib/EcsTimer.ts.meta +9 -0
  34. package/assets/lib/EcsTween.ts +486 -0
  35. package/assets/lib/EcsTween.ts.meta +9 -0
  36. package/assets/lib/EcsUtils.ts +352 -0
  37. package/assets/lib/EcsUtils.ts.meta +9 -0
  38. package/assets/lib.meta +12 -0
  39. package/assets.meta +9 -0
  40. package/index.ts +33 -0
  41. package/index.ts.meta +9 -0
  42. package/package.json +20 -0
  43. package/package.json.meta +11 -0
@@ -0,0 +1,352 @@
1
+
2
+ /**
3
+ * Map、Set、Array都是有序的
4
+ */
5
+ class ListItem<T> {
6
+ static cache: ListItem<any>[] = [];
7
+ static create<T>(value: T = null) {
8
+ if (this.cache.length) {
9
+ const list = this.cache.pop();
10
+ list.value = value;
11
+ return list;
12
+ }
13
+ return new ListItem(value);
14
+ }
15
+ static put<T>(list: ListItem<T>) {
16
+ list.value = null;
17
+ list.next = null;
18
+ this.cache.push(list);
19
+ }
20
+
21
+ next: ListItem<T> = null;
22
+ value: T = null;
23
+ private constructor(value: T = null) {
24
+ this.value = value;
25
+ }
26
+ }
27
+ export class CustomList<T> {
28
+ private head: ListItem<T> = ListItem.create();
29
+ private tail: ListItem<T> = this.head;
30
+ private count = 0;
31
+
32
+ get size() {
33
+ return this.count;
34
+ }
35
+ values(out: T[]) {
36
+ let item = this.head.next;
37
+ while (item) {
38
+ out.push(item.value);
39
+ item = item.next;
40
+ }
41
+ return out;
42
+ }
43
+ has(value: T) {
44
+ let item = this.head.next;
45
+ while (item) {
46
+ if (item.value === value) {
47
+ return true;
48
+ }
49
+ item = item.next;
50
+ }
51
+ return false;
52
+ }
53
+ add(value: T) {
54
+ this.count++;
55
+ this.tail.next = ListItem.create(value);
56
+ this.tail = this.tail.next;
57
+ return this.count;
58
+ }
59
+ remove(value: T) {
60
+ let item = this.head;
61
+ while (item.next) {
62
+ if (item.next.value === value) {
63
+ this.count--;
64
+ const removed = item.next;
65
+ item.next = removed.next;
66
+ if (item.next === null) {
67
+ this.tail = item;
68
+ }
69
+ ListItem.put(removed);
70
+ return true;
71
+ }
72
+ item = item.next;
73
+ }
74
+ return false;
75
+ }
76
+ forEach(callbackfn: (value: T) => void, thisArg?: any) {
77
+ let item = this.head.next;
78
+ while (item) {
79
+ callbackfn.call(thisArg, item.value);
80
+ item = item.next;
81
+ }
82
+ }
83
+ clear() {
84
+ let curr = this.head.next;
85
+ let next = curr && curr.next;
86
+ while (curr) {
87
+ ListItem.put(curr);
88
+ curr = next;
89
+ next = curr && curr.next;
90
+ }
91
+
92
+ this.head.next = null;
93
+ this.tail = this.head;
94
+ this.count = 0;
95
+ }
96
+ }
97
+ export class CustomArray<T> {
98
+ private array: T[] = [];
99
+
100
+ get size() {
101
+ return this.array.length;
102
+ }
103
+ values(out: T[]) {
104
+ Array.prototype.push.apply(out, this.array);
105
+ return out;
106
+ }
107
+ get(index: number) {
108
+ return this.array[index];
109
+ }
110
+ has(value: T) {
111
+ return this.array.indexOf(value) >= 0;
112
+ }
113
+ add(value: T) {
114
+ this.array.push(value);
115
+ }
116
+ remove(value: T) {
117
+ const index = this.array.indexOf(value);
118
+ if (index >= 0) {
119
+ this.array.splice(index, 1);
120
+ return true;
121
+ }
122
+ return false;
123
+ }
124
+ forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any) {
125
+ return this.array.forEach(callbackfn, thisArg);
126
+ }
127
+ find(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any) {
128
+ return this.array.find(callbackfn, thisArg);
129
+ }
130
+ findIndex(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any) {
131
+ return this.array.findIndex(callbackfn, thisArg);
132
+ }
133
+ some(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any) {
134
+ return this.array.some(callbackfn, thisArg);
135
+ }
136
+ every(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any) {
137
+ return this.array.every(callbackfn, thisArg);
138
+ }
139
+ clear() {
140
+ this.array.length = 0;
141
+ }
142
+ }
143
+
144
+ export class CustomMap<K, V> {
145
+ private map: Map<K, V> = new Map();
146
+
147
+ get size() {
148
+ return this.map.size;
149
+ }
150
+ keys(out: K[]) {
151
+ this.map.forEach((v, key) => out.push(key));
152
+ return out;
153
+ }
154
+ values(out: V[]) {
155
+ this.map.forEach((value) => out.push(value));
156
+ return out;
157
+ }
158
+ forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void, thisArg?: any) {
159
+ return this.map.forEach(callbackfn, thisArg);
160
+ }
161
+ get(key: K) {
162
+ return this.map.get(key);
163
+ }
164
+ has(key: K) {
165
+ return this.map.has(key);
166
+ }
167
+ set(key: K, value: V) {
168
+ return this.map.set(key, value);
169
+ }
170
+ delete(key: K) {
171
+ return this.map.delete(key);
172
+ }
173
+ clear() {
174
+ this.map.clear();
175
+ }
176
+ }
177
+
178
+ export class NumberMap<K> extends CustomMap<K, number> {
179
+ add(key: K) {
180
+ const num = (this.get(key) || 0) + 1;
181
+ this.set(key, num);
182
+ return num;
183
+ }
184
+ sub(key: K) {
185
+ if (!this.has(key)) return 0;
186
+ const num = this.get(key) - 1;
187
+ this.set(key, num);
188
+ return num;
189
+ }
190
+ subDel(key: K) {
191
+ if (this.sub(key) === 0) {
192
+ this.delete(key);
193
+ return true;
194
+ }
195
+ return false;
196
+ }
197
+ }
198
+
199
+ /**
200
+ * 数组Map
201
+ */
202
+ export class ArrayMap<K, V> extends CustomMap<K, Array<V>> {
203
+ add(key: K, value: V) {
204
+ if (!this.has(key)) {
205
+ this.set(key, [value]);
206
+ return 1;
207
+ } else {
208
+ const array = this.get(key);
209
+ array.push(value);
210
+ return array.length;
211
+ }
212
+ }
213
+ sub(key: K, value: V) {
214
+ const array = this.get(key);
215
+ if (!array) {
216
+ return 0;
217
+ }
218
+ const index = array.indexOf(value);
219
+ if (index >= 0) {
220
+ array.splice(index, 1);
221
+ }
222
+
223
+ return array.length;
224
+ }
225
+ subDel(key: K, value: V) {
226
+ if (this.sub(key, value) === 0) {
227
+ this.delete(key);
228
+ return true;
229
+ }
230
+ return false;
231
+ }
232
+ }
233
+
234
+ export class SetMap<K, V> extends CustomMap<K, Set<V>> {
235
+ add(key: K, value: V) {
236
+ if (!this.has(key)) {
237
+ return this.set(key, new Set<V>().add(value)).size;
238
+ } else {
239
+ return this.get(key).add(value).size;
240
+ }
241
+ }
242
+ sub(key: K, value: V) {
243
+ if (!this.has(key)) return 0;
244
+ const set = this.get(key);
245
+ set.delete(value);
246
+ return set.size;
247
+ }
248
+ subDel(key: K, value: V) {
249
+ if (this.sub(key, value) === 0) {
250
+ this.delete(key);
251
+ return true;
252
+ }
253
+ return false;
254
+ }
255
+ }
256
+
257
+ /**
258
+ * 唯一数组(同Set)Map
259
+ */
260
+ export class UArrayMap<K, V> extends ArrayMap<K, V> {
261
+ add(key: K, value: V) {
262
+ if (!this.has(key)) {
263
+ this.set(key, [value]);
264
+ return 1;
265
+ } else {
266
+ const array = this.get(key);
267
+ if (array.indexOf(value) === -1) {
268
+ array.push(value);
269
+ }
270
+ return array.length;
271
+ }
272
+ }
273
+ }
274
+
275
+ /**
276
+ * 唯一List(同Set)Map
277
+ */
278
+ export class UListMap<K, V> extends CustomMap<K, CustomList<V>> {
279
+ add(key: K, value: V) {
280
+ if (!this.has(key)) {
281
+ const list = new CustomList<V>();
282
+ this.set(key, list);
283
+ list.add(value);
284
+ return 1;
285
+ } else {
286
+ const list = this.get(key);
287
+ if (!list.has(value)) {
288
+ list.add(value);
289
+ }
290
+ return list.size;
291
+ }
292
+ }
293
+ sub(key: K, value: V) {
294
+ if (!this.has(key)) return 0;
295
+
296
+ const list = this.get(key);
297
+ list.remove(value);
298
+
299
+ return list.size;
300
+ }
301
+ subDel(key: K, value: V) {
302
+ if (this.sub(key, value) === 0) {
303
+ this.delete(key);
304
+ return true;
305
+ }
306
+ return false;
307
+ }
308
+ }
309
+
310
+ export function createMap(forceDictMode: boolean) {
311
+ let map = Object.create(null);
312
+ if (forceDictMode) {
313
+ const INVALID_IDENTIFIER_1 = '.';
314
+ const INVALID_IDENTIFIER_2 = '/';
315
+ map[INVALID_IDENTIFIER_1] = true;
316
+ map[INVALID_IDENTIFIER_2] = true;
317
+ delete map[INVALID_IDENTIFIER_1];
318
+ delete map[INVALID_IDENTIFIER_2];
319
+ }
320
+ return map;
321
+ }
322
+
323
+ /**
324
+ * 过滤原数组,性能高但是会改变原属组的顺序
325
+ */
326
+ export function filterArray<T>(array: Array<T>, filter: (item: T) => boolean) {
327
+ let index = 0,
328
+ item: T = null,
329
+ length = array.length;
330
+ while (index < length) {
331
+ item = array[index];
332
+ if (filter(item)) {
333
+ ++index;
334
+ } else {
335
+ array[index] = array[length - 1];
336
+ array[length - 1] = item;
337
+ --length;
338
+ }
339
+ }
340
+ array.length = length;
341
+ }
342
+
343
+ /**uuid生成器 */
344
+ export class UuidMaker {
345
+ private uuid = 0;
346
+ create() {
347
+ return ++this.uuid;
348
+ }
349
+ clear() {
350
+ this.uuid = 0;
351
+ }
352
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "ver": "4.0.24",
3
+ "importer": "typescript",
4
+ "imported": true,
5
+ "uuid": "b00142db-7a49-4304-b7c3-b32d540b21ff",
6
+ "files": [],
7
+ "subMetas": {},
8
+ "userData": {}
9
+ }
@@ -0,0 +1,12 @@
1
+ {
2
+ "ver": "1.2.0",
3
+ "importer": "directory",
4
+ "imported": true,
5
+ "uuid": "58ad09b0-68bc-4649-b376-7b1ead265c8d",
6
+ "files": [],
7
+ "subMetas": {},
8
+ "userData": {
9
+ "compressionType": {},
10
+ "isRemoteBundle": {}
11
+ }
12
+ }
package/assets.meta ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "ver": "1.2.0",
3
+ "importer": "directory",
4
+ "imported": true,
5
+ "uuid": "0ca49123-159f-4fd8-9aaf-a585f3a41efe",
6
+ "files": [],
7
+ "subMetas": {},
8
+ "userData": {}
9
+ }
package/index.ts ADDED
@@ -0,0 +1,33 @@
1
+ export * from './assets/ecs';
2
+ export * from './assets/ecs/EcsComponent';
3
+ export * from './assets/ecs/EcsDirty';
4
+ export * from './assets/ecs/EcsEntity';
5
+ export * from './assets/ecs/EcsSingleton';
6
+ export * from './assets/ecs/EcsSystem';
7
+
8
+ export { filter } from './assets/lib/EcsFilter';
9
+ export { ecsclass, getClassByName } from './assets/lib/EcsManager';
10
+
11
+ export { Timer, timer } from './assets/lib/EcsTimer';
12
+ export { Tween, tween } from './assets/lib/EcsTween';
13
+
14
+ export * from './assets/common/component/MoveComponent';
15
+ export * from './assets/common/component/NodeComponent';
16
+ export * from './assets/common/system/MoveSystem';
17
+
18
+ export type {
19
+ IComponent,
20
+ IComponentName,
21
+ IComponentUUID,
22
+ IECS,
23
+ IEntity,
24
+ IEntityUUID,
25
+ IFilter,
26
+ IFlag,
27
+ ISingleton,
28
+ ISystem,
29
+ ITypeofComponent,
30
+ ITypeofEntity,
31
+ ITypeofSingleton,
32
+ ITypeofSystem
33
+ } from './assets/lib/EcsObject';
package/index.ts.meta ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "ver": "4.0.24",
3
+ "importer": "typescript",
4
+ "imported": true,
5
+ "uuid": "a8770faf-a383-4aea-80b1-ae2e228e5376",
6
+ "files": [],
7
+ "subMetas": {},
8
+ "userData": {}
9
+ }
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@cjhd/cj-ecs",
3
+ "version": "1.0.0",
4
+ "engine": ">=3.8.0",
5
+ "description": "ECS模块",
6
+ "main": "index.ts",
7
+ "scripts": {
8
+ "test": "echo \"Error: no test specified\" && exit 1"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://gitee.com/cocos2d-zp/cococs-creator-frame-3d"
13
+ },
14
+ "publishConfig": {
15
+ "access": "public",
16
+ "registry": "https://registry.npmjs.org"
17
+ },
18
+ "author": "超M <402879660@qq.com>",
19
+ "license": "MIT"
20
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "ver": "2.0.1",
3
+ "importer": "json",
4
+ "imported": true,
5
+ "uuid": "a2d886f2-c856-4020-844a-4485f4cb228c",
6
+ "files": [
7
+ ".json"
8
+ ],
9
+ "subMetas": {},
10
+ "userData": {}
11
+ }