summary refs log tree commit diff stats
path: root/Classes/Cart.m
blob: cfe7ee719dbed25e139817f2e0c568b0001c6ee6 (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
//
//  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;

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

- (void)tick
{
    if (!immobile)
    {
        // Move the cart based on acceleration gathered from accelerometer
        CGPoint prevLoc = sprite.position;
        sprite.position = ccp(MIN(MAX(sprite.position.x+accelX, 16),464), 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-6));
            } else {
                NSLog(@"Falling is set on a cart without a compatible game mode.");
            }
        }
    }
}

- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
{
    static float prevY=0;
	
#define kFilterFactor 0.05f
	
	float accelY = -((float) acceleration.y * kFilterFactor + (1- kFilterFactor)*prevY);
	
	prevY = accelY;
	accelX = accelY * 750;
}

@end