/
AGK Exporter.rs
529 lines (447 loc) · 19 KB
/
AGK Exporter.rs
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
// ========================================================================
// Name: AGK Rube Exporter v1.04
// Tags: AGKExporter
//
// Version: 1.04
// Created: 28/01/2014
// By: Stuart Tresadern
//
// RUBE Editor Version: 1.5.4
// AGK Version: 108 & current Alpha 2
// ----------------------------------------------------------------------------------------------------------------------------------
// ========================================================================
// Globals
// ----------------------------------------------------------------------------------------------------------------------------------
string seperator = "|";
string newline = "\n";
//string defaultExportPath = "C:\\Development\\TrezSoft\\AGK\\RubeLoader\\media\\";
string defaultExportPath ="C:\\Development\\TrezSoft\\AGK-c++\\template_windows_vs2010\\Debug\\media\\";
string defaultFileName = "myrubescene.txt";
string pathSepartor = "/";
string exportText;
// ========================================================================
// main
// ----------------------------------------------------------------------------------------------------------------------------------
void main()
{
array<int> imageOrder(getAllImages().length);
print("AGK Exporter Started");
print(" Getting Images for AGK image loader.");
imageOrder = GetAllImageFiles();
print(" " + imageOrder.length + " images processed");
print(" Getting Image sprites for AGK");
print(" " + GetAllImageSprites(imageOrder) + " image sprites processed");
print(" Getting Physics sprites for AGK");
print(" " + GetAllPhysicsSprites(imageOrder) + " physics sprites processed");
print(" Getting Joints for AGK");
print(" " + GetAllPhysicsJoints() + " joints processed");
print(" Saving Export File : " + defaultExportPath + defaultFileName);
print("AGK Export complete : " + SaveExportFile());
}
// ========================================================================
// Save the AGK Export File
// ----------------------------------------------------------------------------------------------------------------------------------
bool SaveExportFile()
{
bool exportfile = writeFile(defaultExportPath + defaultFileName , exportText);
return exportfile;
}
// ========================================================================
// Gets all the image files so that they can be pre loaded in AGK
// ----------------------------------------------------------------------------------------------------------------------------------
int[] GetAllImageFiles()
{
image[] bimage = getAllImages();
array<int> imageOrder(bimage.length);
for (uint i = 0; i < bimage.length(); i++)
{
if(bimage[i].valid)
{
int id = bimage[i].id;
exportText += "1" + seperator + id + seperator + GetFileNameFromPath(bimage[i].getFile()) + newline;
imageOrder[i] = bimage[i].getRenderOrder();
}
}
imageOrder.reverse();
return imageOrder;
}
// ========================================================================
// Gets all the image sprites for AGK (Images that are not attached to a physics body
// ----------------------------------------------------------------------------------------------------------------------------------
int GetAllImageSprites(int[] imageOrder)
{
image[] bimages = getAllImages();
for (uint i = 0; i < bimages.length(); i++)
{
if(!bimages[i].getBody().valid)
{
int id = bimages[i].id;
float positionX = bimages[i].center.x ;
float positionY = bimages[i].center.y ;
float angle = rd(bimages[i].getAngle());
float scale = bimages[i].getScale();
float aspect = bimages[i].getAspectScale();
int flipped = BoolToInt(bimages[i].getFlip());
int renderOrder = imageOrder[i];
string colorTint = bimages[i].getColorTint().getStr255();
exportText += "2" + seperator + id + seperator + positionX + seperator + positionY + seperator + angle + seperator + scale + seperator + aspect + seperator + flipped + seperator + renderOrder + seperator + colorTint + newline;
}
}
return 0;
}
// ========================================================================
// Gets all physics bodies
// Bodies without images have an image id of -1 to allow for dummy sprite creation in AGK
// Bodies will only include the first assigned image, AGK only supports one image per body.
// If any fixture has isSensor set to true the body will be set as a sensor in AGK
// Friction & Restitution will be returned from the average of all fixtures on the body.
// Filter category, Mask Bits and group index will be taken from the first returned fixture
// Vertices limit in AGK are between 2 - 12 the results window will show warnings for shapes that exceed this range.
// ----------------------------------------------------------------------------------------------------------------------------------
int GetAllPhysicsSprites(int[] imageOrder)
{
body[] worldbodies = getAllBodies();
for ( uint i = 0; i < worldbodies .length() ; i++)
{
int imageId = GetBodyImageId( worldbodies[i]);
int spriteId = worldbodies[i].id;
int type = BodyTypeConvert(worldbodies[i].getType());
int bullet = BoolToInt(worldbodies[i].isBullet());
int fixedrotation = BoolToInt(!worldbodies[i].isFixedRotation());
float positionX = 0.0;
float positionY = 0.0;
float angle = 0.0;
float scale = 0.0;
float aspect = 0.0;
int flipped = 0;
int renderOrder =0;
string colorTint ="";
if ( imageId != -1)
{
image[] bimages = worldbodies[i].getImages();
positionX = worldbodies[i].pos.x + (bimages[0].center.x );
positionY = worldbodies[i].pos.y + (bimages[0].center.y );
angle = rd(worldbodies[i].angle);
scale = bimages[0].getScale();
aspect = bimages[0].getAspectScale();
flipped = BoolToInt(bimages[0].getFlip());
renderOrder = imageOrder[i];
colorTint = bimages[0].getColorTint().getStr255();
}
else
{
positionX = worldbodies[i].pos.x ;
positionY = worldbodies[i].pos.y ;
angle = rd(worldbodies[i].angle);
}
fixture[] bfixtures = worldbodies[i].getFixtures();
float friction = GetAverageFriction(bfixtures);
float restitution = GetAverageRestitution(bfixtures);
int sensor = CheckFixturesForSensor(bfixtures);
int categorybits = 1;
int maskbits = 65535;
int filtergroup = 0;
if(bfixtures[0].valid)
{
categorybits = bfixtures[0].getFilterCategoryBits();
maskbits = bfixtures[0].getFilterMaskBits();
filtergroup = bfixtures[0].getFilterGroupIndex();
}
exportText += "3" + seperator + spriteId + seperator + imageId + seperator + type + seperator + positionX + seperator + positionY + seperator + angle + seperator + bullet + seperator + fixedrotation + seperator + worldbodies[i].getLinearVelocity().x + seperator + worldbodies[i].getLinearVelocity().y + seperator + worldbodies[i].getAngularVelocity() + seperator + worldbodies[i].getLinearDamping() + seperator + worldbodies[i].getAngularDamping() + seperator + worldbodies[i].getMass() + seperator + worldbodies[i].getLocalCenter().x + seperator + worldbodies[i].getLocalCenter().y + seperator + sensor + seperator + friction + seperator + restitution + seperator + scale + seperator + aspect + seperator + flipped + seperator + renderOrder + seperator + colorTint + seperator + filtergroup + seperator + categorybits + seperator + maskbits + newline;
for (uint f = 0; f < bfixtures.length(); f++)
{
int fixtureType = bfixtures[f].getShape(0).type;
int firstFixture = 0;
if ( f == 0)
{
firstFixture = 1;
}
if (fixtureType == 0 ) // circle
{
exportText += "5" + seperator + spriteId + seperator + firstFixture + seperator + bfixtures[f].getVertex(0).x + seperator + bfixtures[f].getVertex(0).y + seperator + bfixtures[f].getShape(0).getRadius() + newline;
}
else if (fixtureType == 1 || fixtureType == 2)
{
// Check Winding
if (ClockWise( bfixtures[f].getVertices()) == -1)
{
bfixtures[f].reverseWinding();
}
if(Convex(bfixtures[f].getVertices()) == -1)
{
print(" ** WARNING ** Body " + worldbodies[i].id + ":" + worldbodies[i].getName() + " has possible complex fixtures (Concave).");
print(" Fixture " + bfixtures[f].id + ":" + bfixtures[f].getName() );
print(" TIP: Use the action menu to split the fixture into convex polygons." );
}
vertex[] fvertices = bfixtures[f].getVertices();
string vertstring = "";
for (int v = 0; v < bfixtures[f].getNumVertices(); v++)
{
string sep = ",";
if (v == bfixtures[f].getNumVertices() -1)
{
sep ="";
}
vertstring += fvertices[v].x +","+ fvertices[v].y + sep;
}
if ( bfixtures[f].getNumVertices() > 12 )
{
print(" ** WARNING ** Body " + worldbodies[i].id + ":" + worldbodies[i].getName() + " - fixture has " + bfixtures[f].getNumVertices() + "vetices." );
print(" AGK Supports a maximum of 12 vertices, please reduce the vertex count." );
print(" TIP: Consider using Action->Fixture->Split Fixture to create multiple polygons." );
}
exportText += "4" + seperator + spriteId + seperator + firstFixture + seperator + bfixtures[f].getNumVertices() + seperator + vertstring + newline;
}
}
}
return worldbodies .length();
}
// ========================================================================
// Gets All Physics Joints
// AGK does not support friction and motor joints
// ISSUE: Cannot access MotorSpeed and MotorTorque in script.
// ----------------------------------------------------------------------------------------------------------------------------------
int GetAllPhysicsJoints()
{
joint[] bjoints = getAllJoints();
for (uint i = 0; i < bjoints.length() ; i++)
{
int spriteAid = bjoints[i].getBodyA().id;
int spriteBid = bjoints[i].getBodyB().id;
float anchorAx = bjoints[i].getWorldAnchorA().x;
float anchorAy = bjoints[i].getWorldAnchorA().y;
float anchorBx =bjoints[i].getWorldAnchorB().x;
float anchorBy = bjoints[i].getWorldAnchorB().y;
if ( bjoints[i].getType() == 1 ) // Revolute Joint
{
exportText += "6" + seperator + bjoints[i].id + seperator + spriteAid + seperator +spriteBid + seperator + anchorAx + seperator + anchorAy + seperator + anchorBx + seperator + anchorBy + seperator + bjoints[i].getReferenceAngle() + seperator + rd(bjoints[i].getLowerLimit()) + seperator + rd(bjoints[i].getUpperLimit()) + seperator + BoolToInt(bjoints[i].getEnableMotor()) + seperator + BoolToInt(bjoints[i].getEnableLimit()) + seperator + bjoints[i].getMotorSpeed() + seperator + bjoints[i].getMaxMotorTorque() + seperator + bjoints[i].getLocalAxis().x + seperator + bjoints[i].getLocalAxis().y + seperator + bjoints[i].getCollideConnected() + newline;
}
if ( bjoints[i].getType() == 2 ) // Prismatic Joint
{
exportText += "7" + seperator + bjoints[i].id + seperator + spriteAid + seperator +spriteBid + seperator + anchorAx + seperator + anchorAy + seperator + anchorBx + seperator + anchorBy + seperator + bjoints[i].getReferenceAngle() + seperator + bjoints[i].getLowerLimit() + seperator + bjoints[i].getUpperLimit() + seperator + BoolToInt(bjoints[i].getEnableMotor()) + seperator + BoolToInt(bjoints[i].getEnableLimit()) + seperator + bjoints[i].getMotorSpeed() + seperator + bjoints[i].getMaxMotorForce() + seperator + bjoints[i].getLocalAxis().x + seperator + bjoints[i].getLocalAxis().y + seperator + bjoints[i].getCollideConnected() + newline;
}
if ( bjoints[i].getType() == 8 ) // Weld Joint
{
exportText += "10" + seperator + bjoints[i].id + seperator + spriteAid + seperator +spriteBid + seperator + anchorAx + seperator + anchorAy + seperator + anchorBx + seperator + anchorBy + seperator + bjoints[i].getReferenceAngle() + seperator + bjoints[i].getLocalAxis().x + seperator + bjoints[i].getLocalAxis().y + seperator + bjoints[i].getCollideConnected() + newline;
}
if ( bjoints[i].getType() == 3 ) // Distance Joint
{
exportText += "11" + seperator + bjoints[i].id + seperator + spriteAid + seperator +spriteBid + seperator + anchorAx + seperator + anchorAy + seperator + anchorBx + seperator + anchorBy + seperator + bjoints[i].getLocalAxis().x + seperator + bjoints[i].getLocalAxis().y + seperator + bjoints[i].getCollideConnected() + newline;
}
if ( bjoints[i].getType() == 10 ) // Rope Joint
{
exportText += "12" + seperator + bjoints[i].id + seperator + spriteAid + seperator +spriteBid + seperator + anchorAx + seperator + anchorAy + seperator + anchorBx + seperator + anchorBy + seperator + bjoints[i].getLocalAxis().x + seperator + bjoints[i].getLocalAxis().y + seperator + bjoints[i].getCollideConnected() + newline;
}
if ( bjoints[i].getType() == 7 ) // Wheel Joint
{
exportText += "13" + seperator + bjoints[i].id + seperator + spriteAid + seperator +spriteBid + seperator + anchorAx + seperator + anchorAy + seperator + anchorBx + seperator + anchorBy + seperator + bjoints[i].getEnableMotor() + seperator + bjoints[i].getLocalAxis().x + seperator + bjoints[i].getLocalAxis().y + seperator + bjoints[i].getCollideConnected() + newline;
}
}
return bjoints.length();
}
// ========================================================================
// Gets the body image id
// ----------------------------------------------------------------------------------------------------------------------------------
int GetBodyImageId(body currentbody)
{
image[] wbImages = currentbody.getImages();
int imageId = -1;
if ( wbImages.length() >= 1 )
{
imageId = wbImages[0].id;
if ( wbImages.length() > 1 )
{
print(" ** WARNING ** Body " + currentbody.id + ":" + currentbody.getName() + " has " + wbImages.length() + " images assigned.");
print(" only the first image will be exported. AGK only supports one image per body." );
print(" TIP: Consider creating a composite image and creating fixtures as required." );
}
}
return imageId;
}
// ========================================================================
// Converts a bool to integer
// ----------------------------------------------------------------------------------------------------------------------------------
int BoolToInt(bool testValue)
{
if(testValue)
{
return 1;
}
return 0;
}
// ========================================================================
// Coverts RUBE body type to AGK body type
// ----------------------------------------------------------------------------------------------------------------------------------
int BodyTypeConvert(int currentType)
{
if( currentType == 0)
{
return 1;
}
else if (currentType == 1)
{
return 3;
}
else
{
return 2;
}
}
// ========================================================================
// Checks an array of fixtures and returns 1 (true) if any of the
// fixtures has isSensor set to true.
// ----------------------------------------------------------------------------------------------------------------------------------
int CheckFixturesForSensor(fixture[] checkFixtures)
{
int result = 0;
for(uint i = 0; i < checkFixtures.length; i++)
{
if(checkFixtures[i].isSensor() == true)
{
return 1;
}
}
return result;
}
// ========================================================================
// Returns the average friction for the supplied array of fixtures.
// AGK only allows the friction to be set on the physics body.
// ----------------------------------------------------------------------------------------------------------------------------------
float GetAverageFriction(fixture[] checkFixtures)
{
if ( checkFixtures.length == 0 )
{
return 0.0;
}
float result = 0.0;
for(uint i = 0; i < checkFixtures.length; i++)
{
result += checkFixtures[i].getFriction();
}
if ( result == 0.0)
{
return 0.0;
}
result = result / checkFixtures.length;
return result;
}
// ========================================================================
// Returns the average friction for the supplied array of fixtures
// AGK only allows the friction to be set on the physics body.
// ----------------------------------------------------------------------------------------------------------------------------------
float GetAverageRestitution(fixture[] checkFixtures)
{
if ( checkFixtures.length == 0 )
{
return 0.0;
}
float result = 0.0;
for(uint i = 0; i < checkFixtures.length; i++)
{
result += checkFixtures[i].getRestitution();
}
if ( result == 0.0)
{
return 0.0;
}
result = result / checkFixtures.length;
return result;
}
// ========================================================================
// Gets the Filename from the path
// ----------------------------------------------------------------------------------------------------------------------------------
string GetFileNameFromPath(string fullFileName)
{
int fileNameStartPosition = 0;
fileNameStartPosition = fullFileName.findLast(pathSepartor) + 1;
if ( fileNameStartPosition == -1)
{
fileNameStartPosition = 0;
}
string fileName = fullFileName.substr(fileNameStartPosition);
return fileName;
}
// ========================================================================
// Check if polygon is convex : 1 = Convex -1 = Concave 0 = Incomputable
// : Only works for simple polygons (no holes no intersecting.)
// ----------------------------------------------------------------------------------------------------------------------------------
int Convex(vertex[] vertices)
{
int i, j, k;
int flag = 0;
int numVertices = vertices.length;
double z;
if (numVertices < 3)
{
return(0);
}
for (i = 0; i < numVertices;i++)
{
j = (i + 1) % numVertices;
k = (i + 2) % numVertices;
z = (vertices[j].x - vertices[i].x) * (vertices[k].y - vertices[j].y);
z -= (vertices[j].y - vertices[i].y) * (vertices[k].x - vertices[j].x);
if(z < 0)
{
flag |= 1;
}
else if(z > 0)
{
flag |= 2;
}
if (flag == 3)
{
return(-1);
}
}
if (flag != 0)
{
return(1);
}
else
{
return(0);
}
}
// ========================================================================
// Check for Clockwise : 1 = Clockwise -1 = Counter Clockwise 0 = Incomputable
// : Only works for simple polygons (no holes no intersecting.)
// ----------------------------------------------------------------------------------------------------------------------------------
int ClockWise(vertex[] vertices)
{
int i, j, k;
int count = 0;
int numVertices = vertices.length;
double z;
if (numVertices < 3)
{
return(0);
}
for (i = 0; i < numVertices; i++)
{
j = (i + 1) % numVertices;
k = (i + 2) % numVertices;
z = (vertices[j].x - vertices[i].x) * (vertices[k].y - vertices[j].y);
z -= (vertices[j].y - vertices[i].y) * (vertices[k].x - vertices[j].x);
if (z < 0)
{
count--;
}
else if (z > 0)
{
count++;
}
}
if (count > 0)
{
return(-1);
}
else if (count < 0)
{
return(1);
}
else
{
return(0);
}
}