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/CCActionGrid.m | |
| download | cartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.tar.gz cartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.tar.bz2 cartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.zip | |
Initial commit (version 0.2.1)
Diffstat (limited to 'libs/cocos2d/CCActionGrid.m')
| -rwxr-xr-x | libs/cocos2d/CCActionGrid.m | 386 |
1 files changed, 386 insertions, 0 deletions
| diff --git a/libs/cocos2d/CCActionGrid.m b/libs/cocos2d/CCActionGrid.m new file mode 100755 index 0000000..638e27d --- /dev/null +++ b/libs/cocos2d/CCActionGrid.m | |||
| @@ -0,0 +1,386 @@ | |||
| 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 "CCActionGrid.h" | ||
| 28 | #import "CCDirector.h" | ||
| 29 | |||
| 30 | #pragma mark - | ||
| 31 | #pragma mark GridAction | ||
| 32 | |||
| 33 | @implementation CCGridAction | ||
| 34 | |||
| 35 | @synthesize gridSize = gridSize_; | ||
| 36 | |||
| 37 | +(id) actionWithSize:(ccGridSize)size duration:(ccTime)d | ||
| 38 | { | ||
| 39 | return [[[self alloc] initWithSize:size duration:d ] autorelease]; | ||
| 40 | } | ||
| 41 | |||
| 42 | -(id) initWithSize:(ccGridSize)gSize duration:(ccTime)d | ||
| 43 | { | ||
| 44 | if ( (self = [super initWithDuration:d]) ) | ||
| 45 | { | ||
| 46 | gridSize_ = gSize; | ||
| 47 | } | ||
| 48 | |||
| 49 | return self; | ||
| 50 | } | ||
| 51 | |||
| 52 | -(void)startWithTarget:(id)aTarget | ||
| 53 | { | ||
| 54 | [super startWithTarget:aTarget]; | ||
| 55 | |||
| 56 | CCGridBase *newgrid = [self grid]; | ||
| 57 | |||
| 58 | CCNode *t = (CCNode*) target_; | ||
| 59 | CCGridBase *targetGrid = [t grid]; | ||
| 60 | |||
| 61 | if ( targetGrid && targetGrid.reuseGrid > 0 ) | ||
| 62 | { | ||
| 63 | if ( targetGrid.active && targetGrid.gridSize.x == gridSize_.x && targetGrid.gridSize.y == gridSize_.y && [targetGrid isKindOfClass:[newgrid class]] ) | ||
| 64 | [targetGrid reuse]; | ||
| 65 | else | ||
| 66 | [NSException raise:@"GridBase" format:@"Cannot reuse grid"]; | ||
| 67 | } | ||
| 68 | else | ||
| 69 | { | ||
| 70 | if ( targetGrid && targetGrid.active ) | ||
| 71 | targetGrid.active = NO; | ||
| 72 | |||
| 73 | t.grid = newgrid; | ||
| 74 | t.grid.active = YES; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | -(CCGridBase *)grid | ||
| 79 | { | ||
| 80 | [NSException raise:@"GridBase" format:@"Abstract class needs implementation"]; | ||
| 81 | return nil; | ||
| 82 | } | ||
| 83 | |||
| 84 | - (CCActionInterval*) reverse | ||
| 85 | { | ||
| 86 | return [CCReverseTime actionWithAction:self]; | ||
| 87 | } | ||
| 88 | |||
| 89 | -(id) copyWithZone: (NSZone*) zone | ||
| 90 | { | ||
| 91 | CCGridAction *copy = [[[self class] allocWithZone:zone] initWithSize:gridSize_ duration:duration_]; | ||
| 92 | return copy; | ||
| 93 | } | ||
| 94 | @end | ||
| 95 | |||
| 96 | //////////////////////////////////////////////////////////// | ||
| 97 | |||
| 98 | #pragma mark - | ||
| 99 | #pragma mark Grid3DAction | ||
| 100 | |||
| 101 | @implementation CCGrid3DAction | ||
| 102 | |||
| 103 | -(CCGridBase *)grid | ||
| 104 | { | ||
| 105 | return [CCGrid3D gridWithSize:gridSize_]; | ||
| 106 | } | ||
| 107 | |||
| 108 | -(ccVertex3F)vertex:(ccGridSize)pos | ||
| 109 | { | ||
| 110 | CCGrid3D *g = (CCGrid3D *)[target_ grid]; | ||
| 111 | return [g vertex:pos]; | ||
| 112 | } | ||
| 113 | |||
| 114 | -(ccVertex3F)originalVertex:(ccGridSize)pos | ||
| 115 | { | ||
| 116 | CCGrid3D *g = (CCGrid3D *)[target_ grid]; | ||
| 117 | return [g originalVertex:pos]; | ||
| 118 | } | ||
| 119 | |||
| 120 | -(void)setVertex:(ccGridSize)pos vertex:(ccVertex3F)vertex | ||
| 121 | { | ||
| 122 | CCGrid3D *g = (CCGrid3D *)[target_ grid]; | ||
| 123 | [g setVertex:pos vertex:vertex]; | ||
| 124 | } | ||
| 125 | @end | ||
| 126 | |||
| 127 | //////////////////////////////////////////////////////////// | ||
| 128 | |||
| 129 | #pragma mark - | ||
| 130 | #pragma mark TiledGrid3DAction | ||
| 131 | |||
| 132 | @implementation CCTiledGrid3DAction | ||
| 133 | |||
| 134 | -(CCGridBase *)grid | ||
| 135 | { | ||
| 136 | return [CCTiledGrid3D gridWithSize:gridSize_]; | ||
| 137 | } | ||
| 138 | |||
| 139 | -(ccQuad3)tile:(ccGridSize)pos | ||
| 140 | { | ||
| 141 | CCTiledGrid3D *g = (CCTiledGrid3D *)[target_ grid]; | ||
| 142 | return [g tile:pos]; | ||
| 143 | } | ||
| 144 | |||
| 145 | -(ccQuad3)originalTile:(ccGridSize)pos | ||
| 146 | { | ||
| 147 | CCTiledGrid3D *g = (CCTiledGrid3D *)[target_ grid]; | ||
| 148 | return [g originalTile:pos]; | ||
| 149 | } | ||
| 150 | |||
| 151 | -(void)setTile:(ccGridSize)pos coords:(ccQuad3)coords | ||
| 152 | { | ||
| 153 | CCTiledGrid3D *g = (CCTiledGrid3D *)[target_ grid]; | ||
| 154 | [g setTile:pos coords:coords]; | ||
| 155 | } | ||
| 156 | |||
| 157 | @end | ||
| 158 | |||
| 159 | //////////////////////////////////////////////////////////// | ||
| 160 | |||
| 161 | @interface CCActionInterval (Amplitude) | ||
| 162 | -(void)setAmplitudeRate:(CGFloat)amp; | ||
| 163 | -(CGFloat)getAmplitudeRate; | ||
| 164 | @end | ||
| 165 | |||
| 166 | @implementation CCActionInterval (Amplitude) | ||
| 167 | -(void)setAmplitudeRate:(CGFloat)amp | ||
| 168 | { | ||
| 169 | [NSException raise:@"IntervalAction (Amplitude)" format:@"Abstract class needs implementation"]; | ||
| 170 | } | ||
| 171 | |||
| 172 | -(CGFloat)getAmplitudeRate | ||
| 173 | { | ||
| 174 | [NSException raise:@"IntervalAction (Amplitude)" format:@"Abstract class needs implementation"]; | ||
| 175 | return 0; | ||
| 176 | } | ||
| 177 | @end | ||
| 178 | |||
| 179 | //////////////////////////////////////////////////////////// | ||
| 180 | |||
| 181 | #pragma mark - | ||
| 182 | #pragma mark AccelDeccelAmplitude | ||
| 183 | |||
| 184 | @implementation CCAccelDeccelAmplitude | ||
| 185 | |||
| 186 | @synthesize rate=rate_; | ||
| 187 | |||
| 188 | +(id)actionWithAction:(CCAction*)action duration:(ccTime)d | ||
| 189 | { | ||
| 190 | return [[[self alloc] initWithAction:action duration:d ] autorelease]; | ||
| 191 | } | ||
| 192 | |||
| 193 | -(id)initWithAction:(CCAction *)action duration:(ccTime)d | ||
| 194 | { | ||
| 195 | if ( (self = [super initWithDuration:d]) ) | ||
| 196 | { | ||
| 197 | rate_ = 1.0f; | ||
| 198 | other_ = (CCActionInterval*)[action retain]; | ||
| 199 | } | ||
| 200 | |||
| 201 | return self; | ||
| 202 | } | ||
| 203 | |||
| 204 | -(void)dealloc | ||
| 205 | { | ||
| 206 | [other_ release]; | ||
| 207 | [super dealloc]; | ||
| 208 | } | ||
| 209 | |||
| 210 | -(void)startWithTarget:(id)aTarget | ||
| 211 | { | ||
| 212 | [super startWithTarget:aTarget]; | ||
| 213 | [other_ startWithTarget:target_]; | ||
| 214 | } | ||
| 215 | |||
| 216 | -(void) update: (ccTime) time | ||
| 217 | { | ||
| 218 | float f = time*2; | ||
| 219 | |||
| 220 | if (f > 1) | ||
| 221 | { | ||
| 222 | f -= 1; | ||
| 223 | f = 1 - f; | ||
| 224 | } | ||
| 225 | |||
| 226 | [other_ setAmplitudeRate:powf(f, rate_)]; | ||
| 227 | [other_ update:time]; | ||
| 228 | } | ||
| 229 | |||
| 230 | - (CCActionInterval*) reverse | ||
| 231 | { | ||
| 232 | return [CCAccelDeccelAmplitude actionWithAction:[other_ reverse] duration:duration_]; | ||
| 233 | } | ||
| 234 | |||
| 235 | @end | ||
| 236 | |||
| 237 | //////////////////////////////////////////////////////////// | ||
| 238 | |||
| 239 | #pragma mark - | ||
| 240 | #pragma mark AccelAmplitude | ||
| 241 | |||
| 242 | @implementation CCAccelAmplitude | ||
| 243 | |||
| 244 | @synthesize rate=rate_; | ||
| 245 | |||
| 246 | +(id)actionWithAction:(CCAction*)action duration:(ccTime)d | ||
| 247 | { | ||
| 248 | return [[[self alloc] initWithAction:action duration:d ] autorelease]; | ||
| 249 | } | ||
| 250 | |||
| 251 | -(id)initWithAction:(CCAction *)action duration:(ccTime)d | ||
| 252 | { | ||
| 253 | if ( (self = [super initWithDuration:d]) ) | ||
| 254 | { | ||
| 255 | rate_ = 1.0f; | ||
| 256 | other_ = (CCActionInterval*)[action retain]; | ||
| 257 | } | ||
| 258 | |||
| 259 | return self; | ||
| 260 | } | ||
| 261 | |||
| 262 | -(void)dealloc | ||
| 263 | { | ||
| 264 | [other_ release]; | ||
| 265 | [super dealloc]; | ||
| 266 | } | ||
| 267 | |||
| 268 | -(void)startWithTarget:(id)aTarget | ||
| 269 | { | ||
| 270 | [super startWithTarget:aTarget]; | ||
| 271 | [other_ startWithTarget:target_]; | ||
| 272 | } | ||
| 273 | |||
| 274 | -(void) update: (ccTime) time | ||
| 275 | { | ||
| 276 | [other_ setAmplitudeRate:powf(time, rate_)]; | ||
| 277 | [other_ update:time]; | ||
| 278 | } | ||
| 279 | |||
| 280 | - (CCActionInterval*) reverse | ||
| 281 | { | ||
| 282 | return [CCAccelAmplitude actionWithAction:[other_ reverse] duration:self.duration]; | ||
| 283 | } | ||
| 284 | |||
| 285 | @end | ||
| 286 | |||
| 287 | //////////////////////////////////////////////////////////// | ||
| 288 | |||
| 289 | #pragma mark - | ||
| 290 | #pragma mark DeccelAmplitude | ||
| 291 | |||
| 292 | @implementation CCDeccelAmplitude | ||
| 293 | |||
| 294 | @synthesize rate=rate_; | ||
| 295 | |||
| 296 | +(id)actionWithAction:(CCAction*)action duration:(ccTime)d | ||
| 297 | { | ||
| 298 | return [[[self alloc] initWithAction:action duration:d ] autorelease]; | ||
| 299 | } | ||
| 300 | |||
| 301 | -(id)initWithAction:(CCAction *)action duration:(ccTime)d | ||
| 302 | { | ||
| 303 | if ( (self = [super initWithDuration:d]) ) | ||
| 304 | { | ||
| 305 | rate_ = 1.0f; | ||
| 306 | other_ = (CCActionInterval*)[action retain]; | ||
| 307 | } | ||
| 308 | |||
| 309 | return self; | ||
| 310 | } | ||
| 311 | |||
| 312 | -(void)dealloc | ||
| 313 | { | ||
| 314 | [other_ release]; | ||
| 315 | [super dealloc]; | ||
| 316 | } | ||
| 317 | |||
| 318 | -(void)startWithTarget:(id)aTarget | ||
| 319 | { | ||
| 320 | [super startWithTarget:aTarget]; | ||
| 321 | [other_ startWithTarget:target_]; | ||
| 322 | } | ||
| 323 | |||
| 324 | -(void) update: (ccTime) time | ||
| 325 | { | ||
| 326 | [other_ setAmplitudeRate:powf((1-time), rate_)]; | ||
| 327 | [other_ update:time]; | ||
| 328 | } | ||
| 329 | |||
| 330 | - (CCActionInterval*) reverse | ||
| 331 | { | ||
| 332 | return [CCDeccelAmplitude actionWithAction:[other_ reverse] duration:self.duration]; | ||
| 333 | } | ||
| 334 | |||
| 335 | @end | ||
| 336 | |||
| 337 | //////////////////////////////////////////////////////////// | ||
| 338 | |||
| 339 | #pragma mark - | ||
| 340 | #pragma mark StopGrid | ||
| 341 | |||
| 342 | @implementation CCStopGrid | ||
| 343 | |||
| 344 | -(void)startWithTarget:(id)aTarget | ||
| 345 | { | ||
| 346 | [super startWithTarget:aTarget]; | ||
| 347 | |||
| 348 | if ( [[self target] grid] && [[[self target] grid] active] ) { | ||
| 349 | [[[self target] grid] setActive: NO]; | ||
| 350 | |||
| 351 | // [[self target] setGrid: nil]; | ||
| 352 | } | ||
| 353 | } | ||
| 354 | |||
| 355 | @end | ||
| 356 | |||
| 357 | //////////////////////////////////////////////////////////// | ||
| 358 | |||
| 359 | #pragma mark - | ||
| 360 | #pragma mark ReuseGrid | ||
| 361 | |||
| 362 | @implementation CCReuseGrid | ||
| 363 | |||
| 364 | +(id)actionWithTimes:(int)times | ||
| 365 | { | ||
| 366 | return [[[self alloc] initWithTimes:times ] autorelease]; | ||
| 367 | } | ||
| 368 | |||
| 369 | -(id)initWithTimes:(int)times | ||
| 370 | { | ||
| 371 | if ( (self = [super init]) ) | ||
| 372 | t_ = times; | ||
| 373 | |||
| 374 | return self; | ||
| 375 | } | ||
| 376 | |||
| 377 | -(void)startWithTarget:(id)aTarget | ||
| 378 | { | ||
| 379 | [super startWithTarget:aTarget]; | ||
| 380 | |||
| 381 | CCNode *myTarget = (CCNode*) [self target]; | ||
| 382 | if ( myTarget.grid && myTarget.grid.active ) | ||
| 383 | myTarget.grid.reuseGrid += t_; | ||
| 384 | } | ||
| 385 | |||
| 386 | @end | ||
