summary refs log tree commit diff stats
path: root/libs/cocos2d/CCActionInstant.m
diff options
context:
space:
mode:
Diffstat (limited to 'libs/cocos2d/CCActionInstant.m')
-rwxr-xr-xlibs/cocos2d/CCActionInstant.m477
1 files changed, 477 insertions, 0 deletions
diff --git a/libs/cocos2d/CCActionInstant.m b/libs/cocos2d/CCActionInstant.m new file mode 100755 index 0000000..e7f6fad --- /dev/null +++ b/libs/cocos2d/CCActionInstant.m
@@ -0,0 +1,477 @@
1/*
2 * cocos2d for iPhone: http://www.cocos2d-iphone.org
3 *
4 * Copyright (c) 2008-2010 Ricardo Quesada
5 * Copyright (c) 2011 Zynga Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 *
25 */
26
27
28#import "CCBlockSupport.h"
29#import "CCActionInstant.h"
30#import "CCNode.h"
31#import "CCSprite.h"
32
33
34//
35// InstantAction
36//
37#pragma mark CCActionInstant
38
39@implementation CCActionInstant
40
41-(id) init
42{
43 if( (self=[super init]) )
44 duration_ = 0;
45
46 return self;
47}
48
49-(id) copyWithZone: (NSZone*) zone
50{
51 CCActionInstant *copy = [[[self class] allocWithZone: zone] init];
52 return copy;
53}
54
55- (BOOL) isDone
56{
57 return YES;
58}
59
60-(void) step: (ccTime) dt
61{
62 [self update: 1];
63}
64
65-(void) update: (ccTime) t
66{
67 // ignore
68}
69
70-(CCFiniteTimeAction*) reverse
71{
72 return [[self copy] autorelease];
73}
74@end
75
76//
77// Show
78//
79#pragma mark CCShow
80
81@implementation CCShow
82-(void) startWithTarget:(id)aTarget
83{
84 [super startWithTarget:aTarget];
85 ((CCNode *)target_).visible = YES;
86}
87
88-(CCFiniteTimeAction*) reverse
89{
90 return [CCHide action];
91}
92@end
93
94//
95// Hide
96//
97#pragma mark CCHide
98
99@implementation CCHide
100-(void) startWithTarget:(id)aTarget
101{
102 [super startWithTarget:aTarget];
103 ((CCNode *)target_).visible = NO;
104}
105
106-(CCFiniteTimeAction*) reverse
107{
108 return [CCShow action];
109}
110@end
111
112//
113// ToggleVisibility
114//
115#pragma mark CCToggleVisibility
116
117@implementation CCToggleVisibility
118-(void) startWithTarget:(id)aTarget
119{
120 [super startWithTarget:aTarget];
121 ((CCNode *)target_).visible = !((CCNode *)target_).visible;
122}
123@end
124
125//
126// FlipX
127//
128#pragma mark CCFlipX
129
130@implementation CCFlipX
131+(id) actionWithFlipX:(BOOL)x
132{
133 return [[[self alloc] initWithFlipX:x] autorelease];
134}
135
136-(id) initWithFlipX:(BOOL)x
137{
138 if(( self=[super init]))
139 flipX = x;
140
141 return self;
142}
143
144-(void) startWithTarget:(id)aTarget
145{
146 [super startWithTarget:aTarget];
147 [(CCSprite*)aTarget setFlipX:flipX];
148}
149
150-(CCFiniteTimeAction*) reverse
151{
152 return [CCFlipX actionWithFlipX:!flipX];
153}
154
155-(id) copyWithZone: (NSZone*) zone
156{
157 CCActionInstant *copy = [[[self class] allocWithZone: zone] initWithFlipX:flipX];
158 return copy;
159}
160@end
161
162//
163// FlipY
164//
165#pragma mark CCFlipY
166
167@implementation CCFlipY
168+(id) actionWithFlipY:(BOOL)y
169{
170 return [[[self alloc] initWithFlipY:y] autorelease];
171}
172
173-(id) initWithFlipY:(BOOL)y
174{
175 if(( self=[super init]))
176 flipY = y;
177
178 return self;
179}
180
181-(void) startWithTarget:(id)aTarget
182{
183 [super startWithTarget:aTarget];
184 [(CCSprite*)aTarget setFlipY:flipY];
185}
186
187-(CCFiniteTimeAction*) reverse
188{
189 return [CCFlipY actionWithFlipY:!flipY];
190}
191
192-(id) copyWithZone: (NSZone*) zone
193{
194 CCActionInstant *copy = [[[self class] allocWithZone: zone] initWithFlipY:flipY];
195 return copy;
196}
197@end
198
199
200//
201// Place
202//
203#pragma mark CCPlace
204
205@implementation CCPlace
206+(id) actionWithPosition: (CGPoint) pos
207{
208 return [[[self alloc]initWithPosition:pos]autorelease];
209}
210
211-(id) initWithPosition: (CGPoint) pos
212{
213 if( (self=[super init]) )
214 position = pos;
215
216 return self;
217}
218
219-(id) copyWithZone: (NSZone*) zone
220{
221 CCActionInstant *copy = [[[self class] allocWithZone: zone] initWithPosition: position];
222 return copy;
223}
224
225-(void) startWithTarget:(id)aTarget
226{
227 [super startWithTarget:aTarget];
228 ((CCNode *)target_).position = position;
229}
230
231@end
232
233//
234// CallFunc
235//
236#pragma mark CCCallFunc
237
238@implementation CCCallFunc
239
240@synthesize targetCallback = targetCallback_;
241
242+(id) actionWithTarget: (id) t selector:(SEL) s
243{
244 return [[[self alloc] initWithTarget: t selector: s] autorelease];
245}
246
247-(id) initWithTarget: (id) t selector:(SEL) s
248{
249 if( (self=[super init]) ) {
250 self.targetCallback = t;
251 selector_ = s;
252 }
253 return self;
254}
255
256-(NSString*) description
257{
258 return [NSString stringWithFormat:@"<%@ = %08X | Tag = %i | target = %@ | selector = %@>",
259 [self class],
260 self,
261 tag_,
262 [targetCallback_ class],
263 NSStringFromSelector(selector_)
264 ];
265}
266
267-(void) dealloc
268{
269 [targetCallback_ release];
270 [super dealloc];
271}
272
273-(id) copyWithZone: (NSZone*) zone
274{
275 CCActionInstant *copy = [[[self class] allocWithZone: zone] initWithTarget:targetCallback_ selector:selector_];
276 return copy;
277}
278
279-(void) startWithTarget:(id)aTarget
280{
281 [super startWithTarget:aTarget];
282 [self execute];
283}
284
285-(void) execute
286{
287 [targetCallback_ performSelector:selector_];
288}
289@end
290
291//
292// CallFuncN
293//
294#pragma mark CCCallFuncN
295
296@implementation CCCallFuncN
297
298-(void) execute
299{
300 [targetCallback_ performSelector:selector_ withObject:target_];
301}
302@end
303
304//
305// CallFuncND
306//
307#pragma mark CCCallFuncND
308
309@implementation CCCallFuncND
310
311@synthesize callbackMethod = callbackMethod_;
312
313+(id) actionWithTarget:(id)t selector:(SEL)s data:(void*)d
314{
315 return [[[self alloc] initWithTarget:t selector:s data:d] autorelease];
316}
317
318-(id) initWithTarget:(id)t selector:(SEL)s data:(void*)d
319{
320 if( (self=[super initWithTarget:t selector:s]) ) {
321 data_ = d;
322
323#if COCOS2D_DEBUG
324 NSMethodSignature * sig = [t methodSignatureForSelector:s]; // added
325 NSAssert(sig !=0 , @"Signature not found for selector - does it have the following form? -(void)name:(id)sender data:(void*)data");
326#endif
327 callbackMethod_ = (CC_CALLBACK_ND) [t methodForSelector:s];
328 }
329 return self;
330}
331
332-(id) copyWithZone: (NSZone*) zone
333{
334 CCActionInstant *copy = [[[self class] allocWithZone: zone] initWithTarget:targetCallback_ selector:selector_ data:data_];
335 return copy;
336}
337
338-(void) dealloc
339{
340 // nothing to dealloc really. Everything is dealloc on super (CCCallFuncN)
341 [super dealloc];
342}
343
344-(void) execute
345{
346 callbackMethod_(targetCallback_,selector_,target_, data_);
347}
348@end
349
350@implementation CCCallFuncO
351@synthesize object = object_;
352
353+(id) actionWithTarget: (id) t selector:(SEL) s object:(id)object
354{
355 return [[[self alloc] initWithTarget:t selector:s object:object] autorelease];
356}
357
358-(id) initWithTarget:(id) t selector:(SEL) s object:(id)object
359{
360 if( (self=[super initWithTarget:t selector:s] ) )
361 self.object = object;
362
363 return self;
364}
365
366- (void) dealloc
367{
368 [object_ release];
369 [super dealloc];
370}
371
372-(id) copyWithZone: (NSZone*) zone
373{
374 CCActionInstant *copy = [[[self class] allocWithZone: zone] initWithTarget:targetCallback_ selector:selector_ object:object_];
375 return copy;
376}
377
378
379-(void) execute
380{
381 [targetCallback_ performSelector:selector_ withObject:object_];
382}
383
384@end
385
386
387#pragma mark -
388#pragma mark Blocks
389
390#if NS_BLOCKS_AVAILABLE
391
392#pragma mark CCCallBlock
393
394@implementation CCCallBlock
395
396+(id) actionWithBlock:(void(^)())block
397{
398 return [[[self alloc] initWithBlock:block] autorelease];
399}
400
401-(id) initWithBlock:(void(^)())block
402{
403 if ((self = [super init]))
404 block_ = [block copy];
405
406 return self;
407}
408
409-(id) copyWithZone: (NSZone*) zone
410{
411 CCActionInstant *copy = [[[self class] allocWithZone: zone] initWithBlock:block_];
412 return copy;
413}
414
415-(void) startWithTarget:(id)aTarget
416{
417 [super startWithTarget:aTarget];
418 [self execute];
419}
420
421-(void) execute
422{
423 block_();
424}
425
426-(void) dealloc
427{
428 [block_ release];
429 [super dealloc];
430}
431
432@end
433
434#pragma mark CCCallBlockN
435
436@implementation CCCallBlockN
437
438+(id) actionWithBlock:(void(^)(CCNode *node))block
439{
440 return [[[self alloc] initWithBlock:block] autorelease];
441}
442
443-(id) initWithBlock:(void(^)(CCNode *node))block
444{
445 if ((self = [super init]))
446 block_ = [block copy];
447
448 return self;
449}
450
451-(id) copyWithZone: (NSZone*) zone
452{
453 CCActionInstant *copy = [[[self class] allocWithZone: zone] initWithBlock:block_];
454 return copy;
455}
456
457-(void) startWithTarget:(id)aTarget
458{
459 [super startWithTarget:aTarget];
460 [self execute];
461}
462
463-(void) execute
464{
465 block_(target_);
466}
467
468-(void) dealloc
469{
470 [block_ release];
471 [super dealloc];
472}
473
474@end
475
476
477#endif // NS_BLOCKS_AVAILABLE