-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGHSkeleton.mm
More file actions
1006 lines (753 loc) · 30.7 KB
/
Copy pathGHSkeleton.mm
File metadata and controls
1006 lines (753 loc) · 30.7 KB
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// GHSkeleton.m
// cocos2d-ios
//
// Created by Bogdan Vladu on 4/4/13.
//
//
#import "GHSkeleton.h"
#import "GHSprite.h"
#import "GHBoneSkin.h"
#import "GHSkeletalAnimationCache.h"
#import "GHSkeletalAnimation.h"
@interface GHSkeleton()
-(void)loadSprites:(NSArray*)spritesInfo;
-(void)loadBones:(NSDictionary*)rootBoneInfo;
#ifdef GH_DEBUG
-(void)initShader;
#endif
@end
@interface GHBone(GHSkeletonBonePrivate)
-(void)updateMovement;
@end
@implementation GHBone(GHSkeletonBonePrivate)
-(void)updateMovement{
if(self.rigid){
[self setPosition:self.position parent:nil];
}
for(GHBone* bone in self.children){
[bone updateMovement];
}
}
@end
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
@implementation GHSkeleton
-(void)dealloc{
delegate = nil;
#ifdef GH_DEBUG
_shaderProgram = nil;
#endif
[self unscheduleAllSelectors];
[self unscheduleUpdate];
[rootBone release];
[poses release];
[transitionTime release];
transitionTime = nil;
[animation release];
animation = nil;
[super dealloc];
}
-(id)initWithFile:(NSString*)file{
self = [super init];
if(self){
//maybe the file has or does not have extension given - we do this little trick
NSString* plistFile = [file stringByDeletingPathExtension];
plistFile = [plistFile stringByAppendingPathExtension:@"plist"];
//maye the user has given a suffix - not necessary
plistFile = [[CCFileUtils sharedFileUtils] removeSuffixFromFile:plistFile];
NSString *path = [[CCFileUtils sharedFileUtils] fullPathFromRelativePath:plistFile];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSString* pathToSkeleton = [file stringByDeletingLastPathComponent];
NSString* pathToSheet = [pathToSkeleton stringByDeletingLastPathComponent];
NSString* sheetName = [dict objectForKey:@"sheet"];
if(sheetName)
{
NSString* sheetFile = [pathToSheet stringByAppendingPathComponent:sheetName];
batchNode_ = [CCSpriteBatchNode batchNodeWithFile:sheetFile];
[self addChild:batchNode_ z:-1];//this way debug drawing will be 0
[batchNode_ setPosition:ccp(0,0)];
NSString* sheetPlist = [sheetFile stringByDeletingPathExtension];
sheetPlist = [sheetPlist stringByAppendingPathExtension:@"plist"];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:sheetPlist];
}
[self loadBones:[dict objectForKey:@"root"]];
[self loadSprites:[dict objectForKey:@"sprites"]];
[self updateSkins];
NSDictionary* posesDict = [dict objectForKey:@"poses"];
if(posesDict){
poses = [[NSDictionary alloc] initWithDictionary:posesDict];
}
#ifdef GH_DEBUG
[self initShader];
#endif
}
return self;
}
+(id)skeletonWithFile:(NSString*)file{
return [[[self alloc] initWithFile:file] autorelease];
}
-(void)loadSprites:(NSArray*)spritesInfo
{
if(spritesInfo == nil)return;
NSMutableArray* allBones = [NSMutableArray arrayWithArray:[self allBones]];
for(NSDictionary* sprInfo in spritesInfo)
{
//name
//angle
//localPos
//visible
CGPoint localPos = CGPointMake(0, 0);
id sprPos = [sprInfo objectForKey:@"localPos"];
if(sprPos){
localPos = CGPointFromString(sprPos);
}
localPos.x /=CC_CONTENT_SCALE_FACTOR();
localPos.y /=CC_CONTENT_SCALE_FACTOR();
float angle = 0;
id sprAngle = [sprInfo objectForKey:@"angle"];
if(sprAngle){
angle = [sprAngle floatValue];
}
bool visible = true;
id sprVis = [sprInfo objectForKey:@"visible"];
if(sprVis){
visible = [sprVis boolValue];
}
NSString* boneUUID = [sprInfo objectForKey:@"boneUUID"];
NSString* skinName = [sprInfo objectForKey:@"skinName"];
NSString* skinUUID = [sprInfo objectForKey:@"skinUUID"];
NSString* sprName = [sprInfo objectForKey:@"sprName"];
if(sprName){
GHSprite* newSpr = [GHSprite spriteWithSpriteFrameName:sprName];
[newSpr setName:skinName];
[newSpr setPosition:localPos];
[newSpr setRotation:angle];
[newSpr setColor:ccc3(255, 255, 255)];
[newSpr setVisible:visible];
if(batchNode_ != nil){
[batchNode_ addChild:newSpr];
}
if(boneUUID){
for(GHBone* bone in allBones)
{
if([[bone uuid] isEqualToString:boneUUID]){
[self addSkin:[GHBoneSkin skinWithSprite:newSpr bone:bone name:skinName uuid:skinUUID]];
break;//exit for loop
}
}
}
else{
[self addSkin:[GHBoneSkin skinWithSprite:newSpr bone:nil name:skinName uuid:skinUUID]];
}
}
}
}
-(void)loadBones:(NSDictionary*)rootBoneInfo
{
if(!rootBoneInfo)return;
rootBone = [[GHBone alloc] initWithDictionary:rootBoneInfo];
}
-(NSArray*)allBones{
NSMutableArray* array = [NSMutableArray array];
[array addObject:rootBone];
for(GHBone* bone in [rootBone children])
{
[array addObjectsFromArray:[bone allBones]];
}
return array;
}
-(GHBone*)rootBone{
return rootBone;
}
-(void)setDelegate:(id<GHSkeletonDelegate>)del{
delegate = del;
}
-(void)setPosition:(CGPoint)location forBoneNamed:(NSString*)boneName{
GHBone* bone = [rootBone boneWithName:boneName];
if(bone){
CGPoint localPoint = ccp(location.x - [self position].x,
location.y - [self position].y);
[bone setPosition:localPoint parent:nil];
}
[rootBone updateMovement];
[self transformSkins];
}
-(void)setPoseWithName:(NSString*)poseName{
NSAssert( poses != nil, @"\n\nERROR: Skeleton has no poses or poses were not publish.\n\n");
NSDictionary* poseInfo = [poses objectForKey:poseName];
NSAssert( poseInfo != nil, @"\n\nERROR: Skeleton has no pose with the given \"poseName\" argument.\n\n");
NSDictionary* visibility = [poseInfo objectForKey:@"visibility"];
NSAssert( visibility != nil, @"\n\nERROR: Skeleton pose is in wrong format. Skin visibilities were not found.\n\n");
NSDictionary* zOrder = [poseInfo objectForKey:@"zOrder"];
NSAssert( visibility != nil, @"\n\nERROR: Skeleton pose is in wrong format. Skin z orders were not found.\n\n");
NSDictionary* skinTex = [poseInfo objectForKey:@"skinTex"];
NSAssert( skinTex != nil, @"\n\nERROR: Skeleton pose is in wrong format. Skin sprite frame names were not found.\n\n");
NSDictionary* connections = [poseInfo objectForKey:@"connections"];
NSAssert( connections != nil, @"\n\nERROR: Skeleton pose is in wrong format. Skin connections were not found.\n\n");
NSArray* allBones = [self allBones];
for(GHBoneSkin* skin in skins){
[[skin sprite] setVisible:YES];
NSNumber* value = [visibility objectForKey:[skin uuid]];
if(value){
[[skin sprite] setVisible:NO];
}
NSNumber* zValue = [zOrder objectForKey:[skin uuid]];
if(zValue){
[batchNode_ reorderChild:[skin sprite] z:[zValue integerValue]];
}
NSString* spriteFrameName = [skinTex objectForKey:[skin uuid]];
if(spriteFrameName){
CCSpriteFrame* frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:spriteFrameName];
if(frame){
[[skin sprite] setDisplayFrame:frame];
}
}
NSDictionary* connectionInfo = [connections objectForKey:[skin uuid]];
if(connectionInfo){
//angleOff
//boneUUID //this may be missing if no connection
//conAngle
//posOff
NSString* boneUUID = [connectionInfo objectForKey:@"boneUUID"];
if(boneUUID)
{
//check if the current bone is already our connection bone - if not change it
if(!([skin bone] && [[[skin bone] uuid] isEqualToString:boneUUID]))
{
for(GHBone* bone in allBones)
{
if([[bone uuid] isEqualToString:boneUUID]){
[skin setBone:bone];
break;
}
}
}
}
else{
[skin setBone:nil];
}
NSNumber* angleOff = [connectionInfo objectForKey:@"angleOff"];
if(angleOff){
[skin setAngleOffset:[angleOff floatValue]];
}
NSString* posOff = [connectionInfo objectForKey:@"posOff"];
if(posOff)
{
CGPoint newPos = CGPointFromString(posOff);
newPos.x /= CC_CONTENT_SCALE_FACTOR();
newPos.y /= CC_CONTENT_SCALE_FACTOR();
[skin setPositionOffset:newPos];
}
NSNumber* connectionAngle = [connectionInfo objectForKey:@"conAngle"];
if(connectionAngle){
[skin setConnectionAngle:[connectionAngle floatValue]];
}
}
}
NSDictionary* positions = [poseInfo objectForKey:@"positions"];
NSAssert( positions != nil, @"\n\nERROR: Skeleton pose is in wrong format. Bone positions were not found.\n\n");
for(GHBone* bone in allBones)
{
NSString* uuid = [bone uuid];
NSAssert( uuid != nil, @"\n\nERROR: Bone has no UUID.\n\n");
NSString* bonePos = [positions objectForKey:uuid];
NSAssert( bonePos != nil, @"\n\nERROR: Bone pose does not have a position value. Must be in a wrong format.\n\n");
CGPoint newPos = CGPointFromString(bonePos);
newPos.x /= CC_CONTENT_SCALE_FACTOR();
newPos.y /= CC_CONTENT_SCALE_FACTOR();
bone.position = newPos;
}
[self transformSkins];
if(delegate){
if([delegate respondsToSelector:@selector(didLoadPoseWithName:onSkeleton:)])
{
[delegate didLoadPoseWithName:poseName onSkeleton:self];
}
}
}
-(void)addSkin:(GHBoneSkin*)skin{
if(nil == skin)return;
if(nil == skins){
skins = [[NSMutableArray alloc] init];
}
[skins addObject:skin];
}
-(void)removeSkin:(GHBoneSkin*)skin{
[skins removeObject:skin];
}
-(NSArray*)skins{
return skins;
}
-(void)playAnimation:(GHSkeletalAnimation*)anim{
if(nil == anim)return;
[self stopAnimation];
if(transitionTime){[transitionTime release]; transitionTime = nil;}
currentTranstionTime = 0;
animation = anim;
[animation retain];
[anim setCurrentTime:0];
[anim setCurrentLoop:0];
if(delegate){
if([delegate respondsToSelector:@selector(didStartAnimation:onSkeleton:)])
{
[delegate didStartAnimation:anim onSkeleton:self];
}
}
[self scheduleUpdate];
}
-(void)playAnimationWithName:(NSString*)animName{
GHSkeletalAnimation* anim = [[GHSkeletalAnimationCache sharedSkeletalAnimationCache] skeletalAnimationWithName:animName];
[self playAnimation:anim];
}
-(GHSkeletalAnimation*)animation{
return animation;
}
-(void)transitionToAnimation:(GHSkeletalAnimation*)anim inTime:(float)time{
if(nil == anim)return;
NSArray* allBones = [self allBones];
for(GHBone* bone in allBones){
[bone savePosition];
}
[self playAnimation:anim];//this will also rmeove any previous transition time
transitionTime = [[NSNumber numberWithFloat:time] retain];
currentTranstionTime = 0;
}
-(void)transitionToAnimationWithName:(NSString*)animName inTime:(float)time{
GHSkeletalAnimation* anim = [[GHSkeletalAnimationCache sharedSkeletalAnimationCache] skeletalAnimationWithName:animName];
[self transitionToAnimation:anim inTime:time];
}
-(void)stopAnimation{
if(animation){
[animation release];
}
animation = nil;
[self unscheduleUpdate];
}
-(void) update: (ccTime) dt
{
float time = 0;
if(transitionTime != nil)
{
if([transitionTime floatValue] < currentTranstionTime)
{
[transitionTime release];
transitionTime = nil;
[animation setCurrentTime:dt];
[animation setCurrentLoop:0];
currentTranstionTime = 0;
time = dt;
if(delegate){
if([delegate respondsToSelector:@selector(didFinishTransitionToAnimation:onSkeleton:)]){
[delegate didFinishTransitionToAnimation:animation onSkeleton:self];
}
}
}
time = currentTranstionTime;
currentTranstionTime += dt;
}
else{
time = [animation currentTime];
if([animation reversed]){
[animation setCurrentTime:[animation currentTime] - dt];
}else{
[animation setCurrentTime:[animation currentTime] + dt];
}
}
if([animation reversed] && transitionTime == nil)
{
if(time <= 0){
switch ([animation playMode]) {
case GH_SKELETAL_ANIM_PLAY_NORMAL:
case GH_SKELETAL_ANIM_PLAY_LOOP:
[animation setCurrentTime:[animation totalTime]];
break;
case GH_SKELETAL_ANIM_PLAY_PINGPONG:
[animation setCurrentTime:0];
[animation setReversed:NO];
break;
default:
break;
}
if(delegate){
if([delegate respondsToSelector:@selector(didFinishLoopInAnimation:onSkeleton:)]){
[delegate didFinishLoopInAnimation:animation onSkeleton:self];
}
}
[animation setCurrentLoop:[animation currentLoop]+1];
}
}
else{
if(time >= [animation totalTime]){
switch ([animation playMode]) {
case GH_SKELETAL_ANIM_PLAY_NORMAL:
case GH_SKELETAL_ANIM_PLAY_LOOP:
[animation setCurrentTime:0];
break;
case GH_SKELETAL_ANIM_PLAY_PINGPONG:
[animation setCurrentTime:[animation totalTime]];
[animation setReversed:YES];
break;
default:
break;
}
[animation setCurrentLoop:[animation currentLoop]+1];
if(delegate){
if([delegate respondsToSelector:@selector(didFinishLoopInAnimation:onSkeleton:)]){
[delegate didFinishLoopInAnimation:animation onSkeleton:self];
}
}
}
}
if([animation numberOfLoops] != 0 && [animation currentLoop] >= [animation numberOfLoops]){
[self stopAnimation];
}
{ //handle positions
GHSkeletalAnimationFrame* beginFrame = nil;
GHSkeletalAnimationFrame* endFrame = nil;
for(GHSkeletalAnimationFrame* frm in [animation bonePositionFrames]){
if([frm time] <= time){
beginFrame = frm;
}
if([frm time] > time){
endFrame = frm;
break;//exit for
}
}
if(transitionTime)
{
NSArray* positionFrames = [animation bonePositionFrames];
if([positionFrames count] > 0)
beginFrame = [positionFrames objectAtIndex:0];
float beginTime = 0;
float endTime = [transitionTime floatValue];
float framesTimeDistance = endTime - beginTime;
float timeUnit = (time-beginTime)/framesTimeDistance; //a value between 0 and 1
NSMutableDictionary* beginBonesInfo = [beginFrame bonePositions];
if(nil == beginBonesInfo)
return;
NSArray* allBones = [self allBones];
for(GHBone* bone in allBones)
{
NSValue* beginValue = [beginBonesInfo objectForKey:[bone name]];
CGPoint beginPosition = [bone previousPosition];
CGPoint endPosition = [bone position];
if(beginValue){
endPosition = [beginValue CGPointValue];
}
//lets calculate the position of the bone based on the start - end and unit time
float newX = beginPosition.x + (endPosition.x - beginPosition.x)*timeUnit;
float newY = beginPosition.y + (endPosition.y - beginPosition.y)*timeUnit;
CGPoint newPos = CGPointMake(newX, newY);
bone.position = newPos;
[self transformSkins];
}
[rootBone updateMovement];
}
else if(beginFrame && endFrame){
float beginTime = [beginFrame time];
float endTime = [endFrame time];
float framesTimeDistance = endTime - beginTime;
float timeUnit = (time-beginTime)/framesTimeDistance; //a value between 0 and 1
NSMutableDictionary* beginBonesInfo = [beginFrame bonePositions];
NSMutableDictionary* endBonesInfo = [endFrame bonePositions];
if(nil == beginBonesInfo || endBonesInfo == nil)
return;
NSArray* allBones = [self allBones];
for(GHBone* bone in allBones)
{
NSValue* beginValue = [beginBonesInfo objectForKey:[bone name]];
NSValue* endValue = [endBonesInfo objectForKey:[bone name]];
CGPoint beginPosition = [bone position];
CGPoint endPosition = [bone position];
if(beginValue){
beginPosition = [beginValue CGPointValue];
}
if(endValue){
endPosition = [endValue CGPointValue];
}
//lets calculate the position of the bone based on the start - end and unit time
float newX = beginPosition.x + (endPosition.x - beginPosition.x)*timeUnit;
float newY = beginPosition.y + (endPosition.y - beginPosition.y)*timeUnit;
CGPoint newPos = CGPointMake(newX, newY);
bone.position = newPos;
}
[rootBone updateMovement];
}
else if(beginFrame)
{
NSMutableDictionary* beginBonesInfo = [beginFrame bonePositions];
NSArray* allBones = [self allBones];
for(GHBone* bone in allBones)
{
NSValue* beginValue = [beginBonesInfo objectForKey:[bone name]];
CGPoint beginPosition = [bone position];
if(beginValue){
beginPosition = [beginValue CGPointValue];
}
bone.position = beginPosition;
}
[rootBone updateMovement];
}
}
if(transitionTime){
time = 0;
}
{//handle sprites z order
GHSkeletalAnimationFrame* beginFrame = nil;
for(GHSkeletalAnimationFrame* frm in [animation spriteZOrderFrames]){
if([frm time] <= time){
beginFrame = frm;
}
}
//we have the last frame with smaller time
if(beginFrame){
NSDictionary* zOrderInfo = [beginFrame spritesZOrder];
for(GHSprite* sprite in [batchNode_ children])
{
NSString* sprName = [sprite name];
if(sprName){
NSNumber* zNum = [zOrderInfo objectForKey:sprName];
if(zNum)
{
[batchNode_ reorderChild:sprite z:[zNum intValue]];
}
}
}
}
}
{//handle skin connections
GHSkeletalAnimationFrame* beginFrame = nil;
for(GHSkeletalAnimationFrame* frm in [animation skinConnectionFrames]){
if([frm time] <= time){
beginFrame = frm;
}
}
//we have the last frame with smaller time
if(beginFrame){
NSDictionary* connections = [beginFrame skinConnections];
for(GHBoneSkin* skin in skins)
{
GHSprite* sprite = [skin sprite];
if(sprite){
NSString* sprName = [sprite name];
if(sprName){
GHSkeletalSkinConnectionInfo* connectionInfo = [connections objectForKey:sprName];
if(connectionInfo)
{
NSString* boneName = [connectionInfo boneName];
[skin setAngleOffset:[connectionInfo angleOffset]];
[skin setPositionOffset:[connectionInfo positionOffset]];
[skin setConnectionAngle:[connectionInfo connectionAngle]];
if(!boneName)//we may not have a bone
{
[skin setBone:nil];
}
else{
for(GHBone* bone in [self allBones])
{
if([[bone name] isEqualToString:boneName]){
[skin setBone:bone];
break;//exit for loop
}
}
}
}
}
}
}
}
}
{//handle skin sprites
GHSkeletalAnimationFrame* beginFrame = nil;
for(GHSkeletalAnimationFrame* frm in [animation skinSpriteFrames]){
if([frm time] <= time){
beginFrame = frm;
}
}
//we have the last frame with smaller time
if(beginFrame){
NSMutableDictionary* info = [beginFrame skinSprites];
if(info){
for(GHBoneSkin* skin in skins)
{
NSString* newSprFrameName = [info objectForKey:[skin name]];
if(newSprFrameName){
CCSpriteFrame* frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:newSprFrameName];
if(frame){
[[skin sprite] setDisplayFrame:frame];
}
}
}
}
}
}
{//handle sprites visibility
GHSkeletalAnimationFrame* beginFrame = nil;
for(GHSkeletalAnimationFrame* frm in [animation visibilityFrames]){
if([frm time] <= time){
beginFrame = frm;
}
}
//we have the last frame with smaller time
if(beginFrame){
NSMutableDictionary* info = [beginFrame spritesVisibility];
if(info){
for(GHSprite* sprite in [batchNode_ children])
{
NSString* sprFrmName = [sprite name];
if(sprFrmName){
NSNumber* val = [info objectForKey:sprFrmName];
// NSLog(@"SPR FRM NAME %@ %@ ", sprFrmName, val);
if(val){
[sprite setVisible:[val boolValue]];
}
}
}
}
}
}
{ //handle sprites transform
GHSkeletalAnimationFrame* beginFrame = nil;
GHSkeletalAnimationFrame* endFrame = nil;
for(GHSkeletalAnimationFrame* frm in [animation spritesTransformFrames]){
if([frm time] <= time){
beginFrame = frm;
}
if([frm time] > time){
endFrame = frm;
break;//exit for
}
}
if(beginFrame && endFrame){
float beginTime = [beginFrame time];
float endTime = [endFrame time];
float framesTimeDistance = endTime - beginTime;
float timeUnit = (time-beginTime)/framesTimeDistance; //a value between 0 and 1
NSMutableDictionary* beginFrameInfo = [beginFrame spritesTransform];
NSMutableDictionary* endFrameInfo = [endFrame spritesTransform];
if(beginFrameInfo == nil || endFrameInfo == nil)
return;
for(GHBoneSkin* skin in skins)
{
GHSkeletalSkinConnectionInfo* beginInfo = [beginFrameInfo objectForKey:[skin name]];
GHSkeletalSkinConnectionInfo* endInfo = [endFrameInfo objectForKey:[skin name]];
if([skin sprite] && beginInfo && endInfo)
{
//set position
CGPoint beginPos = [beginInfo position];
CGPoint endPos = [endInfo position];
float newX = beginPos.x + (endPos.x - beginPos.x)*timeUnit;
float newY = beginPos.y + (endPos.y - beginPos.y)*timeUnit;
[[skin sprite] setPosition:ccp(newX, newY)];
//set angle
float beginAngle = [beginInfo angle];
float endAngle = [endInfo angle];
float newAngle = beginAngle + (endAngle - beginAngle)*timeUnit;
[[skin sprite] setRotation:newAngle];
//set angle at skin time
float beginSkinAngle = [beginInfo connectionAngle];
float endSkinAngle = [endInfo connectionAngle];
float newSkinAngle = beginSkinAngle + (endSkinAngle - beginSkinAngle)*timeUnit;
[skin setConnectionAngle:newSkinAngle];
{
//set skin angle
float beginAngle = [beginInfo angleOffset];
float endAngle = [endInfo angleOffset];
float newAngle = beginAngle + (endAngle - beginAngle)*timeUnit;
[skin setAngleOffset:newAngle];
//set skin position offset
CGPoint beginPosOff = [beginInfo positionOffset];
CGPoint endPosOff = [endInfo positionOffset];
float newX = beginPosOff.x + (endPosOff.x - beginPosOff.x)*timeUnit;
float newY = beginPosOff.y + (endPosOff.y - beginPosOff.y)*timeUnit;
[skin setPositionOffset:ccp(newX, newY)];
}
}
}
}
}
[self transformSkins];
currentTranstionTime += dt;
}
-(void)updateSkins{
for(GHBoneSkin* skin in skins){
[skin setupTransformations];
}
}
-(void)transformSkins
{
for(GHBoneSkin* skin in skins){
[skin transform];
}
}
-(CGRect)boundingBox{
CGRect bigBox = CGRectZero;
for(GHBoneSkin* skin in skins) {bigBox = CGRectUnion(bigBox, [skin boundingBox]);}
return bigBox;
}
-(NSMutableArray*)boundingBoxesArray{
NSMutableArray* allBoundingBoxes = [NSMutableArray array];
for(GHBoneSkin* skin in skins) {
[allBoundingBoxes addObject:[NSValue valueWithCGRect:[skin boundingBox]]];
}
return allBoundingBoxes;
}
#ifdef GH_DEBUG
-(void)initShader
{
_shaderProgram = [[CCShaderCache sharedShaderCache] programForKey:kCCShader_Position_uColor];
colorLocation_ = glGetUniformLocation( _shaderProgram->_program, "u_color");
}
-(void)debugDrawBone:(GHBone*)bone
{
if([bone rigid]){
[_shaderProgram setUniformLocation:colorLocation_ withF1:0 f2:0 f3:1 f4:1];
}
else{
[_shaderProgram setUniformLocation:colorLocation_ withF1:0 f2:1 f3:0 f4:1];
}
for(GHBone* child in [bone children])
{
GLfloat vertices[] = {
bone.position.x, bone.position.y,
child.position.x, child.position.y
};
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glDrawArrays(GL_LINES, 0, 2);
[self debugDrawBone:child];
}
}
-(void) draw{
if(!_shaderProgram)return;
[_shaderProgram use];
[_shaderProgram setUniformsForBuiltins];
[self debugDrawBone:rootBone];
CC_INCREMENT_GL_DRAWS(1);
CHECK_GL_ERROR_DEBUG();
//DRAW THE RECT FOR EACH SKIN
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_Color );
ccDrawColor4F(1, 1, 1, 1);
glLineWidth(2.0f);
for (NSValue* rectVal in [self boundingBoxesArray]){
[self drawBox:[rectVal CGRectValue]];
}
//DRAW THE BIG RECT
ccDrawColor4F(1, 0, 0, 1);
glLineWidth(4.0f);
[self drawBox:[self boundingBox]];
[super draw];
}
-(void) drawBox: (CGRect) rect
{
CGPoint vertices[4]={
ccp(rect.origin.x,rect.origin.y),
ccp(rect.origin.x+rect.size.width,rect.origin.y),
ccp(rect.origin.x+rect.size.width,rect.origin.y+rect.size.height),
ccp(rect.origin.x,rect.origin.y+rect.size.height),
};
ccDrawPoly(vertices, 4, YES);
}