summary refs log tree commit diff stats
path: root/libs/cocos2d/CCActionTiledGrid.m
diff options
context:
space:
mode:
Diffstat (limited to 'libs/cocos2d/CCActionTiledGrid.m')
-rwxr-xr-xlibs/cocos2d/CCActionTiledGrid.m768
1 files changed, 768 insertions, 0 deletions
diff --git a/libs/cocos2d/CCActionTiledGrid.m b/libs/cocos2d/CCActionTiledGrid.m new file mode 100755 index 0000000..75965ec --- /dev/null +++ b/libs/cocos2d/CCActionTiledGrid.m
@@ -0,0 +1,768 @@
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 "CCActionTiledGrid.h"
28#import "CCDirector.h"
29#import "ccMacros.h"
30#import "Support/CGPointExtension.h"
31
32typedef struct
33{
34 CGPoint position;
35 CGPoint startPosition;
36 ccGridSize delta;
37} Tile;
38
39#pragma mark -
40#pragma mark ShakyTiles3D
41
42@implementation CCShakyTiles3D
43
44+(id)actionWithRange:(int)range shakeZ:(BOOL)shakeZ grid:(ccGridSize)gridSize duration:(ccTime)d
45{
46 return [[[self alloc] initWithRange:range shakeZ:shakeZ grid:gridSize duration:d] autorelease];
47}
48
49-(id)initWithRange:(int)range shakeZ:(BOOL)sz grid:(ccGridSize)gSize duration:(ccTime)d
50{
51 if ( (self = [super initWithSize:gSize duration:d]) )
52 {
53 randrange = range;
54 shakeZ = sz;
55 }
56
57 return self;
58}
59
60-(id) copyWithZone: (NSZone*) zone
61{
62 CCGridAction *copy = [[[self class] allocWithZone:zone] initWithRange:randrange shakeZ:shakeZ grid:gridSize_ duration:duration_];
63 return copy;
64}
65
66
67-(void)update:(ccTime)time
68{
69 int i, j;
70
71 for( i = 0; i < gridSize_.x; i++ )
72 {
73 for( j = 0; j < gridSize_.y; j++ )
74 {
75 ccQuad3 coords = [self originalTile:ccg(i,j)];
76
77 // X
78 coords.bl.x += ( rand() % (randrange*2) ) - randrange;
79 coords.br.x += ( rand() % (randrange*2) ) - randrange;
80 coords.tl.x += ( rand() % (randrange*2) ) - randrange;
81 coords.tr.x += ( rand() % (randrange*2) ) - randrange;
82
83 // Y
84 coords.bl.y += ( rand() % (randrange*2) ) - randrange;
85 coords.br.y += ( rand() % (randrange*2) ) - randrange;
86 coords.tl.y += ( rand() % (randrange*2) ) - randrange;
87 coords.tr.y += ( rand() % (randrange*2) ) - randrange;
88
89 if( shakeZ ) {
90 coords.bl.z += ( rand() % (randrange*2) ) - randrange;
91 coords.br.z += ( rand() % (randrange*2) ) - randrange;
92 coords.tl.z += ( rand() % (randrange*2) ) - randrange;
93 coords.tr.z += ( rand() % (randrange*2) ) - randrange;
94 }
95
96 [self setTile:ccg(i,j) coords:coords];
97 }
98 }
99}
100
101@end
102
103////////////////////////////////////////////////////////////
104
105#pragma mark -
106#pragma mark CCShatteredTiles3D
107
108@implementation CCShatteredTiles3D
109
110+(id)actionWithRange:(int)range shatterZ:(BOOL)sz grid:(ccGridSize)gridSize duration:(ccTime)d
111{
112 return [[[self alloc] initWithRange:range shatterZ:sz grid:gridSize duration:d] autorelease];
113}
114
115-(id)initWithRange:(int)range shatterZ:(BOOL)sz grid:(ccGridSize)gSize duration:(ccTime)d
116{
117 if ( (self = [super initWithSize:gSize duration:d]) )
118 {
119 once = NO;
120 randrange = range;
121 shatterZ = sz;
122 }
123
124 return self;
125}
126
127-(id) copyWithZone: (NSZone*) zone
128{
129 CCGridAction *copy = [[[self class] allocWithZone:zone] initWithRange:randrange shatterZ:shatterZ grid:gridSize_ duration:duration_];
130 return copy;
131}
132
133
134-(void)update:(ccTime)time
135{
136 int i, j;
137
138 if ( once == NO )
139 {
140 for( i = 0; i < gridSize_.x; i++ )
141 {
142 for( j = 0; j < gridSize_.y; j++ )
143 {
144 ccQuad3 coords = [self originalTile:ccg(i,j)];
145
146 // X
147 coords.bl.x += ( rand() % (randrange*2) ) - randrange;
148 coords.br.x += ( rand() % (randrange*2) ) - randrange;
149 coords.tl.x += ( rand() % (randrange*2) ) - randrange;
150 coords.tr.x += ( rand() % (randrange*2) ) - randrange;
151
152 // Y
153 coords.bl.y += ( rand() % (randrange*2) ) - randrange;
154 coords.br.y += ( rand() % (randrange*2) ) - randrange;
155 coords.tl.y += ( rand() % (randrange*2) ) - randrange;
156 coords.tr.y += ( rand() % (randrange*2) ) - randrange;
157
158 if( shatterZ ) {
159 coords.bl.z += ( rand() % (randrange*2) ) - randrange;
160 coords.br.z += ( rand() % (randrange*2) ) - randrange;
161 coords.tl.z += ( rand() % (randrange*2) ) - randrange;
162 coords.tr.z += ( rand() % (randrange*2) ) - randrange;
163 }
164
165 [self setTile:ccg(i,j) coords:coords];
166 }
167 }
168
169 once = YES;
170 }
171}
172
173@end
174
175////////////////////////////////////////////////////////////
176
177#pragma mark -
178#pragma mark CCShuffleTiles
179
180@implementation CCShuffleTiles
181
182+(id)actionWithSeed:(int)s grid:(ccGridSize)gridSize duration:(ccTime)d
183{
184 return [[[self alloc] initWithSeed:s grid:gridSize duration:d] autorelease];
185}
186
187-(id)initWithSeed:(int)s grid:(ccGridSize)gSize duration:(ccTime)d
188{
189 if ( (self = [super initWithSize:gSize duration:d]) )
190 {
191 seed = s;
192 tilesOrder = nil;
193 tiles = nil;
194 }
195
196 return self;
197}
198
199-(id) copyWithZone: (NSZone*) zone
200{
201 CCGridAction *copy = [[[self class] allocWithZone:zone] initWithSeed:seed grid:gridSize_ duration:duration_];
202 return copy;
203}
204
205
206-(void)dealloc
207{
208 if ( tilesOrder ) free(tilesOrder);
209 if ( tiles ) free(tiles);
210 [super dealloc];
211}
212
213-(void)shuffle:(int*)array count:(NSUInteger)len
214{
215 NSInteger i;
216 for( i = len - 1; i >= 0; i-- )
217 {
218 NSInteger j = rand() % (i+1);
219 int v = array[i];
220 array[i] = array[j];
221 array[j] = v;
222 }
223}
224
225-(ccGridSize)getDelta:(ccGridSize)pos
226{
227 CGPoint pos2;
228
229 NSInteger idx = pos.x * gridSize_.y + pos.y;
230
231 pos2.x = tilesOrder[idx] / (int)gridSize_.y;
232 pos2.y = tilesOrder[idx] % (int)gridSize_.y;
233
234 return ccg(pos2.x - pos.x, pos2.y - pos.y);
235}
236
237-(void)placeTile:(ccGridSize)pos tile:(Tile)t
238{
239 ccQuad3 coords = [self originalTile:pos];
240
241 CGPoint step = [[target_ grid] step];
242 coords.bl.x += (int)(t.position.x * step.x);
243 coords.bl.y += (int)(t.position.y * step.y);
244
245 coords.br.x += (int)(t.position.x * step.x);
246 coords.br.y += (int)(t.position.y * step.y);
247
248 coords.tl.x += (int)(t.position.x * step.x);
249 coords.tl.y += (int)(t.position.y * step.y);
250
251 coords.tr.x += (int)(t.position.x * step.x);
252 coords.tr.y += (int)(t.position.y * step.y);
253
254 [self setTile:pos coords:coords];
255}
256
257-(void)startWithTarget:(id)aTarget
258{
259 [super startWithTarget:aTarget];
260
261 if ( seed != -1 )
262 srand(seed);
263
264 tilesCount = gridSize_.x * gridSize_.y;
265 tilesOrder = (int*)malloc(tilesCount*sizeof(int));
266 int i, j;
267
268 for( i = 0; i < tilesCount; i++ )
269 tilesOrder[i] = i;
270
271 [self shuffle:tilesOrder count:tilesCount];
272
273 tiles = malloc(tilesCount*sizeof(Tile));
274 Tile *tileArray = (Tile*)tiles;
275
276 for( i = 0; i < gridSize_.x; i++ )
277 {
278 for( j = 0; j < gridSize_.y; j++ )
279 {
280 tileArray->position = ccp(i,j);
281 tileArray->startPosition = ccp(i,j);
282 tileArray->delta = [self getDelta:ccg(i,j)];
283 tileArray++;
284 }
285 }
286}
287
288-(void)update:(ccTime)time
289{
290 int i, j;
291
292 Tile *tileArray = (Tile*)tiles;
293
294 for( i = 0; i < gridSize_.x; i++ )
295 {
296 for( j = 0; j < gridSize_.y; j++ )
297 {
298 tileArray->position = ccpMult( ccp(tileArray->delta.x, tileArray->delta.y), time);
299 [self placeTile:ccg(i,j) tile:*tileArray];
300 tileArray++;
301 }
302 }
303}
304
305@end
306
307////////////////////////////////////////////////////////////
308
309#pragma mark -
310#pragma mark CCFadeOutTRTiles
311
312@implementation CCFadeOutTRTiles
313
314-(float)testFunc:(ccGridSize)pos time:(ccTime)time
315{
316 CGPoint n = ccpMult( ccp(gridSize_.x,gridSize_.y), time);
317 if ( (n.x+n.y) == 0.0f )
318 return 1.0f;
319
320 return powf( (pos.x+pos.y) / (n.x+n.y), 6 );
321}
322
323-(void)turnOnTile:(ccGridSize)pos
324{
325 [self setTile:pos coords:[self originalTile:pos]];
326}
327
328-(void)turnOffTile:(ccGridSize)pos
329{
330 ccQuad3 coords;
331 bzero(&coords, sizeof(ccQuad3));
332 [self setTile:pos coords:coords];
333}
334
335-(void)transformTile:(ccGridSize)pos distance:(float)distance
336{
337 ccQuad3 coords = [self originalTile:pos];
338 CGPoint step = [[target_ grid] step];
339
340 coords.bl.x += (step.x / 2) * (1.0f - distance);
341 coords.bl.y += (step.y / 2) * (1.0f - distance);
342
343 coords.br.x -= (step.x / 2) * (1.0f - distance);
344 coords.br.y += (step.y / 2) * (1.0f - distance);
345
346 coords.tl.x += (step.x / 2) * (1.0f - distance);
347 coords.tl.y -= (step.y / 2) * (1.0f - distance);
348
349 coords.tr.x -= (step.x / 2) * (1.0f - distance);
350 coords.tr.y -= (step.y / 2) * (1.0f - distance);
351
352 [self setTile:pos coords:coords];
353}
354
355-(void)update:(ccTime)time
356{
357 int i, j;
358
359 for( i = 0; i < gridSize_.x; i++ )
360 {
361 for( j = 0; j < gridSize_.y; j++ )
362 {
363 float distance = [self testFunc:ccg(i,j) time:time];
364 if ( distance == 0 )
365 [self turnOffTile:ccg(i,j)];
366 else if ( distance < 1 )
367 [self transformTile:ccg(i,j) distance:distance];
368 else
369 [self turnOnTile:ccg(i,j)];
370 }
371 }
372}
373
374@end
375
376////////////////////////////////////////////////////////////
377
378#pragma mark -
379#pragma mark CCFadeOutBLTiles
380
381@implementation CCFadeOutBLTiles
382
383-(float)testFunc:(ccGridSize)pos time:(ccTime)time
384{
385 CGPoint n = ccpMult(ccp(gridSize_.x, gridSize_.y), (1.0f-time));
386 if ( (pos.x+pos.y) == 0 )
387 return 1.0f;
388
389 return powf( (n.x+n.y) / (pos.x+pos.y), 6 );
390}
391
392@end
393
394////////////////////////////////////////////////////////////
395
396#pragma mark -
397#pragma mark CCFadeOutUpTiles
398
399@implementation CCFadeOutUpTiles
400
401-(float)testFunc:(ccGridSize)pos time:(ccTime)time
402{
403 CGPoint n = ccpMult(ccp(gridSize_.x, gridSize_.y), time);
404 if ( n.y == 0 )
405 return 1.0f;
406
407 return powf( pos.y / n.y, 6 );
408}
409
410-(void)transformTile:(ccGridSize)pos distance:(float)distance
411{
412 ccQuad3 coords = [self originalTile:pos];
413 CGPoint step = [[target_ grid] step];
414
415 coords.bl.y += (step.y / 2) * (1.0f - distance);
416 coords.br.y += (step.y / 2) * (1.0f - distance);
417 coords.tl.y -= (step.y / 2) * (1.0f - distance);
418 coords.tr.y -= (step.y / 2) * (1.0f - distance);
419
420 [self setTile:pos coords:coords];
421}
422
423@end
424
425////////////////////////////////////////////////////////////
426
427#pragma mark -
428#pragma mark CCFadeOutDownTiles
429
430@implementation CCFadeOutDownTiles
431
432-(float)testFunc:(ccGridSize)pos time:(ccTime)time
433{
434 CGPoint n = ccpMult(ccp(gridSize_.x,gridSize_.y), (1.0f - time));
435 if ( pos.y == 0 )
436 return 1.0f;
437
438 return powf( n.y / pos.y, 6 );
439}
440
441@end
442
443////////////////////////////////////////////////////////////
444
445#pragma mark -
446#pragma mark TurnOffTiles
447
448@implementation CCTurnOffTiles
449
450+(id)actionWithSeed:(int)s grid:(ccGridSize)gridSize duration:(ccTime)d
451{
452 return [[[self alloc] initWithSeed:s grid:gridSize duration:d] autorelease];
453}
454
455-(id)initWithSeed:(int)s grid:(ccGridSize)gSize duration:(ccTime)d
456{
457 if ( (self = [super initWithSize:gSize duration:d]) )
458 {
459 seed = s;
460 tilesOrder = nil;
461 }
462
463 return self;
464}
465
466-(id) copyWithZone: (NSZone*) zone
467{
468 CCGridAction *copy = [[[self class] allocWithZone:zone] initWithSeed:seed grid:gridSize_ duration:duration_];
469 return copy;
470}
471
472-(void)dealloc
473{
474 if ( tilesOrder ) free(tilesOrder);
475 [super dealloc];
476}
477
478-(void)shuffle:(int*)array count:(NSUInteger)len
479{
480 NSInteger i;
481 for( i = len - 1; i >= 0; i-- )
482 {
483 NSInteger j = rand() % (i+1);
484 int v = array[i];
485 array[i] = array[j];
486 array[j] = v;
487 }
488}
489
490-(void)turnOnTile:(ccGridSize)pos
491{
492 [self setTile:pos coords:[self originalTile:pos]];
493}
494
495-(void)turnOffTile:(ccGridSize)pos
496{
497 ccQuad3 coords;
498
499 bzero(&coords, sizeof(ccQuad3));
500 [self setTile:pos coords:coords];
501}
502
503-(void)startWithTarget:(id)aTarget
504{
505 int i;
506
507 [super startWithTarget:aTarget];
508
509 if ( seed != -1 )
510 srand(seed);
511
512 tilesCount = gridSize_.x * gridSize_.y;
513 tilesOrder = (int*)malloc(tilesCount*sizeof(int));
514
515 for( i = 0; i < tilesCount; i++ )
516 tilesOrder[i] = i;
517
518 [self shuffle:tilesOrder count:tilesCount];
519}
520
521-(void)update:(ccTime)time
522{
523 int i, l, t;
524
525 l = (int)(time * (float)tilesCount);
526
527 for( i = 0; i < tilesCount; i++ )
528 {
529 t = tilesOrder[i];
530 ccGridSize tilePos = ccg( t / gridSize_.y, t % gridSize_.y );
531
532 if ( i < l )
533 [self turnOffTile:tilePos];
534 else
535 [self turnOnTile:tilePos];
536 }
537}
538
539@end
540
541////////////////////////////////////////////////////////////
542
543#pragma mark -
544#pragma mark CCWavesTiles3D
545
546@implementation CCWavesTiles3D
547
548@synthesize amplitude;
549@synthesize amplitudeRate;
550
551+(id)actionWithWaves:(int)wav amplitude:(float)amp grid:(ccGridSize)gridSize duration:(ccTime)d
552{
553 return [[[self alloc] initWithWaves:wav amplitude:amp grid:gridSize duration:d] autorelease];
554}
555
556-(id)initWithWaves:(int)wav amplitude:(float)amp grid:(ccGridSize)gSize duration:(ccTime)d
557{
558 if ( (self = [super initWithSize:gSize duration:d]) )
559 {
560 waves = wav;
561 amplitude = amp;
562 amplitudeRate = 1.0f;
563 }
564
565 return self;
566}
567
568-(id) copyWithZone: (NSZone*) zone
569{
570 CCGridAction *copy = [[[self class] allocWithZone:zone] initWithWaves:waves amplitude:amplitude grid:gridSize_ duration:duration_];
571 return copy;
572}
573
574
575-(void)update:(ccTime)time
576{
577 int i, j;
578
579 for( i = 0; i < gridSize_.x; i++ )
580 {
581 for( j = 0; j < gridSize_.y; j++ )
582 {
583 ccQuad3 coords = [self originalTile:ccg(i,j)];
584
585 coords.bl.z = (sinf(time*(CGFloat)M_PI*waves*2 + (coords.bl.y+coords.bl.x) * .01f) * amplitude * amplitudeRate );
586 coords.br.z = coords.bl.z;
587 coords.tl.z = coords.bl.z;
588 coords.tr.z = coords.bl.z;
589
590 [self setTile:ccg(i,j) coords:coords];
591 }
592 }
593}
594@end
595
596////////////////////////////////////////////////////////////
597
598#pragma mark -
599#pragma mark CCJumpTiles3D
600
601@implementation CCJumpTiles3D
602
603@synthesize amplitude;
604@synthesize amplitudeRate;
605
606+(id)actionWithJumps:(int)j amplitude:(float)amp grid:(ccGridSize)gridSize duration:(ccTime)d
607{
608 return [[[self alloc] initWithJumps:j amplitude:amp grid:gridSize duration:d] autorelease];
609}
610
611-(id)initWithJumps:(int)j amplitude:(float)amp grid:(ccGridSize)gSize duration:(ccTime)d
612{
613 if ( (self = [super initWithSize:gSize duration:d]) )
614 {
615 jumps = j;
616 amplitude = amp;
617 amplitudeRate = 1.0f;
618 }
619
620 return self;
621}
622
623-(id) copyWithZone: (NSZone*) zone
624{
625 CCGridAction *copy = [[[self class] allocWithZone:zone] initWithJumps:jumps amplitude:amplitude grid:gridSize_ duration:duration_];
626 return copy;
627}
628
629
630-(void)update:(ccTime)time
631{
632 int i, j;
633
634 float sinz = (sinf((CGFloat)M_PI*time*jumps*2) * amplitude * amplitudeRate );
635 float sinz2 = (sinf((CGFloat)M_PI*(time*jumps*2 + 1)) * amplitude * amplitudeRate );
636
637 for( i = 0; i < gridSize_.x; i++ )
638 {
639 for( j = 0; j < gridSize_.y; j++ )
640 {
641 ccQuad3 coords = [self originalTile:ccg(i,j)];
642
643 if ( ((i+j) % 2) == 0 )
644 {
645 coords.bl.z += sinz;
646 coords.br.z += sinz;
647 coords.tl.z += sinz;
648 coords.tr.z += sinz;
649 }
650 else
651 {
652 coords.bl.z += sinz2;
653 coords.br.z += sinz2;
654 coords.tl.z += sinz2;
655 coords.tr.z += sinz2;
656 }
657
658 [self setTile:ccg(i,j) coords:coords];
659 }
660 }
661}
662@end
663
664////////////////////////////////////////////////////////////
665
666#pragma mark -
667#pragma mark SplitRows
668
669@implementation CCSplitRows
670
671+(id)actionWithRows:(int)r duration:(ccTime)d
672{
673 return [[[self alloc] initWithRows:r duration:d] autorelease];
674}
675
676-(id)initWithRows:(int)r duration:(ccTime)d
677{
678 rows = r;
679 return [super initWithSize:ccg(1,r) duration:d];
680}
681
682-(id) copyWithZone: (NSZone*) zone
683{
684 CCGridAction *copy = [[[self class] allocWithZone:zone] initWithRows:rows duration:duration_];
685 return copy;
686}
687
688-(void)startWithTarget:(id)aTarget
689{
690 [super startWithTarget:aTarget];
691 winSize = [[CCDirector sharedDirector] winSizeInPixels];
692}
693
694-(void)update:(ccTime)time
695{
696 int j;
697
698 for( j = 0; j < gridSize_.y; j++ )
699 {
700 ccQuad3 coords = [self originalTile:ccg(0,j)];
701 float direction = 1;
702
703 if ( (j % 2 ) == 0 )
704 direction = -1;
705
706 coords.bl.x += direction * winSize.width * time;
707 coords.br.x += direction * winSize.width * time;
708 coords.tl.x += direction * winSize.width * time;
709 coords.tr.x += direction * winSize.width * time;
710
711 [self setTile:ccg(0,j) coords:coords];
712 }
713}
714
715@end
716
717////////////////////////////////////////////////////////////
718
719#pragma mark -
720#pragma mark CCSplitCols
721
722@implementation CCSplitCols
723
724+(id)actionWithCols:(int)c duration:(ccTime)d
725{
726 return [[[self alloc] initWithCols:c duration:d] autorelease];
727}
728
729-(id)initWithCols:(int)c duration:(ccTime)d
730{
731 cols = c;
732 return [super initWithSize:ccg(c,1) duration:d];
733}
734
735-(id) copyWithZone: (NSZone*) zone
736{
737 CCGridAction *copy = [[[self class] allocWithZone:zone] initWithCols:cols duration:duration_];
738 return copy;
739}
740
741-(void)startWithTarget:(id)aTarget
742{
743 [super startWithTarget:aTarget];
744 winSize = [[CCDirector sharedDirector] winSizeInPixels];
745}
746
747-(void)update:(ccTime)time
748{
749 int i;
750
751 for( i = 0; i < gridSize_.x; i++ )
752 {
753 ccQuad3 coords = [self originalTile:ccg(i,0)];
754 float direction = 1;
755
756 if ( (i % 2 ) == 0 )
757 direction = -1;
758
759 coords.bl.y += direction * winSize.height * time;
760 coords.br.y += direction * winSize.height * time;
761 coords.tl.y += direction * winSize.height * time;
762 coords.tr.y += direction * winSize.height * time;
763
764 [self setTile:ccg(i,0) coords:coords];
765 }
766}
767
768@end