cocos2d-cli 1.4.0 → 1.5.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 (47) hide show
  1. package/README.md +142 -0
  2. package/bin/cocos2d-cli.js +21 -12
  3. package/package.json +1 -1
  4. package/src/commands/add-component.js +77 -85
  5. package/src/commands/add.js +116 -212
  6. package/src/commands/create-scene.js +133 -109
  7. package/src/commands/get.js +113 -18
  8. package/src/commands/prefab-create.js +49 -139
  9. package/src/commands/remove-component.js +111 -0
  10. package/src/commands/remove.js +59 -122
  11. package/src/commands/set.js +121 -36
  12. package/src/commands/tree.js +3 -8
  13. package/src/lib/cc/CCButton.js +122 -0
  14. package/src/lib/cc/CCCamera.js +93 -0
  15. package/src/lib/cc/CCCanvas.js +64 -0
  16. package/src/lib/cc/CCColor.js +32 -0
  17. package/src/lib/cc/CCComponent.js +60 -0
  18. package/src/lib/cc/CCLabel.js +109 -0
  19. package/src/lib/cc/CCNode.js +242 -0
  20. package/src/lib/cc/CCObject.js +23 -0
  21. package/src/lib/cc/CCPrefab.js +242 -0
  22. package/src/lib/cc/CCRect.js +32 -0
  23. package/src/lib/cc/CCScene.js +42 -0
  24. package/src/lib/cc/CCSceneAsset.js +271 -0
  25. package/src/lib/cc/CCSize.js +26 -0
  26. package/src/lib/cc/CCSprite.js +82 -0
  27. package/src/lib/cc/CCTrs.js +74 -0
  28. package/src/lib/cc/CCVec2.js +26 -0
  29. package/src/lib/cc/CCVec3.js +29 -0
  30. package/src/lib/cc/CCWidget.js +98 -0
  31. package/src/lib/cc/index.js +40 -0
  32. package/src/lib/fire-utils.js +75 -1
  33. package/src/lib/json-parser.js +166 -0
  34. package/src/lib/node-utils.js +64 -28
  35. package/src/lib/templates.js +31 -194
  36. package/src/lib/utils.js +63 -0
  37. package/data/prefab-template.json +0 -72
  38. package/data/scene-template.json +0 -241
  39. package/src/lib/components/button.js +0 -137
  40. package/src/lib/components/camera.js +0 -107
  41. package/src/lib/components/canvas.js +0 -89
  42. package/src/lib/components/index.js +0 -157
  43. package/src/lib/components/label.js +0 -120
  44. package/src/lib/components/layout.js +0 -110
  45. package/src/lib/components/particle-system.js +0 -160
  46. package/src/lib/components/sprite.js +0 -98
  47. package/src/lib/components/widget.js +0 -122
