-
Notifications
You must be signed in to change notification settings - Fork 893
/
TestProject.js
304 lines (277 loc) · 10.4 KB
/
TestProject.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
* Create a dummy project using libGD.js filled with a
* few elements that can be used for testing.
*
* @param gd The GD instance to use to create the project.
*/
export const makeTestProject = gd => {
// Create and expose a game project
const project = gd.ProjectHelper.createNewGDJSProject();
// Add some fake resources
const resource1 = new gd.ImageResource();
const resource2 = new gd.ImageResource();
const resource3 = new gd.ImageResource();
const resource4 = new gd.ImageResource();
const audioResource1 = new gd.AudioResource();
resource1.setName('fake-image1.png');
resource1.setFile('fake-image1.png');
resource2.setName('fake-image2.png');
resource2.setFile('fake-image2.png');
resource3.setName('icon128.png');
resource3.setFile('res/icon128.png');
resource4.setName('pixi');
resource4.setFile('res/powered-pixijs.png');
audioResource1.setName('fake-audio1.mp3');
audioResource1.setFile('fake-audio1.mp3');
project.getResourcesManager().addResource(resource1);
project.getResourcesManager().addResource(resource2);
project.getResourcesManager().addResource(resource3);
project.getResourcesManager().addResource(resource4);
project.getResourcesManager().addResource(audioResource1);
// Create and expose some objects
const shapePainterObject = new gd.ShapePainterObject('MyShapePainterObject');
const adMobObject = new gd.AdMobObject('MyAdMobObject');
const textObject = new gd.TextObject('MyTextObject');
const tiledSpriteObject = new gd.TiledSpriteObject('MyTiledSpriteObject');
const panelSpriteObject = new gd.PanelSpriteObject('MyPanelSpriteObject');
const spriteObject = new gd.SpriteObject('MySpriteObject');
const spriteObjectWithBehaviors = new gd.SpriteObject(
'MySpriteObjectWithBehaviors'
);
{
const animation = new gd.Animation();
animation.setDirectionsCount(1);
const sprite1 = new gd.Sprite();
sprite1.setImageName('icon128.png');
const sprite2 = new gd.Sprite();
sprite2.setImageName('pixi');
const sprite3 = new gd.Sprite();
sprite3.setImageName('icon128.png');
// Add a few points to the first sprite:
sprite1.getOrigin().setX(10);
sprite1.getOrigin().setY(20);
sprite1.getCenter().setX(100);
sprite1.getCenter().setY(200);
const headCustomPoint = new gd.Point('Head');
headCustomPoint.setX(40);
headCustomPoint.setY(50);
sprite1.addPoint(headCustomPoint);
animation.getDirection(0).addSprite(sprite1);
animation.getDirection(0).addSprite(sprite2);
animation.getDirection(0).addSprite(sprite3);
spriteObject.addAnimation(animation);
}
{
const animation = new gd.Animation();
animation.setDirectionsCount(1);
const sprite1 = new gd.Sprite();
sprite1.setImageName('icon128.png');
const sprite2 = new gd.Sprite();
sprite2.setImageName('icon128.png');
const sprite3 = new gd.Sprite();
sprite3.setImageName('icon128.png');
const sprite4 = new gd.Sprite();
sprite4.setImageName('icon128.png');
animation.getDirection(0).addSprite(sprite1);
animation.getDirection(0).addSprite(sprite2);
animation.getDirection(0).addSprite(sprite3);
animation.getDirection(0).addSprite(sprite4);
spriteObject.addAnimation(animation);
}
{
const animation = new gd.Animation();
animation.setDirectionsCount(1);
const sprite1 = new gd.Sprite();
sprite1.setImageName('pixi');
animation.getDirection(0).addSprite(sprite1);
spriteObject.addAnimation(animation);
}
spriteObjectWithBehaviors.addNewBehavior(
project,
'PlatformBehavior::PlatformerObjectBehavior',
'PlatformerObject'
);
spriteObjectWithBehaviors.addNewBehavior(
project,
'DraggableBehavior::Draggable',
'Draggable'
);
// Layout
const testLayout = project.insertNewLayout('TestLayout', 0);
testLayout.insertObject(shapePainterObject, 0);
testLayout.insertObject(textObject, 0);
testLayout.insertObject(tiledSpriteObject, 0);
testLayout.insertObject(panelSpriteObject, 0);
testLayout.insertObject(spriteObject, 0);
testLayout.insertObject(spriteObjectWithBehaviors, 0);
const group1 = new gd.ObjectGroup();
group1.setName('GroupOfSprites');
group1.addObject('MySpriteObject');
const group2 = new gd.ObjectGroup();
group2.setName('GroupOfObjects');
group2.addObject('MySpriteObject');
group2.addObject('MyTextObject');
testLayout.getObjectGroups().insert(group1, 0);
testLayout.getObjectGroups().insert(group2, 1);
const testLayoutInstance1 = testLayout
.getInitialInstances()
.insertNewInitialInstance();
testLayoutInstance1.setX(10);
testLayoutInstance1.setY(15);
// Add layers
testLayout.insertNewLayer("GUI", 0);
testLayout.insertNewLayer("OtherLayer", 1);
//Add a few variables
const testLayoutVariables = testLayout.getVariables();
testLayoutVariables
.insert('Variable1', new gd.Variable(), 0)
.setString('A multiline\nstr value');
testLayoutVariables
.insert('Variable2', new gd.Variable(), 1)
.setString('123456');
const variable3 = new gd.Variable();
variable3.getChild('Child1').setString('Child1 str value');
variable3.getChild('Child2').setString('7891011');
variable3
.getChild('Child3')
.getChild('SubChild1')
.setString('Hello\nMultiline\nWorld');
testLayoutVariables.insert('Variable3', variable3, 2);
//Create a few events
//Add a new "standard" event to the scene:
var evt = testLayout
.getEvents()
.insertNewEvent(project, 'BuiltinCommonInstructions::Standard', 0);
var evt2 = testLayout
.getEvents()
.insertNewEvent(project, 'BuiltinCommonInstructions::Standard', 1);
var evt3 = testLayout
.getEvents()
.insertNewEvent(project, 'BuiltinCommonInstructions::ForEach', 2);
var evt4 = testLayout
.getEvents()
.insertNewEvent(project, 'BuiltinCommonInstructions::While', 3);
var evt5 = testLayout
.getEvents()
.insertNewEvent(project, 'BuiltinCommonInstructions::Repeat', 4);
var evt6 = testLayout
.getEvents()
.insertNewEvent(project, 'BuiltinCommonInstructions::Group', 5);
var evt7 = testLayout
.getEvents()
.insertNewEvent(project, 'BuiltinCommonInstructions::Link', 6);
var evt8 = testLayout
.getEvents()
.insertNewEvent(project, 'BuiltinCommonInstructions::JsCode', 7);
const groupEvent = gd.asGroupEvent(evt6);
groupEvent.setName('Group #1');
const jsCodeEvent = gd.asJsCodeEvent(evt8);
jsCodeEvent.setInlineCode('console.log("Hello, World!");');
jsCodeEvent.setParameterObjects('MyObject');
const makeKeyPressedCondition = () => {
const condition = new gd.Instruction();
condition.setType('KeyPressed');
condition.setParametersCount(2);
condition.setParameter(1, 'Space');
return condition;
};
const makeDeleteAction = objectToDelete => {
var action = new gd.Instruction(); //Add a simple action
action.setType('Delete');
action.setParametersCount(2);
action.setParameter(0, objectToDelete);
return action;
};
var standardEvt = gd.asStandardEvent(evt);
standardEvt.getConditions().push_back(makeKeyPressedCondition());
standardEvt.getActions().push_back(makeDeleteAction('MyCharacter'));
//Add a few sub events:
{
const subEvent = evt
.getSubEvents()
.insertNewEvent(project, 'BuiltinCommonInstructions::Standard', 0);
const subStandardEvt = gd.asStandardEvent(subEvent);
subStandardEvt.getConditions().push_back(makeKeyPressedCondition());
subStandardEvt.getActions().push_back(makeDeleteAction('MyCharacter1'));
subStandardEvt.getActions().push_back(makeDeleteAction('MyCharacter2'));
}
{
const subEvent = evt
.getSubEvents()
.insertNewEvent(project, 'BuiltinCommonInstructions::Comment', 0);
const subCommentEvt = gd.asCommentEvent(subEvent);
subCommentEvt.setComment(
'Kain is deified. The clans tell tales of him, few know the truth. He was mortal once, as were we all. However, his contempt for humanity drove him to create me and my brethren. I am Raziel, first born of his lieutenants. I stood with Kain and my brethren at the dawn of the empire. I have served him a millennium. Over time we became less human and more ... divine.'
);
}
for (let i = 0; i < 20; ++i) {
const evt = testLayout
.getEvents()
.insertNewEvent(
project,
'BuiltinCommonInstructions::Standard',
testLayout.getEvents().getEventsCount()
);
const standardEvt = gd.asStandardEvent(evt);
standardEvt.getConditions().push_back(makeKeyPressedCondition());
standardEvt.getActions().push_back(makeDeleteAction('OtherObject' + i));
}
// Add a disabled event with a sub event
{
const disabledEvent = testLayout
.getEvents()
.insertNewEvent(
project,
'BuiltinCommonInstructions::Standard',
testLayout.getEvents().getEventsCount()
);
const disabledStandardEvt = gd.asStandardEvent(disabledEvent);
disabledStandardEvt.setDisabled(true);
disabledStandardEvt.getConditions().push_back(makeKeyPressedCondition());
disabledStandardEvt.getActions().push_back(makeDeleteAction('YetAnotherObject'));
const subEvent = disabledStandardEvt
.getSubEvents()
.insertNewEvent(project, 'BuiltinCommonInstructions::Standard', 0);
const subStandardEvt = gd.asStandardEvent(subEvent);
subStandardEvt.getConditions().push_back(makeKeyPressedCondition());
subStandardEvt.getActions().push_back(makeDeleteAction('MyCharacter1'));
subStandardEvt.getActions().push_back(makeDeleteAction('MyCharacter2'));
}
const testInstruction = makeKeyPressedCondition();
// Global objects
const globalTextObject = new gd.TextObject('GlobalTextObject');
const globalTiledSpriteObject = new gd.TiledSpriteObject(
'GlobalTiledSpriteObject'
);
project.insertObject(globalTextObject, 0);
project.insertObject(globalTiledSpriteObject, 0);
// External events
const testExternalEvents1 = project.insertNewExternalEvents(
'TestExternalEvents1',
0
);
const testExternalEvents2 = project.insertNewExternalEvents(
'TestExternalEvents2',
1
);
// Empty layout
const emptyLayout = project.insertNewLayout('EmptyLayout', 1);
return {
project,
shapePainterObject,
adMobObject,
textObject,
tiledSpriteObject,
panelSpriteObject,
spriteObject,
spriteObjectWithBehaviors,
testLayout,
group1,
group2,
testLayoutInstance1,
testInstruction,
testExternalEvents1,
testExternalEvents2,
emptyLayout,
};
};