summary refs log tree commit diff stats
path: root/Classes/Cart.m
blob: f9da181295e0dd5234853924c369fc08bf7afa98 (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
//
//  Cart.m
//  Cart Collect
//
//  Created by Starla Insigna on 8/9/11.
//  Copyright 2011 Four Island. All rights reserved.
//

#import "Cart.h"

@implementation Cart

@synthesize sprite, immobile, delegate, falling, boundedByScreen;

- (id)initWithSprite:(CCSprite*)m_sprite
{
    self = [super init];
    
    if (nil != self)
    {
        sprite = m_sprite;
        immobile = NO;
        falling = NO;
        boundedByScreen = YES;
    }
    
    return self;
}

- (void)tick
{
    if (!immobile)
    {
        // Move the cart based on acceleration gathered from accelerometer
        CGPoint prevLoc = sprite.position;
        
        if (boundedByScreen)
        {
            sprite.position = ccp(MIN(MAX(sprite.position.x+accelX, 16),464), sprite.position.y);
        } else {
            sprite.position = ccp(sprite.position.x+accelX, sprite.position.y);
        }
        
        if ((delegate != nil) && ([delegate respondsToSelector:@selector(cartIsObstructed:)]))
        {
            if ([delegate cartIsObstructed:self])
            {
                sprite.position = prevLoc;
            }
        }
        
        if (falling)
        {
            if ((delegate != nil) && ([delegate respondsToSelector:@selector(cartShouldFall:)]))
            {
                int bottom = [delegate cartShouldFall:self];
                sprite.position = ccp(sprite.position.x, MAX(bottom, sprite.position.y-8));
            } else {
                NSLog(@"Falling is set on a cart without a compatible game mode.");
            }
        }
    }
}

#define kFilterFactor 0.05f

- (void)deviceDidRotate:(double)pitch
{
    static float prevY=0;
    float accelY = -((float) pitch * kFilterFactor + (1- kFilterFactor)*prevY);
    
    prevY = accelY;
	accelX = accelY * 750;
}

@end