summary refs log tree commit diff stats
path: root/Classes
diff options
context:
space:
mode:
Diffstat (limited to 'Classes')
-rw-r--r--Classes/JumpGameMode.h21
-rw-r--r--Classes/JumpGameMode.m109
2 files changed, 130 insertions, 0 deletions
diff --git a/Classes/JumpGameMode.h b/Classes/JumpGameMode.h new file mode 100644 index 0000000..9753720 --- /dev/null +++ b/Classes/JumpGameMode.h
@@ -0,0 +1,21 @@
1//
2// JumpGameMode.h
3// Cart Collect
4//
5// Created by Starla Insigna on 8/17/11.
6// Copyright 2011 Four Island. All rights reserved.
7//
8
9#import "GameMode.h"
10
11@interface JumpGameMode : GameMode <CCStandardTouchDelegate> {
12 CCSprite* water;
13 int waterTick;
14 BOOL wave;
15 CGPoint gestureStartPoint;
16 int jumpTick;
17 BOOL jump;
18 float expectedAngle;
19}
20
21@end
diff --git a/Classes/JumpGameMode.m b/Classes/JumpGameMode.m new file mode 100644 index 0000000..e30be6a --- /dev/null +++ b/Classes/JumpGameMode.m
@@ -0,0 +1,109 @@
1//
2// JumpGameMode.m
3// Cart Collect
4//
5// Created by Starla Insigna on 8/17/11.
6// Copyright 2011 Four Island. All rights reserved.
7//
8
9#import "JumpGameMode.h"
10
11#define kMinimumGestureLength 50
12
13@implementation JumpGameMode
14
15- (id)init
16{
17 self = [super init];
18
19 if (nil != self)
20 {
21 water = [CCSprite spriteWithFile:@"water.png"];
22 water.position = ccp(240, -80);
23 [self addChild:water];
24
25 self.isTouchEnabled = YES;
26
27 waterTick = 0;
28 wave = NO;
29 }
30
31 return self;
32}
33
34- (void)onEnterTransitionDidFinish
35{
36 [super onEnterTransitionDidFinish];
37
38 [self scheduleDelayedAction:^{
39 wave = YES;
40 } delay:3.0f];
41}
42
43- (void)tick:(ccTime)dt
44{
45 [super tick:dt];
46
47 if (wave)
48 {
49 waterTick++;
50
51 water.position = ccp(240, 160 * sin(waterTick / (36 * M_PI)) - 80);
52
53 if (waterTick == 180)
54 {
55 wave = NO;
56
57 [self scheduleDelayedAction:^{
58 wave = YES;
59 } delay:10.0f];
60 } else if (waterTick == 360)
61 {
62 wave = NO;
63 waterTick = 0;
64 }
65 }
66
67 if (jump)
68 {
69 jumpTick++;
70
71 cart.sprite.position = ccp(cart.sprite.position.x, MAX(100 * sin(jumpTick / (2 * M_PI)) + 22, water.position.y+80+11));
72
73 if (jumpTick == 20)
74 {
75 jump = NO;
76 jumpTick = 0;
77 }
78 } else {
79 cart.sprite.position = ccp(cart.sprite.position.x, MAX(22, water.position.y+80+11));
80 }
81}
82
83- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
84{
85 UITouch* touch = [touches anyObject];
86 gestureStartPoint = [touch locationInView:nil];
87}
88
89- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
90{
91 UITouch* touch = [touches anyObject];
92 CGPoint gestureCurrentPosition = [touch locationInView:nil];
93 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));
95
96 if ((distance >= kMinimumGestureLength) && (angle >= expectedAngle - M_PI_4) && (angle <= expectedAngle + M_PI_4) && (cart.sprite.position.y == 22))
97 {
98 jump = YES;
99 }
100}
101
102- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
103{
104 [super accelerometer:accelerometer didAccelerate:acceleration];
105
106 expectedAngle = acceleration.y*M_PI_2;
107}
108
109@end