-
Notifications
You must be signed in to change notification settings - Fork 3
/
sk.gml
306 lines (231 loc) Β· 7.16 KB
/
sk.gml
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
#define nearestPoint
///nearestPoint(x, y)
//x,y
near = 0;
for (i=0; i<ds_list_find_value(data,1); i+=1) {
if point_distance(sk_getX(i),sk_getY(i),argument0,argument1) <= point_distance(sk_getX(near),sk_getY(near),argument0,argument1)
near = i;
}
return near;
#define pointDirection
///pointDirection(x1, y1, x2, y2)
/*
argument0 x1
argument1 y1
argument2 x2
argument3 y2
*/
return ((((arctan2(argument0-argument2,argument1-argument3)* (180/pi))) + 450) mod 360);
#define sk_create
///sk_create(data)
/***************************************************
Create Skeleton Structure
***************************************************
sk_create(Skeleton Data);
Creates the actual skeleton structure the system uses
Reference:
Skeleton Structure Data:
0 parent
1 x
2 y
3 x offset
4 y offset
5 angle
6 target angle
7 rotation speed
8 clamp min
9 clamp max
*/
var iData, pData;
iData = argument0;
if iData != -1 {
pData = ds_list_find_value(iData,2);
SK_DATA = ds_grid_create(ds_list_find_value(iData,1),10);
SK_SCALE = 1;
sk_setX(0,x);
sk_setY(0,y);
for (i=1; i<ds_list_find_value(iData,1); i+=1) {
sk_setX(i,0);
sk_setY(i,0);
}
for (i=0; i<ds_list_find_value(iData,1); i+=1) {
sk_setParent(i,ds_grid_get(pData,i,0));
sk_setXOffset(i,ds_grid_get(pData,i,1));
sk_setYOffset(i,ds_grid_get(pData,i,2));
sk_setClampMin(i,ds_grid_get(pData,i,3));
sk_setClampMax(i,ds_grid_get(pData,i,4));
sk_setAngle(i,0);
sk_setTargetAngle(i,0);
sk_setRotationSpeed(i,10);
sk_updateJoint(i);
}
}
#define sk_draw
///sk_draw()
/***************************************************
Draw Skeleton
***************************************************
sk_draw();
Draws the skeleton using the Skeleton Structure Data
*/
var xval, yval, par;
for (i=0; i<ds_grid_width(SK_DATA); i+=1) {
xval = sk_getX(i);
yval = sk_getY(i);
draw_circle(xval,yval,5,false);
if i > 0 {
par = sk_getParent(i);
draw_line(xval,yval,sk_getX(par),sk_getY(par));
}
}
#define sk_getAngle
return ds_grid_get(SK_DATA,argument0,5);
#define sk_getClampMax
return ds_grid_get(SK_DATA,argument0,9);
#define sk_getClampMin
return ds_grid_get(SK_DATA,argument0,8);
#define sk_getParent
return ds_grid_get(SK_DATA,argument0,0);
#define sk_getRotationSpeed
return ds_grid_get(SK_DATA,argument0,7);
#define sk_getTargetAngle
return ds_grid_get(SK_DATA,argument0,6);
#define sk_getX
return ds_grid_get(SK_DATA,argument0,1);
#define sk_getXOffset
return ds_grid_get(SK_DATA,argument0,3);
#define sk_getY
return ds_grid_get(SK_DATA,argument0,2);
#define sk_getYOffset
return ds_grid_get(SK_DATA,argument0,4);
#define sk_import
///sk_import(filename)
/***************************************************
Import Skeleton Data
***************************************************
sk_import(filename);
Returns: Skeleton Data
Imports a skeleton from a .sk file and converts it to Skeleton Data
Skeleton Data can be used to create many skeletons
Skeleton Data is used in sk_create
Reference:
Skeleton Data:
0 name
1 number of joints
2 point data (Point ID, Value ID)
Value ID:
0 parent (-1 = root joint)
1 x offset from parent at angle 0
2 y offset from parent at angle 0
3 minimum clamp range
4 maximum clamp range
*/
var name, noOfJoints, points, data;
if file_exists(argument0) {
ini_open(argument0);
name = ini_read_string("Skeleton","id","Skeleton");
noOfJoints = ini_read_real("Skeleton","joints",0);
points = ds_grid_create(noOfJoints,5);
ds_grid_set(points,0,0,-1);
for (i=1; i<5; i+=1)
ds_grid_set(points,0,i,0);
for (i=1; i<noOfJoints; i+=1) {
ds_grid_set(points,i,0,ini_read_real(string(i),"parent",0));
ds_grid_set(points,i,1,ini_read_real(string(i),"xoffset",0));
ds_grid_set(points,i,2,ini_read_real(string(i),"yoffset",0));
ds_grid_set(points,i,3,ini_read_real(string(i),"clampmin",0));
ds_grid_set(points,i,4,ini_read_real(string(i),"clampmax",0));
}
ini_close();
data = ds_list_create();
ds_list_insert(data,0,name);
ds_list_insert(data,1,noOfJoints);
ds_list_insert(data,2,points);
return data;
} else {
show_message("Error - Could not open skeleton file "+argument0);
return -1;
}
#define sk_pointX
///sk_pointX(parent x, x offset, y offset, angle)
return ((argument1*cos(degtorad(-argument3)))-(argument2*sin(degtorad(-argument3))))+argument0;
#define sk_pointY
///sk_pointY(parent y, x offset, y offset, angle)
return ((argument1*sin(degtorad(-argument3)))+(argument2*cos(degtorad(-argument3))))+argument0;
#define sk_rotate
///sk_rotate(angle, target, speed)
//angle = sk_rotate(angle,target angle,speed)
return (argument0+(sin(degtorad((argument1)-argument0))*argument2));
#define sk_setAngle
///sk_setAngle(id, angle)
ds_grid_set(SK_DATA,argument0,5,argument1);
#define sk_setClampMax
///sk_setClampMax(id, clampMax)
ds_grid_set(SK_DATA,argument0,9,argument1);
#define sk_setClampMin
///sk_setClampMin(id, clampMin)
ds_grid_set(SK_DATA,argument0,8,argument1);
#define sk_setParent
///sk_setParent(id, parentId)
ds_grid_set(SK_DATA,argument0,0,argument1);
#define sk_setRotationSpeed
///sk_setRotationSpeed(id, rotationSpeed)
ds_grid_set(SK_DATA,argument0,7,argument1);
#define sk_setTargetAngle
///sk_setTargetAngle(id, targetAngle)
ds_grid_set(SK_DATA,argument0,6,argument1);
#define sk_setX
///sk_setX(id, x)
ds_grid_set(SK_DATA,argument0,1,argument1);
#define sk_setXOffset
///sk_setXOffset(id, xOffset)
ds_grid_set(SK_DATA,argument0,3,argument1);
#define sk_setY
///sk_setY(id, y)
ds_grid_set(SK_DATA,argument0,2,argument1);
#define sk_setYOffset
///sk_setYOffset(id, yOffset)
ds_grid_set(SK_DATA,argument0,4,argument1);
#define sk_update
///sk_update()
/***************************************************
Update Skeleton Structure
***************************************************
sk_update();
Used in the step event of an object with a skeleton in it
It keeps the structure updated due to rotations
*/
sk_setX(0,x);
sk_setY(0,y);
for (i=0; i<ds_grid_width(SK_DATA); i+=1) {
sk_updateJoint(i);
}
#define sk_updateJoint
///sk_updateJoint(id)
/***************************************************
Update a Joint
***************************************************
sk_updateJoint(point ID);
Updates an individual joint's positioning and angle
*/
var point, par, xoff, yoff, xpar, ypar;
point = argument0;
par = sk_getParent(point);
xoff = sk_getXOffset(point)*SK_SCALE;
yoff = sk_getYOffset(point)*SK_SCALE;
xpar = sk_getX(par);
ypar = sk_getY(par);
curAng = sk_getAngle(point);
parAng = sk_getAngle(par);
if (is_undefined(parAng)) {
parAng = 0;
}
sk_setAngle(point,sk_rotate(curAng,sk_getTargetAngle(point)+parAng,sk_getRotationSpeed(point)));
if point != 0 {
sk_setX(point,sk_pointX(xpar,xoff,yoff,curAng));
sk_setY(point,sk_pointY(ypar,xoff,yoff,curAng));
sk_setTargetAngle(point,clamp(sk_getTargetAngle(point),sk_getClampMin(point),sk_getClampMax(point)));
} else {
sk_setX(0,x);
sk_setY(0,y);
}