summary refs log tree commit diff stats
path: root/Classes/JumpGameMode.m
blob: e8c8c7e8ffb3b4561da9ca4f7cab82743a6a120c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
//  JumpGameMode.m
//  Cart Collect
//
//  Created by Starla Insigna on 8/17/11.
//  Copyright 2011 Four Island. All rights reserved.
//

#import "JumpGameMode.h"

#define kMinimumGestureLength  25
#define kMaximumVariance   5

@implementation JumpGameMode

- (id)init
{
    self = [super init];
    
    if (nil != self)
    {
        water = [CCSprite spriteWithFile:@"water.png"];
        water.position = ccp(240, -80);
        [self addChild:water];
        
        self.isTouchEnabled = YES;
        
        waterTick = 0;
        wave = NO;
    }
    
    return self;
}

- (void)onEnterTransitionDidFinish
{
    [super onEnterTransitionDidFinish];
    
    [self scheduleDelayedAction:^{
        wave = YES;
    } delay:3.0f];
}

- (void)tick:(ccTime)dt
{
    [super tick:dt];
    
    if (wave)
    {
        waterTick++;
        
        water.position = ccp(240, 160 * sin(waterTick / (36 * M_PI)) - 80);
        
        if (waterTick == 180)
        {
            wave = NO;
            
            [self scheduleDelayedAction:^{
                wave = YES;
            } delay:10.0f];
        } else if (waterTick == 360)
        {
            wave = NO;
            waterTick = 0;
        }
    }
    
    if (jump)
    {
        jumpTick++;
        
        cart.sprite.position = ccp(cart.sprite.position.x, MAX(100 * sin(jumpTick / (2 * M_PI)) + 22, water.position.y+80+11));
        
        if (jumpTick == 20)
        {
            jump = NO;
            jumpTick = 0;
        }
    } else {
        cart.sprite.position = ccp(cart.sprite.position.x, MAX(22, water.position.y+80+11));
    }
}

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* touch = [touches anyObject];
    gestureStartPoint = [touch locationInView:nil];
}

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* touch = [touches anyObject];
    CGPoint gestureCurrentPosition = [touch locationInView:nil];
    CGFloat deltaX = fabsf(gestureStartPoint.x - gestureCurrentPosition.x);
    CGFloat deltaY = fabsf(gestureStartPoint.y - gestureCurrentPosition.y);
    
    if ((deltaX >= kMinimumGestureLength) && (deltaY <= kMaximumVariance*swipeVarianceModifer) && (cart.sprite.position.y == 22))
    {
        jump = YES;
    }
}

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    [super accelerometer:accelerometer didAccelerate:acceleration];
    
    swipeVarianceModifer = MAX(1,(fabsf(acceleration.y) * 480)/kMaximumVariance);
}

@end