forked from JohnSundell/SuperSpriteKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SSKTileableNode.m
181 lines (134 loc) · 4.67 KB
/
SSKTileableNode.m
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
#import "SSKTileableNode.h"
static CGFloat SSKTileableNodeNoResizing = -9999;
@interface SSKTileableNode()
@property (nonatomic, strong) NSMutableArray *partNodes;
@end
@implementation SSKTileableNode
+ (instancetype)tileableNodeWithSize:(CGSize)size imageNamed:(NSString *)imageName
{
if (!imageName) {
return nil;
}
return [self tileableNodeWithSize:size
texture:[SKTexture textureWithImageNamed:imageName]];
}
+ (instancetype)tileableNodeWithSize:(CGSize)size texture:(SKTexture *)texture
{
if (!texture) {
return nil;
}
SSKTileableNode *node = [self node];
node.partNodes = [NSMutableArray new];
node.texture = texture;
node.size = size;
return node;
}
- (void)drawPartNodes
{
[self.partNodes makeObjectsPerformSelector:@selector(removeFromParent)];
[self.partNodes removeAllObjects];
if (self.size.width <= 0 || self.size.height <= 0) {
return;
}
const CGSize textureSize = self.texture.size;
CGPoint drawPoint = CGPointZero;
while (true) {
CGSize tileSize;
tileSize.width = MIN(self.size.width - drawPoint.x, textureSize.width);
tileSize.height = MIN(self.size.height - drawPoint.y, textureSize.height);
SKTexture *tileTexture;
if (tileSize.width < textureSize.width || tileSize.height < textureSize.height) {
CGRect textureRect = self.texture.textureRect;
textureRect.size.width *= tileSize.width / textureSize.width;
textureRect.size.height *= tileSize.height / textureSize.height;
tileTexture = [SKTexture textureWithRect:textureRect inTexture:self.texture];
} else {
tileTexture = self.texture;
}
SKSpriteNode *tileNode = [SKSpriteNode spriteNodeWithTexture:tileTexture];
tileNode.anchorPoint = CGPointZero;
tileNode.position = drawPoint;
tileNode.size = tileSize;
[self addChild:tileNode];
[self.partNodes addObject:tileNode];
drawPoint.x += tileSize.width;
if (drawPoint.x >= self.size.width) {
drawPoint.x = 0;
drawPoint.y += tileSize.height;
if (drawPoint.y >= self.size.height) {
break;
}
}
}
}
#pragma mark - Accessor overrides
- (void)setSize:(CGSize)size
{
if (CGSizeEqualToSize(_size, size)) {
return;
}
_size = size;
[self drawPartNodes];
}
- (void)setTexture:(SKTexture *)texture
{
if (_texture == texture) {
return;
}
_texture = texture;
[self drawPartNodes];
}
- (void)setColor:(SKColor *)color
{
if ([_color isEqual:color]) {
return;
}
_color = color;
for (SSKTileableNode *partNode in self.partNodes) {
partNode.color = color;
}
}
- (void)setColorBlendFactor:(CGFloat)colorBlendFactor
{
if (_colorBlendFactor == colorBlendFactor) {
return;
}
_colorBlendFactor = colorBlendFactor;
for (SSKTileableNode *partNode in self.partNodes) {
partNode.colorBlendFactor = colorBlendFactor;
}
}
@end
#pragma mark - SKActions
@implementation SKAction (SSKTileableNodeActions)
+ (SKAction *)resizeTileableNodeToWidth:(CGFloat)width duration:(NSTimeInterval)duration
{
return [SKAction resizeTileableNodeToWidth:width
height:SSKTileableNodeNoResizing
duration:duration];
}
+ (SKAction *)resizeTileableNodeToHeight:(CGFloat)height duration:(NSTimeInterval)duration
{
return [SKAction resizeTileableNodeToWidth:SSKTileableNodeNoResizing
height:height
duration:duration];
}
+ (SKAction *)resizeTileableNodeToWidth:(CGFloat)width height:(CGFloat)height duration:(NSTimeInterval)duration
{
return [SKAction customActionWithDuration:duration actionBlock:^(SKNode *node, CGFloat elapsedTime) {
if (![node isKindOfClass:[SSKTileableNode class]]) {
return;
}
CGFloat percentageComplete = elapsedTime / duration;
SSKTileableNode *tileableNode = (SSKTileableNode *)node;
CGSize nodeSize = tileableNode.size;
if (width != SSKTileableNodeNoResizing) {
nodeSize.width += (width - nodeSize.width) * percentageComplete;
}
if (height != SSKTileableNodeNoResizing) {
nodeSize.height += (height - nodeSize.height) * percentageComplete;
}
tileableNode.size = nodeSize;
}];
}
@end