|
8 | 8 |
|
9 | 9 | #import <XCTest/XCTest.h> |
10 | 10 | #import "cocos2d.h" |
11 | | -#import "CCBAnimationManager.h" |
| 11 | +#import "CCAnimationManager.h" |
12 | 12 | #import "CCBSequence.h" |
13 | 13 | #import "CCBKeyframe.h" |
| 14 | +#import "CCAnimationManager_Private.h" |
| 15 | + |
| 16 | +#define NUM_ELEMENTS(array) (sizeof(array)/sizeof(array[0])) |
14 | 17 |
|
15 | 18 | #define AssertNotEqualWithAccuracy(a1,a2,accuracy,format...)\ |
16 | 19 | {\ |
@@ -51,7 +54,7 @@ - (void)tearDown |
51 | 54 |
|
52 | 55 | - (void)testSmallKeyframesTest |
53 | 56 | { |
54 | | - CCBAnimationManager * animationManager = [[CCBAnimationManager alloc] init]; |
| 57 | + CCAnimationManager * animationManager = [[CCAnimationManager alloc] init]; |
55 | 58 | CCNode * node = [[CCNode alloc] init]; |
56 | 59 | node.name = @"testNode"; |
57 | 60 | node.position = ccp(0.0f,0.0f); |
@@ -98,7 +101,6 @@ - (void)testSmallKeyframesTest |
98 | 101 | [seqs setObject:seqNodeProps forKey:[NSNumber numberWithInt:seq.sequenceId]]; |
99 | 102 |
|
100 | 103 | [animationManager addNode:node andSequences:seqs]; |
101 | | - |
102 | 104 | [animationManager runAnimationsForSequenceId:seq.sequenceId tweenDuration:0]; |
103 | 105 |
|
104 | 106 | CGFloat accuracy = 0.01f; |
@@ -132,4 +134,139 @@ - (void)testSmallKeyframesTest |
132 | 134 | } |
133 | 135 | } |
134 | 136 |
|
| 137 | +struct PositionKeyframes |
| 138 | +{ |
| 139 | + float time; |
| 140 | + CGPoint position; |
| 141 | +}; |
| 142 | + |
| 143 | +NSDictionary * createPositionSequencePropery(struct PositionKeyframes * keyframes, int count) |
| 144 | +{ |
| 145 | + |
| 146 | + const int kSequencerID = 0; //for now; |
| 147 | + |
| 148 | + CCBSequenceProperty* seqProp = [[CCBSequenceProperty alloc] init]; |
| 149 | + |
| 150 | + seqProp.name = @"position"; |
| 151 | + seqProp.type = 0; |
| 152 | + |
| 153 | + |
| 154 | + for (int k = 0; k < count; k++) |
| 155 | + { |
| 156 | + CCBKeyframe* keyframe = [[CCBKeyframe alloc] init]; |
| 157 | + keyframe.time = keyframes[k].time; |
| 158 | + |
| 159 | + keyframe.easingType = kCCBKeyframeEasingLinear; |
| 160 | + keyframe.easingOpt = 0; |
| 161 | + keyframe.value = @[@(keyframes[k].position.x),@(keyframes[k].position.y)]; |
| 162 | + |
| 163 | + [seqProp.keyframes addObject:keyframe]; |
| 164 | + } |
| 165 | + |
| 166 | + NSMutableDictionary* seqNodeProps = [NSMutableDictionary dictionary]; |
| 167 | + [seqNodeProps setObject:seqProp forKey:seqProp.name]; |
| 168 | + |
| 169 | + NSMutableDictionary* seqs = [NSMutableDictionary dictionary]; |
| 170 | + [seqs setObject:seqNodeProps forKey:[NSNumber numberWithInt:kSequencerID]]; |
| 171 | + |
| 172 | + return seqs; |
| 173 | +} |
| 174 | + |
| 175 | +- (void)testAnimationSync |
| 176 | +{ |
| 177 | + CCAnimationManager * animationManager = [[CCAnimationManager alloc] init]; |
| 178 | + CCBSequence* seq = [[CCBSequence alloc] init]; |
| 179 | + seq.duration = 4.0f; |
| 180 | + seq.name = @"TestSequence"; |
| 181 | + seq.sequenceId = 0; |
| 182 | + seq.chainedSequenceId = 0; |
| 183 | + [animationManager.sequences addObject:seq]; |
| 184 | + |
| 185 | + ///////////////////////////////////////////// |
| 186 | + |
| 187 | + CCNode * nodeA = [[CCNode alloc] init]; |
| 188 | + nodeA.name = @"testA"; |
| 189 | + nodeA.position = ccp(0.0f,0.0f); |
| 190 | + |
| 191 | + struct PositionKeyframes positionsA[] = |
| 192 | + {{0.0f,0.0f,0.0f}, |
| 193 | + {1.0f,100.0f,0.0f}, |
| 194 | + {3.0f,100.0f,0.0f}, |
| 195 | + {4.0f,0.0f,0.0f}}; |
| 196 | + |
| 197 | + |
| 198 | + [animationManager addNode:nodeA andSequences:createPositionSequencePropery(positionsA, NUM_ELEMENTS(positionsA))]; |
| 199 | + |
| 200 | + ///////////////////////////////////////////// |
| 201 | + |
| 202 | + CCNode * nodeB = [[CCNode alloc] init]; |
| 203 | + nodeB.name = @"testB"; |
| 204 | + nodeB.position = ccp(0.0f,0.0f); |
| 205 | + |
| 206 | + struct PositionKeyframes positionsB[] = |
| 207 | + {{0.0f,0.0f,0.0f}, |
| 208 | + {1.0f,0.0f,0.0f}, |
| 209 | + {2.0f,100.0f,0.0f}, |
| 210 | + {3.0f,100.0f,0.0f}, |
| 211 | + {4.0f,0.0f,0.0f}}; |
| 212 | + |
| 213 | + [animationManager addNode:nodeB andSequences:createPositionSequencePropery(positionsB, NUM_ELEMENTS(positionsB))]; |
| 214 | + |
| 215 | + |
| 216 | + ///////////////////////////////////////////// |
| 217 | + |
| 218 | + |
| 219 | + CCNode * nodeC = [[CCNode alloc] init]; |
| 220 | + nodeC.name = @"testC"; |
| 221 | + nodeC.position = ccp(0.0f,0.0f); |
| 222 | + |
| 223 | + struct PositionKeyframes positionsC[] = |
| 224 | + { {0.0f,0.0f,0.0f}, |
| 225 | + {2.0f,0.0f,0.0f}, |
| 226 | + {3.0f,100.0f,0.0f}, |
| 227 | + {4.0f,0.0f,0.0f}}; |
| 228 | + |
| 229 | + [animationManager addNode:nodeC andSequences:createPositionSequencePropery(positionsC, NUM_ELEMENTS(positionsC))]; |
| 230 | + |
| 231 | + ///////////////////////////////////////////// |
| 232 | + |
| 233 | + [animationManager runAnimationsForSequenceId:seq.sequenceId tweenDuration:0]; |
| 234 | + |
| 235 | + |
| 236 | + const float kDelta = 0.1f;//100ms; |
| 237 | + const CGFloat kAccuracy = 0.01f; |
| 238 | + |
| 239 | + float timeIntoSeq = 0.0f; |
| 240 | + float elapsed = 0.0f; |
| 241 | + |
| 242 | + while(elapsed <= seq.duration * 10) |
| 243 | + { |
| 244 | + timeIntoSeq = fmod(elapsed, seq.duration); |
| 245 | + [animationManager update:kDelta]; |
| 246 | + |
| 247 | + elapsed += kDelta; |
| 248 | + |
| 249 | + timeIntoSeq = fmod(elapsed, seq.duration); |
| 250 | + |
| 251 | + if(timeIntoSeq >= 3.0f) |
| 252 | + { |
| 253 | + //All final translations go from x=100 -> x=0 over 1 second. |
| 254 | + float perentageIntroSyncedTranlation = 1.0f - (seq.duration - timeIntoSeq); |
| 255 | + float desiredXCoord = (1.0f - perentageIntroSyncedTranlation) * 100.0f; |
| 256 | + |
| 257 | + |
| 258 | + XCTAssertTrue(fabsf(nodeA.position.x - nodeB.position.x) < kAccuracy, @"They should all equal each other"); |
| 259 | + |
| 260 | + XCTAssertTrue(fabsf(nodeA.position.x - nodeC.position.x) < kAccuracy, @"They should all equal each other"); |
| 261 | + |
| 262 | + XCTAssertTrue(fabsf(nodeA.position.x - desiredXCoord) < kAccuracy, @"They should all equal each desiredXCoord: XPos:%0.2f DesiredPos:%0.2f elapsed:%0.2f", nodeA.position.x,desiredXCoord, elapsed); |
| 263 | + |
| 264 | + } |
| 265 | + |
| 266 | + } |
| 267 | + |
| 268 | +} |
| 269 | + |
| 270 | + |
| 271 | + |
135 | 272 | @end |
0 commit comments