forked from JohnSundell/SuperSpriteKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SKNode+SSKTags.m
89 lines (66 loc) · 2.49 KB
/
SKNode+SSKTags.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
#import "SKNode+SSKTags.h"
static NSString * const SSKTagStorageKey = @"SuperSpriteKit_Tag";
@implementation SKNode (SSKTags)
#pragma mark - Public
- (NSInteger)ssk_tag
{
return [[self.userData objectForKey:SSKTagStorageKey] unsignedIntegerValue];
}
- (void)ssk_setTag:(NSInteger)tag
{
if (!self.userData) {
self.userData = [NSMutableDictionary new];
}
[self.userData setObject:@(tag) forKey:SSKTagStorageKey];
}
- (SKNode *)ssk_childNodeWithTag:(NSInteger)tag
{
return [self ssk_childNodeWithTag:tag recursive:NO];
}
- (SKNode *)ssk_childNodeWithTag:(NSInteger)tag recursive:(BOOL)recursive
{
NSArray *matches = [self ssk_childNodesWithTag:tag
recursive:recursive
returnOnFirstMatch:YES];
return [matches firstObject];
}
- (NSArray *)ssk_childNodesWithTag:(NSInteger)tag
{
return [self ssk_childNodesWithTag:tag recursive:NO];
}
- (NSArray *)ssk_childNodesWithTag:(NSInteger)tag recursive:(BOOL)recursive
{
return [self ssk_childNodesWithTag:tag
recursive:recursive
returnOnFirstMatch:NO];
}
- (NSArray *)ssk_nodesAtPoint:(CGPoint)point withTag:(NSInteger)tag
{
NSArray *nodesAtPoint = [self nodesAtPoint:point];
NSPredicate *tagPredicate = [NSPredicate predicateWithFormat:@"ssk_tag == %d", (long)tag];
return [nodesAtPoint filteredArrayUsingPredicate:tagPredicate];
}
#pragma mark - Private
- (NSArray *)ssk_childNodesWithTag:(NSInteger)tag recursive:(BOOL)recursive returnOnFirstMatch:(BOOL)returnOnFirstMatch
{
NSPredicate *tagPredicate = [NSPredicate predicateWithFormat:@"ssk_tag == %d", (long)tag];
NSArray *directChildMatches = [self.children filteredArrayUsingPredicate:tagPredicate];
if (!recursive) {
return directChildMatches;
}
if (returnOnFirstMatch && [directChildMatches count] > 0) {
return directChildMatches;
}
NSMutableArray *foundNodes = [directChildMatches mutableCopy];
for (SKNode *child in self.children) {
NSArray *childMatches = [child ssk_childNodesWithTag:tag
recursive:YES
returnOnFirstMatch:returnOnFirstMatch];
if (returnOnFirstMatch && [childMatches count] > 0) {
return childMatches;
}
[foundNodes addObjectsFromArray:childMatches];
}
return [foundNodes copy];
}
@end