@@ -1,137 +0,0 @@
1
- /**
2
- * cc.Button 组件
3
- */
4
-
5
- const { generateId } = require('../utils');
6
-
7
- const BUTTON_NORMAL_SPRITE = { "__uuid__": "f0048c10-f03e-4c97-b9d3-3506e1d58952" };
8
- const BUTTON_PRESSED_SPRITE = { "__uuid__": "e9ec654c-97a2-4787-9325-e6a10375219a" };
9
- const BUTTON_DISABLED_SPRITE = { "__uuid__": "29158224-f8dd-4661-a796-1ffab537140e" };
10
-
11
- // 属性映射
12
- const PROP_MAP = {
13
- 'interactable': '_N$interactable',
14
- 'transition': '_N$transition',
15
- 'zoomScale': 'zoomScale',
16
- 'duration': 'duration',
17
- 'target': '_N$target'
18
- };
19
-
20
- // 枚举值
21
- const ENUMS = {
22
- transition: ['NONE', 'COLOR', 'SPRITE', 'SCALE']
23
- };
24
-
25
- /**
26
- * 创建 Button 组件
27
- * @param {number} nodeId - 节点索引
28
- * @returns {object} 组件数据
29
- */
30
- function create(nodeId) {
31
- return {
32
- "__type__": "cc.Button",
33
- "_name": "",
34
- "_objFlags": 0,
35
- "node": { "__id__": nodeId },
36
- "_enabled": true,
37
- "_normalMaterial": null,
38
- "_grayMaterial": null,
39
- "duration": 0.1,
40
- "zoomScale": 1.2,
41
- "clickEvents": [],
42
- "_N$interactable": true,
43
- "_N$enableAutoGrayEffect": false,
44
- "_N$transition": 3,
45
- "transition": 3,
46
- "_N$normalColor": {
47
- "__type__": "cc.Color",
48
- "r": 255,
49
- "g": 255,
50
- "b": 255,
51
- "a": 255
52
- },
53
- "_N$pressedColor": {
54
- "__type__": "cc.Color",
55
- "r": 200,
56
- "g": 200,
57
- "b": 200,
58
- "a": 255
59
- },
60
- "pressedColor": {
61
- "__type__": "cc.Color",
62
- "r": 200,
63
- "g": 200,
64
- "b": 200,
65
- "a": 255
66
- },
67
- "_N$hoverColor": {
68
- "__type__": "cc.Color",
69
- "r": 255,
70
- "g": 255,
71
- "b": 255,
72
- "a": 255
73
- },
74
- "hoverColor": {
75
- "__type__": "cc.Color",
76
- "r": 255,
77
- "g": 255,
78
- "b": 255,
79
- "a": 255
80
- },
81
- "_N$disabledColor": {
82
- "__type__": "cc.Color",
83
- "r": 120,
84
- "g": 120,
85
- "b": 120,
86
- "a": 200
87
- },
88
- "_N$normalSprite": BUTTON_NORMAL_SPRITE,
89
- "_N$pressedSprite": BUTTON_PRESSED_SPRITE,
90
- "pressedSprite": BUTTON_PRESSED_SPRITE,
91
- "_N$hoverSprite": BUTTON_NORMAL_SPRITE,
92
- "hoverSprite": BUTTON_NORMAL_SPRITE,
93
- "_N$disabledSprite": BUTTON_DISABLED_SPRITE,
94
- "_N$target": null,
95
- "_id": generateId()
96
- };
97
- }
98
-
99
- /**
100
- * 应用属性
101
- * @param {object} comp - 组件对象
102
- * @param {object} props - 属性对象
103
- */
104
- function applyProps(comp, props) {
105
- if (!props) return;
106
-
107
- for (const [key, value] of Object.entries(props)) {
108
- const compKey = PROP_MAP[key];
109
- if (compKey) {
110
- comp[compKey] = value;
111
- }
112
- }
113
- }
114
-
115
- /**
116
- * 提取属性(用于显示)
117
- * @param {object} comp - 组件对象
118
- * @returns {object} 提取的属性
119
- */
120
- function extractProps(comp) {
121
- return {
122
- interactable: comp._N$interactable,
123
- transition: ENUMS.transition[comp._N$transition] || comp._N$transition,
124
- zoomScale: comp.zoomScale,
125
- duration: comp.duration
126
- };
127
- }
128
-
129
- module.exports = {
130
- type: 'button',
131
- ccType: 'cc.Button',
132
- create,
133
- applyProps,
134
- extractProps,
135
- PROP_MAP,
136
- ENUMS
137
- };
@@ -1,107 +0,0 @@
1
- /**
2
- * cc.Camera 组件
3
- */
4
-
5
- const { generateId, parseColorToCcColor } = require('../utils');
6
-
7
- // 属性映射
8
- const PROP_MAP = {
9
- 'depth': '_depth',
10
- 'zoomRatio': '_zoomRatio',
11
- 'ortho': '_ortho',
12
- 'orthoSize': '_orthoSize',
13
- 'cullingMask': '_cullingMask',
14
- 'backgroundColor': '_backgroundColor',
15
- 'fov': '_fov',
16
- 'nearClip': '_nearClip',
17
- 'farClip': '_farClip'
18
- };
19
-
20
- /**
21
- * 创建 Camera 组件
22
- * @param {number} nodeId - 节点索引
23
- * @returns {object} 组件数据
24
- */
25
- function create(nodeId) {
26
- return {
27
- "__type__": "cc.Camera",
28
- "_name": "",
29
- "_objFlags": 0,
30
- "node": { "__id__": nodeId },
31
- "_enabled": true,
32
- "_cullingMask": 4294967295,
33
- "_clearFlags": 7,
34
- "_backgroundColor": {
35
- "__type__": "cc.Color",
36
- "r": 0,
37
- "g": 0,
38
- "b": 0,
39
- "a": 255
40
- },
41
- "_depth": -1,
42
- "_zoomRatio": 1,
43
- "_targetTexture": null,
44
- "_fov": 60,
45
- "_orthoSize": 10,
46
- "_nearClip": 1,
47
- "_farClip": 4096,
48
- "_ortho": true,
49
- "_rect": {
50
- "__type__": "cc.Rect",
51
- "x": 0,
52
- "y": 0,
53
- "width": 1,
54
- "height": 1
55
- },
56
- "_renderStages": 1,
57
- "_alignWithScreen": true,
58
- "_id": generateId()
59
- };
60
- }
61
-
62
- /**
63
- * 应用属性
64
- * @param {object} comp - 组件对象
65
- * @param {object} props - 属性对象
66
- */
67
- function applyProps(comp, props) {
68
- if (!props) return;
69
-
70
- for (const [key, value] of Object.entries(props)) {
71
- const compKey = PROP_MAP[key];
72
- if (compKey) {
73
- // backgroundColor 特殊处理
74
- if (key === 'backgroundColor' && typeof value === 'string') {
75
- const ccColor = parseColorToCcColor(value);
76
- if (ccColor) {
77
- comp[compKey] = ccColor;
78
- }
79
- } else {
80
- comp[compKey] = value;
81
- }
82
- }
83
- }
84
- }
85
-
86
- /**
87
- * 提取属性(用于显示)
88
- * @param {object} comp - 组件对象
89
- * @returns {object} 提取的属性
90
- */
91
- function extractProps(comp) {
92
- return {
93
- depth: comp._depth,
94
- zoomRatio: comp._zoomRatio,
95
- ortho: comp._ortho,
96
- cullingMask: comp._cullingMask
97
- };
98
- }
99
-
100
- module.exports = {
101
- type: 'camera',
102
- ccType: 'cc.Camera',
103
- create,
104
- applyProps,
105
- extractProps,
106
- PROP_MAP
107
- };
@@ -1,89 +0,0 @@
1
- /**
2
- * cc.Canvas 组件
3
- */
4
-
5
- const { generateId } = require('../utils');
6
-
7
- // 属性映射
8
- const PROP_MAP = {
9
- 'designResolution': '_designResolution',
10
- 'fitWidth': '_fitWidth',
11
- 'fitHeight': '_fitHeight'
12
- };
13
-
14
- /**
15
- * 创建 Canvas 组件
16
- * @param {number} nodeId - 节点索引
17
- * @returns {object} 组件数据
18
- */
19
- function create(nodeId) {
20
- return {
21
- "__type__": "cc.Canvas",
22
- "_name": "",
23
- "_objFlags": 0,
24
- "node": { "__id__": nodeId },
25
- "_enabled": true,
26
- "_designResolution": {
27
- "__type__": "cc.Size",
28
- "width": 960,
29
- "height": 640
30
- },
31
- "_fitWidth": false,
32
- "_fitHeight": true,
33
- "_id": generateId()
34
- };
35
- }
36
-
37
- /**
38
- * 应用属性
39
- * @param {object} comp - 组件对象
40
- * @param {object} props - 属性对象
41
- */
42
- function applyProps(comp, props) {
43
- if (!props) return;
44
-
45
- for (const [key, value] of Object.entries(props)) {
46
- const compKey = PROP_MAP[key];
47
- if (compKey) {
48
- // designResolution 特殊处理
49
- if (key === 'designResolution' && Array.isArray(value)) {
50
- comp[compKey] = { "__type__": "cc.Size", "width": value[0], "height": value[1] };
51
- } else {
52
- comp[compKey] = value;
53
- }
54
- }
55
- }
56
- }
57
-
58
- /**
59
- * 提取属性(用于显示)
60
- * @param {object} comp - 组件对象
61
- * @returns {object} 提取的属性
62
- */
63
- function extractProps(comp) {
64
- const clean = (obj) => {
65
- if (!obj || typeof obj !== 'object') return obj;
66
- const result = {};
67
- for (const [k, v] of Object.entries(obj)) {
68
- if (k !== '__type__') {
69
- result[k] = v;
70
- }
71
- }
72
- return result;
73
- };
74
-
75
- return {
76
- designResolution: clean(comp._designResolution),
77
- fitWidth: comp._fitWidth,
78
- fitHeight: comp._fitHeight
79
- };
80
- }
81
-
82
- module.exports = {
83
- type: 'canvas',
84
- ccType: 'cc.Canvas',
85
- create,
86
- applyProps,
87
- extractProps,
88
- PROP_MAP
89
- };
@@ -1,157 +0,0 @@
1
- /**
2
- * 组件模块入口
3
- * 统一导出所有组件
4
- */
5
-
6
- const sprite = require('./sprite');
7
- const label = require('./label');
8
- const button = require('./button');
9
- const widget = require('./widget');
10
- const layout = require('./layout');
11
- const canvas = require('./canvas');
12
- const camera = require('./camera');
13
- const particleSystem = require('./particle-system');
14
-
15
- // 组件类型映射
16
- const components = {
17
- sprite,
18
- label,
19
- button,
20
- widget,
21
- layout,
22
- canvas,
23
- camera,
24
- particleSystem,
25
- // 别名
26
- particle: particleSystem
27
- };
28
-
29
- // 类型名称映射(支持多种写法)
30
- const typeAliases = {
31
- 'sprite': 'sprite',
32
- 'label': 'label',
33
- 'button': 'button',
34
- 'widget': 'widget',
35
- 'layout': 'layout',
36
- 'canvas': 'canvas',
37
- 'camera': 'camera',
38
- 'particle': 'particleSystem',
39
- 'particlesystem': 'particleSystem',
40
- 'particleSystem': 'particleSystem'
41
- };
42
-
43
- /**
44
- * 获取组件模块
45
- * @param {string} type - 组件类型
46
- * @returns {object|null} 组件模块
47
- */
48
- function getComponent(type) {
49
- const normalizedName = typeAliases[type.toLowerCase()];
50
- return normalizedName ? components[normalizedName] : null;
51
- }
52
-
53
- /**
54
- * 创建组件
55
- * @param {string} type - 组件类型
56
- * @param {number} nodeId - 节点索引
57
- * @returns {object|null} 组件数据
58
- */
59
- function createComponent(type, nodeId) {
60
- const comp = getComponent(type);
61
- return comp ? comp.create(nodeId) : null;
62
- }
63
-
64
- /**
65
- * 应用组件属性
66
- * @param {object} comp - 组件对象
67
- * @param {object} props - 属性对象
68
- * @param {object} node - 节点对象(可选)
69
- */
70
- function applyComponentProps(comp, props, node) {
71
- if (!comp || !props) return;
72
-
73
- const ccType = comp.__type__;
74
- const compModule = getComponentByCcType(ccType);
75
-
76
- if (compModule && compModule.applyProps) {
77
- compModule.applyProps(comp, props, node);
78
- }
79
- }
80
-
81
- /**
82
- * 提取组件属性
83
- * @param {object} comp - 组件对象
84
- * @returns {object} 提取的属性
85
- */
86
- function extractComponentProps(comp) {
87
- if (!comp) return null;
88
-
89
- const ccType = comp.__type__;
90
- const compModule = getComponentByCcType(ccType);
91
-
92
- const base = comp._enabled ? { type: ccType } : { type: ccType, enabled: false };
93
-
94
- if (compModule && compModule.extractProps) {
95
- return { ...base, ...compModule.extractProps(comp) };
96
- }
97
-
98
- // 默认提取:非 _ 开头的属性
99
- const result = { ...base };
100
- for (const key of Object.keys(comp)) {
101
- if (!key.startsWith('_') && key !== '__type__') {
102
- result[key] = comp[key];
103
- }
104
- }
105
- return result;
106
- }
107
-
108
- /**
109
- * 通过 cc.__type__ 获取组件模块
110
- * @param {string} ccType - cc 类型名
111
- * @returns {object|null} 组件模块
112
- */
113
- function getComponentByCcType(ccType) {
114
- for (const comp of Object.values(components)) {
115
- if (comp.ccType === ccType) {
116
- return comp;
117
- }
118
- }
119
- return null;
120
- }
121
-
122
- /**
123
- * 解析组件定义
124
- * 支持两种格式:
125
- * 1. 字符串: "sprite"
126
- * 2. 对象: { "type": "sprite", "sizeMode": 1 }
127
- * @param {string|object} compDef - 组件定义
128
- * @returns {object|null} - { type, props } 或 null
129
- */
130
- function parseComponent(compDef) {
131
- if (typeof compDef === 'string') {
132
- const normalizedName = typeAliases[compDef.toLowerCase()];
133
- return normalizedName ? { type: normalizedName, props: {} } : null;
134
- }
135
-
136
- if (typeof compDef === 'object' && compDef.type) {
137
- const normalizedName = typeAliases[compDef.type.toLowerCase()];
138
- if (!normalizedName) return null;
139
-
140
- const props = { ...compDef };
141
- delete props.type;
142
- return { type: normalizedName, props };
143
- }
144
-
145
- return null;
146
- }
147
-
148
- module.exports = {
149
- components,
150
- typeAliases,
151
- getComponent,
152
- getComponentByCcType,
153
- createComponent,
154
- applyComponentProps,
155
- extractComponentProps,
156
- parseComponent
157
- };
@@ -1,120 +0,0 @@
1
- /**
2
- * cc.Label 组件
3
- */
4
-
5
- const { generateId, parseColorToCcColor } = require('../utils');
6
-
7
- const DEFAULT_MATERIAL = { "__uuid__": "eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432" };
8
-
9
- // 属性映射
10
- const PROP_MAP = {
11
- 'string': '_string',
12
- 'fontSize': '_fontSize',
13
- 'lineHeight': '_lineHeight',
14
- 'horizontalAlign': '_N$horizontalAlign',
15
- 'verticalAlign': '_N$verticalAlign',
16
- 'overflow': '_N$overflow',
17
- 'fontFamily': '_N$fontFamily',
18
- 'wrap': '_enableWrapText',
19
- 'color': '_color' // 特殊处理,设置到节点
20
- };
21
-
22
- // 枚举值
23
- const ENUMS = {
24
- horizontalAlign: ['LEFT', 'CENTER', 'RIGHT'],
25
- verticalAlign: ['TOP', 'CENTER', 'BOTTOM'],
26
- overflow: ['NONE', 'CLAMP', 'SHRINK', 'RESIZE_HEIGHT']
27
- };
28
-
29
- /**
30
- * 创建 Label 组件
31
- * @param {number} nodeId - 节点索引
32
- * @returns {object} 组件数据
33
- */
34
- function create(nodeId) {
35
- return {
36
- "__type__": "cc.Label",
37
- "_name": "",
38
- "_objFlags": 0,
39
- "node": { "__id__": nodeId },
40
- "_enabled": true,
41
- "_materials": [DEFAULT_MATERIAL],
42
- "_srcBlendFactor": 770,
43
- "_dstBlendFactor": 771,
44
- "_string": "Label",
45
- "_N$string": "Label",
46
- "_fontSize": 40,
47
- "_lineHeight": 40,
48
- "_enableWrapText": true,
49
- "_N$file": null,
50
- "_isSystemFontUsed": true,
51
- "_spacingX": 0,
52
- "_batchAsBitmap": false,
53
- "_styleFlags": 0,
54
- "_underlineHeight": 0,
55
- "_N$horizontalAlign": 1,
56
- "_N$verticalAlign": 1,
57
- "_N$fontFamily": "Arial",
58
- "_N$overflow": 0,
59
- "_N$cacheMode": 0,
60
- "_id": generateId()
61
- };
62
- }
63
-
64
- /**
65
- * 应用属性
66
- * @param {object} comp - 组件对象
67
- * @param {object} props - 属性对象
68
- * @param {object} node - 节点对象(可选,用于 color)
69
- */
70
- function applyProps(comp, props, node) {
71
- if (!props) return;
72
-
73
- // color 特殊处理:设置到节点
74
- if (props.color && node) {
75
- const ccColor = parseColorToCcColor(props.color);
76
- if (ccColor) {
77
- node._color = ccColor;
78
- }
79
- delete props.color;
80
- }
81
-
82
- for (const [key, value] of Object.entries(props)) {
83
- const compKey = PROP_MAP[key];
84
- if (compKey) {
85
- comp[compKey] = value;
86
- // string 需要同步到 _N$string
87
- if (key === 'string') {
88
- comp._N$string = value;
89
- }
90
- }
91
- }
92
- }
93
-
94
- /**
95
- * 提取属性(用于显示)
96
- * @param {object} comp - 组件对象
97
- * @returns {object} 提取的属性
98
- */
99
- function extractProps(comp) {
100
- return {
101
- string: comp._string,
102
- fontSize: comp._fontSize,
103
- lineHeight: comp._lineHeight,
104
- horizontalAlign: ENUMS.horizontalAlign[comp._N$horizontalAlign] || comp._N$horizontalAlign,
105
- verticalAlign: ENUMS.verticalAlign[comp._N$verticalAlign] || comp._N$verticalAlign,
106
- overflow: ENUMS.overflow[comp._N$overflow] || comp._N$overflow,
107
- fontFamily: comp._N$fontFamily,
108
- enableWrapText: comp._enableWrapText
109
- };
110
- }
111
-
112
- module.exports = {
113
- type: 'label',
114
- ccType: 'cc.Label',
115
- create,
116
- applyProps,
117
- extractProps,
118
- PROP_MAP,
119
- ENUMS
120
- };