-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGHBoneSkin.mm
More file actions
138 lines (96 loc) · 3.51 KB
/
Copy pathGHBoneSkin.mm
File metadata and controls
138 lines (96 loc) · 3.51 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
//
// GHBoneSkin.m
// cocos2d-ios
//
// Created by Bogdan Vladu on 4/5/13.
//
//
#import "GHBoneSkin.h"
#import "GHBone.h"
#import "GHSprite.h"
@implementation GHBoneSkin
@synthesize positionOffset, angleOffset, connectionAngle, name, uuid;
-(id)initWithSprite:(GHSprite*)spr bone:(GHBone*)bn name:(NSString*)skinName uuid:(NSString*)skinUUID{
self = [super init];
if(self){
sprite = spr;
bone = bn;
if(skinName)
name = [[NSString alloc] initWithString:skinName];
if(skinUUID)
uuid = [[NSString alloc] initWithString:skinUUID];
}
return self;
}
+(id)skinWithSprite:(GHSprite*)spr bone:(GHBone*)bn name:(NSString*)skinName uuid:(NSString*)skinUUID{
return [[[self alloc] initWithSprite:spr bone:bn name:skinName uuid:skinUUID] autorelease];
}
-(void)dealloc{
[name release];
[uuid release];
sprite = nil;
bone = nil;
[super dealloc];
}
-(GHSprite*)sprite{
return sprite;
}
-(void)setSprite:(GHSprite*)spr{
sprite = spr;
}
-(GHBone*)bone{
return bone;
}
-(void)setBone:(GHBone*)val{
bone = val;
}
-(void)setupTransformations
{
if(bone && sprite)
{
GHBone* _father = (GHBone*)[bone parent];
angleOffset = 0;//needs to be calculated
CGPoint bonePoint = _father.position;
CGPoint currentPos = [sprite position];
float curAngle = [sprite rotation];
connectionAngle = curAngle;
//we flip y for cocos2d
CGPoint posOffset = ccp(currentPos.x - bonePoint.x,
bonePoint.y - currentPos.y);
positionOffset = posOffset;
float boneAngle = [bone degrees] ;
angleOffset = boneAngle - curAngle;
}
}
-(void)transform
{
if(sprite == nil || bone == nil)return;
float degrees = [bone degrees];
CGPoint posOffset = positionOffset;
CGPoint bonePos = ccp(((GHBone*)[bone parent]).position.x,
((GHBone*)[bone parent]).position.y);
CGAffineTransform transformOffset = CGAffineTransformTranslate(CGAffineTransformIdentity, bonePos.x, bonePos.y);
CGAffineTransform transform = CGAffineTransformRotate(CGAffineTransformIdentity,
CC_DEGREES_TO_RADIANS(degrees - angleOffset));
//we need 2 points
//origin - upward point, in order to calculate new position and new angle
CGPoint origin = ccp(0,0);
CGPoint upward = CGPointMake(0, -10);
CGAffineTransform transform3 = CGAffineTransformRotate(CGAffineTransformIdentity,
CC_DEGREES_TO_RADIANS(degrees - angleOffset - connectionAngle));
posOffset = CGPointApplyAffineTransform(posOffset, transform3);
origin = CGPointApplyAffineTransform(origin, transform);
upward = CGPointApplyAffineTransform(upward, transform);
origin = CGPointApplyAffineTransform(origin, transformOffset);
upward = CGPointApplyAffineTransform(upward, transformOffset);
//now that we have the 2 points - lets calculate the angle
float newAngle = (atan2(upward.y - origin.y,
upward.x - origin.x)*180.0)/M_PI + 90.0;
[sprite setPosition:CGPointMake(origin.x + posOffset.x, origin.y - posOffset.y)];
[sprite setRotation:newAngle];
}
-(CGRect)boundingBox{
//get the rect of the GHSprite
return self.sprite.boundingBox;
}
@end