summary refs log tree commit diff stats
path: root/Classes/JumpGameMode.m
diff options
context:
space:
mode:
authorStarla Insigna <starla4444@gmail.com>2011-09-04 11:17:44 -0400
committerStarla Insigna <starla4444@gmail.com>2011-09-04 11:17:44 -0400
commit339f8e2cb2fce18f6d32b3881a5cd390c0be488e (patch)
treecb17c635d664838259e591f4b345ae6349931d49 /Classes/JumpGameMode.m
parent9f4bf94a763afba795fb991006081505c3ae466b (diff)
downloadcartcollect-339f8e2cb2fce18f6d32b3881a5cd390c0be488e.tar.gz
cartcollect-339f8e2cb2fce18f6d32b3881a5cd390c0be488e.tar.bz2
cartcollect-339f8e2cb2fce18f6d32b3881a5cd390c0be488e.zip
Implemented Jump mode (BETA) jump
Dear god this is beta. I think I'll release a build for the testers soon so I can see what people think and then hammer out the bugs.

Refs #204
Diffstat (limited to 'Classes/JumpGameMode.m')
-rw-r--r--Classes/JumpGameMode.m344
1 files changed, 336 insertions, 8 deletions
diff --git a/Classes/JumpGameMode.m b/Classes/JumpGameMode.m index e30be6a..bac3fa5 100644 --- a/Classes/JumpGameMode.m +++ b/Classes/JumpGameMode.m
@@ -7,8 +7,27 @@
7// 7//
8 8
9#import "JumpGameMode.h" 9#import "JumpGameMode.h"
10#import "SimpleAudioEngine.h"
11#import "FallingObject.h"
12#import "Cherry.h"
13#import "Bottle.h"
14#import "OneUp.h"
15#import "Rock.h"
16#import "GameOverScene.h"
10 17
11#define kMinimumGestureLength 50 18#define kMinimumGestureLength 25
19
20@interface LedgeFactory : NSObject {
21 UIImage* leftSprite;
22 UIImage* midSprite;
23 UIImage* rightSprite;
24 UIImage* singleSprite;
25}
26
27- (id)init;
28- (UIImage*)createLedgeWithWidth:(int)width height:(int)height;
29
30@end
12 31
13@implementation JumpGameMode 32@implementation JumpGameMode
14 33
@@ -18,14 +37,33 @@
18 37
19 if (nil != self) 38 if (nil != self)
20 { 39 {
40 CCSprite* backgroundImage = [CCSprite spriteWithFile:@"SeaBeach.png"];
41 backgroundImage.position = ccp(240, 160);
42 [self addChild:backgroundImage z:-1];
43
21 water = [CCSprite spriteWithFile:@"water.png"]; 44 water = [CCSprite spriteWithFile:@"water.png"];
22 water.position = ccp(240, -80); 45 water.position = ccp(240, -60);
23 [self addChild:water]; 46 [self addChild:water];
24 47
48 cart.sprite.position = ccp(120, 22+64); //86
49 cart.falling = YES;
50 cart.delegate = self;
51
25 self.isTouchEnabled = YES; 52 self.isTouchEnabled = YES;
26 53
27 waterTick = 0; 54 waterTick = 0;
28 wave = NO; 55 wave = NO;
56
57 factory = [[LedgeFactory alloc] init];
58 ledges = [[NSMutableSet alloc] init];
59 CCSprite* ledge = [CCSprite spriteWithTexture:[[CCTexture2D alloc] initWithImage:[factory createLedgeWithWidth:6 height:2]]];
60 ledge.position = ccp(80, 32);
61 [self addChild:ledge];
62 [ledges addObject:ledge];
63
64 ledgeScrollSpeed = 0;
65 ledgeAccelerationRate = 20.0f;
66 addSpeed = 2.5f;
29 } 67 }
30 68
31 return self; 69 return self;
@@ -34,21 +72,122 @@
34- (void)onEnterTransitionDidFinish 72- (void)onEnterTransitionDidFinish
35{ 73{
36 [super onEnterTransitionDidFinish]; 74 [super onEnterTransitionDidFinish];
75
76 [self schedule:@selector(accelerateLedgeScrolling) interval:ledgeAccelerationRate];
77 [self schedule:@selector(randomlyAddObject:) interval:addSpeed];
78 [self schedule:@selector(incrementScore) interval:1.0f];
37 79
38 [self scheduleDelayedAction:^{ 80 [self scheduleDelayedAction:^{
39 wave = YES; 81 wave = YES;
40 } delay:3.0f]; 82 } delay:60.0f];
41} 83}
42 84
43- (void)tick:(ccTime)dt 85- (void)tick:(ccTime)dt
44{ 86{
87 NSMutableSet* discardedSet = [NSMutableSet set];
88 int rightmost = 0;
89 int rightwidth = 0;
90
91 for (CCSprite* sprite in ledges)
92 {
93 sprite.position = ccp(sprite.position.x - ledgeScrollSpeed, sprite.position.y);
94
95 if ((sprite.position.x + sprite.boundingBox.size.width/2) < 0)
96 {
97 [discardedSet addObject:sprite];
98 [self removeChild:sprite cleanup:YES];
99 }
100
101 if (sprite.position.x > rightmost)
102 {
103 rightmost = sprite.position.x;
104 rightwidth = sprite.boundingBox.size.width/2;
105 }
106 }
107
108 for (FallingObject* object in objects)
109 {
110 if (waterTick > 0)
111 {
112 object.sprite.position = ccp(object.sprite.position.x, MAX(object.sprite.position.y, water.position.y+80+11));
113 } else {
114 object.sprite.position = ccp(object.sprite.position.x-ledgeScrollSpeed, object.sprite.position.y);
115 }
116 }
117
118 [ledges minusSet:discardedSet];
119
120 if (rightmost <= 480)
121 {
122 CCSprite* ledge = [CCSprite spriteWithTexture:[[CCTexture2D alloc] initWithImage:[factory createLedgeWithWidth:(arc4random() % 10) height:2]]];
123 ledge.position = ccp(rightmost + rightwidth + ledge.boundingBox.size.width/2+64, 32);
124 [self addChild:ledge];
125 [ledges addObject:ledge];
126 }
127
128 if ([self cartIsObstructed:cart])
129 {
130 cart.sprite.position = ccp(cart.sprite.position.x-ledgeScrollSpeed, cart.sprite.position.y);
131 }
132
133 int lastScore = score;
134
45 [super tick:dt]; 135 [super tick:dt];
46 136
137 if (cart.sprite.position.y == (0-cart.sprite.boundingBox.size.height/2))
138 {
139 [self setLives:self.lives-1];
140
141 [[SimpleAudioEngine sharedEngine] playEffect:[[NSBundle mainBundle] pathForResource:@"Damage1" ofType:@"wav"]];
142
143 cart.sprite.position = ccp(cart.sprite.position.x, 320 + cart.sprite.boundingBox.size.height/2);
144 }
145
146 if (lives == 0)
147 {
148 [self unscheduleAllSelectors];
149
150 [[CCDirector sharedDirector] replaceScene:[CCTransitionSlideInT transitionWithDuration:1.5f scene:[GameOverScene sceneWithScore:score gameMode:@"Jump"]]];
151 } else if (score > lastScore)
152 {
153 if ((lastScore < 240) && (score >= 240))
154 {
155 [self unschedule:@selector(randomlyAddObject:)];
156 [self schedule:@selector(randomlyAddObject:) interval:0.6f];
157 addSpeed = 0.6f;
158 } else if ((lastScore < 180) && (score >= 180))
159 {
160 [self unschedule:@selector(randomlyAddObject:)];
161 [self schedule:@selector(randomlyAddObject:) interval:0.7f];
162 addSpeed = 0.7f;
163 } else if ((lastScore < 120) && (score >= 120))
164 {
165 [self unschedule:@selector(randomlyAddObject:)];
166 [self schedule:@selector(randomlyAddObject:) interval:0.8f];
167 addSpeed = 0.8f;
168 } else if ((lastScore < 90) && (score >= 90))
169 {
170 [self unschedule:@selector(randomlyAddObject:)];
171 [self schedule:@selector(randomlyAddObject:) interval:0.9f];
172 addSpeed = 0.9f;
173 } else if ((lastScore < 60) && (score >= 60))
174 {
175 [self unschedule:@selector(randomlyAddObject:)];
176 [self schedule:@selector(randomlyAddObject:) interval:1.0f];
177 addSpeed = 1.0f;
178 } else if ((lastScore < 30) && (score >= 30))
179 {
180 [self unschedule:@selector(randomlyAddObject:)];
181 [self schedule:@selector(randomlyAddObject:) interval:2.0f];
182 addSpeed = 2.0f;
183 }
184 }
185
47 if (wave) 186 if (wave)
48 { 187 {
49 waterTick++; 188 waterTick++;
50 189
51 water.position = ccp(240, 160 * sin(waterTick / (36 * M_PI)) - 80); 190 water.position = ccp(240, 140 * sin(waterTick / (36 * M_PI)) - 60);
52 191
53 if (waterTick == 180) 192 if (waterTick == 180)
54 { 193 {
@@ -61,6 +200,10 @@
61 { 200 {
62 wave = NO; 201 wave = NO;
63 waterTick = 0; 202 waterTick = 0;
203
204 [self scheduleDelayedAction:^{
205 wave = YES;
206 } delay:60.0f];
64 } 207 }
65 } 208 }
66 209
@@ -68,15 +211,14 @@
68 { 211 {
69 jumpTick++; 212 jumpTick++;
70 213
71 cart.sprite.position = ccp(cart.sprite.position.x, MAX(100 * sin(jumpTick / (2 * M_PI)) + 22, water.position.y+80+11)); 214 cart.sprite.position = ccp(cart.sprite.position.x, MAX(100 * sin(jumpTick / (2 * M_PI)) + 86, water.position.y+80+11));
72 215
73 if (jumpTick == 20) 216 if (jumpTick == 20)
74 { 217 {
75 jump = NO; 218 jump = NO;
76 jumpTick = 0; 219 jumpTick = 0;
220 cart.falling = YES;
77 } 221 }
78 } else {
79 cart.sprite.position = ccp(cart.sprite.position.x, MAX(22, water.position.y+80+11));
80 } 222 }
81} 223}
82 224
@@ -93,9 +235,10 @@
93 CGFloat angle = atan2f(gestureCurrentPosition.y - gestureStartPoint.y, gestureCurrentPosition.x - gestureStartPoint.x); 235 CGFloat angle = atan2f(gestureCurrentPosition.y - gestureStartPoint.y, gestureCurrentPosition.x - gestureStartPoint.x);
94 CGFloat distance = sqrt(powf((gestureCurrentPosition.x - gestureStartPoint.x),2) + powf((gestureCurrentPosition.y - gestureStartPoint.y),2)); 236 CGFloat distance = sqrt(powf((gestureCurrentPosition.x - gestureStartPoint.x),2) + powf((gestureCurrentPosition.y - gestureStartPoint.y),2));
95 237
96 if ((distance >= kMinimumGestureLength) && (angle >= expectedAngle - M_PI_4) && (angle <= expectedAngle + M_PI_4) && (cart.sprite.position.y == 22)) 238 if ((distance >= kMinimumGestureLength) && (angle >= expectedAngle - M_PI_4) && (angle <= expectedAngle + M_PI_4) && ((cart.sprite.position.y >= 80) && (cart.sprite.position.y <= 90)))
97 { 239 {
98 jump = YES; 240 jump = YES;
241 cart.falling = NO;
99 } 242 }
100} 243}
101 244
@@ -106,4 +249,189 @@
106 expectedAngle = acceleration.y*M_PI_2; 249 expectedAngle = acceleration.y*M_PI_2;
107} 250}
108 251
252- (int)cartShouldFall:(Cart *)m_cart
253{
254 int bottom = 0-m_cart.sprite.boundingBox.size.height/2;
255
256 for (CCSprite* sprite in ledges)
257 {
258 CGSize first = [m_cart.sprite boundingBox].size;
259 CGSize second = [sprite boundingBox].size;
260
261 if (m_cart.sprite.position.x > (sprite.position.x - second.width/2 - first.width/2))
262 {
263 if (m_cart.sprite.position.x < (sprite.position.x + second.width/2 + first.width/2))
264 {
265 bottom = sprite.position.y + second.height/2 + first.height/2;
266 break;
267 }
268 }
269 }
270
271 if (waterTick > 0)
272 {
273 bottom = MAX(water.position.y+80+11, bottom);
274 }
275
276 return bottom;
277}
278
279- (BOOL)cartIsObstructed:(Cart *)m_cart
280{
281 for (CCSprite* sprite in ledges)
282 {
283 CGSize first = [m_cart.sprite boundingBox].size;
284 CGSize second = [sprite boundingBox].size;
285
286 if (m_cart.sprite.position.x > (sprite.position.x - second.width/2 - first.width/2))
287 {
288 if (m_cart.sprite.position.x < (sprite.position.x + second.width/2 + first.width/2))
289 {
290 if (m_cart.sprite.position.y > (sprite.position.y - second.height/2 - first.height/2))
291 {
292 if (m_cart.sprite.position.y < (sprite.position.y + second.height/2 + first.height/2))
293 {
294 return YES;
295 }
296 }
297 }
298 }
299 }
300
301 return NO;
302}
303
304- (void)accelerateLedgeScrolling
305{
306 [self unschedule:@selector(accelerateLedgeScrolling)];
307
308 ledgeScrollSpeed += 2;
309 ledgeAccelerationRate *= 2;
310
311 [self schedule:@selector(accelerateLedgeScrolling) interval:ledgeAccelerationRate];
312}
313
314- (void)randomlyAddObject:(ccTime)dt
315{
316 FallingObject* object;
317
318 if (score < 120)
319 {
320 int randomval = arc4random()%100;
321
322 if (randomval < 80)
323 {
324 object = [[Rock alloc] init];
325 } else {
326 object = [[OneUp alloc] init];
327 }
328 } else {
329 int randomval = arc4random()%100;
330
331 if (randomval < 70)
332 {
333 object = [[Rock alloc] init];
334 } else {
335 object = [[OneUp alloc] init];
336 }
337 }
338
339 int objectX = arc4random()%448+16;
340 object.sprite.position = ccp(objectX, 360);
341 object.sprite.scale = 1;
342 [self addChild:object.sprite];
343
344 [objects addObject:object];
345 [object release];
346
347 if (score >= 120)
348 {
349 if (arc4random() % 100 > 80)
350 {
351 object = [[Rock alloc] init];
352
353 objectX = arc4random()%448+16;
354 object.sprite.position = ccp(objectX, 360);
355 object.sprite.scale = 1;
356 [self addChild:object.sprite];
357
358 [objects addObject:object];
359 [object release];
360 }
361 }
362
363 if (score >= 240)
364 {
365 if (arc4random() % 100 > 80)
366 {
367 object = [[Rock alloc] init];
368
369 objectX = arc4random()%448+16;
370 object.sprite.position = ccp(objectX, 360);
371 object.sprite.scale = 1;
372 [self addChild:object.sprite];
373
374 [objects addObject:object];
375 [object release];
376 }
377 }
378}
379
380- (void)incrementScore
381{
382 [self setScore:self.score+1];
383}
384
109@end 385@end
386
387@implementation LedgeFactory
388
389- (id)init
390{
391 self = [super init];
392
393 CGImageRef framestuff = [[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ledge" ofType:@"png"]] CGImage];
394 CGImageRef leftRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(0, 0, 32, 32));
395 CGImageRef midRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(32, 0, 32, 32));
396 CGImageRef rightRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(64, 0, 32, 32));
397 CGImageRef singleRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(96, 0, 32, 32));
398 leftSprite = [[UIImage alloc] initWithCGImage:leftRef];
399 midSprite = [[UIImage alloc] initWithCGImage:midRef];
400 rightSprite = [[UIImage alloc] initWithCGImage:rightRef];
401 singleSprite = [[UIImage alloc] initWithCGImage:singleRef];
402 CGImageRelease(leftRef);
403 CGImageRelease(midRef);
404 CGImageRelease(rightRef);
405 CGImageRelease(singleRef);
406
407 return self;
408}
409
410- (UIImage*)createLedgeWithWidth:(int)width height:(int)height
411{
412 UIGraphicsBeginImageContext(CGSizeMake(width*32, height*32));
413
414 for (int y=0; y<height; y++)
415 {
416 if (width == 1)
417 {
418 [singleSprite drawInRect:CGRectMake(0, y*32, 32, 32)];
419 } else {
420 [leftSprite drawInRect:CGRectMake(0, y*32, 32, 32)];
421
422 for (int x=1; x<(width-1); x++)
423 {
424 [midSprite drawInRect:CGRectMake(x*32, y*32, 32, 32)];
425 }
426
427 [rightSprite drawInRect:CGRectMake((width-1)*32, y*32, 32, 32)];
428 }
429 }
430
431 UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
432 UIGraphicsEndImageContext();
433
434 return result;
435}
436
437@end \ No newline at end of file