summary refs log tree commit diff stats
path: root/libs/cocos2d/CCActionGrid3D.m
diff options
context:
space:
mode:
authorStarla Insigna <starla4444@gmail.com>2011-07-30 11:19:14 -0400
committerStarla Insigna <starla4444@gmail.com>2011-07-30 11:19:14 -0400
commit9cd57b731ab1c666d4a1cb725538fdc137763d12 (patch)
tree5bac45ae5157a1cb10c6e45500cbf72789917980 /libs/cocos2d/CCActionGrid3D.m
downloadcartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.tar.gz
cartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.tar.bz2
cartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.zip
Initial commit (version 0.2.1)
Diffstat (limited to 'libs/cocos2d/CCActionGrid3D.m')
-rwxr-xr-xlibs/cocos2d/CCActionGrid3D.m659
1 files changed, 659 insertions, 0 deletions
diff --git a/libs/cocos2d/CCActionGrid3D.m b/libs/cocos2d/CCActionGrid3D.m new file mode 100755 index 0000000..1d4a783 --- /dev/null +++ b/libs/cocos2d/CCActionGrid3D.m
@@ -0,0 +1,659 @@
1/*
2 * cocos2d for iPhone: http://www.cocos2d-iphone.org
3 *
4 * Copyright (c) 2009 On-Core
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#import "CCActionGrid3D.h"
28#import "ccMacros.h"
29#import "Support/CGPointExtension.h"
30
31#pragma mark -
32#pragma mark Waves3D
33
34@implementation CCWaves3D
35
36@synthesize amplitude;
37@synthesize amplitudeRate;
38
39+(id)actionWithWaves:(int)wav amplitude:(float)amp grid:(ccGridSize)gridSize duration:(ccTime)d
40{
41 return [[[self alloc] initWithWaves:wav amplitude:amp grid:gridSize duration:d] autorelease];
42}
43
44-(id)initWithWaves:(int)wav amplitude:(float)amp grid:(ccGridSize)gSize duration:(ccTime)d
45{
46 if ( (self = [super initWithSize:gSize duration:d]) )
47 {
48 waves = wav;
49 amplitude = amp;
50 amplitudeRate = 1.0f;
51 }
52
53 return self;
54}
55
56-(id) copyWithZone: (NSZone*) zone
57{
58 CCGridAction *copy = [[[self class] allocWithZone:zone] initWithWaves:waves amplitude:amplitude grid:gridSize_ duration:duration_];
59 return copy;
60}
61
62
63-(void)update:(ccTime)time
64{
65 int i, j;
66
67 for( i = 0; i < (gridSize_.x+1); i++ )
68 {
69 for( j = 0; j < (gridSize_.y+1); j++ )
70 {
71 ccVertex3F v = [self originalVertex:ccg(i,j)];
72 v.z += (sinf((CGFloat)M_PI*time*waves*2 + (v.y+v.x) * .01f) * amplitude * amplitudeRate);
73 [self setVertex:ccg(i,j) vertex:v];
74 }
75 }
76}
77@end
78
79////////////////////////////////////////////////////////////
80
81#pragma mark -
82#pragma mark FlipX3D
83
84@implementation CCFlipX3D
85
86+(id) actionWithDuration:(ccTime)d
87{
88 return [[[self alloc] initWithSize:ccg(1,1) duration:d] autorelease];
89}
90
91-(id) initWithDuration:(ccTime)d
92{
93 return [super initWithSize:ccg(1,1) duration:d];
94}
95
96-(id)initWithSize:(ccGridSize)gSize duration:(ccTime)d
97{
98 if ( gSize.x != 1 || gSize.y != 1 )
99 {
100 [NSException raise:@"FlipX3D" format:@"Grid size must be (1,1)"];
101 }
102
103 return [super initWithSize:gSize duration:d];
104}
105
106-(id) copyWithZone: (NSZone*) zone
107{
108 CCGridAction *copy = [[[self class] allocWithZone:zone] initWithSize:gridSize_ duration:duration_];
109 return copy;
110}
111
112
113-(void)update:(ccTime)time
114{
115 CGFloat angle = (CGFloat)M_PI * time; // 180 degrees
116 CGFloat mz = sinf( angle );
117 angle = angle / 2.0f; // x calculates degrees from 0 to 90
118 CGFloat mx = cosf( angle );
119
120 ccVertex3F v0, v1, v, diff;
121
122 v0 = [self originalVertex:ccg(1,1)];
123 v1 = [self originalVertex:ccg(0,0)];
124
125 CGFloat x0 = v0.x;
126 CGFloat x1 = v1.x;
127 CGFloat x;
128 ccGridSize a, b, c, d;
129
130 if ( x0 > x1 )
131 {
132 // Normal Grid
133 a = ccg(0,0);
134 b = ccg(0,1);
135 c = ccg(1,0);
136 d = ccg(1,1);
137 x = x0;
138 }
139 else
140 {
141 // Reversed Grid
142 c = ccg(0,0);
143 d = ccg(0,1);
144 a = ccg(1,0);
145 b = ccg(1,1);
146 x = x1;
147 }
148
149 diff.x = ( x - x * mx );
150 diff.z = fabsf( floorf( (x * mz) / 4.0f ) );
151
152// bottom-left
153 v = [self originalVertex:a];
154 v.x = diff.x;
155 v.z += diff.z;
156 [self setVertex:a vertex:v];
157
158// upper-left
159 v = [self originalVertex:b];
160 v.x = diff.x;
161 v.z += diff.z;
162 [self setVertex:b vertex:v];
163
164// bottom-right
165 v = [self originalVertex:c];
166 v.x -= diff.x;
167 v.z -= diff.z;
168 [self setVertex:c vertex:v];
169
170// upper-right
171 v = [self originalVertex:d];
172 v.x -= diff.x;
173 v.z -= diff.z;
174 [self setVertex:d vertex:v];
175}
176
177@end
178
179////////////////////////////////////////////////////////////
180
181#pragma mark -
182#pragma mark FlipY3D
183
184@implementation CCFlipY3D
185
186-(void)update:(ccTime)time
187{
188 CGFloat angle = (CGFloat)M_PI * time; // 180 degrees
189 CGFloat mz = sinf( angle );
190 angle = angle / 2.0f; // x calculates degrees from 0 to 90
191 CGFloat my = cosf( angle );
192
193 ccVertex3F v0, v1, v, diff;
194
195 v0 = [self originalVertex:ccg(1,1)];
196 v1 = [self originalVertex:ccg(0,0)];
197
198 CGFloat y0 = v0.y;
199 CGFloat y1 = v1.y;
200 CGFloat y;
201 ccGridSize a, b, c, d;
202
203 if ( y0 > y1 )
204 {
205 // Normal Grid
206 a = ccg(0,0);
207 b = ccg(0,1);
208 c = ccg(1,0);
209 d = ccg(1,1);
210 y = y0;
211 }
212 else
213 {
214 // Reversed Grid
215 b = ccg(0,0);
216 a = ccg(0,1);
217 d = ccg(1,0);
218 c = ccg(1,1);
219 y = y1;
220 }
221
222 diff.y = y - y * my;
223 diff.z = fabsf( floorf( (y * mz) / 4.0f ) );
224
225 // bottom-left
226 v = [self originalVertex:a];
227 v.y = diff.y;
228 v.z += diff.z;
229 [self setVertex:a vertex:v];
230
231 // upper-left
232 v = [self originalVertex:b];
233 v.y -= diff.y;
234 v.z -= diff.z;
235 [self setVertex:b vertex:v];
236
237 // bottom-right
238 v = [self originalVertex:c];
239 v.y = diff.y;
240 v.z += diff.z;
241 [self setVertex:c vertex:v];
242
243 // upper-right
244 v = [self originalVertex:d];
245 v.y -= diff.y;
246 v.z -= diff.z;
247 [self setVertex:d vertex:v];
248}
249
250@end
251
252////////////////////////////////////////////////////////////
253
254#pragma mark -
255#pragma mark Lens3D
256
257@implementation CCLens3D
258
259@synthesize lensEffect=lensEffect_;
260
261+(id)actionWithPosition:(CGPoint)pos radius:(float)r grid:(ccGridSize)gridSize duration:(ccTime)d
262{
263 return [[[self alloc] initWithPosition:pos radius:r grid:gridSize duration:d] autorelease];
264}
265
266-(id)initWithPosition:(CGPoint)pos radius:(float)r grid:(ccGridSize)gSize duration:(ccTime)d
267{
268 if ( (self = [super initWithSize:gSize duration:d]) )
269 {
270 position_ = ccp(-1,-1);
271 self.position = pos;
272 radius_ = r;
273 lensEffect_ = 0.7f;
274 dirty_ = YES;
275 }
276
277 return self;
278}
279
280-(id) copyWithZone: (NSZone*) zone
281{
282 CCGridAction *copy = [[[self class] allocWithZone:zone] initWithPosition:position_ radius:radius_ grid:gridSize_ duration:duration_];
283 return copy;
284}
285
286-(void) setPosition:(CGPoint)pos
287{
288 if( ! CGPointEqualToPoint(pos, position_) ) {
289 position_ = pos;
290 positionInPixels_.x = pos.x * CC_CONTENT_SCALE_FACTOR();
291 positionInPixels_.y = pos.y * CC_CONTENT_SCALE_FACTOR();
292
293 dirty_ = YES;
294 }
295}
296
297-(CGPoint) position
298{
299 return position_;
300}
301
302-(void)update:(ccTime)time
303{
304 if ( dirty_ )
305 {
306 int i, j;
307
308 for( i = 0; i < gridSize_.x+1; i++ )
309 {
310 for( j = 0; j < gridSize_.y+1; j++ )
311 {
312 ccVertex3F v = [self originalVertex:ccg(i,j)];
313 CGPoint vect = ccpSub(positionInPixels_, ccp(v.x,v.y));
314 CGFloat r = ccpLength(vect);
315
316 if ( r < radius_ )
317 {
318 r = radius_ - r;
319 CGFloat pre_log = r / radius_;
320 if ( pre_log == 0 ) pre_log = 0.001f;
321 float l = logf(pre_log) * lensEffect_;
322 float new_r = expf( l ) * radius_;
323
324 if ( ccpLength(vect) > 0 )
325 {
326 vect = ccpNormalize(vect);
327 CGPoint new_vect = ccpMult(vect, new_r);
328 v.z += ccpLength(new_vect) * lensEffect_;
329 }
330 }
331
332 [self setVertex:ccg(i,j) vertex:v];
333 }
334 }
335
336 dirty_ = NO;
337 }
338}
339
340@end
341
342////////////////////////////////////////////////////////////
343
344#pragma mark -
345#pragma mark Ripple3D
346
347@implementation CCRipple3D
348
349@synthesize amplitude = amplitude_;
350@synthesize amplitudeRate = amplitudeRate_;
351
352+(id)actionWithPosition:(CGPoint)pos radius:(float)r waves:(int)wav amplitude:(float)amp grid:(ccGridSize)gridSize duration:(ccTime)d
353{
354 return [[[self alloc] initWithPosition:pos radius:r waves:wav amplitude:amp grid:gridSize duration:d] autorelease];
355}
356
357-(id)initWithPosition:(CGPoint)pos radius:(float)r waves:(int)wav amplitude:(float)amp grid:(ccGridSize)gSize duration:(ccTime)d
358{
359 if ( (self = [super initWithSize:gSize duration:d]) )
360 {
361 self.position = pos;
362 radius_ = r;
363 waves_ = wav;
364 amplitude_ = amp;
365 amplitudeRate_ = 1.0f;
366 }
367
368 return self;
369}
370
371-(CGPoint) position
372{
373 return position_;
374}
375
376-(void) setPosition:(CGPoint)pos
377{
378 position_ = pos;
379 positionInPixels_.x = pos.x * CC_CONTENT_SCALE_FACTOR();
380 positionInPixels_.y = pos.y * CC_CONTENT_SCALE_FACTOR();
381}
382
383-(id) copyWithZone: (NSZone*) zone
384{
385 CCGridAction *copy = [[[self class] allocWithZone:zone] initWithPosition:position_ radius:radius_ waves:waves_ amplitude:amplitude_ grid:gridSize_ duration:duration_];
386 return copy;
387}
388
389
390-(void)update:(ccTime)time
391{
392 int i, j;
393
394 for( i = 0; i < (gridSize_.x+1); i++ )
395 {
396 for( j = 0; j < (gridSize_.y+1); j++ )
397 {
398 ccVertex3F v = [self originalVertex:ccg(i,j)];
399 CGPoint vect = ccpSub(positionInPixels_, ccp(v.x,v.y));
400 CGFloat r = ccpLength(vect);
401
402 if ( r < radius_ )
403 {
404 r = radius_ - r;
405 CGFloat rate = powf( r / radius_, 2);
406 v.z += (sinf( time*(CGFloat)M_PI*waves_*2 + r * 0.1f) * amplitude_ * amplitudeRate_ * rate );
407 }
408
409 [self setVertex:ccg(i,j) vertex:v];
410 }
411 }
412}
413
414@end
415
416////////////////////////////////////////////////////////////
417
418#pragma mark -
419#pragma mark Shaky3D
420
421@implementation CCShaky3D
422
423+(id)actionWithRange:(int)range shakeZ:(BOOL)sz grid:(ccGridSize)gridSize duration:(ccTime)d
424{
425 return [[[self alloc] initWithRange:range shakeZ:sz grid:gridSize duration:d] autorelease];
426}
427
428-(id)initWithRange:(int)range shakeZ:(BOOL)sz grid:(ccGridSize)gSize duration:(ccTime)d
429{
430 if ( (self = [super initWithSize:gSize duration:d]) )
431 {
432 randrange = range;
433 shakeZ = sz;
434 }
435
436 return self;
437}
438
439-(id) copyWithZone: (NSZone*) zone
440{
441 CCGridAction *copy = [[[self class] allocWithZone:zone] initWithRange:randrange shakeZ:shakeZ grid:gridSize_ duration:duration_];
442 return copy;
443}
444
445-(void)update:(ccTime)time
446{
447 int i, j;
448
449 for( i = 0; i < (gridSize_.x+1); i++ )
450 {
451 for( j = 0; j < (gridSize_.y+1); j++ )
452 {
453 ccVertex3F v = [self originalVertex:ccg(i,j)];
454 v.x += ( rand() % (randrange*2) ) - randrange;
455 v.y += ( rand() % (randrange*2) ) - randrange;
456 if( shakeZ )
457 v.z += ( rand() % (randrange*2) ) - randrange;
458
459 [self setVertex:ccg(i,j) vertex:v];
460 }
461 }
462}
463
464@end
465
466////////////////////////////////////////////////////////////
467
468#pragma mark -
469#pragma mark Liquid
470
471@implementation CCLiquid
472
473@synthesize amplitude;
474@synthesize amplitudeRate;
475
476+(id)actionWithWaves:(int)wav amplitude:(float)amp grid:(ccGridSize)gridSize duration:(ccTime)d
477{
478 return [[[self alloc] initWithWaves:wav amplitude:amp grid:gridSize duration:d] autorelease];
479}
480
481-(id)initWithWaves:(int)wav amplitude:(float)amp grid:(ccGridSize)gSize duration:(ccTime)d
482{
483 if ( (self = [super initWithSize:gSize duration:d]) )
484 {
485 waves = wav;
486 amplitude = amp;
487 amplitudeRate = 1.0f;
488 }
489
490 return self;
491}
492
493-(void)update:(ccTime)time
494{
495 int i, j;
496
497 for( i = 1; i < gridSize_.x; i++ )
498 {
499 for( j = 1; j < gridSize_.y; j++ )
500 {
501 ccVertex3F v = [self originalVertex:ccg(i,j)];
502 v.x = (v.x + (sinf(time*(CGFloat)M_PI*waves*2 + v.x * .01f) * amplitude * amplitudeRate));
503 v.y = (v.y + (sinf(time*(CGFloat)M_PI*waves*2 + v.y * .01f) * amplitude * amplitudeRate));
504 [self setVertex:ccg(i,j) vertex:v];
505 }
506 }
507}
508
509-(id) copyWithZone: (NSZone*) zone
510{
511 CCGridAction *copy = [[[self class] allocWithZone:zone] initWithWaves:waves amplitude:amplitude grid:gridSize_ duration:duration_];
512 return copy;
513}
514
515@end
516
517////////////////////////////////////////////////////////////
518
519#pragma mark -
520#pragma mark Waves
521
522@implementation CCWaves
523
524@synthesize amplitude;
525@synthesize amplitudeRate;
526
527+(id)actionWithWaves:(int)wav amplitude:(float)amp horizontal:(BOOL)h vertical:(BOOL)v grid:(ccGridSize)gridSize duration:(ccTime)d
528{
529 return [[[self alloc] initWithWaves:wav amplitude:amp horizontal:h vertical:v grid:gridSize duration:d] autorelease];
530}
531
532-(id)initWithWaves:(int)wav amplitude:(float)amp horizontal:(BOOL)h vertical:(BOOL)v grid:(ccGridSize)gSize duration:(ccTime)d
533{
534 if ( (self = [super initWithSize:gSize duration:d]) )
535 {
536 waves = wav;
537 amplitude = amp;
538 amplitudeRate = 1.0f;
539 horizontal = h;
540 vertical = v;
541 }
542
543 return self;
544}
545
546-(void)update:(ccTime)time
547{
548 int i, j;
549
550 for( i = 0; i < (gridSize_.x+1); i++ )
551 {
552 for( j = 0; j < (gridSize_.y+1); j++ )
553 {
554 ccVertex3F v = [self originalVertex:ccg(i,j)];
555
556 if ( vertical )
557 v.x = (v.x + (sinf(time*(CGFloat)M_PI*waves*2 + v.y * .01f) * amplitude * amplitudeRate));
558
559 if ( horizontal )
560 v.y = (v.y + (sinf(time*(CGFloat)M_PI*waves*2 + v.x * .01f) * amplitude * amplitudeRate));
561
562 [self setVertex:ccg(i,j) vertex:v];
563 }
564 }
565}
566
567-(id) copyWithZone: (NSZone*) zone
568{
569 CCGridAction *copy = [[[self class] allocWithZone:zone] initWithWaves:waves amplitude:amplitude horizontal:horizontal vertical:vertical grid:gridSize_ duration:duration_];
570 return copy;
571}
572
573@end
574
575////////////////////////////////////////////////////////////
576
577#pragma mark -
578#pragma mark Twirl
579
580@implementation CCTwirl
581
582@synthesize amplitude = amplitude_;
583@synthesize amplitudeRate = amplitudeRate_;
584
585+(id)actionWithPosition:(CGPoint)pos twirls:(int)t amplitude:(float)amp grid:(ccGridSize)gridSize duration:(ccTime)d
586{
587 return [[[self alloc] initWithPosition:pos twirls:t amplitude:amp grid:gridSize duration:d] autorelease];
588}
589
590-(id)initWithPosition:(CGPoint)pos twirls:(int)t amplitude:(float)amp grid:(ccGridSize)gSize duration:(ccTime)d
591{
592 if ( (self = [super initWithSize:gSize duration:d]) )
593 {
594 self.position = pos;
595 twirls_ = t;
596 amplitude_ = amp;
597 amplitudeRate_ = 1.0f;
598 }
599
600 return self;
601}
602
603-(void) setPosition:(CGPoint)pos
604{
605 position_ = pos;
606 positionInPixels_.x = pos.x * CC_CONTENT_SCALE_FACTOR();
607 positionInPixels_.y = pos.y * CC_CONTENT_SCALE_FACTOR();
608}
609
610-(CGPoint) position
611{
612 return position_;
613}
614
615-(void)update:(ccTime)time
616{
617 int i, j;
618 CGPoint c = positionInPixels_;
619
620 for( i = 0; i < (gridSize_.x+1); i++ )
621 {
622 for( j = 0; j < (gridSize_.y+1); j++ )
623 {
624 ccVertex3F v = [self originalVertex:ccg(i,j)];
625
626 CGPoint avg = ccp(i-(gridSize_.x/2.0f), j-(gridSize_.y/2.0f));
627 CGFloat r = ccpLength( avg );
628
629 CGFloat amp = 0.1f * amplitude_ * amplitudeRate_;
630 CGFloat a = r * cosf( (CGFloat)M_PI/2.0f + time * (CGFloat)M_PI * twirls_ * 2 ) * amp;
631
632 float cosA = cosf(a);
633 float sinA = sinf(a);
634
635 CGPoint d = {
636 sinA * (v.y-c.y) + cosA * (v.x-c.x),
637 cosA * (v.y-c.y) - sinA * (v.x-c.x)
638 };
639
640 v.x = c.x + d.x;
641 v.y = c.y + d.y;
642
643 [self setVertex:ccg(i,j) vertex:v];
644 }
645 }
646}
647
648-(id) copyWithZone: (NSZone*) zone
649{
650 CCGridAction *copy = [[[self class] allocWithZone:zone] initWithPosition:position_
651 twirls:twirls_
652 amplitude:amplitude_
653 grid:gridSize_
654 duration:duration_];
655 return copy;
656}
657
658
659@end