diff options
author | Starla Insigna <starla4444@gmail.com> | 2011-07-30 11:19:14 -0400 |
---|---|---|
committer | Starla Insigna <starla4444@gmail.com> | 2011-07-30 11:19:14 -0400 |
commit | 9cd57b731ab1c666d4a1cb725538fdc137763d12 (patch) | |
tree | 5bac45ae5157a1cb10c6e45500cbf72789917980 /libs/cocos2d/CCActionEase.m | |
download | cartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.tar.gz cartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.tar.bz2 cartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.zip |
Initial commit (version 0.2.1)
Diffstat (limited to 'libs/cocos2d/CCActionEase.m')
-rwxr-xr-x | libs/cocos2d/CCActionEase.m | 534 |
1 files changed, 534 insertions, 0 deletions
diff --git a/libs/cocos2d/CCActionEase.m b/libs/cocos2d/CCActionEase.m new file mode 100755 index 0000000..f28be11 --- /dev/null +++ b/libs/cocos2d/CCActionEase.m | |||
@@ -0,0 +1,534 @@ | |||
1 | /* | ||
2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
3 | * | ||
4 | * Copyright (c) 2008-2009 Jason Booth | ||
5 | * | ||
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
7 | * of this software and associated documentation files (the "Software"), to deal | ||
8 | * in the Software without restriction, including without limitation the rights | ||
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
10 | * copies of the Software, and to permit persons to whom the Software is | ||
11 | * furnished to do so, subject to the following conditions: | ||
12 | * | ||
13 | * The above copyright notice and this permission notice shall be included in | ||
14 | * all copies or substantial portions of the Software. | ||
15 | * | ||
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
22 | * THE SOFTWARE. | ||
23 | * | ||
24 | */ | ||
25 | |||
26 | |||
27 | /* | ||
28 | * Elastic, Back and Bounce actions based on code from: | ||
29 | * http://github.com/NikhilK/silverlightfx/ | ||
30 | * | ||
31 | * by http://github.com/NikhilK | ||
32 | */ | ||
33 | |||
34 | #import "CCActionEase.h" | ||
35 | |||
36 | #ifndef M_PI_X_2 | ||
37 | #define M_PI_X_2 (float)M_PI * 2.0f | ||
38 | #endif | ||
39 | |||
40 | #pragma mark EaseAction | ||
41 | |||
42 | // | ||
43 | // EaseAction | ||
44 | // | ||
45 | @implementation CCActionEase | ||
46 | |||
47 | +(id) actionWithAction: (CCActionInterval*) action | ||
48 | { | ||
49 | return [[[self alloc] initWithAction: action] autorelease ]; | ||
50 | } | ||
51 | |||
52 | -(id) initWithAction: (CCActionInterval*) action | ||
53 | { | ||
54 | NSAssert( action!=nil, @"Ease: arguments must be non-nil"); | ||
55 | |||
56 | if( (self=[super initWithDuration: action.duration]) ) | ||
57 | other = [action retain]; | ||
58 | |||
59 | return self; | ||
60 | } | ||
61 | |||
62 | -(id) copyWithZone: (NSZone*) zone | ||
63 | { | ||
64 | CCAction *copy = [[[self class] allocWithZone:zone] initWithAction:[[other copy] autorelease]]; | ||
65 | return copy; | ||
66 | } | ||
67 | |||
68 | -(void) dealloc | ||
69 | { | ||
70 | [other release]; | ||
71 | [super dealloc]; | ||
72 | } | ||
73 | |||
74 | -(void) startWithTarget:(id)aTarget | ||
75 | { | ||
76 | [super startWithTarget:aTarget]; | ||
77 | [other startWithTarget:target_]; | ||
78 | } | ||
79 | |||
80 | -(void) stop | ||
81 | { | ||
82 | [other stop]; | ||
83 | [super stop]; | ||
84 | } | ||
85 | |||
86 | -(void) update: (ccTime) t | ||
87 | { | ||
88 | [other update: t]; | ||
89 | } | ||
90 | |||
91 | -(CCActionInterval*) reverse | ||
92 | { | ||
93 | return [[self class] actionWithAction: [other reverse]]; | ||
94 | } | ||
95 | @end | ||
96 | |||
97 | |||
98 | #pragma mark - | ||
99 | #pragma mark EaseRate | ||
100 | |||
101 | // | ||
102 | // EaseRateAction | ||
103 | // | ||
104 | @implementation CCEaseRateAction | ||
105 | @synthesize rate; | ||
106 | +(id) actionWithAction: (CCActionInterval*) action rate:(float)aRate | ||
107 | { | ||
108 | return [[[self alloc] initWithAction: action rate:aRate] autorelease ]; | ||
109 | } | ||
110 | |||
111 | -(id) initWithAction: (CCActionInterval*) action rate:(float)aRate | ||
112 | { | ||
113 | if( (self=[super initWithAction:action ]) ) | ||
114 | self.rate = aRate; | ||
115 | |||
116 | return self; | ||
117 | } | ||
118 | |||
119 | -(id) copyWithZone: (NSZone*) zone | ||
120 | { | ||
121 | CCAction *copy = [[[self class] allocWithZone:zone] initWithAction:[[other copy] autorelease] rate:rate]; | ||
122 | return copy; | ||
123 | } | ||
124 | |||
125 | -(void) dealloc | ||
126 | { | ||
127 | [super dealloc]; | ||
128 | } | ||
129 | |||
130 | -(CCActionInterval*) reverse | ||
131 | { | ||
132 | return [[self class] actionWithAction: [other reverse] rate:1/rate]; | ||
133 | } | ||
134 | @end | ||
135 | |||
136 | // | ||
137 | // EeseIn | ||
138 | // | ||
139 | @implementation CCEaseIn | ||
140 | -(void) update: (ccTime) t | ||
141 | { | ||
142 | [other update: powf(t,rate)]; | ||
143 | } | ||
144 | @end | ||
145 | |||
146 | // | ||
147 | // EaseOut | ||
148 | // | ||
149 | @implementation CCEaseOut | ||
150 | -(void) update: (ccTime) t | ||
151 | { | ||
152 | [other update: powf(t,1/rate)]; | ||
153 | } | ||
154 | @end | ||
155 | |||
156 | // | ||
157 | // EaseInOut | ||
158 | // | ||
159 | @implementation CCEaseInOut | ||
160 | -(void) update: (ccTime) t | ||
161 | { | ||
162 | int sign =1; | ||
163 | int r = (int) rate; | ||
164 | if (r % 2 == 0) | ||
165 | sign = -1; | ||
166 | t *= 2; | ||
167 | if (t < 1) | ||
168 | [other update: 0.5f * powf (t, rate)]; | ||
169 | else | ||
170 | [other update: sign*0.5f * (powf (t-2, rate) + sign*2)]; | ||
171 | } | ||
172 | |||
173 | // InOut and OutIn are symmetrical | ||
174 | -(CCActionInterval*) reverse | ||
175 | { | ||
176 | return [[self class] actionWithAction: [other reverse] rate:rate]; | ||
177 | } | ||
178 | |||
179 | @end | ||
180 | |||
181 | #pragma mark - | ||
182 | #pragma mark EaseExponential | ||
183 | |||
184 | // | ||
185 | // EaseExponentialIn | ||
186 | // | ||
187 | @implementation CCEaseExponentialIn | ||
188 | -(void) update: (ccTime) t | ||
189 | { | ||
190 | [other update: (t==0) ? 0 : powf(2, 10 * (t/1 - 1)) - 1 * 0.001f]; | ||
191 | } | ||
192 | |||
193 | - (CCActionInterval*) reverse | ||
194 | { | ||
195 | return [CCEaseExponentialOut actionWithAction: [other reverse]]; | ||
196 | } | ||
197 | @end | ||
198 | |||
199 | // | ||
200 | // EaseExponentialOut | ||
201 | // | ||
202 | @implementation CCEaseExponentialOut | ||
203 | -(void) update: (ccTime) t | ||
204 | { | ||
205 | [other update: (t==1) ? 1 : (-powf(2, -10 * t/1) + 1)]; | ||
206 | } | ||
207 | |||
208 | - (CCActionInterval*) reverse | ||
209 | { | ||
210 | return [CCEaseExponentialIn actionWithAction: [other reverse]]; | ||
211 | } | ||
212 | @end | ||
213 | |||
214 | // | ||
215 | // EaseExponentialInOut | ||
216 | // | ||
217 | @implementation CCEaseExponentialInOut | ||
218 | -(void) update: (ccTime) t | ||
219 | { | ||
220 | t /= 0.5f; | ||
221 | if (t < 1) | ||
222 | t = 0.5f * powf(2, 10 * (t - 1)); | ||
223 | else | ||
224 | t = 0.5f * (-powf(2, -10 * (t -1) ) + 2); | ||
225 | |||
226 | [other update:t]; | ||
227 | } | ||
228 | @end | ||
229 | |||
230 | |||
231 | #pragma mark - | ||
232 | #pragma mark EaseSin actions | ||
233 | |||
234 | // | ||
235 | // EaseSineIn | ||
236 | // | ||
237 | @implementation CCEaseSineIn | ||
238 | -(void) update: (ccTime) t | ||
239 | { | ||
240 | [other update:-1*cosf(t * (float)M_PI_2) +1]; | ||
241 | } | ||
242 | |||
243 | - (CCActionInterval*) reverse | ||
244 | { | ||
245 | return [CCEaseSineOut actionWithAction: [other reverse]]; | ||
246 | } | ||
247 | @end | ||
248 | |||
249 | // | ||
250 | // EaseSineOut | ||
251 | // | ||
252 | @implementation CCEaseSineOut | ||
253 | -(void) update: (ccTime) t | ||
254 | { | ||
255 | [other update:sinf(t * (float)M_PI_2)]; | ||
256 | } | ||
257 | |||
258 | - (CCActionInterval*) reverse | ||
259 | { | ||
260 | return [CCEaseSineIn actionWithAction: [other reverse]]; | ||
261 | } | ||
262 | @end | ||
263 | |||
264 | // | ||
265 | // EaseSineInOut | ||
266 | // | ||
267 | @implementation CCEaseSineInOut | ||
268 | -(void) update: (ccTime) t | ||
269 | { | ||
270 | [other update:-0.5f*(cosf( (float)M_PI*t) - 1)]; | ||
271 | } | ||
272 | @end | ||
273 | |||
274 | #pragma mark - | ||
275 | #pragma mark EaseElastic actions | ||
276 | |||
277 | // | ||
278 | // EaseElastic | ||
279 | // | ||
280 | @implementation CCEaseElastic | ||
281 | |||
282 | @synthesize period = period_; | ||
283 | |||
284 | +(id) actionWithAction: (CCActionInterval*) action | ||
285 | { | ||
286 | return [[[self alloc] initWithAction:action period:0.3f] autorelease]; | ||
287 | } | ||
288 | |||
289 | +(id) actionWithAction: (CCActionInterval*) action period:(float)period | ||
290 | { | ||
291 | return [[[self alloc] initWithAction:action period:period] autorelease]; | ||
292 | } | ||
293 | |||
294 | -(id) initWithAction: (CCActionInterval*) action | ||
295 | { | ||
296 | return [self initWithAction:action period:0.3f]; | ||
297 | } | ||
298 | |||
299 | -(id) initWithAction: (CCActionInterval*) action period:(float)period | ||
300 | { | ||
301 | if( (self=[super initWithAction:action]) ) | ||
302 | period_ = period; | ||
303 | |||
304 | return self; | ||
305 | } | ||
306 | |||
307 | -(id) copyWithZone: (NSZone*) zone | ||
308 | { | ||
309 | CCAction *copy = [[[self class] allocWithZone:zone] initWithAction:[[other copy] autorelease] period:period_]; | ||
310 | return copy; | ||
311 | } | ||
312 | |||
313 | -(CCActionInterval*) reverse | ||
314 | { | ||
315 | NSAssert(NO,@"Override me"); | ||
316 | return nil; | ||
317 | } | ||
318 | |||
319 | @end | ||
320 | |||
321 | // | ||
322 | // EaseElasticIn | ||
323 | // | ||
324 | |||
325 | @implementation CCEaseElasticIn | ||
326 | -(void) update: (ccTime) t | ||
327 | { | ||
328 | ccTime newT = 0; | ||
329 | if (t == 0 || t == 1) | ||
330 | newT = t; | ||
331 | |||
332 | else { | ||
333 | float s = period_ / 4; | ||
334 | t = t - 1; | ||
335 | newT = -powf(2, 10 * t) * sinf( (t-s) *M_PI_X_2 / period_); | ||
336 | } | ||
337 | [other update:newT]; | ||
338 | } | ||
339 | |||
340 | - (CCActionInterval*) reverse | ||
341 | { | ||
342 | return [CCEaseElasticOut actionWithAction: [other reverse] period:period_]; | ||
343 | } | ||
344 | |||
345 | @end | ||
346 | |||
347 | // | ||
348 | // EaseElasticOut | ||
349 | // | ||
350 | @implementation CCEaseElasticOut | ||
351 | |||
352 | -(void) update: (ccTime) t | ||
353 | { | ||
354 | ccTime newT = 0; | ||
355 | if (t == 0 || t == 1) { | ||
356 | newT = t; | ||
357 | |||
358 | } else { | ||
359 | float s = period_ / 4; | ||
360 | newT = powf(2, -10 * t) * sinf( (t-s) *M_PI_X_2 / period_) + 1; | ||
361 | } | ||
362 | [other update:newT]; | ||
363 | } | ||
364 | |||
365 | - (CCActionInterval*) reverse | ||
366 | { | ||
367 | return [CCEaseElasticIn actionWithAction: [other reverse] period:period_]; | ||
368 | } | ||
369 | |||
370 | @end | ||
371 | |||
372 | // | ||
373 | // EaseElasticInOut | ||
374 | // | ||
375 | @implementation CCEaseElasticInOut | ||
376 | -(void) update: (ccTime) t | ||
377 | { | ||
378 | ccTime newT = 0; | ||
379 | |||
380 | if( t == 0 || t == 1 ) | ||
381 | newT = t; | ||
382 | else { | ||
383 | t = t * 2; | ||
384 | if(! period_ ) | ||
385 | period_ = 0.3f * 1.5f; | ||
386 | ccTime s = period_ / 4; | ||
387 | |||
388 | t = t -1; | ||
389 | if( t < 0 ) | ||
390 | newT = -0.5f * powf(2, 10 * t) * sinf((t - s) * M_PI_X_2 / period_); | ||
391 | else | ||
392 | newT = powf(2, -10 * t) * sinf((t - s) * M_PI_X_2 / period_) * 0.5f + 1; | ||
393 | } | ||
394 | [other update:newT]; | ||
395 | } | ||
396 | |||
397 | - (CCActionInterval*) reverse | ||
398 | { | ||
399 | return [CCEaseElasticInOut actionWithAction: [other reverse] period:period_]; | ||
400 | } | ||
401 | |||
402 | @end | ||
403 | |||
404 | #pragma mark - | ||
405 | #pragma mark EaseBounce actions | ||
406 | |||
407 | // | ||
408 | // EaseBounce | ||
409 | // | ||
410 | @implementation CCEaseBounce | ||
411 | -(ccTime) bounceTime:(ccTime) t | ||
412 | { | ||
413 | if (t < 1 / 2.75) { | ||
414 | return 7.5625f * t * t; | ||
415 | } | ||
416 | else if (t < 2 / 2.75) { | ||
417 | t -= 1.5f / 2.75f; | ||
418 | return 7.5625f * t * t + 0.75f; | ||
419 | } | ||
420 | else if (t < 2.5 / 2.75) { | ||
421 | t -= 2.25f / 2.75f; | ||
422 | return 7.5625f * t * t + 0.9375f; | ||
423 | } | ||
424 | |||
425 | t -= 2.625f / 2.75f; | ||
426 | return 7.5625f * t * t + 0.984375f; | ||
427 | } | ||
428 | @end | ||
429 | |||
430 | // | ||
431 | // EaseBounceIn | ||
432 | // | ||
433 | |||
434 | @implementation CCEaseBounceIn | ||
435 | |||
436 | -(void) update: (ccTime) t | ||
437 | { | ||
438 | ccTime newT = 1 - [self bounceTime:1-t]; | ||
439 | [other update:newT]; | ||
440 | } | ||
441 | |||
442 | - (CCActionInterval*) reverse | ||
443 | { | ||
444 | return [CCEaseBounceOut actionWithAction: [other reverse]]; | ||
445 | } | ||
446 | |||
447 | @end | ||
448 | |||
449 | @implementation CCEaseBounceOut | ||
450 | |||
451 | -(void) update: (ccTime) t | ||
452 | { | ||
453 | ccTime newT = [self bounceTime:t]; | ||
454 | [other update:newT]; | ||
455 | } | ||
456 | |||
457 | - (CCActionInterval*) reverse | ||
458 | { | ||
459 | return [CCEaseBounceIn actionWithAction: [other reverse]]; | ||
460 | } | ||
461 | |||
462 | @end | ||
463 | |||
464 | @implementation CCEaseBounceInOut | ||
465 | |||
466 | -(void) update: (ccTime) t | ||
467 | { | ||
468 | ccTime newT = 0; | ||
469 | if (t < 0.5) { | ||
470 | t = t * 2; | ||
471 | newT = (1 - [self bounceTime:1-t] ) * 0.5f; | ||
472 | } else | ||
473 | newT = [self bounceTime:t * 2 - 1] * 0.5f + 0.5f; | ||
474 | |||
475 | [other update:newT]; | ||
476 | } | ||
477 | @end | ||
478 | |||
479 | #pragma mark - | ||
480 | #pragma mark Ease Back actions | ||
481 | |||
482 | // | ||
483 | // EaseBackIn | ||
484 | // | ||
485 | @implementation CCEaseBackIn | ||
486 | |||
487 | -(void) update: (ccTime) t | ||
488 | { | ||
489 | ccTime overshoot = 1.70158f; | ||
490 | [other update: t * t * ((overshoot + 1) * t - overshoot)]; | ||
491 | } | ||
492 | |||
493 | - (CCActionInterval*) reverse | ||
494 | { | ||
495 | return [CCEaseBackOut actionWithAction: [other reverse]]; | ||
496 | } | ||
497 | @end | ||
498 | |||
499 | // | ||
500 | // EaseBackOut | ||
501 | // | ||
502 | @implementation CCEaseBackOut | ||
503 | -(void) update: (ccTime) t | ||
504 | { | ||
505 | ccTime overshoot = 1.70158f; | ||
506 | |||
507 | t = t - 1; | ||
508 | [other update: t * t * ((overshoot + 1) * t + overshoot) + 1]; | ||
509 | } | ||
510 | |||
511 | - (CCActionInterval*) reverse | ||
512 | { | ||
513 | return [CCEaseBackIn actionWithAction: [other reverse]]; | ||
514 | } | ||
515 | @end | ||
516 | |||
517 | // | ||
518 | // EaseBackInOut | ||
519 | // | ||
520 | @implementation CCEaseBackInOut | ||
521 | |||
522 | -(void) update: (ccTime) t | ||
523 | { | ||
524 | ccTime overshoot = 1.70158f * 1.525f; | ||
525 | |||
526 | t = t * 2; | ||
527 | if (t < 1) | ||
528 | [other update: (t * t * ((overshoot + 1) * t - overshoot)) / 2]; | ||
529 | else { | ||
530 | t = t - 2; | ||
531 | [other update: (t * t * ((overshoot + 1) * t + overshoot)) / 2 + 1]; | ||
532 | } | ||
533 | } | ||
534 | @end | ||